1   package org.codehaus.plexus.util;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
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  
32  
33  @SuppressWarnings("Since15")
34  public class NioFiles {
35      public static boolean isSymbolicLink(File file) {
36          return Files.isSymbolicLink(file.toPath());
37      }
38  
39      public static void chmod(File file, int mode) throws IOException {
40          Path path = file.toPath();
41          if (!Files.isSymbolicLink(path)) {
42              Files.setPosixFilePermissions(path, getPermissions(mode));
43          }
44      }
45  
46      @SuppressWarnings({"OctalInteger", "MagicNumber"})
47      private static Set<PosixFilePermission> getPermissions(int mode) {
48          Set<PosixFilePermission> perms = new HashSet<>();
49          
50          if ((mode & 0400) > 0) {
51              perms.add(PosixFilePermission.OWNER_READ);
52          }
53          if ((mode & 0200) > 0) {
54              perms.add(PosixFilePermission.OWNER_WRITE);
55          }
56          if ((mode & 0100) > 0) {
57              perms.add(PosixFilePermission.OWNER_EXECUTE);
58          }
59          
60          if ((mode & 0040) > 0) {
61              perms.add(PosixFilePermission.GROUP_READ);
62          }
63          if ((mode & 0020) > 0) {
64              perms.add(PosixFilePermission.GROUP_WRITE);
65          }
66          if ((mode & 0010) > 0) {
67              perms.add(PosixFilePermission.GROUP_EXECUTE);
68          }
69          
70          if ((mode & 0004) > 0) {
71              perms.add(PosixFilePermission.OTHERS_READ);
72          }
73          if ((mode & 0002) > 0) {
74              perms.add(PosixFilePermission.OTHERS_WRITE);
75          }
76          if ((mode & 0001) > 0) {
77              perms.add(PosixFilePermission.OTHERS_EXECUTE);
78          }
79          return perms;
80      }
81  
82      public static long getLastModified(File file) throws IOException {
83          BasicFileAttributes basicFileAttributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
84          return basicFileAttributes.lastModifiedTime().toMillis();
85      }
86  
87      
88  
89  
90  
91  
92  
93  
94      public static File readSymbolicLink(File symlink) throws IOException {
95          Path path = Files.readSymbolicLink(symlink.toPath());
96          return path.toFile();
97      }
98  
99      public static File createSymbolicLink(File symlink, File target) throws IOException {
100         Path link = symlink.toPath();
101         if (Files.exists(link, LinkOption.NOFOLLOW_LINKS)) {
102             Files.delete(link);
103         }
104         link = Files.createSymbolicLink(link, target.toPath());
105         return link.toFile();
106     }
107 
108     public static boolean deleteIfExists(File file) throws IOException {
109         return Files.deleteIfExists(file.toPath());
110     }
111 
112     public static File copy(File source, File target) throws IOException {
113         Path copy = Files.copy(
114                 source.toPath(),
115                 target.toPath(),
116                 StandardCopyOption.REPLACE_EXISTING,
117                 StandardCopyOption.COPY_ATTRIBUTES);
118         return copy.toFile();
119     }
120 }