Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #71 from bakins/atomic_write

use atomic write
  • Loading branch information...
commit ed8127e2cb020d8d0d9ef4bc4fec3e446b76fdc5 2 parents 80f9fd9 + f4c2641
@cwninja cwninja authored
Showing with 14 additions and 3 deletions.
  1. +14 −3 lib/geminabox.rb
View
17 lib/geminabox.rb
@@ -7,6 +7,7 @@
require 'hostess'
require 'geminabox/version'
require 'rss/atom'
+require 'tempfile'
class Geminabox < Sinatra::Base
enable :static, :methodoverride
@@ -122,14 +123,14 @@ def fixup_bundler_rubygems!
error_response(200, "Ignoring upload, you uploaded the same thing previously.")
end
end
-
- File.open(dest_filename, "wb") do |f|
+
+ atomic_write(dest_filename) do |f|
while blk = tmpfile.read(65536)
f << blk
end
end
reindex
-
+
if api_request?
"Gem #{gem_name} received and indexed."
else
@@ -201,6 +202,16 @@ def index_gems(gems)
Set.new(gems.map{|gem| gem.name[0..0].downcase})
end
+ # based on http://as.rubyonrails.org/classes/File.html
+ def atomic_write(file_name)
+ temp_dir = File.join(settings.data, "_temp")
+ FileUtils.mkdir_p(temp_dir)
+ temp_file = Tempfile.new("." + File.basename(file_name), temp_dir)
+ yield temp_file
+ temp_file.close
+ File.rename(temp_file.path, file_name)
+ end
+
helpers do
def spec_for(gem_name, version)
spec_file = File.join(settings.data, "quick", "Marshal.#{Gem.marshal_version}", "#{gem_name}-#{version}.gemspec.rz")
Please sign in to comment.
Something went wrong with that request. Please try again.