Class: WinRM::FS::FileManager
- Inherits:
-
Object
- Object
- WinRM::FS::FileManager
- Defined in:
- lib/winrm-fs/file_manager.rb
Overview
Perform file transfer operations between a local machine and winrm endpoint
Instance Method Summary collapse
-
#_output_from_file(remote_path, chunk_size, index) ⇒ Object
rubocop:enable Metrics/MethodLength.
- #_write_file(tofd, output) ⇒ Object
-
#checksum(path, digest = 'SHA1') ⇒ Object
Gets the SHA1 checksum of the specified file if it exists, otherwise ”.
-
#create_dir(path) ⇒ Boolean
Create the specifed directory recursively.
-
#delete(path) ⇒ Boolean
Deletes the file or directory at the specified path.
-
#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.
-
#exists?(path) ⇒ Boolean
Checks to see if the given path exists on the target file system.
-
#initialize(connection) ⇒ FileManager
constructor
Creates a new FileManager instance.
-
#temp_dir ⇒ String
Gets the current user’s TEMP directory on the remote system, for example ‘C:/Windows/Temp’.
-
#upload(local_path, remote_path) {|Number, The, Path, Target| ... } ⇒ Fixnum
Upload one or more local files and directories to a remote directory.
Constructor Details
#initialize(connection) ⇒ FileManager
Creates a new FileManager instance
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 ”
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
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
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
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.
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_dir ⇒ String
Gets the current user’s TEMP directory on the remote system, for example ‘C:/Windows/Temp’
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
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 |