mogilefs-client.git  about / heads / tags
MogileFS client library for Ruby
blob 202b2e3c09ed5e6d5d041c7b2ea8a954a8533e7b 872 bytes (raw)
$ git show HEAD:lib/mogilefs/copy_stream.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
# -*- encoding: binary -*-

# 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, 0666 ]
  def self.copy_stream(src, 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)
      begin
        src_io.readpartial(0x4000, buf)
        written += dst_io.write(buf)
      rescue EOFError
        break
      end while true
    else
      while src_io.read(0x4000, buf)
        written += dst_io.write(buf)
      end
    end
    dst_io.flush if dst_io.respond_to?(:flush)
    written
  ensure
    src_io.close if src.respond_to?(:to_str)
    dst_io.close if dst.respond_to?(:to_str)
  end
end

git clone https://yhbt.net/mogilefs-client.git