copy_stream: enough to get this working under MRI 1.8
[ruby_io_splice.git] / lib / io / splice.rb
blob574e681a78442595099818a512ae2b33a5f93ba5
1 # -*- encoding: binary -*-
2 require 'io_splice_ext'
3 require 'io/wait'
5 module IO::Splice
7   # The maximum default capacity of the pipe in bytes.
8   # Under stock Linux, this is 65536 bytes as of 2.6.11, and 4096 before
9   # We detect this at runtime as it is easy to recompile the kernel
10   # and set a new value.
11   # Starting with Linux 2.6.35, pipe capacity will be tunable
12   # and this will only represent the default capacity of a
13   # newly-created pipe.
14   PIPE_CAPA = begin
15     rd, wr = IO.pipe
16     buf = ' ' * PIPE_BUF
17     n = 0
18     begin
19       n += wr.write_nonblock(buf)
20     rescue Errno::EAGAIN
21       break
22     end while true
23     wr.close
24     rd.close
25     n
26   end
28   # copies the contents of the IO object given by +src+ to +dst+
29   # If +len+ is specified, then only +len+ bytes are copied and
30   # +EOFError+ is raised if fewer than +len+ bytes could be copied.
31   # Otherwise the copy will be until EOF is reached on the +src+.
32   # +src+ and +dst+ must be IO objects or respond to +to_io+
33   #
34   # This is nearly a drop-in replacement for IO.copy_stream (in Ruby 1.9)
35   # but does not take into account userspace I/O buffers nor IO-like
36   # objects with no underlying file descriptor (e.g. StringIO).
37   def self.copy_stream(src, dst, len = nil, src_offset = nil)
38     close = []
39     src.kind_of?(String) and close << (src = File.open(src))
40     dst.kind_of?(String) and close << (dst = File.open(dst, "w"))
41     src, dst = src.to_io, dst.to_io
42     rv = len
44     if src.stat.pipe? || dst.stat.pipe?
45       if len
46         len -= full(src, dst, len, src_offset) until len == 0
47       else
48         rv = 0
49         while n = partial(src, dst, PIPE_CAPA, src_offset)
50           rv += n
51           src_offset += n if src_offset
52         end
53       end
54     else
55       r, w = tmp = IO.pipe
56       close.concat(tmp)
57       if len
58         while len != 0 && n = partial(src, w, len, src_offset)
59           src_offset += n if src_offset
60           len -= full(r, dst, n, nil)
61         end
62       else
63         rv = 0
64         while n = partial(src, w, PIPE_CAPA, src_offset)
65           src_offset += n if src_offset
66           rv += full(r, dst, n, nil)
67         end
68       end
69     end
71     rv
72     ensure
73       close.each { |io| io.close }
74   end
76   # splice the full amount specified from +src+ to +dst+
77   # Either +dst+ or +src+ must be a pipe.  +dst+ and +src+
78   # may BOTH be pipes in Linux 2.6.31 or later.
79   # This will block and wait for IO completion of +len+
80   # Raises +EOFError+ if end of file is reached.
81   # bytes.  Returns the number of bytes actually spliced (always +len+)
82   def self.full(src, dst, len, src_offset)
83     IO.splice(src, src_offset, dst, nil, len, F_MOVE | WAITALL)
84   end
86   # splice up to +len+ bytes from +src+ to +dst+.
87   # Either +dst+ or +src+ must be a pipe.  +dst+ and +src+
88   # may BOTH be pipes in Linux 2.6.31 or later.
89   # Returns the number of bytes actually spliced.
90   # Like IO#readpartial, this never returns Errno::EAGAIN
91   def self.partial(src, dst, len, src_offset)
92     IO.splice(src, src_offset, dst, nil, len, F_MOVE)
93     rescue EOFError
94       nil
95     rescue Errno::EAGAIN
96       begin
97         src.to_io.wait
98         IO.select(nil, [dst])
99         rv = IO.trysplice(src, src_offset, dst, nil, len, F_MOVE)
100       end while rv == :EAGAIN
101       rv
102   end
104 if (! defined?(RUBY_ENGINE) || RUBY_ENGINE == "ruby") &&
105    RUBY_VERSION.to_f <= 1.8
106   require "io/splice/mri_18"