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

吃可爱长大的小学妹 提交于 2019-12-10 19:30:02

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/17715726/in-java-1-6-file-renameto-atomic-on-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!