|
|
|
@ -47,7 +47,6 @@ package org.eclipse.jgit.util;
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.nio.channels.FileLock; |
|
|
|
|
import java.nio.file.AtomicMoveNotSupportedException; |
|
|
|
|
import java.nio.file.CopyOption; |
|
|
|
|
import java.nio.file.Files; |
|
|
|
@ -118,10 +117,9 @@ public class FileUtils {
|
|
|
|
|
* @param f |
|
|
|
|
* {@code File} to be converted to {@code Path} |
|
|
|
|
* @return the path represented by the file |
|
|
|
|
* @throws IOException |
|
|
|
|
* @throws java.io.IOException |
|
|
|
|
* in case the path represented by the file is not valid ( |
|
|
|
|
* {@link java.nio.file.InvalidPathException}) |
|
|
|
|
* |
|
|
|
|
* @since 4.10 |
|
|
|
|
*/ |
|
|
|
|
public static Path toPath(final File f) throws IOException { |
|
|
|
@ -137,11 +135,11 @@ public class FileUtils {
|
|
|
|
|
* |
|
|
|
|
* @param f |
|
|
|
|
* {@code File} to be deleted |
|
|
|
|
* @throws IOException |
|
|
|
|
* @throws java.io.IOException |
|
|
|
|
* if deletion of {@code f} fails. This may occur if {@code f} |
|
|
|
|
* didn't exist when the method was called. This can therefore |
|
|
|
|
* cause IOExceptions during race conditions when multiple |
|
|
|
|
* concurrent threads all try to delete the same file. |
|
|
|
|
* cause java.io.IOExceptions during race conditions when |
|
|
|
|
* multiple concurrent threads all try to delete the same file. |
|
|
|
|
*/ |
|
|
|
|
public static void delete(final File f) throws IOException { |
|
|
|
|
delete(f, NONE); |
|
|
|
@ -157,12 +155,12 @@ public class FileUtils {
|
|
|
|
|
* a subtree, {@code RETRY} to retry when deletion failed. |
|
|
|
|
* Retrying may help if the underlying file system doesn't allow |
|
|
|
|
* deletion of files being read by another thread. |
|
|
|
|
* @throws IOException |
|
|
|
|
* @throws java.io.IOException |
|
|
|
|
* if deletion of {@code f} fails. This may occur if {@code f} |
|
|
|
|
* didn't exist when the method was called. This can therefore |
|
|
|
|
* cause IOExceptions during race conditions when multiple |
|
|
|
|
* concurrent threads all try to delete the same file. This |
|
|
|
|
* exception is not thrown when IGNORE_ERRORS is set. |
|
|
|
|
* cause java.io.IOExceptions during race conditions when |
|
|
|
|
* multiple concurrent threads all try to delete the same file. |
|
|
|
|
* This exception is not thrown when IGNORE_ERRORS is set. |
|
|
|
|
*/ |
|
|
|
|
public static void delete(final File f, int options) throws IOException { |
|
|
|
|
FS fs = FS.DETECTED; |
|
|
|
@ -239,7 +237,7 @@ public class FileUtils {
|
|
|
|
|
* the old {@code File} |
|
|
|
|
* @param dst |
|
|
|
|
* the new {@code File} |
|
|
|
|
* @throws IOException |
|
|
|
|
* @throws java.io.IOException |
|
|
|
|
* if the rename has failed |
|
|
|
|
* @since 3.0 |
|
|
|
|
*/ |
|
|
|
@ -249,18 +247,20 @@ public class FileUtils {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Rename a file or folder using the passed {@link CopyOption}s. If the |
|
|
|
|
* rename fails and if we are running on a filesystem where it makes sense |
|
|
|
|
* to repeat a failing rename then repeat the rename operation up to 9 times |
|
|
|
|
* with 100ms sleep time between two calls. Furthermore if the destination |
|
|
|
|
* exists and is a directory hierarchy with only directories in it, the |
|
|
|
|
* whole directory hierarchy will be deleted. If the target represents a |
|
|
|
|
* non-empty directory structure, empty subdirectories within that structure |
|
|
|
|
* may or may not be deleted even if the method fails. Furthermore if the |
|
|
|
|
* destination exists and is a file then the file will be replaced if |
|
|
|
|
* {@link StandardCopyOption#REPLACE_EXISTING} has been set. If |
|
|
|
|
* {@link StandardCopyOption#ATOMIC_MOVE} has been set the rename will be |
|
|
|
|
* done atomically or fail with an {@link AtomicMoveNotSupportedException} |
|
|
|
|
* Rename a file or folder using the passed |
|
|
|
|
* {@link java.nio.file.CopyOption}s. If the rename fails and if we are |
|
|
|
|
* running on a filesystem where it makes sense to repeat a failing rename |
|
|
|
|
* then repeat the rename operation up to 9 times with 100ms sleep time |
|
|
|
|
* between two calls. Furthermore if the destination exists and is a |
|
|
|
|
* directory hierarchy with only directories in it, the whole directory |
|
|
|
|
* hierarchy will be deleted. If the target represents a non-empty directory |
|
|
|
|
* structure, empty subdirectories within that structure may or may not be |
|
|
|
|
* deleted even if the method fails. Furthermore if the destination exists |
|
|
|
|
* and is a file then the file will be replaced if |
|
|
|
|
* {@link java.nio.file.StandardCopyOption#REPLACE_EXISTING} has been set. |
|
|
|
|
* If {@link java.nio.file.StandardCopyOption#ATOMIC_MOVE} has been set the |
|
|
|
|
* rename will be done atomically or fail with an |
|
|
|
|
* {@link java.nio.file.AtomicMoveNotSupportedException} |
|
|
|
|
* |
|
|
|
|
* @param src |
|
|
|
|
* the old file |
|
|
|
@ -268,10 +268,10 @@ public class FileUtils {
|
|
|
|
|
* the new file |
|
|
|
|
* @param options |
|
|
|
|
* options to pass to |
|
|
|
|
* {@link Files#move(java.nio.file.Path, java.nio.file.Path, CopyOption...)} |
|
|
|
|
* @throws AtomicMoveNotSupportedException |
|
|
|
|
* {@link java.nio.file.Files#move(java.nio.file.Path, java.nio.file.Path, CopyOption...)} |
|
|
|
|
* @throws java.nio.file.AtomicMoveNotSupportedException |
|
|
|
|
* if file cannot be moved as an atomic file system operation |
|
|
|
|
* @throws IOException |
|
|
|
|
* @throws java.io.IOException |
|
|
|
|
* @since 4.1 |
|
|
|
|
*/ |
|
|
|
|
public static void rename(final File src, final File dst, |
|
|
|
@ -314,11 +314,12 @@ public class FileUtils {
|
|
|
|
|
* |
|
|
|
|
* @param d |
|
|
|
|
* directory to be created |
|
|
|
|
* @throws IOException |
|
|
|
|
* @throws java.io.IOException |
|
|
|
|
* if creation of {@code d} fails. This may occur if {@code d} |
|
|
|
|
* did exist when the method was called. This can therefore |
|
|
|
|
* cause IOExceptions during race conditions when multiple |
|
|
|
|
* concurrent threads all try to create the same directory. |
|
|
|
|
* cause java.io.IOExceptions during race conditions when |
|
|
|
|
* multiple concurrent threads all try to create the same |
|
|
|
|
* directory. |
|
|
|
|
*/ |
|
|
|
|
public static void mkdir(final File d) |
|
|
|
|
throws IOException { |
|
|
|
@ -333,11 +334,12 @@ public class FileUtils {
|
|
|
|
|
* @param skipExisting |
|
|
|
|
* if {@code true} skip creation of the given directory if it |
|
|
|
|
* already exists in the file system |
|
|
|
|
* @throws IOException |
|
|
|
|
* @throws java.io.IOException |
|
|
|
|
* if creation of {@code d} fails. This may occur if {@code d} |
|
|
|
|
* did exist when the method was called. This can therefore |
|
|
|
|
* cause IOExceptions during race conditions when multiple |
|
|
|
|
* concurrent threads all try to create the same directory. |
|
|
|
|
* cause java.io.IOExceptions during race conditions when |
|
|
|
|
* multiple concurrent threads all try to create the same |
|
|
|
|
* directory. |
|
|
|
|
*/ |
|
|
|
|
public static void mkdir(final File d, boolean skipExisting) |
|
|
|
|
throws IOException { |
|
|
|
@ -357,11 +359,12 @@ public class FileUtils {
|
|
|
|
|
* |
|
|
|
|
* @param d |
|
|
|
|
* directory to be created |
|
|
|
|
* @throws IOException |
|
|
|
|
* @throws java.io.IOException |
|
|
|
|
* if creation of {@code d} fails. This may occur if {@code d} |
|
|
|
|
* did exist when the method was called. This can therefore |
|
|
|
|
* cause IOExceptions during race conditions when multiple |
|
|
|
|
* concurrent threads all try to create the same directory. |
|
|
|
|
* cause java.io.IOExceptions during race conditions when |
|
|
|
|
* multiple concurrent threads all try to create the same |
|
|
|
|
* directory. |
|
|
|
|
*/ |
|
|
|
|
public static void mkdirs(final File d) throws IOException { |
|
|
|
|
mkdirs(d, false); |
|
|
|
@ -378,11 +381,12 @@ public class FileUtils {
|
|
|
|
|
* @param skipExisting |
|
|
|
|
* if {@code true} skip creation of the given directory if it |
|
|
|
|
* already exists in the file system |
|
|
|
|
* @throws IOException |
|
|
|
|
* @throws java.io.IOException |
|
|
|
|
* if creation of {@code d} fails. This may occur if {@code d} |
|
|
|
|
* did exist when the method was called. This can therefore |
|
|
|
|
* cause IOExceptions during race conditions when multiple |
|
|
|
|
* concurrent threads all try to create the same directory. |
|
|
|
|
* cause java.io.IOExceptions during race conditions when |
|
|
|
|
* multiple concurrent threads all try to create the same |
|
|
|
|
* directory. |
|
|
|
|
*/ |
|
|
|
|
public static void mkdirs(final File d, boolean skipExisting) |
|
|
|
|
throws IOException { |
|
|
|
@ -402,12 +406,12 @@ public class FileUtils {
|
|
|
|
|
* filesystem activities that might affect the file. |
|
|
|
|
* <p> |
|
|
|
|
* Note: this method should not be used for file-locking, as the resulting |
|
|
|
|
* protocol cannot be made to work reliably. The {@link FileLock} facility |
|
|
|
|
* should be used instead. |
|
|
|
|
* protocol cannot be made to work reliably. The |
|
|
|
|
* {@link java.nio.channels.FileLock} facility should be used instead. |
|
|
|
|
* |
|
|
|
|
* @param f |
|
|
|
|
* the file to be created |
|
|
|
|
* @throws IOException |
|
|
|
|
* @throws java.io.IOException |
|
|
|
|
* if the named file already exists or if an I/O error occurred |
|
|
|
|
*/ |
|
|
|
|
public static void createNewFile(File f) throws IOException { |
|
|
|
@ -424,7 +428,7 @@ public class FileUtils {
|
|
|
|
|
* @param target |
|
|
|
|
* the target of the symbolic link |
|
|
|
|
* @return the path to the symbolic link |
|
|
|
|
* @throws IOException |
|
|
|
|
* @throws java.io.IOException |
|
|
|
|
* @since 4.2 |
|
|
|
|
*/ |
|
|
|
|
public static Path createSymLink(File path, String target) |
|
|
|
@ -447,9 +451,12 @@ public class FileUtils {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Read target path of the symlink. |
|
|
|
|
* |
|
|
|
|
* @param path |
|
|
|
|
* a {@link java.io.File} object. |
|
|
|
|
* @return target path of the symlink, or null if it is not a symbolic link |
|
|
|
|
* @throws IOException |
|
|
|
|
* @throws java.io.IOException |
|
|
|
|
* @since 3.0 |
|
|
|
|
*/ |
|
|
|
|
public static String readSymLink(File path) throws IOException { |
|
|
|
@ -468,11 +475,13 @@ public class FileUtils {
|
|
|
|
|
* Create a temporary directory. |
|
|
|
|
* |
|
|
|
|
* @param prefix |
|
|
|
|
* prefix string |
|
|
|
|
* @param suffix |
|
|
|
|
* suffix string |
|
|
|
|
* @param dir |
|
|
|
|
* The parent dir, can be null to use system default temp dir. |
|
|
|
|
* @return the temp dir created. |
|
|
|
|
* @throws IOException |
|
|
|
|
* @throws java.io.IOException |
|
|
|
|
* @since 3.4 |
|
|
|
|
*/ |
|
|
|
|
public static File createTempDir(String prefix, String suffix, File dir) |
|
|
|
@ -491,18 +500,19 @@ public class FileUtils {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Relativize a path |
|
|
|
|
* |
|
|
|
|
* @deprecated Use the more-clearly-named |
|
|
|
|
* {@link FileUtils#relativizeNativePath(String, String)} |
|
|
|
|
* {@link org.eclipse.jgit.util.FileUtils#relativizeNativePath(String, String)} |
|
|
|
|
* instead, or directly call |
|
|
|
|
* {@link FileUtils#relativizePath(String, String, String, boolean)} |
|
|
|
|
* {@link org.eclipse.jgit.util.FileUtils#relativizePath(String, String, String, boolean)} |
|
|
|
|
* |
|
|
|
|
* Expresses <code>other</code> as a relative file path from |
|
|
|
|
* <code>base</code>. File-separator and case sensitivity are |
|
|
|
|
* based on the current file system. |
|
|
|
|
* |
|
|
|
|
* See also |
|
|
|
|
* {@link FileUtils#relativizePath(String, String, String, boolean)}. |
|
|
|
|
* |
|
|
|
|
* {@link org.eclipse.jgit.util.FileUtils#relativizePath(String, String, String, boolean)}. |
|
|
|
|
* @param base |
|
|
|
|
* Base path |
|
|
|
|
* @param other |
|
|
|
@ -516,15 +526,17 @@ public class FileUtils {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Expresses <code>other</code> as a relative file path from <code>base</code>. |
|
|
|
|
* File-separator and case sensitivity are based on the current file system. |
|
|
|
|
* Expresses <code>other</code> as a relative file path from |
|
|
|
|
* <code>base</code>. File-separator and case sensitivity are based on the |
|
|
|
|
* current file system. |
|
|
|
|
* |
|
|
|
|
* See also {@link FileUtils#relativizePath(String, String, String, boolean)}. |
|
|
|
|
* See also |
|
|
|
|
* {@link org.eclipse.jgit.util.FileUtils#relativizePath(String, String, String, boolean)}. |
|
|
|
|
* |
|
|
|
|
* @param base |
|
|
|
|
* Base path |
|
|
|
|
* @param other |
|
|
|
|
* Destination path |
|
|
|
|
* Destination path |
|
|
|
|
* @return Relative path from <code>base</code> to <code>other</code> |
|
|
|
|
* @since 4.8 |
|
|
|
|
*/ |
|
|
|
@ -533,15 +545,17 @@ public class FileUtils {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Expresses <code>other</code> as a relative file path from <code>base</code>. |
|
|
|
|
* File-separator and case sensitivity are based on Git's internal representation of files (which matches Unix). |
|
|
|
|
* Expresses <code>other</code> as a relative file path from |
|
|
|
|
* <code>base</code>. File-separator and case sensitivity are based on Git's |
|
|
|
|
* internal representation of files (which matches Unix). |
|
|
|
|
* |
|
|
|
|
* See also {@link FileUtils#relativizePath(String, String, String, boolean)}. |
|
|
|
|
* See also |
|
|
|
|
* {@link org.eclipse.jgit.util.FileUtils#relativizePath(String, String, String, boolean)}. |
|
|
|
|
* |
|
|
|
|
* @param base |
|
|
|
|
* Base path |
|
|
|
|
* @param other |
|
|
|
|
* Destination path |
|
|
|
|
* Destination path |
|
|
|
|
* @return Relative path from <code>base</code> to <code>other</code> |
|
|
|
|
* @since 4.8 |
|
|
|
|
*/ |
|
|
|
@ -620,6 +634,7 @@ public class FileUtils {
|
|
|
|
|
* Determine if an IOException is a Stale NFS File Handle |
|
|
|
|
* |
|
|
|
|
* @param ioe |
|
|
|
|
* an {@link java.io.IOException} object. |
|
|
|
|
* @return a boolean true if the IOException is a Stale NFS FIle Handle |
|
|
|
|
* @since 4.1 |
|
|
|
|
*/ |
|
|
|
@ -635,6 +650,7 @@ public class FileUtils {
|
|
|
|
|
* File Handle |
|
|
|
|
* |
|
|
|
|
* @param throwable |
|
|
|
|
* a {@link java.lang.Throwable} object. |
|
|
|
|
* @return a boolean true if the throwable or a cause in its causal chain is |
|
|
|
|
* a Stale NFS File Handle |
|
|
|
|
* @since 4.7 |
|
|
|
@ -697,9 +713,13 @@ public class FileUtils {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Set a file hidden (on Windows) |
|
|
|
|
* |
|
|
|
|
* @param file |
|
|
|
|
* a {@link java.io.File} object. |
|
|
|
|
* @param hidden |
|
|
|
|
* @throws IOException |
|
|
|
|
* a boolean. |
|
|
|
|
* @throws java.io.IOException |
|
|
|
|
* @since 4.1 |
|
|
|
|
*/ |
|
|
|
|
public static void setHidden(File file, boolean hidden) throws IOException { |
|
|
|
@ -708,9 +728,12 @@ public class FileUtils {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get file length |
|
|
|
|
* |
|
|
|
|
* @param file |
|
|
|
|
* a {@link java.io.File}. |
|
|
|
|
* @return length of the given file |
|
|
|
|
* @throws IOException |
|
|
|
|
* @throws java.io.IOException |
|
|
|
|
* @since 4.1 |
|
|
|
|
*/ |
|
|
|
|
public static long getLength(File file) throws IOException { |
|
|
|
@ -740,8 +763,11 @@ public class FileUtils {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Whether the given file can be executed. |
|
|
|
|
* |
|
|
|
|
* @param file |
|
|
|
|
* @return {@code true} if the given file can be executed |
|
|
|
|
* a {@link java.io.File} object. |
|
|
|
|
* @return {@code true} if the given file can be executed. |
|
|
|
|
* @since 4.1 |
|
|
|
|
*/ |
|
|
|
|
public static boolean canExecute(File file) { |
|
|
|
@ -783,9 +809,13 @@ public class FileUtils {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get file system attributes for the given file. |
|
|
|
|
* |
|
|
|
|
* @param fs |
|
|
|
|
* a {@link org.eclipse.jgit.util.FS} object. |
|
|
|
|
* @param file |
|
|
|
|
* @return file system attributes for the given file |
|
|
|
|
* a {@link java.io.File}. |
|
|
|
|
* @return file system attributes for the given file. |
|
|
|
|
* @since 4.1 |
|
|
|
|
*/ |
|
|
|
|
public static Attributes getFileAttributesPosix(FS fs, File file) { |
|
|
|
@ -816,8 +846,12 @@ public class FileUtils {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* NFC normalize a file (on Mac), otherwise do nothing |
|
|
|
|
* |
|
|
|
|
* @param file |
|
|
|
|
* @return on Mac: NFC normalized {@link File}, otherwise the passed file |
|
|
|
|
* a {@link java.io.File}. |
|
|
|
|
* @return on Mac: NFC normalized {@link java.io.File}, otherwise the passed |
|
|
|
|
* file |
|
|
|
|
* @since 4.1 |
|
|
|
|
*/ |
|
|
|
|
public static File normalize(File file) { |
|
|
|
@ -832,7 +866,10 @@ public class FileUtils {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* On Mac: get NFC normalized form of given name, otherwise the given name. |
|
|
|
|
* |
|
|
|
|
* @param name |
|
|
|
|
* a {@link java.lang.String} object. |
|
|
|
|
* @return on Mac: NFC normalized form of given name |
|
|
|
|
* @since 4.1 |
|
|
|
|
*/ |
|
|
|
@ -846,16 +883,16 @@ public class FileUtils {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Best-effort variation of {@link File#getCanonicalFile()} returning the |
|
|
|
|
* input file if the file cannot be canonicalized instead of throwing |
|
|
|
|
* {@link IOException}. |
|
|
|
|
* Best-effort variation of {@link java.io.File#getCanonicalFile()} |
|
|
|
|
* returning the input file if the file cannot be canonicalized instead of |
|
|
|
|
* throwing {@link java.io.IOException}. |
|
|
|
|
* |
|
|
|
|
* @param file |
|
|
|
|
* to be canonicalized; may be {@code null} |
|
|
|
|
* @return canonicalized file, or the unchanged input file if |
|
|
|
|
* canonicalization failed or if {@code file == null} |
|
|
|
|
* @throws SecurityException |
|
|
|
|
* if {@link File#getCanonicalFile()} throws one |
|
|
|
|
* @throws java.lang.SecurityException |
|
|
|
|
* if {@link java.io.File#getCanonicalFile()} throws one |
|
|
|
|
* @since 4.2 |
|
|
|
|
*/ |
|
|
|
|
public static File canonicalize(File file) { |
|
|
|
@ -873,6 +910,7 @@ public class FileUtils {
|
|
|
|
|
* Convert a path to String, replacing separators as necessary. |
|
|
|
|
* |
|
|
|
|
* @param file |
|
|
|
|
* a {@link java.io.File}. |
|
|
|
|
* @return file's path as a String |
|
|
|
|
* @since 4.10 |
|
|
|
|
*/ |
|
|
|
|