You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Gary D. Gregory (Jira)" <ji...@apache.org> on 2022/08/08 14:46:00 UTC

[jira] [Commented] (IO-598) FileUtils.moveFile should support atomic file replacement; introduce FileUtils.renameTo to force target file replacement?

    [ https://issues.apache.org/jira/browse/IO-598?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17576826#comment-17576826 ] 

Gary D. Gregory commented on IO-598:
------------------------------------

Hi [~gWdfL59U] 

Why not use {{{}java.nio.file.Files.move(Path, Path, CopyOption...){}}}?

> FileUtils.moveFile should support atomic file replacement; introduce FileUtils.renameTo to force target file replacement?
> -------------------------------------------------------------------------------------------------------------------------
>
>                 Key: IO-598
>                 URL: https://issues.apache.org/jira/browse/IO-598
>             Project: Commons IO
>          Issue Type: Improvement
>            Reporter: Mark
>            Priority: Trivial
>
> Looking at current openjdk (11) source code, the regular File.rename() method calls the standard native rename function on UNIX-like systems. At least on Linux, this allows me to (atomically?) replace a file with another one, ie. without deleting the target file first.
>  
> FileUtils.moveFile however, throws an exception in case the target file alrady exists.
>  
> Here is the code bloat I currently use to rename-overwrite a file (and which I want to get rid of):
> {code:java}
>              // try to rename atomically first
>             try {
>                 if (!tempFile.renameTo(mdFile)) {
>                     throw new IOException();
>                 }
>             } catch (Exception ex) {
>                 // then explicitly delete the target file first
>                 if (mdFile.exists()) {
>                     mdFile.delete();
>                 }
>                 if (!tempFile.renameTo(mdFile)) {
>                     throw new IOException("failed to rename " + tempFile + " to " + mdFile);
>                 }
>             }
> {code}
> Can we please have a function that force-fully replaces existing target files, first atomically, then falling back to delete and move? Maybe call it FileUtils.forceRenameTo and don't delete target directories, only target files.
> It could be simply a modification of the existing moveFile function: remove check for existing target, check and throw exception only if it is a directory. Then, upon moving, catch the exception and check if it could be due to existing target file. If so, retry after manually removing the target file.
> Also, having a function with proper fsync support would be nice in that context: https://stackoverflow.com/questions/7433057/is-rename-without-fsync-safe
> FielUtils.syncRenameTo?



--
This message was sent by Atlassian Jira
(v8.20.10#820010)