class FTPLiar::FTPLiar

Attributes

binary[RW]
debug_mode[W]

FTP Liar Simple class for test aplication using Net::FTP

ftp_directory[RW]

:nocov:

is_connection[RW]
open_timeout[W]

FTP Liar Simple class for test aplication using Net::FTP

passive[W]

FTP Liar Simple class for test aplication using Net::FTP

read_timeout[W]

FTP Liar Simple class for test aplication using Net::FTP

Public Class Methods

new(host = nil, user = nil, passwd = nil, acct = nil) click to toggle source
# File lib/ftp_liar.rb, line 14
def initialize(host = nil, user = nil, passwd = nil, acct = nil)
  @ftp_directory = File.join(Dir.tmpdir, '.ftp_liar')
  FileUtils.mkdir_p(@ftp_directory)
  @binary = true
  @passive = false
  ObjectSpace.define_finalizer(self, self.method(:finalize))
  if !(user.nil? && passwd.nil?) && (user.nil? || passwd.nil?)
    raise Net::FTPPermError.new("530 User cannot log in.")
  else
    @is_connect = true
  end
  chdir("/")
end

Private Class Methods

open(host, *args) click to toggle source
# File lib/ftp_liar.rb, line 399
def open(host, *args)
  FTPLiar.new(host, *args)
end

Public Instance Methods

abort() click to toggle source

:nocov:

# File lib/ftp_liar.rb, line 34
def abort()
  # Aborts the previous command (ABOR command).
  raise NotImplementedError("Method not implemented. Override it if you want use this method.")
end
acct(account) click to toggle source
# File lib/ftp_liar.rb, line 39
def acct(account)
  # Sends the ACCT command.
  #
  # This is a less common FTP command, to send account information if the destination host requires it.
  raise NotImplementedError("Method not implemented. Override it if you want use this method.")
end
chdir(path) click to toggle source

:nocov:

# File lib/ftp_liar.rb, line 47
def chdir(path)
  # Changes the (remote) directory.
  raise Net::FTPPermError.new("530 Please login with USER and PASS.") unless @is_connect
  if path[0] == "/"
    path = if path.length == 1
      @ftp_directory
    else
      File.join(@ftp_directory, path[1..-1])
    end
  end

  unless absolute_path_indicates_to_ftp_directory?(path) && Dir.exist?(path)
    raise Net::FTPPermError.new("500")
  end

  FileUtils.cd(path)
  nil
end
close() click to toggle source
# File lib/ftp_liar.rb, line 66
def close()
  # Closes the connection. Further operations are impossible until you open a new connection with connect.
  chdir("/")
  @is_connect = false
  ""
end
closed?() click to toggle source
# File lib/ftp_liar.rb, line 73
def closed?()
  # Returns true if the connection is closed.
  !@is_connect
end
connect(host, port = 21) click to toggle source
# File lib/ftp_liar.rb, line 78
def connect(host, port = 21)
  # Method imitate connect method in Net::FTP
  @is_connect = true
  nil
end
delete(filename) click to toggle source
# File lib/ftp_liar.rb, line 84
def delete(filename)
  # Method remove file on FTP
  raise Net::FTPPermError.new("530 Please login with USER and PASS.") unless @is_connect
  filename = if filename[0] == "/"
    File.join(@ftp_directory, filename[1..-1])
  else
    File.join(@ftp_directory, pwd[1..-1], filename)
  end
  unless absolute_path_indicates_to_ftp_directory?(filename) && File.exist?(filename)
    raise Net::FTPPermError.new("550")
  end
  File.delete(filename)
  nil
end
dir(*args) click to toggle source

:nocov:

# File lib/ftp_liar.rb, line 100
def dir(*args)
  # Alias for list
  list(*args)
end
finalize(object_id) click to toggle source
# File lib/ftp_liar.rb, line 28
def finalize(object_id)
  # Finalizer to delete ftp_liar directory
  FileUtils.rm_rf(@ftp_directory)
end
get(remotefile, localfile = File.basename(remotefile), blocksize = nil) click to toggle source

:nocov:

# File lib/ftp_liar.rb, line 106
def get(remotefile, localfile = File.basename(remotefile), blocksize = nil)
  # A simple method that manages to copy a remote file to local
  raise Net::FTPPermError("530 Please login with USER and PASS.") unless @is_connect
  if remotefile[0] == "/"
    if remotefile.length > 1
      remotefile = File.join(@ftp_directory, pwd[1..-1], remotefile[1..-1])
    elsif remotefile == "/"
      raise Errno::EISDIR
    end
  end

  unless absolute_path_indicates_to_ftp_directory?(remotefile) && File.exist?(remotefile)
    raise Net::FTPPermError.new("550")
  end

  localdir = localfile.split("/")[0...-1].join("/")
  if File.directory?(localfile)
    raise Errno::EISDIR
  end
  unless Dir.exist?(localdir)
    raise Errno::ENOENT
  end
  copy_file(remotefile, localfile)
  nil
