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      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          // add owners permission
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          // add group permissions
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          // add others permissions
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       * Reads the target of the symbolic link
89       *
90       * @param symlink A file that is a symlink
91       * @return A file that is the target of the symlink
92       * @throws java.io.IOException io issue
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 }