aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/AttributeSync.java
blob: 73eddd2bbe2a2fa23b84d608e3d8354ad91f22c4 (plain) (blame)
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
// Copyright Vespa.ai. 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<Integer> ownerId = Optional.empty();
    private Optional<Integer> groupId = 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<Integer> ownerId() {
        return ownerId;
    }

    public AttributeSync withOwnerId(int ownerId) {
        this.ownerId = Optional.of(ownerId);
        return this;
    }

    public Optional<Integer> groupId() {
        return groupId;
    }

    public AttributeSync withGroupId(int groupId) {
        this.groupId = Optional.of(groupId);
        return this;
    }

    public AttributeSync with(PartialFileData fileData) {
        ownerId = fileData.getOwnerId();
        groupId = fileData.getGroupId();
        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,
                "user ID",
                ownerId,
                () -> currentAttributes.getOrThrow().ownerId(),
                path::setOwnerId);

        systemModified |= updateAttribute(
                context,
                "group ID",
                groupId,
                () -> currentAttributes.getOrThrow().groupId(),
                path::setGroupId);

        systemModified |= updateAttribute(
                context,
                "permissions",
                permissions,
                () -> currentAttributes.getOrThrow().permissions(),
                path::setPermissions);

        return systemModified;
    }

    private <T> boolean updateAttribute(TaskContext context,
                                    String attributeName,
                                    Optional<T> wantedValue,
                                    Supplier<T> currentValueSupplier,
                                    Consumer<T> valueSetter) {
        if (wantedValue.isEmpty()) {
            return false;
        }

        T 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,
                        currentValue,
                        wantedValue.get()));

        valueSetter.accept(wantedValue.get());

        return true;
    }
}