aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/FileDeleter.java
blob: a443e683df02ee0cfa5b6fd0fdc315569f148e3d (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
// 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.Files;
import java.nio.file.Path;
import java.util.logging.Logger;

import static com.yahoo.yolean.Exceptions.uncheck;

/**
 * Deletes a file or empty directory.
 *
 * @author hakonhall
 */
public class FileDeleter {
    private static final Logger logger = Logger.getLogger(FileDeleter.class.getName());

    private final Path path;

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

    public boolean converge(TaskContext context) {
        boolean deleted = uncheck(() -> Files.deleteIfExists(path));
        if (deleted) {
            context.recordSystemModification(logger, "Deleted " + path);
        }

        return deleted;
    }
}