winrm-fs 1.3.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d7898b5512c391163a77d694860db51a5c15c5745031c2b34c0cd181a9bbda37
4
- data.tar.gz: 87864314b0863331944c604d26c0721db568db238da87923b836d80097531dad
3
+ metadata.gz: d4af545258de812839e8ff58480b26128f23b9335207bf62eafed48ef9220d0a
4
+ data.tar.gz: b77310f1de5e0d6a8eb1cd2df45948ac4b848684c4646d25e75045cd8967a235
5
5
  SHA512:
6
- metadata.gz: d468205639de56be5fcf4c4896a823f96760e03e23153b6e09cc9c1bc2779725d84f429a759d67088e622a288a3e002bb528f9e23749cded6416baac38b26614
7
- data.tar.gz: b8787f4e3ae986aa32ae324cd1e6113081b83cb291ba519b027dea7ee67f0c4a0d9d15e50e38222328d94829735156a525c066cab56966c6e92de25754efc5b4
6
+ metadata.gz: 2ba78d2418a7cab543ac316dced1053c935a07c56b052053f84dfaf8d676dd4600dffe51e5c040204e2ba6f83131fd90180513725135849e8eacda72e23e49bb
7
+ data.tar.gz: fbb8b91d4283677593d68391d1be8e5d168230a1d82cfecbf3cf06c9a7dfa55adf4bac02d53e39c78c9a83a7a408abb0d0235d8c71b223f99581280bb0451e8e
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.0
1
+ 1.3.1
@@ -1,4 +1,7 @@
1
1
  # WinRM-fs Gem Changelog
2
+ # 1.3.1
3
+ - Download files in chunks
4
+
2
5
  # 1.3.0
3
6
  - Upload from StringIO object
4
7
  - Add missing winrm/exceptions require in file_transporter
@@ -347,6 +347,7 @@ module WinRM
347
347
  path = false
348
348
  locals.each do |local|
349
349
  raise UploadSourceError if string_io
350
+
350
351
  if local.is_a?(StringIO)
351
352
  string_io = true
352
353
  else
@@ -103,6 +103,7 @@ module WinRM
103
103
  def clean_dirname(dir)
104
104
  paths = Pathname.glob(dir)
105
105
  raise "Expected Pathname.glob(dir) to return only dir, got #{paths}" if paths.length != 1
106
+
106
107
  paths.first
107
108
  end
108
109
 
@@ -59,16 +59,44 @@ module WinRM
59
59
  # Downloads the specified remote file to the specified local path
60
60
  # @param [String] The full path on the remote machine
61
61
  # @param [String] The full path to write the file to locally
62
- def download(remote_path, local_path, first = true)
63
- @logger.debug("downloading: #{remote_path} -> #{local_path}")
64
- script = WinRM::FS::Scripts.render('download', path: remote_path)
65
- output = @connection.shell(:powershell) { |e| e.run(script) }
66
- contents = output.stdout.gsub('\n\r', '')
67
- return false if output.exitcode != 0
68
- download_dir(remote_path, local_path, first) if contents.empty?
69
- IO.binwrite(local_path, Base64.decode64(contents)) unless contents.empty?
62
+ # rubocop:disable Metrics/MethodLength
63
+ def download(remote_path, local_path, chunk_size = 1024 * 1024, first = true)
64
+ @logger.debug("downloading: #{remote_path} -> #{local_path} #{chunk_size}")
65
+ index = 0
66
+ output = _output_from_file(remote_path, chunk_size, index)
67
+ return download_dir(remote_path, local_path, chunk_size, first) if output.exitcode == 2
68
+
69
+ return false if output.exitcode >= 1
70
+
71
+ File.open(local_path, 'wb') do |fd|
72
+ out = _write_file(fd, output)
73
+ index += out.length
74
+ until out.empty?
75
+ output = _output_from_file(remote_path, chunk_size, index)
76
+ return false if output.exitcode >= 1
77
+
78
+ out = _write_file(fd, output)
79
+ index += out.length
80
+ end
81
+ end
70
82
  true
71
83
  end
