1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.plexus.components.io.attributes;
17
18 import javax.annotation.Nonnull;
19 import javax.annotation.Nullable;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.nio.file.Files;
24 import java.nio.file.LinkOption;
25 import java.nio.file.Path;
26 import java.nio.file.attribute.BasicFileAttributes;
27 import java.nio.file.attribute.FileOwnerAttributeView;
28 import java.nio.file.attribute.PosixFileAttributes;
29 import java.nio.file.attribute.PosixFilePermission;
30 import java.util.HashSet;
31 import java.util.Set;
32
33
34
35
36 @SuppressWarnings("OctalInteger")
37 public class AttributeUtils {
38
39
40
41 public static long getLastModified(@Nonnull File file) {
42 try {
43 BasicFileAttributes basicFileAttributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
44 return basicFileAttributes.lastModifiedTime().toMillis();
45 } catch (IOException e) {
46 throw new RuntimeException(e);
47 }
48 }
49
50 public static void chmod(@Nonnull File file, int mode) throws IOException {
51 final Path path = file.toPath();
52 if (!Files.isSymbolicLink(path)) {
53 Files.setPosixFilePermissions(path, getPermissions(mode));
54 }
55 }
56
57 @Nonnull
58 public static Set<PosixFilePermission> getPermissions(int mode) {
59 Set<PosixFilePermission> perms = new HashSet<>();
60
61 if ((mode & 0400) > 0) {
62 perms.add(PosixFilePermission.OWNER_READ);
63 }
64 if ((mode & 0200) > 0) {
65 perms.add(PosixFilePermission.OWNER_WRITE);
66 }
67 if ((mode & 0100) > 0) {
68 perms.add(PosixFilePermission.OWNER_EXECUTE);
69 }
70
71 if ((mode & 0040) > 0) {
72 perms.add(PosixFilePermission.GROUP_READ);
73 }
74 if ((mode & 0020) > 0) {
75 perms.add(PosixFilePermission.GROUP_WRITE);
76 }
77 if ((mode & 0010) > 0) {
78 perms.add(PosixFilePermission.GROUP_EXECUTE);
79 }
80
81 if ((mode & 0004) > 0) {
82 perms.add(PosixFilePermission.OTHERS_READ);
83 }
84 if ((mode & 0002) > 0) {
85 perms.add(PosixFilePermission.OTHERS_WRITE);
86 }
87 if ((mode & 0001) > 0) {
88 perms.add(PosixFilePermission.OTHERS_EXECUTE);
89 }
90 return perms;
91 }
92
93 @Nonnull
94 public static PosixFileAttributes getPosixFileAttributes(@Nonnull File file) throws IOException {
95 return Files.readAttributes(file.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
96 }
97
98 @Nonnull
99 public static BasicFileAttributes getFileAttributes(@Nonnull File file) throws IOException {
100 return getFileAttributes(file.toPath());
101 }
102
103 public static BasicFileAttributes getFileAttributes(Path path) throws IOException {
104 if (isUnix(path)) {
105 try {
106 return Files.readAttributes(path, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
107 } catch (UnsupportedOperationException ignore) {
108
109 }
110 }
111 return Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
112 }
113
114 public static boolean isUnix(Path path) {
115 return path.getFileSystem().supportedFileAttributeViews().contains("unix");
116 }
117
118 @Nullable
119 public static FileOwnerAttributeView getFileOwnershipInfo(@Nonnull File file) throws IOException {
120 try {
121 return Files.getFileAttributeView(file.toPath(), FileOwnerAttributeView.class, LinkOption.NOFOLLOW_LINKS);
122 } catch (UnsupportedOperationException e) {
123 return null;
124 }
125 }
126 }