end
getbinaryfile(*args) click to toggle source

:nocov:

# File lib/ftp_liar.rb, line 133
def getbinaryfile(*args)
  # A simple method that manages to copy a remote file to local
  get(*args)
end
getdir() click to toggle source
# File lib/ftp_liar.rb, line 138
def getdir()
  pwd
end
gettextfile(*args) click to toggle source
# File lib/ftp_liar.rb, line 142
def gettextfile(*args)
  get(*args)
end
help(arg = nil) click to toggle source
# File lib/ftp_liar.rb, line 146
def help(arg = nil)
  raise NotImplementedError("Method not implemented. Override it if you want use this method.")
end
list(*args) click to toggle source
# File lib/ftp_liar.rb, line 150
def list(*args)
  raise NotImplementedError("Method not implemented. Override it if you want use this method.")
end
login(user = "anonymous", passwd = nil, acct = nil) click to toggle source

:nocov:

# File lib/ftp_liar.rb, line 155
def login(user = "anonymous", passwd = nil, acct = nil)
  # Method imitate login to ftp. When login is "anonymous" it "connect" without password
  if user != "anonymous" && (user.nil? || passwd.nil?)
    raise Net::FTPPermError.new("530 User cannot log in.")
  end
  @is_connect = true
end
ls(*args) click to toggle source

:nocov:

# File lib/ftp_liar.rb, line 164
def ls(*args)
  # Alias for list
  list(*args)
end
mdtm(filename) click to toggle source
# File lib/ftp_liar.rb, line 169
def mdtm(filename)
  raise NotImplementedError("Method not implemented. Override it if you want use this method.")
end
mkdir(dirname) click to toggle source

:nocov:

# File lib/ftp_liar.rb, line 174
def mkdir(dirname)
  # Creates a remote directory.
  raise Net::FTPPermError.new("530 Please login with USER and PASS.") unless @is_connect
  new_dirname = create_path(dirname)

  if !absolute_path_indicates_to_ftp_directory?(new_dirname) || File.exist?(new_dirname)
    raise Net::FTPPermError.new("550")
  end

  Dir.mkdir(new_dirname)
  dirname
end
mtime(filename, local = false) click to toggle source

:nocov:

# File lib/ftp_liar.rb, line 188
def mtime(filename, local = false)
  raise NotImplementedError("Method not implemented. Override it if you want use this method.")
end
nlst(path = '.') click to toggle source

:nocov:

# File lib/ftp_liar.rb, line 193
def nlst(path = '.')
  # A simple method to list data in directory, return list with filename if file
  # Method does not work as method in Net::FTP. I started topic about it on https://bugs.ruby-lang.org/issues/11407 because I think original method has bugs
  raise Net::FTPPermError.new("530 Please login with USER and PASS.") unless @is_connect
  new_path = create_path(path)

  unless absolute_path_indicates_to_ftp_directory?(new_path)
    raise Net::FTPPermError.new("550")
  end

  if File.file?(new_path)
    [path]
  else
    Dir.entries(new_path).sort[2..-1]
  end
end
noop() click to toggle source

:nocov:

# File lib/ftp_liar.rb, line 211
def noop()
  # Does nothing
  nil
end
put(localfile, remotefile = File.basename(localfile), blocksize = nil) click to toggle source

:nocov:

# File lib/ftp_liar.rb, line 217
def put(localfile, remotefile = File.basename(localfile), blocksize = nil)
  # A simple method that manages to copy a local file on the FTP.
  raise Net::FTPPermError.new("530 Please login with USER and PASS.") unless @is_connect
  if File.directory?(localfile)
    raise Errno::EISDIR
  end
  unless File.exist?(localfile)
    raise Errno::ENOENT
  end

  if remotefile[0] == "/" && remotefile.length > 1
    remotefile = File.join(@ftp_directory, remotefile[1..-1])
  end

  unless absolute_path_indicates_to_ftp_directory?(remotefile)
    raise Net::FTPPermError.new("550")
  end

  if  File.exist?(remotefile)
    raise Net::FTPPermError.new("550")
  end

  copy_file(localfile, remotefile)
  nil
end
putbinaryfile(*args) click to toggle source

:nocov:

# File lib/ftp_liar.rb, line 244
def putbinaryfile(*args)
  # A simple method that manages to copy a local file on the FTP.
  put(*args)
end
puttextfile(*args) click to toggle source
# File lib/ftp_liar.rb, line 249
def puttextfile(*args)
  put(*args)
end
pwd() click to toggle source

:nocov:

# File lib/ftp_liar.rb, line 254
def pwd
  # Method return actual directory
  raise Net::FTPPermError.new("530 Please login with USER and PASS.") unless @is_connect
  ftp_directory_length = @ftp_directory.length
  if Dir.pwd == @ftp_directory
    "/"
  else
    Dir.pwd[ftp_directory_length..-1]
  end
