View Javadoc
1   package org.codehaus.plexus.components.io.attributes;
2   
3   /*
4    * Copyright 2007 The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.codehaus.plexus.util.FileUtils;
20  
21  import javax.annotation.Nonnull;
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.LinkedHashMap;
27  import java.util.List;
28  import java.util.Map;
29  
30  @SuppressWarnings( { "NullableProblems" } )
31  public final class PlexusIoResourceAttributeUtils
32  {
33  
34      private PlexusIoResourceAttributeUtils()
35      {
36      }
37  
38      public static PlexusIoResourceAttributes mergeAttributes( PlexusIoResourceAttributes override,
39                                                                PlexusIoResourceAttributes base,
40                                                                PlexusIoResourceAttributes def )
41      {
42          if ( override == null )
43          {
44              return base;
45          }
46          SimpleResourceAttributes result;
47          if ( base == null )
48          {
49              result = new SimpleResourceAttributes();
50          }
51          else
52          {
53              result = new SimpleResourceAttributes( base.getUserId(), base.getUserName(), base.getGroupId(),
54                                                     base.getGroupName(), base.getOctalMode() );
55              result.setSymbolicLink( base.isSymbolicLink() );
56          }
57  
58          if ( override.getGroupId() != null && override.getGroupId() != -1 )
59          {
60              result.setGroupId( override.getGroupId() );
61          }
62  
63          if ( def != null && def.getGroupId() >= 0 && ( result.getGroupId() == null || result.getGroupId() < 0 ) )
64          {
65              result.setGroupId( def.getGroupId() );
66          }
67  
68          if ( override.getGroupName() != null )
69          {
70              result.setGroupName( override.getGroupName() );
71          }
72  
73          if ( def != null && result.getGroupName() == null )
74          {
75              result.setGroupName( def.getGroupName() );
76          }
77  
78          if ( override.getUserId() != null && override.getUserId() != -1 )
79          {
80              result.setUserId( override.getUserId() );
81          }
82  
83          if ( def != null && def.getUserId() >= 0 && ( result.getUserId() == null || result.getUserId() < 0 ) )
84          {
85              result.setUserId( def.getUserId() );
86          }
87  
88          if ( override.getUserName() != null )
89          {
90              result.setUserName( override.getUserName() );
91          }
92  
93          if ( def != null && result.getUserName() == null )
94          {
95              result.setUserName( def.getUserName() );
96          }
97  
98          if ( override.getOctalMode() > 0 )
99          {
100             result.setOctalMode( override.getOctalMode() );
101         }
102 
103         if ( def != null && result.getOctalMode() < 0 )
104         {
105             result.setOctalMode( def.getOctalMode() );
106         }
107 
108         return result;
109     }
110 
111     public static boolean isGroupExecutableInOctal( int mode )
112     {
113         return isOctalModeEnabled( mode, AttributeConstants.OCTAL_GROUP_EXECUTE );
114     }
115 
116     public static boolean isGroupReadableInOctal( int mode )
117     {
118         return isOctalModeEnabled( mode, AttributeConstants.OCTAL_GROUP_READ );
119     }
120 
121     public static boolean isGroupWritableInOctal( int mode )
122     {
123         return isOctalModeEnabled( mode, AttributeConstants.OCTAL_GROUP_WRITE );
124     }
125 
126     public static boolean isOwnerExecutableInOctal( int mode )
127     {
128         return isOctalModeEnabled( mode, AttributeConstants.OCTAL_OWNER_EXECUTE );
129     }
130 
131     public static boolean isOwnerReadableInOctal( int mode )
132     {
133         return isOctalModeEnabled( mode, AttributeConstants.OCTAL_OWNER_READ );
134     }
135 
136     public static boolean isOwnerWritableInOctal( int mode )
137     {
138         return isOctalModeEnabled( mode, AttributeConstants.OCTAL_OWNER_WRITE );
139     }
140 
141     public static boolean isWorldExecutableInOctal( int mode )
142     {
143         return isOctalModeEnabled( mode, AttributeConstants.OCTAL_WORLD_EXECUTE );
144     }
145 
146     public static boolean isWorldReadableInOctal( int mode )
147     {
148         return isOctalModeEnabled( mode, AttributeConstants.OCTAL_WORLD_READ );
149     }
150 
151     public static boolean isWorldWritableInOctal( int mode )
152     {
153         return isOctalModeEnabled( mode, AttributeConstants.OCTAL_WORLD_WRITE );
154     }
155 
156     public static boolean isOctalModeEnabled( int mode, int targetMode )
157     {
158         return ( mode & targetMode ) != 0;
159     }
160 
161     @SuppressWarnings( { "UnusedDeclaration" } )
162     public static PlexusIoResourceAttributes getFileAttributes( File file )
163         throws IOException
164     {
165         Map<String, PlexusIoResourceAttributes> byPath = getFileAttributesByPath( file, false );
166         final PlexusIoResourceAttributes o = byPath.get( file.getAbsolutePath() );
167         if ( o == null )
168         {
169             // We're on a crappy old java version (5) or the OS from hell. Just "fail".
170             return SimpleResourceAttributes.lastResortDummyAttributesForBrokenOS();
171 
172         }
173         return o;
174     }
175 
176     public static Map<String, PlexusIoResourceAttributes> getFileAttributesByPath( File dir )
177         throws IOException
178     {
179         return getFileAttributesByPath( dir, true );
180     }
181 
182     public static @Nonnull
183     Map<String, PlexusIoResourceAttributes> getFileAttributesByPath( @Nonnull File dir,
184                                                                      boolean recursive )
185         throws IOException
186     {
187         Map<Integer, String> userCache = new HashMap<>();
188         Map<Integer, String> groupCache = new HashMap<>();
189         final List<String> fileAndDirectoryNames;
190         if ( recursive && dir.isDirectory() )
191         {
192             fileAndDirectoryNames = FileUtils.getFileAndDirectoryNames( dir, null, null, true, true, true, true );
193         }
194         else
195         {
196             fileAndDirectoryNames = Collections.singletonList( dir.getAbsolutePath() );
197         }
198 
199         final Map<String, PlexusIoResourceAttributes> attributesByPath = new LinkedHashMap<>();
200 
201         for ( String fileAndDirectoryName : fileAndDirectoryNames )
202         {
203             attributesByPath.put( fileAndDirectoryName,
204                                   new FileAttributes( new File( fileAndDirectoryName ), userCache, groupCache ) );
205         }
206         return attributesByPath;
207     }
208 
209 }