diff options
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java | 112 |
1 files changed, 64 insertions, 48 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java index f013e7e095..39c67f1b86 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java @@ -288,12 +288,14 @@ public class FileUtils { * @throws java.nio.file.AtomicMoveNotSupportedException * if file cannot be moved as an atomic file system operation * @throws java.io.IOException + * if an IO error occurred * @since 4.1 */ public static void rename(final File src, final File dst, CopyOption... options) throws AtomicMoveNotSupportedException, IOException { int attempts = FS.DETECTED.retryFailedLockFileCommit() ? 10 : 1; + IOException finalError = null; while (--attempts >= 0) { try { Files.move(toPath(src), toPath(dst), options); @@ -301,29 +303,35 @@ public class FileUtils { } catch (AtomicMoveNotSupportedException e) { throw e; } catch (IOException e) { - try { - if (!dst.delete()) { - delete(dst, EMPTY_DIRECTORIES_ONLY | RECURSIVE); + if (attempts == 0) { + // Only delete on the last attempt. + try { + if (!dst.delete()) { + delete(dst, EMPTY_DIRECTORIES_ONLY | RECURSIVE); + } + // On *nix there is no try, you do or do not + Files.move(toPath(src), toPath(dst), options); + return; + } catch (IOException e2) { + e2.addSuppressed(e); + finalError = e2; } - // On *nix there is no try, you do or do not - Files.move(toPath(src), toPath(dst), options); - return; - } catch (IOException e2) { - // ignore and continue retry } } - try { - Thread.sleep(100); - } catch (InterruptedException e) { - throw new IOException( - MessageFormat.format(JGitText.get().renameFileFailed, - src.getAbsolutePath(), dst.getAbsolutePath()), - e); + if (attempts > 0) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + throw new IOException(MessageFormat.format( + JGitText.get().renameFileFailed, + src.getAbsolutePath(), dst.getAbsolutePath()), e); + } } } throw new IOException( MessageFormat.format(JGitText.get().renameFileFailed, - src.getAbsolutePath(), dst.getAbsolutePath())); + src.getAbsolutePath(), dst.getAbsolutePath()), + finalError); } /** @@ -446,6 +454,7 @@ public class FileUtils { * the target of the symbolic link * @return the path to the symbolic link * @throws java.io.IOException + * if an IO error occurred * @since 4.2 */ public static Path createSymLink(File path, String target) @@ -474,6 +483,7 @@ public class FileUtils { * a {@link java.io.File} object. * @return target path of the symlink, or null if it is not a symbolic link * @throws java.io.IOException + * if an IO error occurred * @since 3.0 */ public static String readSymLink(File path) throws IOException { @@ -499,6 +509,7 @@ public class FileUtils { * The parent dir, can be null to use system default temp dir. * @return the temp dir created. * @throws java.io.IOException + * if an IO error occurred * @since 3.4 */ public static File createTempDir(String prefix, String suffix, File dir) @@ -620,11 +631,11 @@ public class FileUtils { } /** - * Determine if an IOException is a Stale NFS File Handle + * 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 + * @return a boolean true if the IOException is a stale NFS file handle * @since 4.1 */ public static boolean isStaleFileHandle(IOException ioe) { @@ -635,13 +646,13 @@ public class FileUtils { } /** - * Determine if a throwable or a cause in its causal chain is a Stale NFS - * File Handle + * Determine if a throwable or a cause in its causal chain is a stale NFS + * 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 + * a stale NFS file handle * @since 4.7 */ public static boolean isStaleFileHandleInCausalChain(Throwable throwable) { @@ -749,7 +760,10 @@ public class FileUtils { } /** + * Check if file is a symlink + * * @param file + * the file to be checked if it is a symbolic link * @return {@code true} if the passed file is a symbolic link */ static boolean isSymlink(File file) { @@ -757,21 +771,10 @@ public class FileUtils { } /** - * @param file - * @return lastModified attribute for given file, not following symbolic - * links - * @throws IOException - * @deprecated use {@link #lastModifiedInstant(Path)} instead which returns - * FileTime - */ - @Deprecated - static long lastModified(File file) throws IOException { - return Files.getLastModifiedTime(toPath(file), LinkOption.NOFOLLOW_LINKS) - .toMillis(); - } - - /** + * Get last modified timestamp of a file + * * @param path + * file path * @return lastModified attribute for given file, not following symbolic * links */ @@ -795,8 +798,10 @@ public class FileUtils { * Return all the attributes of a file, without following symbolic links. * * @param file + * the file * @return {@link BasicFileAttributes} of the file - * @throws IOException in case of any I/O errors accessing the file + * @throws IOException + * in case of any I/O errors accessing the file * * @since 4.5.6 */ @@ -807,21 +812,12 @@ public class FileUtils { /** * Set the last modified time of a file system object. * - * @param file - * @param time - * @throws IOException - */ - @Deprecated - static void setLastModified(File file, long time) throws IOException { - Files.setLastModifiedTime(toPath(file), FileTime.fromMillis(time)); - } - - /** - * Set the last modified time of a file system object. - * * @param path + * file path * @param time + * last modified timestamp of the file * @throws IOException + * if an IO error occurred */ static void setLastModified(Path path, Instant time) throws IOException { @@ -829,7 +825,10 @@ public class FileUtils { } /** + * Whether the file exists + * * @param file + * the file * @return {@code true} if the given file exists, not following symbolic * links */ @@ -838,9 +837,13 @@ public class FileUtils { } /** + * Check if file is hidden (on Windows) + * * @param file + * the file * @return {@code true} if the given file is hidden * @throws IOException + * if an IO error occurred */ static boolean isHidden(File file) throws IOException { return Files.isHidden(toPath(file)); @@ -854,6 +857,7 @@ public class FileUtils { * @param hidden * a boolean. * @throws java.io.IOException + * if an IO error occurred * @since 4.1 */ public static void setHidden(File file, boolean hidden) throws IOException { @@ -868,6 +872,7 @@ public class FileUtils { * a {@link java.io.File}. * @return length of the given file * @throws java.io.IOException + * if an IO error occurred * @since 4.1 */ public static long getLength(File file) throws IOException { @@ -879,7 +884,10 @@ public class FileUtils { } /** + * Check if file is directory + * * @param file + * the file * @return {@code true} if the given file is a directory, not following * symbolic links */ @@ -888,7 +896,10 @@ public class FileUtils { } /** + * Check if File is a file + * * @param file + * the file * @return {@code true} if the given file is a file, not following symbolic * links */ @@ -929,8 +940,12 @@ public class FileUtils { } /** + * Get basic file attributes + * * @param fs + * a {@link org.eclipse.jgit.util.FS} object. * @param file + * the file * @return non null attributes object */ static Attributes getFileAttributesBasic(FS fs, File file) { @@ -1079,6 +1094,7 @@ public class FileUtils { * @param f * the file to touch * @throws IOException + * if an IO error occurred * @since 5.1.8 */ public static void touch(Path f) throws IOException { |