Class: Redwood::MBox::SSHFile

Inherits:
Object
  • Object
show all
Defined in:
lib/sup/mbox/ssh-file.rb

Overview

the file-like interface to a remote file

Constant Summary

MAX_BUF_SIZE =

bytes

1024 * 1024
MAX_TRANSFER_SIZE =
1024 * 128
REASONABLE_TRANSFER_SIZE =
1024 * 32
SIZE_CHECK_INTERVAL =

seconds

60 * 1
RECOVERABLE_ERRORS =

upon these errors we'll try to rereconnect a few times

[ Errno::EPIPE, Errno::ETIMEDOUT ]
@@shells =
{}
@@shells_mutex =
Mutex.new

Instance Method Summary collapse

Constructor Details

#initialize(host, fn, ssh_opts = {}) ⇒ SSHFile



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/sup/mbox/ssh-file.rb', line 101

def initialize host, fn, ssh_opts={}
  @buf = Buffer.new
  @host = host
  @fn = fn
  @ssh_opts = ssh_opts
  @file_size = nil
  @offset = 0
  @say_id = nil
  @shell = nil
  @shell_mutex = nil
  @buf_mutex = Mutex.new
end

Instance Method Details

#connectObject



116
117
118
# File 'lib/sup/mbox/ssh-file.rb', line 116

def connect
  do_remote nil
end

#eofObject

lame but IO's method is named this and rmail calls that



121
# File 'lib/sup/mbox/ssh-file.rb', line 121

def eof; eof?; end

#eof?Boolean



120
# File 'lib/sup/mbox/ssh-file.rb', line 120

def eof?; @offset >= size; end

#getsObject



135
136
137
138
139
140
141
142
# File 'lib/sup/mbox/ssh-file.rb', line 135

def gets
  return nil if eof?
  @buf_mutex.synchronize do
    make_buf_include @offset
    expand_buf_forward while @buf.index("\n", @offset).nil? && @buf.endd < size
    returning(@buf[@offset .. (@buf.index("\n", @offset) || -1)]) { |line| @offset += line.length }
  end
end

#pathObject



125
# File 'lib/sup/mbox/ssh-file.rb', line 125

def path; @fn end

#read(n) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/sup/mbox/ssh-file.rb', line 144

def read n
  return nil if eof?
  @buf_mutex.synchronize do
    make_buf_include @offset, n
    @buf[@offset ... (@offset += n)]
  end
end

#seek(loc) ⇒ Object



122
# File 'lib/sup/mbox/ssh-file.rb', line 122

def seek loc; @offset = loc; end

#sizeObject



127
128
129
130
131
132
133
# File 'lib/sup/mbox/ssh-file.rb', line 127

def size
  if @file_size.nil? || (Time.now - @last_size_check) > SIZE_CHECK_INTERVAL
    @last_size_check = Time.now
    @file_size = do_remote("wc -c #@fn").split.first.to_i
  end
  @file_size
end

#tellObject



123
# File 'lib/sup/mbox/ssh-file.rb', line 123

def tell; @offset; end

#to_sObject

TODO: remove this EVILness



114
# File 'lib/sup/mbox/ssh-file.rb', line 114

def to_s; "mbox+ssh://#@host/#@fn"; end

#totalObject



124
# File 'lib/sup/mbox/ssh-file.rb', line 124

def total; size; end