Class: WinRM::FS::FileManager

Inherits:
Object
  • Object
show all
Defined in:
lib/winrm-fs/file_manager.rb

Overview

Perform file transfer operations between a local machine and winrm endpoint

Instance Method Summary collapse

Constructor Details

#initialize(service) ⇒ FileManager

Creates a new FileManager instance



26
27
28
29
# File 'lib/winrm-fs/file_manager.rb', line 26

def initialize(service)
  @service = service
  @logger = Logging.logger[self]
end

Instance Method Details

#checksum(path) ⇒ Object

Gets the MD5 checksum of the specified file if it exists, otherwise ”



34
35
36
37
38
# File 'lib/winrm-fs/file_manager.rb', line 34

def checksum(path)
  @logger.debug("checksum: #{path}")
  script = WinRM::FS::Scripts.render('checksum', path: path)
  @service.powershell(script).stdout.chomp
end

#create_dir(path) ⇒ Boolean

Create the specifed directory recursively



43
44
45
46
47
# File 'lib/winrm-fs/file_manager.rb', line 43

def create_dir(path)
  @logger.debug("create_dir: #{path}")
  script = WinRM::FS::Scripts.render('create_dir', path: path)
  @service.powershell(script)[:exitcode] == 0
end

#delete(path) ⇒ Boolean

Deletes the file or directory at the specified path



52
53
54
55
56
# File 'lib/winrm-fs/file_manager.rb', line 52

def delete(path)
  @logger.debug("deleting: #{path}")
  script = WinRM::FS::Scripts.render('delete', path: path)
  @service.powershell(script)[:exitcode] == 0
end

#download(remote_path, local_path) ⇒ Object

Downloads the specified remote file to the specified local path



61
62
63
64
65
66
67
68
69
70
# File 'lib/winrm-fs/file_manager.rb', line 61

def download(remote_path, local_path)
  @logger.debug("downloading: #{remote_path} -> #{local_path}")
  script = WinRM::FS::Scripts.render('download', path: remote_path)
  output = @service.powershell(script)
  return false if output[:exitcode] != 0
  contents = output.stdout.gsub('\n\r', '')
  out = Base64.decode64(contents)
  IO.binwrite(local_path, out)
  true
end

#exists?(path) ⇒ Boolean

Checks to see if the given path exists on the target file system.



75
76
77
78
79
# File 'lib/winrm-fs/file_manager.rb', line 75

def exists?(path)
  @logger.debug("exists?: #{path}")
  script = WinRM::FS::Scripts.render('exists', path: path)
  @service.powershell(script)[:exitcode] == 0
end

#temp_dirString

Gets the current user’s TEMP directory on the remote system, for example ‘C:/Windows/Temp’



84
85
86
# File 'lib/winrm-fs/file_manager.rb', line 84

def temp_dir
  @guest_temp ||= (@service.cmd('echo %TEMP%')).stdout.chomp.gsub('\\', '/')
end

#upload(local_path, remote_path) {|Number, The, Path, Target| ... } ⇒ Fixnum

Upload one or more local files and directories to a remote directory

Examples:

copy a single file to a winrm endpoint


file_manager.upload('/Users/sneal/myfile.txt', 'c:/foo/myfile.txt')

copy a single directory to a winrm endpoint


file_manager.upload('c:/dev/my_dir', '$env:AppData')

Yield Parameters:

  • Number (Fixnum)

    of bytes copied in current payload sent to the winrm endpoint

  • The (Fixnum)

    total number of bytes to be copied

  • Path (String)

    of file being copied

  • Target (String)

    path on the winrm endpoint



106
107
108
109
110
111
112
113
114
115
# File 'lib/winrm-fs/file_manager.rb', line 106

def upload(local_path, remote_path, &block)
  @logger.debug("uploading: #{local_path} -> #{remote_path}")

  upload_orchestrator = WinRM::FS::Core::UploadOrchestrator.new(@service)
  if File.file?(local_path)
    upload_orchestrator.upload_file(local_path, remote_path, &block)
  else
    upload_orchestrator.upload_directory(local_path, remote_path, &block)
  end
end