about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-12-08 06:15:06 +0000
committerEric Wong <normalperson@yhbt.net>2011-12-08 06:15:06 +0000
commit346b21f715e484bf9280d2d6b7ac2003b29a59ed (patch)
treedd4ac90290f7206a63f44b77eaa0028eb71bb844
parent83df70292440787ff32be893f7dd0fd2713bfdef (diff)
downloadmogilefs-client-346b21f715e484bf9280d2d6b7ac2003b29a59ed.tar.gz
Unlinking Tempfiles is good practice, so we should
allow/encourage people to use unlinked Tempfiles
with store_file.
-rw-r--r--lib/mogilefs/http_file.rb24
-rw-r--r--test/test_mogilefs_integration.rb13
2 files changed, 25 insertions, 12 deletions
diff --git a/lib/mogilefs/http_file.rb b/lib/mogilefs/http_file.rb
index b3a664f..b2d5620 100644
--- a/lib/mogilefs/http_file.rb
+++ b/lib/mogilefs/http_file.rb
@@ -101,20 +101,20 @@ class MogileFS::HTTPFile < StringIO
     if @streaming_io
       file_size = put_streaming_io(sock, uri)
     elsif @big_io
-      if String === @big_io || @big_io.respond_to?(:to_path)
-        file = @big_io.respond_to?(:stat) ? @big_io : File.open(@big_io)
+      stat = file = size = nil
+      if @big_io.respond_to?(:stat)
+        stat = @big_io.stat
+      elsif String === @big_io || @big_io.respond_to?(:to_path)
+        file = File.open(@big_io)
         stat = file.stat
-        file_size = request_put(sock, uri, stat.file? ? stat.size : nil, file)
-      else
-        size = nil
-        if @big_io.respond_to?(:stat)
-          stat = @big_io.stat
-          size = stat.size if stat.file?
-        elsif @big_io.respond_to?(:size)
-          size = @big_io.size
-        end
-        file_size = request_put(sock, uri, size, @big_io)
+      elsif @big_io.respond_to?(:size)
+        size = @big_io.size
       end
+      if stat && stat.file?
+        size ||= stat.size
+        file ||= @big_io.to_io if @big_io.respond_to?(:to_io)
+      end
+      file_size = request_put(sock, uri, size, file || @big_io)
     else
       rewind
       request_put(sock, uri, file_size, self)
diff --git a/test/test_mogilefs_integration.rb b/test/test_mogilefs_integration.rb
index 88657a9..b6a181a 100644
--- a/test/test_mogilefs_integration.rb
+++ b/test/test_mogilefs_integration.rb
@@ -269,4 +269,17 @@ class TestMogileFSIntegration < TestMogIntegration
 
     assert_equal true, checked, "expect_md5 lambda called"
   end
+
+  def test_store_file_unlinked_tempfile
+    tmp = Tempfile.new("store_file_unlinked")
+    tmp.unlink
+    tmp.sync = true
+    tmp.write "HIHI"
+    tmp.rewind
+    assert_nothing_raised do
+      @client.store_file("unlinked", nil, tmp)
+    end
+
+    assert_equal "HIHI", @client.get_file_data("unlinked")
+  end
 end