end
quit() click to toggle source
# File lib/ftp_liar.rb, line 265
def quit
  raise Errno::EPIPE unless @is_connect
  chdir("/")
  @is_connect = false
  nil
end
rename(fromname, toname) click to toggle source
# File lib/ftp_liar.rb, line 272
def rename(fromname, toname)
  raise Net::FTPPermError.new("530 Please login with USER and PASS.") unless @is_connect
  [fromname, toname].each do |path|
    eval(%Q{
      if path == "/"
        raise Net::FTPPermError.new("550")
      end
    })
  end
  fromname = create_path(fromname)
  toname = create_path(toname)

  [fromname, toname].each do |path|
    eval(%Q{
      unless absolute_path_indicates_to_ftp_directory?(path)
        raise Net::FTPPermError.new("550")
      end

      if File.directory?(path) && !Dir.entries(path).sort[2..-1].empty?
        raise Net::FTPPermError.new("550")
      end
    })
  end

  FileUtils.mv(fromname, toname)
  nil
end
retrbinary(cmd, blocksize, rest_offset = nil) click to toggle source

:nocov:

# File lib/ftp_liar.rb, line 301
def retrbinary(cmd, blocksize, rest_offset = nil)
  raise NotImplementedError("Method not implemented. Override it if you want use this method.")
end
retrlines(cmd) click to toggle source
# File lib/ftp_liar.rb, line 305
def retrlines(cmd)
  raise NotImplementedError("Method not implemented. Override it if you want use this method.")
end
rmdir(dirname) click to toggle source

:nocov:

# File lib/ftp_liar.rb, line 310
def rmdir(dirname)
  # Method remove directory on FTP
  raise Net::FTPPermError.new("530 Please login with USER and PASS.") unless @is_connect
  dirname = if dirname[0] == "/"
    File.join(@ftp_directory, dirname[1..-1])
  else
    File.join(@ftp_directory, pwd[1..-1], dirname)
  end

  if File.file?(dirname) || !File.exist?(dirname)
    raise Net::FTPPermError.new("550")
  end
  Dir.delete(dirname)
  nil
end
sendcmd(cmd) click to toggle source

:nocov:

# File lib/ftp_liar.rb, line 327
def sendcmd(cmd)
  raise NotImplementedError("Method not implemented. Override it if you want use this method.")
end
set_socket(sock, get_greeting=true) click to toggle source
# File lib/ftp_liar.rb, line 331
def set_socket(sock, get_greeting=true)
  raise NotImplementedError("Method not implemented. Override it if you want use this method.")
end
site(arg) click to toggle source
# File lib/ftp_liar.rb, line 335
def site(arg)
  raise NotImplementedError("Method not implemented. Override it if you want use this method.")
end
size(filename) click to toggle source

:nocov:

# File lib/ftp_liar.rb, line 340
def size(filename)
  raise Net::FTPPermError.new("530 Please login with USER and PASS.") unless @is_connect
  filename = create_path(filename)
  unless absolute_path_indicates_to_ftp_directory?(filename)
    raise Net::FTPPermError.new("550")
  end

  if File.directory?(filename)
    raise Net::FTPPermError.new("550")
  end

  File.size(filename)
end
sotrlines(cmd, file) click to toggle source
# File lib/ftp_liar.rb, line 363
def sotrlines(cmd, file)
  raise NotImplementedError("Method not implemented. Override it if you want use this method.")
end
status() click to toggle source

:nocov:

# File lib/ftp_liar.rb, line 355
def status()
  raise NotImplementedError("Method not implemented. Override it if you want use this method.")
end
storbinary(cmd, file, blocksize, rest_offset = nil) click to toggle source
# File lib/ftp_liar.rb, line 359
def storbinary(cmd, file, blocksize, rest_offset = nil)
  raise NotImplementedError("Method not implemented. Override it if you want use this method.")
end
system() click to toggle source
# File lib/ftp_liar.rb, line 367
def system()
  raise NotImplementedError("Method not implemented. Override it if you want use this method.")
end
voidcmd(cmd) click to toggle source
# File lib/ftp_liar.rb, line 371
def voidcmd(cmd)
  raise NotImplementedError("Method not implemented. Override it if you want use this method.")
end

Private Instance Methods

absolute_path_indicates_to_ftp_directory?(path) click to toggle source
# File lib/ftp_liar.rb, line 384
def absolute_path_indicates_to_ftp_directory?(path)
  File.absolute_path(path).start_with?(@ftp_directory)
end
copy_file(from, to) click to toggle source
# File lib/ftp_liar.rb, line 380
def copy_file(from, to)
  FileUtils.copy_file(from, to)
end
create_path(path) click to toggle source
# File lib/ftp_liar.rb, line 388
def create_path(path)
  if path[0] == "/"
    File.join(@ftp_directory, path[1..-1])
  elsif pwd == "/"
    File.join(@ftp_directory, path)
  else
    File.join(@ftp_directory, pwd[1..-1], path)
  end
end