Class: Snapsync::RemotePathname

Inherits:
AgnosticPath show all
Defined in:
lib/snapsync/remote_pathname.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ RemotePathname

Returns a new instance of RemotePathname.

Parameters:

  • dir (String)


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/snapsync/remote_pathname.rb', line 138

def initialize(dir)
  if dir.instance_of? RemotePathname
    @uri = dir.uri.dup
    @ssh = dir.ssh
    @sftp = dir.sftp
    @sftp_f = dir.sftp_f
  else
    @uri = URI::SshGit.parse(dir)

    raise RuntimeError.new('Host cannot be nil for remote pathname') if uri.host.nil?

    Snapsync.debug "Opening new ssh session: "+uri.to_s
    @ssh = Net::SSH.start(uri.host, uri.user, password: uri.password, non_interactive: true)

    @sftp = @ssh.sftp
    @sftp_f = @sftp.file

    # # FIXME: these probably don't work
    # @ssh_thr = Thread.new {
    #   ssh = WeakRef.new(@ssh)
    #   while ssh.weakref_alive?
    #     ssh.process 0.1
    #   end
    # }
    #
    # @sftp_thr = Thread.new {
    #   sftp = WeakRef.new(@sftp)
    #   sftp.loop do
    #     sftp.weakref_alive?
    #   end
    # }
  end
end

Instance Attribute Details

#sftpNet::SFTP::Session (readonly)

Returns:

  • (Net::SFTP::Session)


132
133
134
# File 'lib/snapsync/remote_pathname.rb', line 132

def sftp
  @sftp
end

#sftp_fNet::SFTP::Operations::FileFactory (readonly)

Returns:

  • (Net::SFTP::Operations::FileFactory)


135
136
137
# File 'lib/snapsync/remote_pathname.rb', line 135

def sftp_f
  @sftp_f
end

#sshNet::SSH::Connection::Session (readonly)

Returns:

  • (Net::SSH::Connection::Session)


129
130
131
# File 'lib/snapsync/remote_pathname.rb', line 129

def ssh
  @ssh
end

#uriURI::SshGit::Generic (readonly)

Returns:

  • (URI::SshGit::Generic)


126
127
128
# File 'lib/snapsync/remote_pathname.rb', line 126

def uri
  @uri
end

Instance Method Details

#+(path) ⇒ Object



296
297
298
299
300
# File 'lib/snapsync/remote_pathname.rb', line 296

def +(path)
  o = self.dup
  o.uri.path = (Pathname.new(uri.path) + path).to_s
  o
end

#basenameObject



237
238
239
# File 'lib/snapsync/remote_pathname.rb', line 237

def basename
  Pathname.new(uri.path).basename
end

#cleanpathObject



274
275
276
277
278
# File 'lib/snapsync/remote_pathname.rb', line 274

def cleanpath
  o = self.dup
  o.uri.path = Pathname.new(uri.path).cleanpath.to_s
  o
end

#directory?Boolean

Returns:

  • (Boolean)


203
204
205
206
207
208
209
# File 'lib/snapsync/remote_pathname.rb', line 203

def directory?
  begin
    sftp_f.directory? uri.path
  rescue Net::SFTP::StatusException
    return false
  end
end

#dirnameObject



241
242
243
244
245
# File 'lib/snapsync/remote_pathname.rb', line 241

def dirname
  o = self.dup
  o.uri.path = Pathname.new(uri.path).dirname.to_s
  o
end

#dup_ssh {|ssh| ... } ⇒ Net::SSH::Connection::Session

Duplicates a new ssh session with same connection options

Yield Parameters:

  • ssh (Net::SSH::Connection::Session)

Returns:

  • (Net::SSH::Connection::Session)


180
181
182
183
# File 'lib/snapsync/remote_pathname.rb', line 180

def dup_ssh(&block)
  Snapsync.debug "Opening new ssh session: "+uri.to_s
  Net::SSH.start(uri.host, uri.user, password: uri.password, non_interactive: true, &block)
end

#each_child(with_directory = true, &block) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
# File 'lib/snapsync/remote_pathname.rb', line 256

def each_child(with_directory=true, &block)
  raise 'Only supports default value for with_directory' if not with_directory

  sftp.dir.foreach(uri.path) do |entry|
    next if entry.name == '.' or entry.name == '..'

    o = self.dup
    o.uri.path = (Pathname.new(o.uri.path) + entry.name).to_s
    yield o
  end
