Package org.apache.cassandra.io.util
Class PathUtils
java.lang.Object
org.apache.cassandra.io.util.PathUtils
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.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfacestatic interface -
Method Summary
Modifier and TypeMethodDescriptionstatic voidstatic booleanstatic booleanstatic booleancreateFileIfNotExists(Path path) static voidstatic voidstatic Throwablestatic Throwablestatic voiddeleteIfExists(Path file) static voiddeleteOnExit(Path file) Schedules deletion of the file only on JVM shutdown.static voiddeleteRecursive(Path path) Deletes all files and subdirectories under "path".static voiddeleteRecursive(Path path, com.google.common.util.concurrent.RateLimiter rateLimiter) Deletes all files and subdirectories under "path".static voidSchedules deletion of all file and subdirectories under "dir" on JVM shutdown.static booleanstatic Stringstatic PathfindExistingAncestor(Path file) static voidstatic voidforEachRecursive(Path path, Consumer<Path> forEach) static booleanisContained(Path folder, Path file) Return true if file's canonical path is contained in folder's canonical path.static booleanisDirectory(Path path) static booleanstatic <T> T[]static FileChannelnewReadChannel(Path path) static FileChannelnewReadWriteChannel(Path path) static FileChannelnewWriteAppendChannel(Path path) static FileChannelnewWriteOverwriteChannel(Path path) static <E extends IOException>
Epropagate an IOException either as itself or an FSWriteError or FSReadErrorstatic RuntimeExceptionpropagateUnchecked(IOException ioe, Path path, boolean write) propagate an IOException as an FSWriteError, FSReadError or UncheckedIOExceptionstatic RuntimeExceptionpropagateUnchecked(String message, IOException ioe, Path path, boolean write) propagate an IOException as an FSWriteError, FSReadError or UncheckedIOExceptionstatic NoSuchFileExceptionpropagateUncheckedOrNoSuchFileException(IOException ioe, Path path, boolean write) propagate an IOException as an FSWriteError, FSReadError or UncheckedIOException - except for NoSuchFileExceptionstatic voidstatic voidstatic voidsetDeletionListener(Consumer<Path> newOnDeletion) static PathtoCanonicalPath(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 therestatic booleantryCreateDirectories(Path path) static booleantryCreateDirectory(Path path) static booleanstatic longtryGetLastModified(Path path) static longtryGetLength(Path path) static longtryGetSpace(Path path, PathUtils.IOToLongFunction<FileStore> getSpace) Returns the number of bytes (determined by the provided MethodHandle) on the specified partition.static longtryGetSpace(Path path, PathUtils.IOToLongFunction<FileStore> getSpace, Consumer<IOException> orElse) 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) static booleanstatic booleantrySet(Path path, PosixFilePermission permission, boolean set) static booleantrySetExecutable(Path path, boolean executable) static booleantrySetLastModified(Path path, long lastModified) static booleantrySetReadable(Path path, boolean readable) static booleantrySetWritable(Path path, boolean writeable)
-
Method Details
-
newReadChannel
- Throws:
NoSuchFileException
-
newReadWriteChannel
- Throws:
NoSuchFileException
-
newWriteOverwriteChannel
- Throws:
NoSuchFileException
-
newWriteAppendChannel
- Throws:
NoSuchFileException
-
setDeletionListener
-
filename
-
list
-
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, throws TV[], T> orElse) - Throws:
T extends Throwable
-
forEach
-
forEachRecursive
-
tryGetLength
-
tryGetLastModified
-
trySetLastModified
-
trySetReadable
-
trySetWritable
-
trySetExecutable
-
trySet
-
delete
-
delete
-
deleteIfExists
-
tryDelete
-
delete
public static void delete(Path file, @Nullable com.google.common.util.concurrent.RateLimiter rateLimiter) -
delete
-
deleteRecursive
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
Schedules deletion of all file and subdirectories under "dir" on JVM shutdown.- Parameters:
dir- Directory to be deleted
-
deleteOnExit
Schedules deletion of the file only on JVM shutdown.- Parameters:
file- File to be deleted
-
tryRename
-
rename
-
exists
-
isDirectory
-
isFile
-
createFileIfNotExists
- Parameters:
path- create file if not exists- Returns:
- true if a new file was created
- Throws:
IOError- if cannot perform the operation
-
createDirectoryIfNotExists
- Parameters:
path- create directory if not exists- Returns:
- true if a new directory was created
- Throws:
IOError- if cannot perform the operation
-
createDirectoriesIfNotExists
- 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
- 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
- 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
- Returns:
- file if exists, otherwise nearest parent that exists; null if nothing in path exists
-
toCanonicalPath
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
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
Returns the number of bytes (determined by the provided MethodHandle) on the specified partition.This method handles large file system by returning
Long.MAX_VALUEif 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
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
propagate an IOException either as itself or an FSWriteError or FSReadError- Throws:
E extends IOException
-