blob: 3c53609b84efa3f74b5bd03c66dac02ebf4cd681 (
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
|
// 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.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;
import static com.yahoo.yolean.Exceptions.uncheck;
/**
* Utility for idempotent move of (any type of) file.
*
* @author hakonhall
*/
public class FileMover {
private static final Logger logger = Logger.getLogger(FileMover.class.getName());
private final Path source;
private final Path destination;
private final Set<CopyOption> moveOptions = new HashSet<>();
public FileMover(Path source, Path destination) {
this.source = source;
this.destination = destination;
}
public FileMover replaceExisting() {
moveOptions.add(StandardCopyOption.REPLACE_EXISTING);
return this;
}
public FileMover atomic() {
moveOptions.add(StandardCopyOption.ATOMIC_MOVE);
return this;
}
/**
* Move file.
*
* @return false if the source doesn't exist while the destination do.
* @see Files#move(Path, Path, CopyOption...) Files.move()
*/
public boolean converge(TaskContext context) {
if (!Files.exists(source) && Files.exists(destination)) return false;
uncheck(() -> Files.move(source, destination, moveOptions.toArray(CopyOption[]::new)));
context.recordSystemModification(logger, "Moved " + source + " to " + destination);
return true;
}
}
|