Feature #13696
open
Add exchange and noreplace options to File.rename
Added by Glass_saga (Masaki Matsushita) over 7 years ago.
Updated 8 months ago.
Description
renameat2(2) introduced in linux kernel 3.15 takes following flags.
RENAME_EXCHANGE: Atomically exchange oldpath and newpath.
RENAME_NOREPLACE: Don't overwrite newpath of the rename. Return an error if newpath already exists.
This change makes File.rename
take these flags as keyword arguments.
Example:
File.write("hoge", "hoge")
File.write("fuga", "fuga")
File.rename("hoge", "fuga", exchange: true) # atomically exchanged
File.read("fuga") #=> "hoge"
File.rename("hoge", "fuga", noreplace: true) #=> File exists @ syserr_fail2_in - fuga (Errno::EEXIST)
Files
- Description updated (diff)
Glass_saga (Masaki Matsushita) wrote:
File.rename("hoge", "fuga", exchange: true) # atomically exchanged
How about another method, say, File.exchange
?
File.rename("hoge", "fuga", noreplace: true) #=> File exists @ syserr_fail2_in - fuga (Errno::EEXIST)
I prefer positive-form to negative-form as an option, i.e., File.rename(old, new[, replace: true])
.
I prefer positive-form to negative-form as an option
I do too. I found it in general easier (for me) to understand positive/forward
declarations.
Thank you for your comments.
I implemented FIle.exchange and made the noreplace flag in positive-form.
File.write("hoge", "hoge")
File.write("fuga", "fuga")
File.exchange("hoge", "fuga")
p File.read("fuga") #=> "hoge"
File.rename("hoge", "fuga", replace: false) #=> File exists @ syserr_fail2_in - fuga (Errno::EEXIST)
Is "atomically" important?
If no, it'd be possible to implement without kernel support.
nobu@ruby-lang.org wrote:
Is "atomically" important?
If no, it'd be possible to implement without kernel support.
Yes, atomicity is an important distinction.
We should not try to implement this non-atomically without kernel
support; it will quietly break code which expects the atomicity.
I am recalling similar problems around posix_fallocate in glibc:
https://sourceware.org/bugzilla/show_bug.cgi?id=15661
I think atomicity is important. If the kernel doesn't support atomic exchange, File.exchange should raise NotImplementedError and users can handle it properly.
begin
File.exchange("hoge", "fuga")
rescue NotImplementedError
# exchange files in another way
end
nobu (Nobuyoshi Nakada) wrote:
Glass_saga (Masaki Matsushita) wrote:
File.rename("hoge", "fuga", exchange: true) # atomically exchanged
How about another method, say, File.exchange
?
File.rename("hoge", "fuga", noreplace: true) #=> File exists @ syserr_fail2_in - fuga (Errno::EEXIST)
I prefer positive-form to negative-form as an option, i.e., File.rename(old, new[, replace: true])
.
I think both exchange:
and noreplace:
are difficult to understand for an outside user. I think overwrite:
is much easier to understand.
I think it's hard to have platform-specific API extensions like this to be in-core. If atomicity is such an important feature (and I'm sure it is), it's hard to emulate this in pure-userland for people without OS that support it.
This should be implemented as a gem. That way, gem install can bail out purposefully for unsupported platforms. That should prevent hidden race condition that you want to avoid.
- Target version deleted (
2.5)
- Status changed from Open to Assigned
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0