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
122
123
124
125
126
|
// 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 com.yahoo.vespa.hosted.node.admin.component.TaskContext;
import java.nio.file.Path;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.logging.Logger;
/**
* Class to converge file/directory attributes like owner and permissions to wanted values.
* Typically used by higher abstraction layers working on files (FileSync/FileWriter) or
* directories (MakeDirectory).
*
* @author hakonhall
*/
public class AttributeSync {
private static final Logger logger = Logger.getLogger(AttributeSync.class.getName());
private final UnixPath path;
private Optional<String> owner = Optional.empty();
private Optional<String> group = Optional.empty();
private Optional<String> permissions = Optional.empty();
public AttributeSync(Path path) {
this.path = new UnixPath(path);
}
public Optional<String> getPermissions() {
return permissions;
}
public AttributeSync withPermissions(String permissions) {
this.permissions = Optional.of(permissions);
return this;
}
public Optional<String> getOwner() {
return owner;
}
public AttributeSync withOwner(String owner) {
this.owner = Optional.of(owner);
return this;
}
public Optional<String> getGroup() {
return group;
}
public AttributeSync withGroup(String group) {
this.group = Optional.of(group);
return this;
}
public AttributeSync with(PartialFileData fileData) {
owner = fileData.getOwner();
group = fileData.getGroup();
permissions = fileData.getPermissions();
return this;
}
public boolean converge(TaskContext context) {
return converge(context, new FileAttributesCache(path));
}
/**
* Path must exist before calling converge.
*/
public boolean converge(TaskContext context, FileAttributesCache currentAttributes) {
boolean systemModified = updateAttribute(
context,
"owner",
owner,
() -> currentAttributes.get().owner(),
path::setOwner);
systemModified |= updateAttribute(
context,
"group",
group,
() -> currentAttributes.get().group(),
path::setGroup);
systemModified |= updateAttribute(
context,
"permissions",
permissions,
() -> currentAttributes.get().permissions(),
path::setPermissions);
return systemModified;
}
private boolean updateAttribute(TaskContext context,
String attributeName,
Optional<String> wantedValue,
Supplier<String> currentValueSupplier,
Consumer<String> valueSetter) {
if (!wantedValue.isPresent()) {
return false;
}
String currentValue = currentValueSupplier.get();
if (Objects.equals(currentValue, wantedValue.get())) {
return false;
}
context.recordSystemModification(
logger,
String.format("Changing %s of %s from %s to %s",
attributeName,
path.toString(),
currentValue,
wantedValue.get()));
valueSetter.accept(wantedValue.get());
return true;
}
}
|