Class PathUtils

java.lang.Object
org.apache.cassandra.io.util.PathUtils

public final class PathUtils extends Object
Vernacular: tryX means return false or 0L on any failure; XIfNotY means propagate any exceptions besides those caused by Y This class tries to apply uniform IOException handling, and does not propagate IOException except for NoSuchFileException. Any harmless/application error exceptions are propagated as UncheckedIOException, and anything else as an FSReadError or FSWriteError. Semantically this is a little incoherent throughout the codebase, as we intercept IOException haphazardly and treaat it inconsistently - we should ideally migrate to using propagate(IOException, Path, boolean) et al globally.
  • Method Details

    • newReadChannel

      public static FileChannel newReadChannel(Path path) throws NoSuchFileException
      Throws:
      NoSuchFileException
    • newReadWriteChannel

      public static FileChannel newReadWriteChannel(Path path) throws NoSuchFileException
      Throws:
      NoSuchFileException
    • newWriteOverwriteChannel

      public static FileChannel newWriteOverwriteChannel(Path path) throws NoSuchFileException
      Throws:
      NoSuchFileException
    • newWriteAppendChannel

      public static FileChannel newWriteAppendChannel(Path path) throws NoSuchFileException
      Throws:
      NoSuchFileException
    • setDeletionListener

      public static void setDeletionListener(Consumer<Path> newOnDeletion)
    • filename

      public static String filename(Path path)
    • list

      public static <T> T[] list(Path path, Function<Stream<Path>,Stream<T>> transform, IntFunction<T[]> arrayFactory)
    • tryList

      public static <T extends Throwable, V> V[] tryList(Path path, Function<Stream<Path>,Stream<V>> transform, IntFunction<V[]> arrayFactory, net.openhft.chronicle.core.util.ThrowingFunction<IOException,V[],T> orElse) throws T
      Throws:
      T extends Throwable
    • forEach

      public static void forEach(Path path, Consumer<Path> forEach)
    • forEachRecursive

      public static void forEachRecursive(Path path, Consumer<Path> forEach)
    • tryGetLength

      public static long tryGetLength(Path path)
    • tryGetLastModified

      public static long tryGetLastModified(Path path)
    • trySetLastModified

      public static boolean trySetLastModified(Path path, long lastModified)
    • trySetReadable

      public static boolean trySetReadable(Path path, boolean readable)
    • trySetWritable

      public static boolean trySetWritable(Path path, boolean writeable)
    • trySetExecutable

      public static boolean trySetExecutable(Path path, boolean executable)
    • trySet

      public static boolean trySet(Path path, PosixFilePermission permission, boolean set)
    • delete

      public static Throwable delete(Path file, Throwable accumulate)
    • delete

      public static void delete(Path file)
    • deleteIfExists

      public static void deleteIfExists(Path file)
    • tryDelete

      public static boolean tryDelete(Path file)
    • delete

      public static void delete(Path file, @Nullable com.google.common.util.concurrent.RateLimiter rateLimiter)
    • delete

      public static Throwable delete(Path file, Throwable accumulate, @Nullable com.google.common.util.concurrent.RateLimiter rateLimiter)
    • deleteRecursive

      public static void deleteRecursive(Path path)
      Deletes all files and subdirectories under "path".
      Parameters:
      path - file to be deleted
      Throws:
      FSWriteError - if any part of the tree cannot be deleted
    • deleteRecursive

      public static void deleteRecursive(Path path, com.google.common.util.concurrent.RateLimiter rateLimiter)
      Deletes all files and subdirectories under "path".
      Parameters:
      path - file to be deleted
      Throws:
      FSWriteError - if any part of the tree cannot be deleted
    • deleteRecursiveOnExit

      public static void deleteRecursiveOnExit(Path dir)
      Schedules deletion of all file and subdirectories under "dir" on JVM shutdown.
      Parameters:
      dir - Directory to be deleted
    • deleteOnExit

      public static void deleteOnExit(Path file)
      Schedules deletion of the file only on JVM shutdown.
      Parameters:
      file - File to be deleted
    • tryRename

      public static boolean tryRename(Path from, Path to)
    • rename

      public static void rename(Path from, Path to)
    • exists

      public static boolean exists(Path path)
    • isDirectory

      public static boolean isDirectory(Path path)
    • isFile

      public static boolean isFile(Path path)
    • createFileIfNotExists

      public static boolean createFileIfNotExists(Path path)
      Parameters:
      path - create file if not exists
      Returns:
      true if a new file was created
      Throws:
      IOError - if cannot perform the operation
    • createDirectoryIfNotExists

      public static boolean createDirectoryIfNotExists(Path path)
      Parameters:
      path - create directory if not exists
      Returns:
      true if a new directory was created
      Throws:
      IOError - if cannot perform the operation
    • createDirectoriesIfNotExists

      public static boolean createDirectoriesIfNotExists(Path path)
      Parameters:
      path - create directory (and parents) if not exists
      Returns:
      true if a new directory was created
      Throws:
      IOError - if cannot perform the operation
    • tryCreateDirectory

      public static boolean tryCreateDirectory(Path path)
      Parameters:
      path - create directory if not exists and action can be performed
      Returns:
      true if a new directory was created, false otherwise (for any reason)
    • tryCreateDirectories

      public static boolean tryCreateDirectories(Path path)
      Parameters:
      path - create directory (and parents) if not exists and action can be performed
      Returns:
      true if the new directory was created, false otherwise (for any reason)
    • findExistingAncestor

      public static Path findExistingAncestor(Path file)
      Returns:
      file if exists, otherwise nearest parent that exists; null if nothing in path exists
    • toCanonicalPath

      public static Path toCanonicalPath(Path file)
      1) Convert to an absolute path without redundant path elements; 2) If the file exists, resolve any links to the underlying fille; 3) If the file does not exist, find the first ancestor that does and resolve the path from there
    • isContained

      public static boolean isContained(Path folder, Path file)
      Return true if file's canonical path is contained in folder's canonical path. Propagates any exceptions encountered finding canonical paths.
    • runOnExitThreadsAndClear

      public static void runOnExitThreadsAndClear()
    • clearOnExitThreads

      public static void clearOnExitThreads()
    • tryGetSpace

      public static long tryGetSpace(Path path, PathUtils.IOToLongFunction<FileStore> getSpace)
      Returns the number of bytes (determined by the provided MethodHandle) on the specified partition.

      This method handles large file system by returning Long.MAX_VALUE if the number of available bytes overflow. See JDK-8179320 for more information

      Parameters:
      path - the partition (or a file within it)
    • tryGetSpace

      public static long tryGetSpace(Path path, PathUtils.IOToLongFunction<FileStore> getSpace, Consumer<IOException> orElse)
    • propagateUnchecked

      public static RuntimeException propagateUnchecked(IOException ioe, Path path, boolean write)
      propagate an IOException as an FSWriteError, FSReadError or UncheckedIOException
    • propagateUnchecked

      public static RuntimeException propagateUnchecked(String message, IOException ioe, Path path, boolean write)
      propagate an IOException as an FSWriteError, FSReadError or UncheckedIOException
    • propagateUncheckedOrNoSuchFileException

      public static NoSuchFileException propagateUncheckedOrNoSuchFileException(IOException ioe, Path path, boolean write) throws NoSuchFileException
      propagate an IOException as an FSWriteError, FSReadError or UncheckedIOException - except for NoSuchFileException
      Throws:
      NoSuchFileException
    • propagate

      public static <E extends IOException> E propagate(E ioe, Path path, boolean write) throws E
      propagate an IOException either as itself or an FSWriteError or FSReadError
      Throws:
      E extends IOException