diff options
author | Valerij Fredriksen <freva@users.noreply.github.com> | 2021-10-07 11:19:46 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-07 11:19:46 +0200 |
commit | faf59afb680e8939bf24d1e8497cd9ec42a5b7fa (patch) | |
tree | ef10e7e1b78f4e7a436cf26effd02df5a208af09 /node-admin | |
parent | 5f96fad42a48c72aab4270b11c7ddda07a7851b0 (diff) | |
parent | e9292260b5bd2ec9ff7a8a8f8e4ae59da275e447 (diff) |
Merge pull request #19450 from vespa-engine/hakonhall/allow-disk-wipe-and-encryption-if-data-is-empty
Add UnixPath::isEmptyDirectory
Diffstat (limited to 'node-admin')
2 files changed, 28 insertions, 0 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java index aa88647fee8..431ff81dfad 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java @@ -7,6 +7,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; import java.nio.file.NoSuchFileException; +import java.nio.file.NotDirectoryException; import java.nio.file.OpenOption; import java.nio.file.Path; import java.nio.file.Paths; @@ -253,6 +254,17 @@ public class UnixPath { return this; } + /** @return false path does not exist, is not a directory, or has at least one entry. */ + public boolean isEmptyDirectory() { + try (var entryStream = Files.list(path)) { + return entryStream.findAny().isEmpty(); + } catch (NotDirectoryException | NoSuchFileException e) { + return false; + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + /** Lists the contents of this as a stream. Callers should use try-with to ensure that the stream is closed */ public Stream<UnixPath> listContentsOfDirectory() { try { diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPathTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPathTest.java index cabf716ce13..37bd92a8606 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPathTest.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPathTest.java @@ -127,6 +127,22 @@ public class UnixPathTest { } @Test + public void isEmptyDirectory() { + var path = new UnixPath((fs.getPath("/foo"))); + assertFalse(path.isEmptyDirectory()); + + path.writeUtf8File(""); + assertFalse(path.isEmptyDirectory()); + + path.deleteIfExists(); + path.createDirectory(); + assertTrue(path.isEmptyDirectory()); + + path.resolve("bar").writeUtf8File(""); + assertFalse(path.isEmptyDirectory()); + } + + @Test public void atomicWrite() { var path = new UnixPath(fs.getPath("/dir/foo")); path.createParents(); |