Coverage Report - org.codehaus.plexus.archiver.util.FilePermissionUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
FilePermissionUtils
40%
31/76
36%
11/30
15
 
 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  0
     {
 35  
         // no op
 36  0
     }
 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  4
         if ( StringUtils.isBlank( mode ) )
 46  
         {
 47  0
             throw new IllegalArgumentException( " file mode cannot be empty" );
 48  
         }
 49  
         // 4 characters works on some unix (ie solaris)
 50  4
         if ( mode.length() != 3 && mode.length() != 4 )
 51  
         {
 52  0
             throw new IllegalArgumentException( " file mode must be 3 or 4 characters" );
 53  
         }
 54  
 
 55  4
         List<String> modes = new ArrayList<String>( mode.length() );
 56  16
         for ( int i = 0, size = mode.length(); i < size; i++ )
 57  
         {
 58  12
             modes.add( String.valueOf( mode.charAt( i ) ) );
 59  
         }
 60  
 
 61  4
         boolean executable = false, ownerOnlyExecutable = true, ownerOnlyReadable = true, readable = false,
 62  4
             ownerOnlyWritable = true, writable = false;
 63  
 
 64  
         // handle user perm
 65  
         try
 66  
         {
 67  4
             int userMode = Integer.valueOf( modes.get( mode.length() == 4 ? 1 : 0 ) );
 68  4
             switch ( userMode )
 69  
             {
 70  
                 case 0:
 71  0
                     break;
 72  
                 case 1:
 73  0
                     executable = true;
 74  0
                     break;
 75  
                 case 2:
 76  1
                     writable = true;
 77  1
                     break;
 78  
                 case 3:
 79  0
                     writable = true;
 80  0
                     executable = true;
 81  0
                     break;
 82  
                 case 4:
 83  0
                     readable = true;
 84  0
                     break;
 85  
                 case 5:
 86  1
                     readable = true;
 87  1
                     executable = true;
 88  1
                     break;
 89  
                 case 6:
 90  0
                     readable = true;
 91  0
                     writable = true;
 92  0
                     break;
 93  
                 case 7:
 94  2
                     writable = true;
 95  2
                     readable = true;
 96  2
                     executable = true;
 97  2
                     break;
 98  
                 default:
 99  0
                     logger.warn( "ignore file mode " + userMode );
 100  
             }
 101  
         }
 102  0
         catch ( NumberFormatException e )
 103  
         {
 104  0
             throw new IllegalArgumentException( " file mode must contains only number " + mode );
 105  4
         }
 106  
 
 107  
         // handle all perm
 108  
         try
 109  
         {
 110  4
             int allMode = Integer.valueOf( modes.get( mode.length() == 4 ? 3 : 2 ) );
 111  4
             switch ( allMode )
 112  
             {
 113  
                 case 0:
 114  3
                     break;
 115  
                 case 1:
 116  0
                     executable = true;
 117  0
                     ownerOnlyExecutable = false;
 118  0
                     break;
 119  
                 case 2:
 120  0
                     writable = true;
 121  0
                     ownerOnlyWritable = false;
 122  0
                     break;
 123  
                 case 3:
 124  0
                     writable = true;
 125  0
                     executable = true;
 126  0
                     ownerOnlyExecutable = false;
 127  0
                     ownerOnlyWritable = false;
 128  0
                     break;
 129  
                 case 4:
 130  0
                     readable = true;
 131  0
                     ownerOnlyReadable = false;
 132  0
                     break;
 133  
                 case 5:
 134  0
                     readable = true;
 135  0
                     executable = true;
 136  0
                     ownerOnlyReadable = false;
 137  0
                     ownerOnlyExecutable = false;
 138  0
                     break;
 139  
                 case 6:
 140  0
                     readable = true;
 141  0
                     ownerOnlyReadable = false;
 142  0
                     writable = true;
 143  0
                     ownerOnlyWritable = false;
 144  0
                     break;
 145  
                 case 7:
 146  1
                     writable = true;
 147  1
                     readable = true;
 148  1
                     executable = true;
 149  1
                     ownerOnlyReadable = false;
 150  1
                     ownerOnlyExecutable = false;
 151  1
                     ownerOnlyWritable = false;
 152  1
                     break;
 153  
                 default:
 154  0
                     logger.warn( "ignore file mode " + allMode );
 155  
             }
 156  
         }
 157  0
         catch ( NumberFormatException e )
 158  
         {
 159  0
             throw new IllegalArgumentException( " file mode must contains only number " + mode );
 160  4
         }
 161  
 
 162  4
         return new FilePermission( executable, ownerOnlyExecutable, ownerOnlyReadable, readable, ownerOnlyWritable,
 163  
                                    writable );
 164  
 
 165  
     }
 166  
 
 167  
 }