From f8156e239ea4da4cbcd020f24bf06c4fb9e7cde7 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 19 Nov 2011 08:45:07 +0000 Subject: support MogileFS::MogileFS#get_file_data to dest path Destination path will be created with 0600 permissions to be consistent with IO.copy_stream [ruby-core:41151] --- lib/mogilefs/copy_stream.rb | 6 ++++-- lib/mogilefs/mogilefs.rb | 4 +--- test/test_mogilefs_integration.rb | 9 +++++++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/mogilefs/copy_stream.rb b/lib/mogilefs/copy_stream.rb index 9397e89..d49112a 100644 --- a/lib/mogilefs/copy_stream.rb +++ b/lib/mogilefs/copy_stream.rb @@ -2,9 +2,11 @@ # internal compatibility class for older Rubies module MogileFS::CopyStream # :nodoc: + @r_args = IO::RDONLY | IO::NOCTTY + @w_args = [ IO::WRONLY|IO::CREAT|IO::NOCTTY|IO::TRUNC, 0600 ] def self.copy_stream(src, dst) - src_io = src.respond_to?(:to_str) ? File.open(src) : src - dst_io = dst.respond_to?(:to_str) ? File.open(dst, "w") : dst + src_io = src.respond_to?(:to_str) ? File.open(src, @r_args) : src + dst_io = dst.respond_to?(:to_str) ? File.open(dst, *@w_args) : dst buf = "" written = 0 if src_io.respond_to?(:readpartial) diff --git a/lib/mogilefs/mogilefs.rb b/lib/mogilefs/mogilefs.rb index a3d88db..40ee1ca 100644 --- a/lib/mogilefs/mogilefs.rb +++ b/lib/mogilefs/mogilefs.rb @@ -22,9 +22,7 @@ # # # Retrieve the contents of 'my_image' into '/path/to/huge_file' # # without slurping the entire contents into memory: -# File.open('/path/to/huge_file', 'w') do |fp| -# mg.get_file_data('my_image', fp) -# end +# mg.get_file_data('my_image', '/path/to/huge_file') # # # Remove the key 'my_image' and 'some_key'. # mg.delete('my_image') diff --git a/test/test_mogilefs_integration.rb b/test/test_mogilefs_integration.rb index 4bc8e27..18ecd21 100644 --- a/test/test_mogilefs_integration.rb +++ b/test/test_mogilefs_integration.rb @@ -14,6 +14,15 @@ class TestMogileFSIntegration < TestMogIntegration assert_equal "DAT", @client.get_file_data("CRUD", nil, 3) assert_equal "AT", @client.get_file_data("CRUD", nil, 2, 1) + tmp = Tempfile.new("z") + tmp_path = tmp.path + tmp.close! + assert_equal 4, @client.get_file_data("CRUD", tmp_path) + assert_equal "DATA", File.read(tmp_path) + st = File.stat(tmp_path) + assert_equal 0100600, st.mode + File.unlink(tmp_path) + sio = StringIO.new("") rv = @client.get_file_data("CRUD", sio) assert_equal 4, rv -- cgit v1.2.3-24-ge0c7