Package io.camunda.zeebe.util
Class FileUtil
java.lang.Object
io.camunda.zeebe.util.FileUtil
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcopySnapshot(Path snapshotDirectory, Path runtimeDirectory) static voiddeleteFolder(String path) static voiddeleteFolder(Path folder) static voiddeleteFolderContents(Path folder) A variant ofdeleteFolder(String)} which deletes everything but the top level directory.static voiddeleteFolderIfExists(Path folder) A variant ofdeleteFolder(Path), which ignores missing files.static voidensureDirectoryExists(Path directory) static voidEnsures updates to the given path are flushed to disk, with the same guarantees asFileChannel.force(boolean).static voidflushDirectory(Path path) This method flushes the given directory.static booleanReturn true if directory does not exists, or if it is empty.static voidmoveDurably(Path source, Path target, CopyOption... options) Moves the givensourcefile to thetargetlocation, flushing the target's parent directory afterwards to guarantee that the file will be visible and avoid the classic 0-length problem.
-
Method Details
-
flush
Ensures updates to the given path are flushed to disk, with the same guarantees asFileChannel.force(boolean). This can be used for files only; for directories, callflushDirectory(Path). Note that if you already have a file channel open, then use theFileChannel.force(boolean)method directly.- Parameters:
path- the path to synchronize- Throws:
IOException- can be thrown on opening and on flushing the file
-
flushDirectory
This method flushes the given directory. Note that on Windows this is a no-op, as Windows does not allow to flush directories except when opening the sys handle with a specific backup flag, which is not possible to do without custom JNI code. However, as the main use case here is for durability to sync the parent directory after creating a new file or renaming it, this is safe to do as Windows (or rather NTFS) does not need this.- Parameters:
path- the path to synchronize- Throws:
IOException- can be thrown on opening and on flushing the file
-
moveDurably
Moves the givensourcefile to thetargetlocation, flushing the target's parent directory afterwards to guarantee that the file will be visible and avoid the classic 0-length problem.- Parameters:
source- the file or directory to movetarget- the new path the file or directory should have afteroptions- copy options, e.g.StandardCopyOption- Throws:
IOException- on either move or flush error
-
deleteFolder
- Throws:
IOException
-
ensureDirectoryExists
- Throws:
IOException
-
deleteFolderIfExists
A variant ofdeleteFolder(Path), which ignores missing files. Inspired fromFiles.deleteIfExists(Path)which is implemented in much the same way. To be preferred overdeleteFolder(Path)preceded by aFiles.exists(Path, LinkOption...), as that is more prone to race conditions.- Parameters:
folder- the directory to delete (if or any of its files exists)- Throws:
IOException- on failure to scan the directory and/or delete the file
-
deleteFolderContents
A variant ofdeleteFolder(String)} which deletes everything but the top level directory. This can be useful for deleting everything inside a folder that is itself not delete-able, for example because the folder is mounted as a docker volume- Parameters:
folder- the directory which contents should be deleted- Throws:
IOException- on failure to scan the directory and/or delete the file
-
deleteFolder
- Throws:
IOException
-
copySnapshot
- Throws:
Exception
-
isEmpty
Return true if directory does not exists, or if it is empty. Returns false if the path is not a directory- Throws:
IOException
-