View Javadoc
1   /*
2    * Copyright 2014 The Codehaus Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.plexus.components.io.attributes;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.nio.file.Files;
21  import java.nio.file.LinkOption;
22  import java.nio.file.Path;
23  import java.nio.file.attribute.BasicFileAttributes;
24  import java.nio.file.attribute.FileOwnerAttributeView;
25  import java.nio.file.attribute.PosixFileAttributes;
26  import java.nio.file.attribute.PosixFilePermission;
27  import java.util.HashSet;
28  import java.util.Set;
29  
30  import javax.annotation.Nonnull;
31  import javax.annotation.Nullable;
32  
33  /**
34   * @author Kristian Rosenvold
35   */
36  public class AttributeUtils
37  {
38      /*
39      Reads last-modified with proper failure handling if something goes wrong.
40       */
41      public static long getLastModified( @Nonnull File file )
42      {
43          try
44          {
45              BasicFileAttributes basicFileAttributes = Files.readAttributes( file.toPath(), BasicFileAttributes.class );
46              return basicFileAttributes.lastModifiedTime().toMillis();
47          }
48          catch ( IOException e )
49          {
50              throw new RuntimeException( e );
51          }
52  
53      }
54  
55      public static void chmod( @Nonnull File file, int mode )
56          throws IOException
57      {
58          final Path path = file.toPath();
59          if ( !Files.isSymbolicLink( path ) )
60          {
61              Files.setPosixFilePermissions( path, getPermissions( mode ) );
62          }
63      }
64  
65      @Nonnull
66      public static Set<PosixFilePermission> getPermissions( int mode )
67      {
68          Set<PosixFilePermission> perms = new HashSet<>();
69          // add owners permission
70          if ( ( mode & 0400 ) > 0 )
71          {
72              perms.add( PosixFilePermission.OWNER_READ );
73          }
74          if ( ( mode & 0200 ) > 0 )
75          {
76              perms.add( PosixFilePermission.OWNER_WRITE );
77          }
78          if ( ( mode & 0100 ) > 0 )
79          {
80              perms.add( PosixFilePermission.OWNER_EXECUTE );
81          }
82          // add group permissions
83          if ( ( mode & 0040 ) > 0 )
84          {
85              perms.add( PosixFilePermission.GROUP_READ );
86          }
87          if ( ( mode & 0020 ) > 0 )
88          {
89              perms.add( PosixFilePermission.GROUP_WRITE );
90          }
91          if ( ( mode & 0010 ) > 0 )
92          {
93              perms.add( PosixFilePermission.GROUP_EXECUTE );
94          }
95          // add others permissions
96          if ( ( mode & 0004 ) > 0 )
97          {
98              perms.add( PosixFilePermission.OTHERS_READ );
99          }
100         if ( ( mode & 0002 ) > 0 )
101         {
102             perms.add( PosixFilePermission.OTHERS_WRITE );
103         }
104         if ( ( mode & 0001 ) > 0 )
105         {
106             perms.add( PosixFilePermission.OTHERS_EXECUTE );
107         }
108         return perms;
109     }
110 
111     @Nonnull
112     public static PosixFileAttributes getPosixFileAttributes( @Nonnull File file )
113         throws IOException
114     {
115         return Files.readAttributes( file.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS );
116     }
117 
118     @Nonnull
119     public static BasicFileAttributes getFileAttributes( @Nonnull File file )
120         throws IOException
121     {
122         return getFileAttributes( file.toPath() );
123     }
124 
125     public static BasicFileAttributes getFileAttributes( Path path )
126         throws IOException
127     {
128         if ( isUnix( path ) )
129         {
130 
131             try
132             {
133                 return Files.readAttributes( path, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS );
134             }
135             catch ( UnsupportedOperationException ignore )
136             {
137                 // Maybe ignoring is dramatic. Maybe not. But we do get the basic attrs anyqway
138             }
139         }
140         return Files.readAttributes( path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS );
141     }
142 
143     public static boolean isUnix( Path path )
144     {
145         return path.getFileSystem().supportedFileAttributeViews().contains( "unix" );
146     }
147 
148     @Nullable
149     public static FileOwnerAttributeView getFileOwnershipInfo( @Nonnull File file )
150         throws IOException
151     {
152         try
153         {
154             return Files.getFileAttributeView( file.toPath(), FileOwnerAttributeView.class, LinkOption.NOFOLLOW_LINKS );
155         }
156         catch ( UnsupportedOperationException e )
157         {
158             return null;
159         }
160     }
161 }