View Javadoc
1   package org.codehaus.plexus.util;
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 java.io.File;
20  import java.io.IOException;
21  import java.nio.file.Files;
22  import java.nio.file.LinkOption;
23  import java.nio.file.Path;
24  import java.nio.file.StandardCopyOption;
25  import java.nio.file.attribute.BasicFileAttributes;
26  import java.nio.file.attribute.PosixFilePermission;
27  import java.util.HashSet;
28  import java.util.Set;
29  
30  /**
31   * Encapsulates use of java7 features, exposing mostly backward compatible types
32   */
33  @SuppressWarnings( "Since15" )
34  public class NioFiles
35  {
36      public static boolean isSymbolicLink( File file )
37      {
38          return Files.isSymbolicLink( file.toPath() );
39      }
40  
41      public static void chmod( File file, int mode )
42          throws IOException
43      {
44          Path path = file.toPath();
45          if ( !Files.isSymbolicLink( path ) )
46          {
47              Files.setPosixFilePermissions( path, getPermissions( mode ) );
48          }
49      }
50  
51      @SuppressWarnings( { "OctalInteger", "MagicNumber" } )
52      private static Set<PosixFilePermission> getPermissions( int mode )
53      {
54          Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
55          // add owners permission
56          if ( ( mode & 0400 ) > 0 )
57          {
58              perms.add( PosixFilePermission.OWNER_READ );
59          }
60          if ( ( mode & 0200 ) > 0 )
61          {
62              perms.add( PosixFilePermission.OWNER_WRITE );
63          }
64          if ( ( mode & 0100 ) > 0 )
65          {
66              perms.add( PosixFilePermission.OWNER_EXECUTE );
67          }
68          // add group permissions
69          if ( ( mode & 0040 ) > 0 )
70          {
71              perms.add( PosixFilePermission.GROUP_READ );
72          }
73          if ( ( mode & 0020 ) > 0 )
74          {
75              perms.add( PosixFilePermission.GROUP_WRITE );
76          }
77          if ( ( mode & 0010 ) > 0 )
78          {
79              perms.add( PosixFilePermission.GROUP_EXECUTE );
80          }
81          // add others permissions
82          if ( ( mode & 0004 ) > 0 )
83          {
84              perms.add( PosixFilePermission.OTHERS_READ );
85          }
86          if ( ( mode & 0002 ) > 0 )
87          {
88              perms.add( PosixFilePermission.OTHERS_WRITE );
89          }
90          if ( ( mode & 0001 ) > 0 )
91          {
92              perms.add( PosixFilePermission.OTHERS_EXECUTE );
93          }
94          return perms;
95      }
96  
97      public static long getLastModified( File file )
98          throws IOException
99      {
100         BasicFileAttributes basicFileAttributes = Files.readAttributes( file.toPath(), BasicFileAttributes.class );
101         return basicFileAttributes.lastModifiedTime().toMillis();
102     }
103 
104     /**
105      * Reads the target of the symbolic link
106      *
107      * @param symlink A file that is a symlink
108      * @return A file that is the target of the symlink
109      * @throws java.io.IOException
110      */
111 
112     public static File readSymbolicLink( File symlink )
113         throws IOException
114     {
115         Path path = Files.readSymbolicLink( symlink.toPath() );
116         return path.toFile();
117     }
118 
119     public static File createSymbolicLink( File symlink, File target )
120         throws IOException
121     {
122         Path link = symlink.toPath();
123         if ( Files.exists( link, LinkOption.NOFOLLOW_LINKS ) )
124         {
125             Files.delete( link );
126         }
127         link = Files.createSymbolicLink( link, target.toPath() );
128         return link.toFile();
129     }
130 
131     public static boolean deleteIfExists( File file )
132         throws IOException
133     {
134         return Files.deleteIfExists( file.toPath() );
135     }
136 
137     public static File copy( File source, File target )
138         throws IOException
139     {
140         Path copy = Files.copy( source.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING,
141                                 StandardCopyOption.COPY_ATTRIBUTES, LinkOption.NOFOLLOW_LINKS );
142         return copy.toFile();
143     }
144 
145 }