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



28
29
30
31
# File 'lib/winrm-fs/file_manager.rb', line 28

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

Instance Method Details

#_output_from_file(remote_path, chunk_size, index) ⇒ Object

rubocop:enable Metrics/MethodLength



88
89
90
91
# File 'lib/winrm-fs/file_manager.rb', line 88

def _output_from_file(remote_path, chunk_size, index)
  script = WinRM::FS::Scripts.render('download', path: remote_path, chunk_size: chunk_size, index: index)
  @connection.shell(:powershell) { |e| e.run(script) }
end

#_write_file(tofd, output) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/winrm-fs/file_manager.rb', line 93

def _write_file(tofd, output)
  contents = output.stdout.gsub('\n\r', '')
  out = Base64.decode64(contents)
  out = out[0, out.length - 1] if out.end_with? "\x00"
  return out if out.empty?

  tofd.write(out)
  out
end

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

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

Parameters:

  • The (String)

    remote file path



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

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



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

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



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

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, chunk_size = 1024 * 1024, first = true) ⇒ Object

Downloads the specified remote file to the specified local path rubocop:disable Metrics/MethodLength

Parameters:

  • The (String)

    full path on the remote machine

  • The (String)

    full path to write the file to locally



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/winrm-fs/file_manager.rb', line 65

def download(remote_path, local_path, chunk_size = 1024 * 1024, first = true)
  @logger.debug("downloading: #{remote_path} -> #{local_path} #{chunk_size}")
  index = 0
  output = _output_from_file(remote_path, chunk_size, index)
  return download_dir(remote_path, local_path, chunk_size, first) if output.exitcode == 2

  return false if output.exitcode >= 1

  File.open(local_path, 'wb') do |fd|
    out = _write_file(fd, output)
    index += out.length
    until out.empty?
      output = _output_from_file(remote_path, chunk_size, index)
      return false if output.exitcode >= 1

      out = _write_file(fd, output)
      index += out.length
    end
  end
  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.



106
107
108
109
110
# File 'lib/winrm-fs/file_manager.rb', line 106

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



115
116
117
118
119
# File 'lib/winrm-fs/file_manager.rb', line 115

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



139
140
141
142
143
144
# File 'lib/winrm-fs/file_manager.rb', line 139

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