module SMB::ClientHelper

Helper methods to get simpler access to smbclient's capabilities

Public Instance Methods

del(path, raise = true) click to toggle source

Delete a remote file @param [String] path The remote file to be deleted @param [Boolean] raise raise raise Error or just return false @return [Boolean] true on success

# File lib/smb/client/client_helper.rb, line 96
def del(path, raise = true)
  path = '"' + path + '"' if path.include? ' '
  exec 'del ' + path
  true
rescue Client::RuntimeError => e
  raise e if raise
  false
end
Also aliased as: rm
dir(mask = '', raise = true)

dir is an alias for ls

Alias for: ls
exist?(mask) click to toggle source

Returns `true` if mask exist on server @param [String] mask Mask to search for @return [Boolean] TrueClass if mask exist

# File lib/smb/client/client_helper.rb, line 149
def exist?(mask)
  ls_items = ls mask, false
  !ls_items.empty?
end
get(from, to = nil, overwrite = false, raise = true) click to toggle source

Receive a file from the smb server to local. If to was not passed, a tempfile will be generated. @param [String] from The remote file to be read @param [String] to local file path to be created @param [Boolean] overwrite Overwrite if exist locally? @param [Boolean] raise raise Error or just return false @return [String] path to local file

# File lib/smb/client/client_helper.rb, line 115
def get(from, to = nil, overwrite = false, raise = true)
  # Create a new tempfile but delete it
  # The tempfile.path should be free to use now
  tempfile = Tempfile.new
  to ||= tempfile.path
  tempfile.unlink

  if !overwrite && File.exist?(to)
    raise Client::RuntimeError, "File [#{to}] already exist locally"
  end
  from = '"' + from + '"' if from.include? ' '
  exec 'get ' + from + ' ' + to
  to
rescue Client::RuntimeError => e
  raise e if raise
  false
end
ls(mask = '', raise = true) click to toggle source

List contents of a remote directory @param [String] mask The matching mask @param [Boolean] raise If set, an error will be raised. If set to false, an empty array will be returned @return [Array] List of mask matching LsItems

# File lib/smb/client/client_helper.rb, line 14
def ls(mask = '', raise = true)
  ls_items = []
  mask = '"' + mask + '"' if mask.include? ' '
  output = exec 'ls ' + mask
  output.lines.each do |line|
    ls_item = LsItem.from_line(line)
    ls_items << ls_item if ls_item
  end
  ls_items
rescue Client::RuntimeError => e
  raise e if raise
  []
end
Also aliased as: dir
mkdir(path, raise = true) click to toggle source

Creates a new directory on the server @param [String] path The path to be created @param [Boolean] raise raise Error or just return false @return [Boolean] true on success

# File lib/smb/client/client_helper.rb, line 35
def mkdir(path, raise = true)
  path = '"' + path + '"' if path.include? ' '
  exec 'mkdir ' + path
  true
rescue Client::RuntimeError => e
  raise e if raise
  false
end
put(from, to, overwrite = false, raise = true) click to toggle source

Upload a local file @param [String] from The source file path (on local machine) @param [String] to The destination file path @param [Boolean] overwrite Overwrite if exist on server? @param [Boolean] raise raise Error or just return false @return [Boolean] true on success

# File lib/smb/client/client_helper.rb, line 63
def put(from, to, overwrite = false, raise = true)
  ls_items = ls to, false
  if !overwrite && !ls_items.empty?
    raise Client::RuntimeError, "File [#{to}] already exist"
  end
  from = '"' + from + '"' if from.include? ' '
  to = '"' + to + '"' if to.include? ' '
  exec 'put ' + from + ' ' + to
  true
rescue Client::RuntimeError => e
  raise e if raise
  false
end
read(from, overwrite = false, raise = true) click to toggle source

Reads a remote file and return its content @param [String] from The file to be read from server @param [Boolean] overwrite Overwrite if exist locally? @param [Boolean] raise raise Error or just return false @return [String] The content of the remote file

# File lib/smb/client/client_helper.rb, line 138
def read(from, overwrite = false, raise = true)
  tempfile = Tempfile.new
  to = tempfile.path
  tempfile.unlink
  get from, to, overwrite, raise
  File.read to
end
rm(path, raise = true)

rm is an alias for del

Alias for: del
rmdir(path, raise = true) click to toggle source

Removes a directory on the server @param [String] path The path to be removed @param [TrueClass/FalseClass] raise raise Error or just return false @return [Boolean] true on success

# File lib/smb/client/client_helper.rb, line 48
def rmdir(path, raise = true)
  path = '"' + path + '"' if path.include? ' '
  exec 'rmdir ' + path
  true
rescue Client::RuntimeError => e
  raise e if raise
  false
end
write(content, to, overwrite = false, raise = true) click to toggle source

Writes content to remote file @param [String] content The content to be written @param [String] to The destination file path @param [Boolean] overwrite Overwrite if exist on server? @param [Boolean] raise raise Error or just return false @return [Boolean] true on success

# File lib/smb/client/client_helper.rb, line 83
def write(content, to, overwrite = false, raise = true)
  # This is just a hack around +put+
  tempfile = Tempfile.new
  tempfile.write content
  tempfile.close

  put tempfile.path, to, overwrite, raise
end