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.
394
395
396
397
398
399
400
401
|
# File 'lib/etna/filesystem.rb', line 394
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
407
408
409
|
# File 'lib/etna/filesystem.rb', line 407
def authn
"#{@username}:#{@password}"
end
|
#curl_cmd(path, opts = []) ⇒ Object
411
412
413
414
415
416
417
418
|
# File 'lib/etna/filesystem.rb', line 411
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
458
459
460
461
|
# File 'lib/etna/filesystem.rb', line 458
def exist?(src)
files = ls(::File.dirname(src))
files.include?(::File.basename(src))
end
|
#ls(dir) ⇒ Object
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
# File 'lib/etna/filesystem.rb', line 463
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
# File 'lib/etna/filesystem.rb', line 432
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
420
421
422
423
424
425
426
427
428
429
430
|
# File 'lib/etna/filesystem.rb', line 420
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
478
479
480
|
# File 'lib/etna/filesystem.rb', line 478
def stat(src)
sftp_file_from_path(src)
end
|
#url(src) ⇒ Object
403
404
405
|
# File 'lib/etna/filesystem.rb', line 403
def url(src)
"sftp://#{@host}/#{src}"
end
|
#with_readable(src, opts = 'r', &block) ⇒ Object
450
451
452
453
454
455
456
|
# File 'lib/etna/filesystem.rb', line 450
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
|