]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/jmh/src/main/java/org/rocksdb/util/FileUtils.java
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / rocksdb / java / jmh / src / main / java / org / rocksdb / util / FileUtils.java
1 /**
2 * Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
3 * This source code is licensed under both the GPLv2 (found in the
4 * COPYING file in the root directory) and Apache 2.0 License
5 * (found in the LICENSE.Apache file in the root directory).
6 */
7 package org.rocksdb.util;
8
9 import java.io.IOException;
10 import java.nio.file.FileVisitResult;
11 import java.nio.file.Files;
12 import java.nio.file.Path;
13 import java.nio.file.SimpleFileVisitor;
14 import java.nio.file.attribute.BasicFileAttributes;
15
16 public final class FileUtils {
17 private static final SimpleFileVisitor<Path> DELETE_DIR_VISITOR = new DeleteDirVisitor();
18
19 /**
20 * Deletes a path from the filesystem
21 *
22 * If the path is a directory its contents
23 * will be recursively deleted before it itself
24 * is deleted.
25 *
26 * Note that removal of a directory is not an atomic-operation
27 * and so if an error occurs during removal, some of the directories
28 * descendants may have already been removed
29 *
30 * @param path the path to delete.
31 *
32 * @throws IOException if an error occurs whilst removing a file or directory
33 */
34 public static void delete(final Path path) throws IOException {
35 if (!Files.isDirectory(path)) {
36 Files.deleteIfExists(path);
37 } else {
38 Files.walkFileTree(path, DELETE_DIR_VISITOR);
39 }
40 }
41
42 private static class DeleteDirVisitor extends SimpleFileVisitor<Path> {
43 @Override
44 public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
45 Files.deleteIfExists(file);
46 return FileVisitResult.CONTINUE;
47 }
48
49 @Override
50 public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
51 if (exc != null) {
52 throw exc;
53 }
54
55 Files.deleteIfExists(dir);
56 return FileVisitResult.CONTINUE;
57 }
58 }
59 }