View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.codehaus.plexus.archiver.util;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import org.codehaus.plexus.logging.Logger;
24  import org.codehaus.plexus.util.StringUtils;
25  
26  /**
27   * @author Olivier Lamy
28   * @since 1.0.1
29   */
30  public class FilePermissionUtils
31  {
32  
33      private FilePermissionUtils()
34      {
35          // no op
36      }
37  
38      /**
39       * @param mode file mode "a la" unix ie 664, 440, etc
40       *
41       * @return FilePermission associated to the mode (group permission are ignored here)
42       */
43      public static FilePermission getFilePermissionFromMode( String mode, Logger logger )
44      {
45          if ( StringUtils.isBlank( mode ) )
46          {
47              throw new IllegalArgumentException( " file mode cannot be empty" );
48          }
49          // 4 characters works on some unix (ie solaris)
50          if ( mode.length() != 3 && mode.length() != 4 )
51          {
52              throw new IllegalArgumentException( " file mode must be 3 or 4 characters" );
53          }
54  
55          List<String> modes = new ArrayList<String>( mode.length() );
56          for ( int i = 0, size = mode.length(); i < size; i++ )
57          {
58              modes.add( String.valueOf( mode.charAt( i ) ) );
59          }
60  
61          boolean executable = false, ownerOnlyExecutable = true, ownerOnlyReadable = true, readable = false,
62              ownerOnlyWritable = true, writable = false;
63  
64          // handle user perm
65          try
66          {
67              int userMode = Integer.valueOf( modes.get( mode.length() == 4 ? 1 : 0 ) );
68              switch ( userMode )
69              {
70                  case 0:
71                      break;
72                  case 1:
73                      executable = true;
74                      break;
75                  case 2:
76                      writable = true;
77                      break;
78                  case 3:
79                      writable = true;
80                      executable = true;
81                      break;
82                  case 4:
83                      readable = true;
84                      break;
85                  case 5:
86                      readable = true;
87                      executable = true;
88                      break;
89                  case 6:
90                      readable = true;
91                      writable = true;
92                      break;
93                  case 7:
94                      writable = true;
95                      readable = true;
96                      executable = true;
97                      break;
98                  default:
99                      logger.warn( "ignore file mode " + userMode );
100             }
101         }
102         catch ( NumberFormatException e )
103         {
104             throw new IllegalArgumentException( " file mode must contains only number " + mode );
105         }
106 
107         // handle all perm
108         try
109         {
110             int allMode = Integer.valueOf( modes.get( mode.length() == 4 ? 3 : 2 ) );
111             switch ( allMode )
112             {
113                 case 0:
114                     break;
115                 case 1:
116                     executable = true;
117                     ownerOnlyExecutable = false;
118                     break;
119                 case 2:
120                     writable = true;
121                     ownerOnlyWritable = false;
122                     break;
123                 case 3:
124                     writable = true;
125                     executable = true;
126                     ownerOnlyExecutable = false;
127                     ownerOnlyWritable = false;
128                     break;
129                 case 4:
130                     readable = true;
131                     ownerOnlyReadable = false;
132                     break;
133                 case 5:
134                     readable = true;
135                     executable = true;
136                     ownerOnlyReadable = false;
137                     ownerOnlyExecutable = false;
138                     break;
139                 case 6:
140                     readable = true;
141                     ownerOnlyReadable = false;
142                     writable = true;
143                     ownerOnlyWritable = false;
144                     break;
145                 case 7:
146                     writable = true;
147                     readable = true;
148                     executable = true;
149                     ownerOnlyReadable = false;
150                     ownerOnlyExecutable = false;
151                     ownerOnlyWritable = false;
152                     break;
153                 default:
154                     logger.warn( "ignore file mode " + allMode );
155             }
156         }
157         catch ( NumberFormatException e )
158         {
159             throw new IllegalArgumentException( " file mode must contains only number " + mode );
160         }
161 
162         return new FilePermission( executable, ownerOnlyExecutable, ownerOnlyReadable, readable, ownerOnlyWritable,
163                                    writable );
164 
165     }
166 
167 }