1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.task.util.file;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.GroupPrincipal;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.nio.file.attribute.UserPrincipal;
import java.nio.file.attribute.UserPrincipalLookupService;
import java.time.Instant;
import java.util.Optional;
import java.util.Set;
import static com.yahoo.vespa.hosted.node.admin.task.util.file.IOExceptionUtil.uncheck;
/**
* Thin wrapper around java.nio.file.Path, especially nice for UNIX-specific features.
*
* @author hakonhall
*/
// @Immutable
public class UnixPath {
private final Path path;
public UnixPath(Path path) {
this.path = path;
}
public UnixPath(String path) {
this(Paths.get(path));
}
public Path toPath() {
return path;
}
public boolean createParents() {
Path parent = path.getParent();
if (Files.isDirectory(parent)) {
return false;
}
uncheck(() -> Files.createDirectories(parent));
return true;
}
public String readUtf8File() {
byte[] byteContent = uncheck(() -> Files.readAllBytes(path));
return new String(byteContent, StandardCharsets.UTF_8);
}
public void writeUtf8File(String content, OpenOption... options) {
byte[] contentInUtf8 = content.getBytes(StandardCharsets.UTF_8);
uncheck(() -> Files.write(path, contentInUtf8, options));
}
public String getPermissions() {
return getAttributes().permissions();
}
/**
* @param permissions Example: "rwxr-x---" means rwx for owner, rx for group,
* and no permissions for others.
*/
public void setPermissions(String permissions) {
Set<PosixFilePermission> permissionSet;
try {
permissionSet = PosixFilePermissions.fromString(permissions);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Failed to set permissions '" +
permissions + "' on path " + path, e);
}
uncheck(() -> Files.setPosixFilePermissions(path, permissionSet));
}
public String getOwner() {
return getAttributes().owner();
}
public void setOwner(String owner) {
UserPrincipalLookupService service = path.getFileSystem().getUserPrincipalLookupService();
UserPrincipal principal = uncheck(() -> service.lookupPrincipalByName(owner));
uncheck(() -> Files.setOwner(path, principal));
}
public String getGroup() {
return getAttributes().group();
}
public void setGroup(String group) {
UserPrincipalLookupService service = path.getFileSystem().getUserPrincipalLookupService();
GroupPrincipal principal = uncheck(() -> service.lookupPrincipalByGroupName(group));
uncheck(() -> Files.getFileAttributeView(path, PosixFileAttributeView.class).setGroup(principal));
}
public Instant getLastModifiedTime() {
return getAttributes().lastModifiedTime();
}
public FileAttributes getAttributes() {
PosixFileAttributes attributes = uncheck(() ->
Files.getFileAttributeView(path, PosixFileAttributeView.class).readAttributes());
return new FileAttributes(attributes);
}
public Optional<FileAttributes> getAttributesIfExists() {
return IOExceptionUtil.ifExists(() -> getAttributes());
}
@Override
public String toString() {
return path.toString();
}
}
|