Class FileUtil

java.lang.Object
io.camunda.zeebe.util.FileUtil

public final class FileUtil extends Object
  • Method Details

    • flush

      public static void flush(Path path) throws IOException
      Ensures updates to the given path are flushed to disk, with the same guarantees as FileChannel.force(boolean). This can be used for files only; for directories, call flushDirectory(Path). Note that if you already have a file channel open, then use the FileChannel.force(boolean) method directly.
      Parameters:
      path - the path to synchronize
      Throws:
      IOException - can be thrown on opening and on flushing the file
    • flushDirectory

      public static void flushDirectory(Path path) throws IOException
      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

      public static void moveDurably(Path source, Path target, CopyOption... options) throws IOException
      Moves the given source file to the target location, 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 move
      target - the new path the file or directory should have after
      options - copy options, e.g. StandardCopyOption
      Throws:
      IOException - on either move or flush error
    • deleteFolder

      public static void deleteFolder(String path) throws IOException
      Throws:
      IOException
    • ensureDirectoryExists

      public static void ensureDirectoryExists(Path directory) throws IOException
      Throws:
      IOException
    • deleteFolderIfExists

      public static void deleteFolderIfExists(Path folder) throws IOException
      A variant of deleteFolder(Path), which ignores missing files. Inspired from Files.deleteIfExists(Path) which is implemented in much the same way. To be preferred over deleteFolder(Path) preceded by a Files.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
    • deleteFolder

      public static void deleteFolder(Path folder) throws IOException
      Throws:
      IOException
    • copySnapshot

      public static void copySnapshot(Path snapshotDirectory, Path runtimeDirectory) throws Exception
      Throws:
      Exception