summaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/io/FileSystem.java
blob: c5c2df9e38edb50832acc0845639a41c971cc5b3 (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
// 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.io;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
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.util.Set;

/**
 * File system operations to be mocked in unit tests.
 */
public class FileSystem {
    public FileSystemPath withPath(Path path) {
        return new FileSystemPath(this, path);
    }

    public boolean isDirectory(Path path) {
        return path.toFile().isDirectory();
    }

    public boolean isRegularFile(Path path) {
        return path.toFile().isFile();
    }

    public void createDirectory(Path path, FileAttribute<?>... attributes) {
        uncheck(() -> Files.createDirectory(path, attributes));
    }

    public String readUtf8File(Path path) {
        byte[] byteContent = uncheck(() -> Files.readAllBytes(path));
        return new String(byteContent, StandardCharsets.UTF_8);
    }

    public void writeUtf8File(Path path, String content, OpenOption... options) {
        byte[] contentInUtf8 = content.getBytes(StandardCharsets.UTF_8);
        uncheck(() -> Files.write(path, contentInUtf8, options));
    }

    private PosixFileAttributes getAttributes(Path path) {
        return uncheck(() ->
                Files.getFileAttributeView(path, PosixFileAttributeView.class).readAttributes());
    }

    public String getPermissions(Path path) {
        return PosixFilePermissions.toString(getAttributes(path).permissions());
    }

    /**
     * @param permissions Example: "rwxr-x---" means rwx for owner, rx for group,
     *                    and no permissions for others.
     */
    public void setPermissions(Path path, 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(Path path) {
        return getAttributes(path).owner().getName();
    }

    public void setOwner(Path path, String owner) {
        UserPrincipalLookupService service = path.getFileSystem().getUserPrincipalLookupService();
        UserPrincipal principal = uncheck(() -> service.lookupPrincipalByName(owner));
        uncheck(() -> Files.setOwner(path, principal));
    }

    public String getGroup(Path path) {
        return getAttributes(path).group().getName();
    }

    public void setGroup(Path path, String group) {
        UserPrincipalLookupService service = path.getFileSystem().getUserPrincipalLookupService();
        GroupPrincipal principal = uncheck(() -> service.lookupPrincipalByGroupName(group));
        uncheck(() -> Files.getFileAttributeView(path, PosixFileAttributeView.class).setGroup(principal));
    }

    @FunctionalInterface
    private interface SupplierThrowingIOException<T> {
        T get() throws IOException;
    }

    private static <T> T uncheck(SupplierThrowingIOException<T> supplier) {
        try {
            return supplier.get();
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    @FunctionalInterface
    private interface RunnableThrowingIOException<T> {
        void run() throws IOException;
    }

    private static <T> void uncheck(RunnableThrowingIOException<T> runnable) {
        try {
            runnable.run();
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
}