84
+ # rubocop:enable Metrics/MethodLength
85
+
86
+ def _output_from_file(remote_path, chunk_size, index)
87
+ script = WinRM::FS::Scripts.render('download', path: remote_path, chunk_size: chunk_size, index: index)
88
+ @connection.shell(:powershell) { |e| e.run(script) }
89
+ end
90
+
91
+ def _write_file(tofd, output)
92
+ contents = output.stdout.gsub('\n\r', '')
93
+ out = Base64.decode64(contents)
94
+ out = out[0, out.length - 1] if out.end_with? "\x00"
95
+ return out if out.empty?
96
+
97
+ tofd.write(out)
98
+ out
99
+ end
72
100
 
73
101
  # Checks to see if the given path exists on the target file system.
74
102
  # @param [String] The full path to the directory or file
@@ -115,12 +143,12 @@ module WinRM
115
143
 
116
144
  private
117
145
 
118
- def download_dir(remote_path, local_path, first)
146
+ def download_dir(remote_path, local_path, chunk_size, first)
119
147
  local_path = File.join(local_path, File.basename(remote_path.to_s)) if first
120
148
  FileUtils.mkdir_p(local_path) unless File.directory?(local_path)
121
149
  command = "Get-ChildItem #{remote_path} | Select-Object Name"
122
150
  @connection.shell(:powershell) { |e| e.run(command) }.stdout.strip.split(/\n/).drop(2).each do |file|
123
- download(File.join(remote_path.to_s, file.strip), File.join(local_path, file.strip), first = false)
151
+ download(File.join(remote_path.to_s, file.strip), File.join(local_path, file.strip), chunk_size, false)
124
152
  end
125
153
  end
126
154
  end
@@ -1,11 +1,17 @@
1
1
  $p = $ExecutionContext.SessionState.Path
2
2
  $path = $p.GetUnresolvedProviderPathFromPSPath("<%= path %>")
3
- If (Test-Path $path -PathType Leaf) {
4
- $bytes = [System.convert]::ToBase64String([System.IO.File]::ReadAllBytes($path))
3
+ $index = <%= index %>
4
+ $chunkSize = <%= chunk_size %>
5
+ if (Test-Path $path -PathType Leaf) {
6
+ $file = [System.IO.File]::OpenRead($path)
7
+ $seekedTo = $file.Seek($index, [System.IO.SeekOrigin]::Begin)
8
+ $chunk = New-Object byte[] $chunkSize
9
+ $bytesRead = $file.Read($chunk, 0, $chunkSize)
10
+ $bytes = [System.convert]::ToBase64String($chunk[0..$bytesRead])
5
11
  Write-Host $bytes
6
12
  exit 0
7
13
  }
8
14
  ElseIf (Test-Path $path -PathType Container) {
9
- exit 0
15
+ exit 2
10
16
  }
11
17
  exit 1
@@ -58,6 +58,24 @@ describe WinRM::FS::FileManager do
58
58
  end
59
59
  end
60
60
 
61
+ context 'download file chunked' do
62
+ let(:download_dir) { File.join(spec_dir, 'temp') }
63
+ let(:dest_file) { Pathname.new(File.join(dest_dir, File.basename(this_file))) }
64
+ let(:download_file) { Pathname.new(File.join(download_dir, File.basename(this_file))) }
65
+
66
+ before(:each) do
67
+ expect(subject.delete(dest_dir)).to be true
68
+ FileUtils.rm_rf(Dir.glob("#{download_dir}/*"))
69
+ FileUtils.mkdir_p(download_dir)
70
+ end
71
+
72
+ it 'should download the specified file in 1000 byte chunks' do
73
+ subject.upload(this_file, dest_file)
74
+ subject.download(dest_file, download_file, 1000)
75
+ expect(File.open(download_file).read).to eq(File.open(this_file).read)
76
+ end
77
+ end
78
+
61
79
  context 'download directory' do
62
80
  let(:download_dir) { File.join(spec_dir, 'temp') }
63
81
  let(:dest_file) { Pathname.new(File.join(dest_dir, File.basename(this_file))) }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: winrm-fs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shawn Neal
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-08-31 00:00:00.000000000 Z
12
+ date: 2018-10-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: erubis