-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
plugin: Write out state file via create-new-then-rename #7550
Conversation
tmpfile = Tempfile.new(@path.basename.to_s, @path.dirname.to_s) | ||
tmpfile.write(JSON.dump(@data)) | ||
tmpfile.close | ||
File.rename(tmpfile.path, @path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, so there's a reason we don't do this...
File.rename
has historically been a problem on WindowsFile.rename
does not work across multiple volumes, particularly if the volumes are different formats- The tmpfile could be removed by GC before the mv takes place (aka there's a slight race between lines 97 and 98 here)
I think it's probably a better idea to do something like:
Tempfile.open("vagrant-plugins") do |f|
f.binmode
f.write(JSON.dump(@data))
f.close
FileUtils.mv(f.path, @path)
end
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re 2: The tempfile is created in the same directory as the target
Re 3: I'll be honest and say this is my first Ruby patch, but I have a strong suspicion it can't be removed by GC because the local variable holds a strong reference across the function scope
But yes, this is just an implementation of a general pattern for "replace file atomically" which is harder on Windows indeed. I'm an upstream developer for GLib where for g_file_set_contents()
we do https://git.gnome.org/browse/glib/tree/glib/gfileutils.c#n1217
It's the sort of thing I'd expect Ruby to have in the standard library, but if it exists I couldn't find it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think FileUtils.mv
handles this better than File.rename
7fad888
to
0da24c9
Compare
Okay, updated ⬆️ to use |
The tempfile does still call (There has to be some Ruby codebase that does things like this right and On Thu, Jul 7, 2016, at 03:19 PM, Seth Vargo wrote:
|
Sorry, when reworking this I introduced a regression - we need to chmod back to 0644. Will fix. |
This makes updates atomic, so it's useful generally. However I'm making this change specifically to support rpm-ostree. For more information, see: https://bugzilla.redhat.com/show_bug.cgi?id=1352152
0da24c9
to
72c1972
Compare
This makes updates atomic, so it's useful generally. However
I'm making this change specifically to support rpm-ostree.
For more information, see:
https://bugzilla.redhat.com/show_bug.cgi?id=1352152