You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Mark (JIRA)" <ji...@apache.org> on 2019/02/07 15:19:00 UTC
[jira] [Created] (IO-598) FileUtils.moveFile should support atomic
file replacement; introduce FileUtils.renameTo to force target file
replacement?
Mark created IO-598:
-----------------------
Summary: 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
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.
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.renameTo and don't delete target directories, only target files.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)