Class: Etna::Filesystem::SftpFilesystem
Defined Under Namespace
Classes: SftpFile
Instance Method Summary
collapse
-
#authn ⇒ Object
-
#curl_cmd(path, opts = []) ⇒ Object
-
#exist?(src) ⇒ Boolean
-
#initialize(host:, username:, password: nil, port: 22, **args) ⇒ SftpFilesystem
constructor
A new instance of SftpFilesystem.
-
#ls(dir) ⇒ Object
-
#mkcommand(rd, wd, file, opts, size_hint: nil) ⇒ Object
-
#sftp_file_from_path(src) ⇒ Object
-
#stat(src) ⇒ Object
-
#url(src) ⇒ Object
-
#with_readable(src, opts = 'r', &block) ⇒ Object
#mkio
#mkdir_p, #mv, #rm_rf, #tmpdir, #with_writeable
Constructor Details
#initialize(host:, username:, password: nil, port: 22, **args) ⇒ SftpFilesystem
Returns a new instance of SftpFilesystem.
414
415
416
417
418
419
420
421
|
# File 'lib/etna/filesystem.rb', line 414
def initialize(host:, username:, password: nil, port: 22, **args)
@username = username
@password = password
@host = host
@port = port
@dir_listings = {}
end
|
Instance Method Details
#authn ⇒ Object
427
428
429
|
# File 'lib/etna/filesystem.rb', line 427
def authn
"#{@username}:#{@password}"
end
|
#curl_cmd(path, opts = []) ⇒ Object
431
432
433
434
435
436
437
438
|
# File 'lib/etna/filesystem.rb', line 431
def curl_cmd(path, opts=[])
connection = Curl::Easy.new(url(path))
connection.http_auth_types = :basic
connection.username = @username
connection.password = @password
connection
end
|
#exist?(src) ⇒ Boolean
478
479
480
481
|
# File 'lib/etna/filesystem.rb', line 478
def exist?(src)
files = ls(::File.dirname(src))
files.include?(::File.basename(src))
end
|
#ls(dir) ⇒ Object
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
# File 'lib/etna/filesystem.rb', line 483
def ls(dir)
dir = dir + "/" unless "/" == dir[-1]
return @dir_listings[dir] if @dir_listings.has_key?(dir)
listing = ''
connection = curl_cmd(dir)
connection.on_body { |data| listing << data; data.size }
connection.perform
@dir_listings[dir] = listing
listing
end
|
#mkcommand(rd, wd, file, opts, size_hint: nil) ⇒ Object
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
# File 'lib/etna/filesystem.rb', line 452
def mkcommand(rd, wd, file, opts, size_hint: nil)
env = {}
cmd = [env, "curl"]
cmd << "-u"
cmd << authn
cmd << "-o"
cmd << "-"
cmd << "-N"
cmd << url(file)
if opts.include?('r')
cmd << {out: wd}
end
cmd
end
|
#sftp_file_from_path(src) ⇒ Object
440
441
442
443
444
445
446
447
448
449
450
|
# File 'lib/etna/filesystem.rb', line 440
def sftp_file_from_path(src)
file = ls(::File.dirname(src)).split("\n").map do |listing|
SftpFile.new(listing)
end.select do |file|
file.name == ::File.basename(src)
end
raise "#{src} not found" if file.empty?
file.first
end
|
#stat(src) ⇒ Object
498
499
500
|
# File 'lib/etna/filesystem.rb', line 498
def stat(src)
sftp_file_from_path(src)
end
|
#url(src) ⇒ Object
423
424
425
|
# File 'lib/etna/filesystem.rb', line 423
def url(src)
"sftp://#{@host}/#{src}"
end
|
#with_readable(src, opts = 'r', &block) ⇒ Object
470
471
472
473
474
475
476
|
# File 'lib/etna/filesystem.rb', line 470
def with_readable(src, opts = 'r', &block)
raise "#{src} does not exist" unless exist?(src)
sftp_file = sftp_file_from_path(src)
mkio(src, opts, size_hint: sftp_file.size, &block)
end
|