end

#exist?Boolean

Returns:

  • (Boolean)


185
186
187
188
189
190
191
192
# File 'lib/snapsync/remote_pathname.rb', line 185

def exist?
  begin
    sftp_f.open(uri.path).close
    return true
  rescue Net::SFTP::StatusException
    return directory?
  end
end

#expand_pathObject



268
269
270
271
272
# File 'lib/snapsync/remote_pathname.rb', line 268

def expand_path
  o = self.dup
  o.uri.path = ssh.exec!(Shellwords.join ['readlink', '-f', uri.path]).chomp
  o
end

#file?Boolean

Returns:

  • (Boolean)


194
195
196
197
198
199
200
201
# File 'lib/snapsync/remote_pathname.rb', line 194

def file?
  begin
    sftp_f.open(uri.path).close
    return true
  rescue Net::SFTP::StatusException
    return false
  end
end

#findmntObject



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/snapsync/remote_pathname.rb', line 211

def findmnt
  cached = Snapsync._mountpointCache.fetch(self.to_s, nil)
  cached = Snapsync._mountpointCache.fetch(self.parent.to_s, nil) unless cached
  return cached if cached

  Snapsync.debug "RemotePathname ('#{uri}').findmnt"

  path = ssh.exec!(Shellwords.join ['findmnt','-n','-o','TARGET','-T',uri.path]).strip
  p = self.dup
  p.uri.path = path

  # Update cache
  p2 = self.dup
  while p.uri.path != p2.uri.path
    Snapsync._mountpointCache[p2.to_s] = p
    p2 = p2.parent
  end

  p
end

#initialize_dup(other) ⇒ Object



172
173
174
175
# File 'lib/snapsync/remote_pathname.rb', line 172

def initialize_dup(other)
  super
  @uri = @uri.dup
end

#inspectObject



326
327
328
# File 'lib/snapsync/remote_pathname.rb', line 326

def inspect
  uri.to_s
end

#mkdirObject



280
281
282
# File 'lib/snapsync/remote_pathname.rb', line 280

def mkdir
  sftp.mkdir!(uri.path)
end

#mkpathObject



284
285
286
# File 'lib/snapsync/remote_pathname.rb', line 284

def mkpath
  sftp.mkdir!(uri.path)
end

#mountpoint?Boolean

Returns:

  • (Boolean)


232
233
234
235
# File 'lib/snapsync/remote_pathname.rb', line 232

def mountpoint?
  Snapsync.debug "RemotePathname ('#{uri}').mountpoint?"
  ssh.exec!(Shellwords.join ['mountpoint','-q',uri.path]).exitstatus == 0
end

#open(flags, &block) ⇒ Object



310
311
312
# File 'lib/snapsync/remote_pathname.rb', line 310

def open(flags, &block)
  sftp_f.open uri.path, flags, &block
end

#parentObject



247
248
249
250
251
252
253
254
# File 'lib/snapsync/remote_pathname.rb', line 247

def parent
  o = self.dup
  if o.uri.path == '/'
    raise "Trying to get parent of root directory"
  end
  o.uri.path = Pathname.new(uri.path).parent.to_s
  o
end

#path_partObject



318
319
320
# File 'lib/snapsync/remote_pathname.rb', line 318

def path_part
  uri.path
end

#readObject



302
303
304
305
306
307
308
# File 'lib/snapsync/remote_pathname.rb', line 302

def read
  begin
    sftp_f.open(uri.path).read
  rescue Net::SFTP::StatusException => e
    raise Errno::ENOENT, e.message, e.backtrace
  end
end

#rmtreeObject



288
289
290
# File 'lib/snapsync/remote_pathname.rb', line 288

def rmtree
  raise 'Failed' unless ssh.exec!(Shellwords.join ['rm','-r', uri.path]).exitstatus == 0
end

#to_sObject



322
323
324
# File 'lib/snapsync/remote_pathname.rb', line 322

def to_s
  uri.to_s
end

#touchObject



314
315
316
# File 'lib/snapsync/remote_pathname.rb', line 314

def touch
  raise 'Failed' unless ssh.exec!(Shellwords.join ['touch', uri.path]).exitstatus == 0
end


292
293
294
# File 'lib/snapsync/remote_pathname.rb', line 292

def unlink
  raise 'Failed' unless ssh.exec!(Shellwords.join ['rm', uri.path]).exitstatus == 0
end