summaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/MakeDirectoryTask.java
blob: 522abb8124846e450b71359ab39ff4245e723254 (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
// 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;

import com.yahoo.vespa.hosted.node.admin.io.FileSystem;

import java.nio.file.Path;

public class MakeDirectoryTask implements Task {
    private final Path path;
    private boolean withParents = false;

    public MakeDirectoryTask(Path path) {
        this.path = path;
    }

    public MakeDirectoryTask withParents() {
        this.withParents = true;
        return this;
    }

    Path getPath() {
        return path;
    }

    boolean getWithParents() {
        return withParents;
    }

    private boolean makeDirectory(FileSystem fileSystem,
                                  Path directory,
                                  boolean withParents) {
        if (fileSystem.isDirectory(directory)) {
            return false;
        }

        if (withParents) {
            makeDirectory(fileSystem, directory.getParent(), withParents);
        }

        fileSystem.createDirectory(directory);

        return true;
    }

    @Override
    public boolean execute(TaskContext context) {
        return makeDirectory(context.getFileSystem(), path, withParents);
    }
}