6 class SSHFileError < StandardError; end
8 ## this is a file-like interface to a file that actually lives on the
9 ## other end of an ssh connection. it works by using wc, head and tail
10 ## to simulate (buffered) random access. on a fast connection, this
11 ## can have a good bandwidth, but the latency is pretty terrible:
12 ## about 1 second (!) per request. luckily, we're either just reading
13 ## straight through the mbox (an import) or we're reading a few
14 ## messages at a time (viewing messages) so the latency is not a problem.
16 ## all of the methods here can throw SSHFileErrors, SocketErrors,
17 ## Net::SSH::Exceptions and Errno::ENOENTs.
19 ## debugging TODO: remove me
23 module_function :debug
25 ## a simple buffer of contiguous data
36 def empty?; @start.nil?; end
37 def start; @start; end
38 def endd; @start + @buf.length; end
40 def add data, offset=endd
41 #MBox::debug "+ adding #{data.length} bytes; size will be #{size + data.length}; limit #{SSHFile::MAX_BUF_SIZE}"
49 raise "non-continguous data added to buffer (data #{offset}:#{offset + data.length}, buf range #{start}:#{endd})" if offset + data.length < start || offset > endd
52 @buf = data[0 ... (start - offset)] + @buf
55 return if offset + data.length < endd
56 @buf += data[(endd - offset) .. -1]
61 raise "only ranges supported due to programmer's laziness" unless o.is_a? Range
62 @buf[Range.new(o.first - @start, o.last - @start, o.exclude_end?)]
65 def index what, start=0
66 x = @buf.index(what, start - @start)
67 x.nil? ? nil : x + @start
70 def rindex what, start=0
71 x = @buf.rindex(what, start - @start)
72 x.nil? ? nil : x + @start
75 def size; empty? ? 0 : @buf.size; end
76 def to_s; empty? ? "<empty>" : "[#{start}, #{endd})"; end # for debugging
79 ## sharing a ssh connection to one machines between sources seems to
80 ## create lots of broken situations: commands returning bizarre (large
81 ## positive integer) return codes despite working; commands
82 ## occasionally not working, etc. i suspect this is because of the
83 ## fragile nature of the ssh syncshell.
85 ## at any rate, we now open up one ssh connection per file, which is
86 ## probably silly in the extreme case.
88 ## the file-like interface to a remote file
90 MAX_BUF_SIZE = 1024 * 1024 # bytes
91 MAX_TRANSFER_SIZE = 1024 * 128
92 REASONABLE_TRANSFER_SIZE = 1024 * 32
93 SIZE_CHECK_INTERVAL = 60 * 1 # seconds
95 ## upon these errors we'll try to rereconnect a few times
96 RECOVERABLE_ERRORS = [ Errno::EPIPE, Errno::ETIMEDOUT ]
99 @@shells_mutex = Mutex.new
101 def initialize host, fn, ssh_opts={}
111 @buf_mutex = Mutex.new
114 def to_s; "mbox+ssh://#@host/#@fn"; end ## TODO: remove this EVILness
120 def eof?; @offset >= size; end
121 def eof; eof?; end # lame but IO's method is named this and rmail calls that
122 def seek loc; @offset = loc; end
123 def tell; @offset; end
128 if @file_size.nil? || (Time.now - @last_size_check) > SIZE_CHECK_INTERVAL
129 @last_size_check = Time.now
130 @file_size = do_remote("wc -c #@fn").split.first.to_i
137 @buf_mutex.synchronize do
138 make_buf_include @offset
139 expand_buf_forward while @buf.index("\n", @offset).nil? && @buf.endd < size
140 returning(@buf[@offset .. (@buf.index("\n", @offset) || -1)]) { |line| @offset += line.length }
146 @buf_mutex.synchronize do
147 make_buf_include @offset, n
148 @buf[@offset ... (@offset += n)]
154 ## TODO: share this code with imap
156 @say_id = BufferManager.say s, @say_id if BufferManager.instantiated?
161 BufferManager.clear @say_id if BufferManager.instantiated? && @say_id
168 @key = [@host, @ssh_opts[:username]]
170 @shell, @shell_mutex = @@shells_mutex.synchronize do
171 unless @@shells.member? @key
172 say "Opening SSH connection to #{@host} for #@fn..."
173 session = Net::SSH.start @host, @ssh_opts
174 say "Starting SSH shell..."
175 @@shells[@key] = [session.shell.sync, Mutex.new]
180 say "Checking for #@fn..."
181 @shell_mutex.synchronize { raise Errno::ENOENT, @fn unless @shell.test("-e #@fn").status == 0 }
187 def do_remote cmd, expected_size=0
194 # MBox::debug "sending command: #{cmd.inspect}"
195 result = @shell_mutex.synchronize { x = @shell.send_command cmd; sleep 0.25; x }
196 raise SSHFileError, "Failure during remote command #{cmd.inspect}: #{(result.stderr || result.stdout || "")[0 .. 100]}" unless result.status == 0
198 ## Net::SSH::Exceptions seem to happen every once in a while for
200 rescue Net::SSH::Exception, *RECOVERABLE_ERRORS
201 if (retries += 1) <= 3
202 @@shells_mutex.synchronize do
214 def get_bytes offset, size
215 do_remote "tail -c +#{offset + 1} #@fn | head -c #{size}", size
218 def expand_buf_forward n=REASONABLE_TRANSFER_SIZE
219 @buf.add get_bytes(@buf.endd, n)
222 ## try our best to transfer somewhere between
223 ## REASONABLE_TRANSFER_SIZE and MAX_TRANSFER_SIZE bytes
224 def make_buf_include offset, size=0
225 good_size = [size, REASONABLE_TRANSFER_SIZE].max
227 trans_start, trans_size =
230 elsif offset < @buf.start
231 if @buf.start - offset <= good_size
232 start = [@buf.start - good_size, 0].max
233 [start, @buf.start - start]
234 elsif @buf.start - offset < MAX_TRANSFER_SIZE
235 [offset, @buf.start - offset]
237 MBox::debug "clearing SSH buffer because buf.start #{@buf.start} - offset #{offset} >= #{MAX_TRANSFER_SIZE}"
242 return if [offset + size, self.size].min <= @buf.endd # whoohoo!
243 if offset - @buf.endd <= good_size
244 [@buf.endd, good_size]
245 elsif offset - @buf.endd < MAX_TRANSFER_SIZE
246 [@buf.endd, offset - @buf.endd]
248 MBox::debug "clearing SSH buffer because offset #{offset} - buf.end #{@buf.endd} >= #{MAX_TRANSFER_SIZE}"
254 @buf.clear! if @buf.size > MAX_BUF_SIZE
255 @buf.add get_bytes(trans_start, trans_size), trans_start