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(connection) ⇒ FileManager

Creates a new FileManager instance

Parameters:

  • WinRM (WinRM::Connection)

    web connection client



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

def initialize(connection)
  @connection = connection
  @logger = connection.logger
end

Instance Method Details

#checksum(path, digest = 'SHA1') ⇒ Object

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

Parameters:

  • The (String)

    remote file path



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

def checksum(path, digest = 'SHA1')
  @logger.debug("checksum with #{digest}: #{path}")
  script = WinRM::FS::Scripts.render('checksum', path: path, digest: digest)
  @connection.shell(:powershell) { |e| e.run(script).stdout.chomp }
end

#create_dir(path) ⇒ Boolean

Create the specifed directory recursively

Parameters:

  • The (String)

    remote dir to create

Returns:

  • (Boolean)

    True if successful, otherwise false



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

def create_dir(path)
  @logger.debug("create_dir: #{path}")
  script = WinRM::FS::Scripts.render('create_dir', path: path)
  @connection.shell(:powershell) { |e| e.run(script).exitcode == 0 }
end

#delete(path) ⇒ Boolean

Deletes the file or directory at the specified path

Parameters:

  • The (String)

    path to remove

Returns:

  • (Boolean)

    True if successful, otherwise False



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

def delete(path)
  @logger.debug("deleting: #{path}")
  script = WinRM::FS::Scripts.render('delete', path: path)
  @connection.shell(:powershell) { |e| e.run(script).exitcode == 0 }
end

#download(remote_path, local_path, first = true) ⇒ Object

Downloads the specified remote file to the specified local path

Parameters:

  • The (String)

    full path on the remote machine

  • The (String)

    full path to write the file to locally



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

def download(remote_path, local_path, first = true)
  @logger.debug("downloading: #{remote_path} -> #{local_path}")
  script = WinRM::FS::Scripts.render('download', path: remote_path)
  output = @connection.shell(:powershell) { |e| e.run(script) }
  contents = output.stdout.gsub('\n\r', '')
  return false if output.exitcode != 0
  download_dir(remote_path, local_path, first) if contents.empty?
  IO.binwrite(local_path, Base64.decode64(contents)) unless contents.empty?
  true
end

#exists?(path) ⇒ Boolean

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

Parameters:

  • The (String)

    full path to the directory or file

Returns:

  • (Boolean)

    True if the file/dir exists, otherwise false.



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

def exists?(path)
  @logger.debug("exists?: #{path}")
  script = WinRM::FS::Scripts.render('exists', path: path)
  @connection.shell(:powershell) { |e| e.run(script).exitcode == 0 }
end

#temp_dirString

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

Returns:

  • (String)

    Full path to the temp directory



85
86
87
88
89
# File 'lib/winrm-fs/file_manager.rb', line 85

def temp_dir
  @temp_dir ||= begin
    (@connection.shell(:powershell) { |e| e.run('$env:TEMP') }).stdout.chomp.tr('\\', '/')
  end
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')

Parameters:

  • A (String)

    path to a local directory or file that will be copied to the remote Windows box.

  • The (String)

    target directory or file This path may contain powershell style environment variables

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

Returns:

  • (Fixnum)

    The total number of bytes copied



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

def upload(local_path, remote_path, &block)
  @connection.shell(:powershell) do |shell|
    file_transporter ||= WinRM::FS::Core::FileTransporter.new(shell)
    file_transporter.upload(local_path, remote_path, &block)[0]
  end
end