Skip to content
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

Atomic object writes #157

Merged
merged 2 commits into from
Feb 23, 2013
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fsync loose objects before moving into place
When we write a loose object to disk, we simply close the
file object before moving it into place. If the machine
crashes shortly after our write, the contents may not have
been committed to disk (depending your filesystem, usually
the metadata is, and you end up with a corrupt, zero-length
loose object file).

This is especially bad because we report that the object is
successfully written, which means we may have updated refs
to point to it. A corrupt object at that point means not
only does the operation fail, but the repository is left in
a corrupted and unusable state.

We can fix this by calling fsync on the object file before
linking it into place. Between this and the previous commit,
our object writing should now behave exactly like git's
internal routines.
  • Loading branch information
peff committed Feb 22, 2013
commit cdf0fdb3e19237a2fe8a2667d774a25b5fa46e76
1 change: 1 addition & 0 deletions lib/grit/git-ruby/internal/loose.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def get_raw_object(buf)
def safe_write(path, content)
Tempfile.open("tmp_obj_", File.dirname(path), :opt => "wb") do |f|
f.write content
f.fsync
f.close
begin
File.link(f.path, path)
Expand Down