about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-04-10 18:09:43 -0700
committerEric Wong <normalperson@yhbt.net>2009-04-11 19:44:20 -0700
commitba24d98b90b17ca59e9d274b7f5925e237418b4a (patch)
tree1c2cd930729279f80995b66c8a06aa083427316b
parent33ce9880cff9eaf8b7ad7558357cd0fcaed8144a (diff)
downloadmogilefs-client-ba24d98b90b17ca59e9d274b7f5925e237418b4a.tar.gz
It's useful to know the size of the file we're storing.
-rw-r--r--lib/mogilefs/mogilefs.rb16
-rw-r--r--test/test_mogilefs.rb46
2 files changed, 50 insertions, 12 deletions
diff --git a/lib/mogilefs/mogilefs.rb b/lib/mogilefs/mogilefs.rb
index 390e354..12bf15a 100644
--- a/lib/mogilefs/mogilefs.rb
+++ b/lib/mogilefs/mogilefs.rb
@@ -131,20 +131,22 @@ class MogileFS::MogileFS < MogileFS::Client
 
   ##
   # Copies the contents of +file+ into +key+ in class +klass+.  +file+ can be
-  # either a file name or an object that responds to #read.
+  # either a file name or an object that responds to #sysread.
+  # Returns size of +file+ stored
 
   def store_file(key, klass, file)
     raise MogileFS::ReadOnlyError if readonly?
 
     new_file key, klass do |mfp|
       if file.respond_to? :sysread then
-        return sysrwloop(file, mfp)
+        sysrwloop(file, mfp)
       else
-        if File.size(file) > 0x10000 # Bigass file, handle differently
-          mfp.big_io = file
-          return
-        else
-          return File.open(file, "rb") { |fp| sysrwloop(fp, mfp) }
+        size = File.size(file)
+        if size > 0x10000 # Bigass file, handle differently
+          mfp.big_io = file
+          size
+        else
+          File.open(file, "rb") { |fp| sysrwloop(fp, mfp) }
         end
       end
     end
diff --git a/test/test_mogilefs.rb b/test/test_mogilefs.rb
index fd5e235..8eb4f52 100644
--- a/test/test_mogilefs.rb
+++ b/test/test_mogilefs.rb
@@ -295,6 +295,32 @@ class TestMogileFS__MogileFS < TestMogileFS
     assert_equal 1, tmp.stat.size
   end
 
+  def test_store_file_small_http
+    received = Tempfile.new('received')
+    to_store = Tempfile.new('small')
+    to_store.syswrite('data')
+
+    expected = "PUT /path HTTP/1.0\r\nContent-Length: 4\r\n\r\ndata"
+    t = TempServer.new(Proc.new do |serv, accept|
+      client, client_addr = serv.accept
+      client.sync = true
+      received.syswrite(client.recv(4096, 0))
+      client.send("HTTP/1.0 200 OK\r\n\r\n", 0)
+      client.close
+    end)
+
+    @backend.create_open = {
+      'devid' => '1',
+      'path' => "http://127.0.0.1:#{t.port}/path",
+    }
+    nr = @client.store_file 'new_key', 'test', to_store.path
+    assert_equal 4, nr
+    received.sysseek(0)
+    assert_equal expected, received.sysread(4096)
+    ensure
+      TempServer.destroy_all!
+  end
+
   def test_store_content_http
     received = Tempfile.new('recieved')
     expected = "PUT /path HTTP/1.0\r\nContent-Length: 4\r\n\r\ndata"
@@ -312,7 +338,9 @@ class TestMogileFS__MogileFS < TestMogileFS
       'path' => "http://127.0.0.1:#{t.port}/path",
     }
 
-    @client.store_content 'new_key', 'test', 'data'
+    nr = @client.store_content 'new_key', 'test', 'data'
+    assert nr
+    assert_equal 4, nr
 
     received.sysseek(0)
     assert_equal expected, received.sysread(4096)
@@ -352,7 +380,9 @@ class TestMogileFS__MogileFS < TestMogileFS
         write_callback.call("data")
       end
     end
-    @client.store_content('new_key', 'test', cbk)
+    assert_equal 40, cbk.length
+    nr = @client.store_content('new_key', 'test', cbk)
+    assert_equal 40, nr
 
     received.sysseek(0)
     assert_equal expected, received.sysread(4096)
@@ -389,7 +419,8 @@ class TestMogileFS__MogileFS < TestMogileFS
       'path_2' => "http://127.0.0.1:#{t2.port}/path",
     }
 
-    @client.store_content 'new_key', 'test', 'data'
+    nr = @client.store_content 'new_key', 'test', 'data'
+    assert_equal 4, nr
     received1.sysseek(0)
     received2.sysseek(0)
     assert_equal expected, received1.sysread(4096)
@@ -433,7 +464,8 @@ class TestMogileFS__MogileFS < TestMogileFS
       'path' => "http://127.0.0.1:#{t.port}/path",
     }
 
-    @client.store_content 'new_key', 'test', ''
+    nr = @client.store_content 'new_key', 'test', ''
+    assert_equal 0, nr
     received.sysseek(0)
     assert_equal expected, received.sysread(4096)
   end
@@ -490,7 +522,11 @@ class TestMogileFS__MogileFS < TestMogileFS
       'path' => "http://127.0.0.1:#{t.port}/path",
     }
 
-    @client.store_file('new_key', 'test', to_put.path)
+    orig_size = to_put.size
+    nr = @client.store_file('new_key', 'test', to_put.path)
+    assert nr
+    assert_equal orig_size, nr
+    assert_equal orig_size, to_put.size
     readed.sysseek(0)
     assert_equal expect.stat.size, readed.sysread(4096).to_i