Permalink
Browse files

Fix Tempfile usage under ruby 1.8.7

There's no third argument to Tempfile#open under 1.8.7. The default
mode is w+ which should be fine and there's no encoding support so
the binary flag doesn't matter.
  • Loading branch information...
rtomayko committed Feb 23, 2013
1 parent 58621d9 commit b49a6ff4ccd169eef6671263ccb29d3ead957697
Showing with 14 additions and 9 deletions.
  1. +14 −9 lib/grit/git-ruby/internal/loose.rb
@@ -64,18 +64,23 @@ def get_raw_object(buf)
# write an object to a temporary file, then atomically rename it
# into place; this ensures readers never see a half-written file
def safe_write(path, content)
- Tempfile.open("tmp_obj_", File.dirname(path), :opt => "wb") do |f|
+ f =
+ if RUBY_VERSION >= '1.9'
+ Tempfile.open("tmp_obj_", File.dirname(path), :opt => "wb")
+ else
+ Tempfile.open("tmp_obj_", File.dirname(path))
+ end
+ begin
f.write content
f.fsync
- f.close
- begin
- File.link(f.path, path)
- rescue Errno::EEXIST
- # The path already exists; we raced with another process,
- # but it's OK, because by definition the content is the
- # same. So we can just ignore the error.
- end
+ File.link(f.path, path)
+ rescue Errno::EEXIST
+ # The path already exists; we raced with another process,
+ # but it's OK, because by definition the content is the
+ # same. So we can just ignore the error.
+ ensure
f.unlink
+ f.close

This comment has been minimized.

Show comment Hide comment
@peff

peff Feb 23, 2013

Contributor

Before we closed prior to linking, and here we close after. I think this should be OK, because we should have no pending data after the write+fsync.

@peff

peff Feb 23, 2013

Contributor

Before we closed prior to linking, and here we close after. I think this should be OK, because we should have no pending data after the write+fsync.

end
end

0 comments on commit b49a6ff

Please sign in to comment.