copy_stream emulator respects creation umask
[ruby-mogilefs-client.git] / lib / mogilefs / copy_stream.rb
blobf50bf0819c44f76cd3163dc1727ed611a4a1c92f
1 # -*- encoding: binary -*-
3 # internal compatibility class for older Rubies
4 module MogileFS::CopyStream # :nodoc:
5   @r_args = IO::RDONLY | IO::NOCTTY
6   @w_args = [ IO::WRONLY|IO::CREAT|IO::NOCTTY|IO::TRUNC, 0666 ]
7   def self.copy_stream(src, dst)
8     src_io = src.respond_to?(:to_str) ? File.open(src, @r_args) : src
9     dst_io = dst.respond_to?(:to_str) ? File.open(dst, *@w_args) : dst
10     buf = ""
11     written = 0
12     if src_io.respond_to?(:readpartial)
13       begin
14         src_io.readpartial(0x4000, buf)
15         written += dst_io.write(buf)
16       rescue EOFError
17         break
18       end while true
19     else
20       while src_io.read(0x4000, buf)
21         written += dst_io.write(buf)
22       end
23     end
24     dst_io.flush if dst_io.respond_to?(:flush)
25     written
26     ensure
27       src_io.close if src.respond_to?(:to_str)
28       dst_io.close if dst.respond_to?(:to_str)
29   end
30 end