Search code examples
javalinuxposixrenameatomic

In Java 1.6 File.renameTo() atomic on linux?


As the title says, in Java 1.6 File.renameTo() an atomic operation on POSIX linux?

According to this link, the rename operation in POSIX Linux is atomic, however, does it hold true for File.renameTo?


Solution

  • With Linux, the rename is atomic if and only if the source path and target path are under the same mount point (not filesystem).

    File.renameTo() is essentially a call to rename(2) under Linux, so you'll have to test for its return value to see if the file could be renamed.

    With Java 7, drop File, instead use:

    Files.move(src, dst, StandardCopyOption.ATOMIC_MOVE);
    

    Here is an example. On my system, /home is a different mount point to /, so the first rename will succeed (same filesystem), the second rename fails:

    fge@alustriel:~/tmp/t$ cat Rename.java 
    import java.io.File;
    
    public final class Rename
    {
        private Rename()
        {
        }
    
        public static void main(final String... args)
        {
            final File f1 = new File("/home/fge/tmp/t/foo");
            final File f2 = new File("/home/fge/tmp/t/bar");
            final File f3 = new File("/tmp/foo");
    
            if (f1.renameTo(f2))
                f2.renameTo(f3);
        }
    }
    
    fge@alustriel:~/tmp/t$ javac Rename.java
    fge@alustriel:~/tmp/t$ strace -ff -o TRACE java Rename
    fge@alustriel:~/tmp/t$ grep -w rename TRACE.*
    TRACE.17107:rename("/home/fge/tmp/t/foo", "/home/fge/tmp/t/bar") = 0
    TRACE.17107:rename("/home/fge/tmp/t/bar", "/tmp/foo") = -1 EXDEV (Invalid cross-device link)