In Java 1.6 File.renameTo() atomic on linux? -
as title says, in java 1.6 file.renameto() atomic operation on posix linux?
according this link, rename operation in posix linux atomic, however, hold true file.renameto?
with linux, rename atomic if , if source path , target path under same mount point (not filesystem).
file.renameto()
call rename(2)
under linux, you'll have test return value see if file renamed.
with java 7, drop file
, instead use:
files.move(src, dst, standardcopyoption.atomic_move);
here example. on system, /home
different mount point /
, first rename succeed (same filesystem), 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)
Comments
Post a Comment