net-sftp 4.0.0 → 4.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/CHANGES.txt +52 -0
- data/README.rdoc +3 -0
- data/lib/net/sftp/operations/dir.rb +5 -4
- data/lib/net/sftp/operations/download.rb +265 -34
- data/lib/net/sftp/operations/file.rb +60 -26
- data/lib/net/sftp/operations/upload.rb +152 -28
- data/lib/net/sftp/protocol/01/attributes.rb +6 -2
- data/lib/net/sftp/protocol/01/base.rb +2 -1
- data/lib/net/sftp/protocol/01/name.rb +1 -1
- data/lib/net/sftp/protocol/03/base.rb +11 -1
- data/lib/net/sftp/protocol/04/attributes.rb +12 -2
- data/lib/net/sftp/protocol/04/base.rb +2 -1
- data/lib/net/sftp/protocol/04/name.rb +23 -14
- data/lib/net/sftp/protocol/05/base.rb +9 -6
- data/lib/net/sftp/protocol/base.rb +10 -2
- data/lib/net/sftp/protocol/bounded_read.rb +25 -0
- data/lib/net/sftp/session.rb +21 -3
- data/lib/net/sftp/version.rb +2 -2
- data/lib/net/sftp.rb +16 -10
- data.tar.gz.sig +2 -1
- metadata +30 -36
- metadata.gz.sig +2 -4
- data/.github/workflows/ci.yml +0 -35
- data/.gitignore +0 -6
- data/Gemfile +0 -15
- data/Manifest +0 -55
- data/Rakefile +0 -53
- data/net-sftp-public_cert.pem +0 -20
- data/net-sftp.gemspec +0 -48
- data/setup.rb +0 -1331
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 618cad45c3de8f52cf2579f2616d27df2ecd69d35dce35c6b80822dab1a0f8ac
|
|
4
|
+
data.tar.gz: 7b07b32f66e9109e037582827bc2dc602b19479d93b6be3e526f263cd499062e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 47d88176bf4883648da4339a5d250e69c84921bc96a5424ff9bb5bb65d4c83b97fa2543e2a11f03d3043c6a04bf8bde5026c167ef6ccd9c1d880f7e8dcb08d10
|
|
7
|
+
data.tar.gz: c7568857412af07782a37dbc7516fb59af76e47bff76e9229a9fa2429076fca96ca06d92b6f9706de20c700917ecb887ea8e793d23d03c969fa698ad79c37280
|
checksums.yaml.gz.sig
CHANGED
|
Binary file
|
data/CHANGES.txt
CHANGED
|
@@ -1,3 +1,55 @@
|
|
|
1
|
+
=== 4.1.1
|
|
2
|
+
|
|
3
|
+
* A read error during a single-file download is raised once, after the outstanding reads complete and the handle is closed, instead of once per in-flight read [#212]
|
|
4
|
+
* `abort!` now stops uploads and downloads: no new write or read is sent, the requests in flight finish and the open files are closed, so `wait` returns once their replies arrive. Previously the transfer kept running to the end, and an upload reported `:finish` twice. [#211]
|
|
5
|
+
* Single-file uploads keep up to `:requests` writes in flight (default 64, like OpenSSH's sftp client, previously 2) so large uploads are no longer bound by round-trip time. The file is closed once every write is acknowledged, and a failed write is raised once, after the close. [#210]
|
|
6
|
+
* Package only `lib`, `CHANGES.txt`, `LICENSE.txt` and `README.rdoc` in the gem, about 20% smaller. Thanks @yuri-zubov [#199]
|
|
7
|
+
|
|
8
|
+
=== 4.1.0
|
|
9
|
+
|
|
10
|
+
* BREAKING: Require Ruby 2.6 or later. Bundler on older Rubies resolves to 4.0.2. [#207]
|
|
11
|
+
* BREAKING: `upload` and `download` no longer close an IO object passed in as the local side; `download` flushes it instead. Callers that relied on the close must close it themselves. [#202]
|
|
12
|
+
* Security: An `FXP_VERSION` packet after the session is open now raises `Net::SFTP::Exception` instead of switching protocol version mid-session and dropping pending requests. [#193]
|
|
13
|
+
* Security: A server can no longer make the client allocate without bound, or hang, by sending an ACL, extended attribute or directory listing count larger than the packet holds; such packets now raise `Net::SFTP::Exception`. [#192]
|
|
14
|
+
* Single-file downloads keep up to `:requests` reads in flight (default 64) instead of waiting for each 32 KB reply, so large downloads are no longer bound by round-trip time. [#204]
|
|
15
|
+
* `upload!` and `download!` now give new files and directories the source's permissions (masked to 0777, subject to the umask) like OpenSSH's sftp client, so executables stay executable and private files are no longer created world-readable. The new `:preserve` option also copies access and modification times and sets permissions exactly. As with OpenSSH, attributes are best effort: if they can't be read the transfer uses default permissions, and if they can't be set a warning is logged and the transfer still succeeds. [#203]
|
|
16
|
+
* Added `Net::SFTP::Session#extensions`, the extensions the server advertised in its `FXP_VERSION` packet (such as `posix-rename@openssh.com`). [#195]
|
|
17
|
+
* The `:version` option now caps the negotiated SFTP protocol version; servers that always reply with their own version (OpenSSH replies 3) used to be negotiated at that version instead of the one requested. [#194]
|
|
18
|
+
|
|
19
|
+
=== 4.0.2
|
|
20
|
+
|
|
21
|
+
* Security: Recursive downloads refuse to follow an existing local symlink at a server-named path instead of writing through it. The local path passed by the caller may still be a symlink. Suggested in #162 by srosenhamer [#186]
|
|
22
|
+
* Security: Recursive downloads reject file names from the server that are not a single path component (such as `../x` or `a/b`) instead of writing outside the target directory. Reported in #162 by srosenhamer [#185, #162]
|
|
23
|
+
* Net::SFTP now works with frozen string literals enabled: `File` reads and `Dir#glob` no longer mutate frozen strings, and `glob` no longer modifies entry names in place (adds `Name#name=`). Based on #157 by @frederikspang [#198, #157]
|
|
24
|
+
* `File#read` and `File#gets` no longer loop forever when the server returns an empty data packet, and `read` with a negative length raises `ArgumentError` like `IO#read`. Based on #174 by @OskarEichler [#189, #174]
|
|
25
|
+
* Show fields an SFTP v4+ server omitted as `?` in directory listings (`longname`), like `ls -l` does, instead of blank, `-` or `---------` [#188]
|
|
26
|
+
* Fixed `File#write` hanging on data of 256 KiB or more by sending it in 32 KiB write requests [#99, #187]
|
|
27
|
+
* SFTP request ids now wrap around at the uint32 limit, skipping ids still awaiting a response, instead of growing past it so that responses on very long-lived sessions could no longer be matched to their requests. Thanks @OskarEichler [#179]
|
|
28
|
+
* Uploads keep exactly `:requests` writes in flight (one more was sent before, so the default is now 2 rather than 3), and a zero, negative or non-numeric `:requests` or `:read_size` on `upload` or `download` now raises `ArgumentError` when the transfer is set up instead of stalling. Thanks @OskarEichler [#178]
|
|
29
|
+
* Reconnecting a closed `Net::SFTP::Session` now discards partially received packet data from the old channel, so stale bytes are no longer parsed as the start of the new channel's first packet. Thanks @OskarEichler [#177]
|
|
30
|
+
* Don't crash formatting a directory listing (`longname`) when an SFTP v4+ server omits the modification time, size or permissions, and don't show omitted permissions as `rwxrwxrwx`. Thanks @OskarEichler [#175, #146]
|
|
31
|
+
* `Dir#glob` no longer lists every subdirectory for a pattern without `/` (which can only match top-level names), saving a round trip per directory and no longer failing on unreadable subdirectories. Thanks @OskarEichler [#173, #180]
|
|
32
|
+
* `File#gets` no longer rescans the whole line after each chunk, so reading very long lines is no longer quadratic (a 16 MiB line ending in `"\r\n"` went from about 5 s to 0.01 s). Thanks @OskarEichler [#172]
|
|
33
|
+
* `File#gets` treats a nil or negative limit as no limit, like `IO#gets`, instead of returning nil and corrupting the position (or raising for nil). Thanks @OskarEichler [#171]
|
|
34
|
+
* Fixed SFTP v5/v6 `open` ignoring `IO::TRUNC` and `IO::CREAT` in numeric flags: `IO::WRONLY | IO::TRUNC` now requests `TRUNCATE_EXISTING` instead of opening without truncation, and read-only opens with `IO::CREAT` now request `OPEN_OR_CREATE`. Thanks @OskarEichler [#170]
|
|
35
|
+
* Include the server's error message in `StatusException` and `Response#to_s` for SFTP protocol v3 and later, instead of only the status code. Thanks @OskarEichler [#169, #156]
|
|
36
|
+
* Fixed SFTP v4+ attribute serialization raising `TypeError` when nanoseconds were given for only some timestamps (e.g. `atime_nseconds` with `mtime` but no `mtime_nseconds`); the missing subsecond fields are now sent as 0. Thanks @OskarEichler [#168]
|
|
37
|
+
* `Net::SFTP.start` with a block now closes the SSH session when the block exits with `break`, `return` or `throw`, instead of leaking it. Thanks @OskarEichler [#167]
|
|
38
|
+
* Attributes objects now copy the hash they are given, so a frozen hash no longer raises `FrozenError` and the caller's hash is no longer modified by the default `:type` or by cached owner/group/uid/gid lookups. Thanks @OskarEichler [#166]
|
|
39
|
+
* `File#gets(limit)` no longer makes an extra remote read when the buffered data already satisfies the limit, and `gets(0)` returns an empty string like `IO#gets`. Thanks @OskarEichler [#165]
|
|
40
|
+
* `File#puts` now behaves like `IO#puts`: with no arguments it writes a newline (it used to write nothing), `nil` and `false` are written instead of raising, objects with `to_ary` are expanded, and a recursive array no longer overflows the stack. Thanks @OskarEichler [#164]
|
|
41
|
+
* Fixed `Net::SFTP::Operations::File#write` writing at the read-ahead offset instead of the current position after a buffered read (e.g. `read(2)` on an `r+` file), which corrupted data. Thanks @OskarEichler [#163]
|
|
42
|
+
* Accept a `Pathname` as the local or remote path for uploads and downloads. Thanks @Tietew and @til [#153, #102]
|
|
43
|
+
|
|
44
|
+
=== 4.0.1
|
|
45
|
+
|
|
46
|
+
* Allow net-ssh 8 [#181]
|
|
47
|
+
|
|
48
|
+
=== 4.0.0
|
|
49
|
+
|
|
50
|
+
* Allow net-ssh 7
|
|
51
|
+
* Fix multibyte string write [#110]
|
|
52
|
+
|
|
1
53
|
=== 3.0.0
|
|
2
54
|
|
|
3
55
|
* Pass protocol version via Net::SFTP.start [#107]
|
data/README.rdoc
CHANGED
|
@@ -35,6 +35,9 @@ In a nutshell:
|
|
|
35
35
|
# download a file or directory from the remote host
|
|
36
36
|
sftp.download!("/path/to/remote", "/path/to/local")
|
|
37
37
|
|
|
38
|
+
# also copy access and modification times (permissions are always copied)
|
|
39
|
+
sftp.download!("/path/to/remote", "/path/to/local", :preserve => true)
|
|
40
|
+
|
|
38
41
|
# grab data off the remote host directly to a buffer
|
|
39
42
|
data = sftp.download!("/path/to/remote")
|
|
40
43
|
|
|
@@ -58,17 +58,18 @@ module Net; module SFTP; module Operations
|
|
|
58
58
|
def glob(path, pattern, flags=0)
|
|
59
59
|
flags |= ::File::FNM_PATHNAME
|
|
60
60
|
path = path.chop if path.end_with?('/') && path != '/'
|
|
61
|
+
recursive = !pattern.is_a?(String) || pattern.include?('/')
|
|
61
62
|
|
|
62
63
|
results = [] unless block_given?
|
|
63
64
|
queue = entries(path).reject { |e| %w(. ..).include?(e.name) }
|
|
64
65
|
while queue.any?
|
|
65
66
|
entry = queue.shift
|
|
66
67
|
|
|
67
|
-
if entry.directory? && !%w(. ..).include?(::File.basename(entry.name))
|
|
68
|
-
queue
|
|
69
|
-
e.name
|
|
68
|
+
if recursive && entry.directory? && !%w(. ..).include?(::File.basename(entry.name))
|
|
69
|
+
queue.concat(entries("#{path}/#{entry.name}").map do |e|
|
|
70
|
+
e.name = "#{entry.name}/#{e.name}"
|
|
70
71
|
e
|
|
71
|
-
end
|
|
72
|
+
end)
|
|
72
73
|
end
|
|
73
74
|
|
|
74
75
|
if ::File.fnmatch(pattern, entry.name, flags)
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
require 'pathname'
|
|
1
2
|
require 'net/ssh/loggable'
|
|
2
3
|
|
|
3
4
|
module Net; module SFTP; module Operations
|
|
@@ -48,12 +49,26 @@ module Net; module SFTP; module Operations
|
|
|
48
49
|
# * <tt>:progress</tt> - either a block or an object to act as a progress
|
|
49
50
|
# callback. See the discussion of "progress monitoring" below.
|
|
50
51
|
# * <tt>:requests</tt> - the number of pending SFTP requests to allow at
|
|
51
|
-
# any given time. When downloading
|
|
52
|
-
#
|
|
53
|
-
#
|
|
52
|
+
# any given time. When downloading a single file, this is the number of
|
|
53
|
+
# reads kept in flight and defaults to 64. When downloading an entire
|
|
54
|
+
# directory tree recursively, it is the number of files and directories
|
|
55
|
+
# transferred at once and defaults to 16. Setting this higher might
|
|
56
|
+
# improve throughput, particularly on high-latency links. Reducing it
|
|
57
|
+
# will reduce throughput.
|
|
54
58
|
# * <tt>:read_size</tt> - the maximum number of bytes to read at a time
|
|
55
59
|
# from the source. Increasing this value might improve throughput. It
|
|
56
60
|
# defaults to 32,000 bytes.
|
|
61
|
+
# * <tt>:preserve</tt> - if true, the local files and directories get the
|
|
62
|
+
# access and modification times of the remote ones, and their permissions
|
|
63
|
+
# are set exactly, even on files that already exist and regardless of the
|
|
64
|
+
# umask.
|
|
65
|
+
#
|
|
66
|
+
# Like OpenSSH's sftp client, new local files and directories are created
|
|
67
|
+
# with the permissions of the remote ones, without setuid, setgid and sticky
|
|
68
|
+
# bits and subject to the umask. Nothing is set when +local+ is an IO object.
|
|
69
|
+
# If the remote attributes can't be read, the download goes ahead with
|
|
70
|
+
# default permissions; if they can't be applied locally, a warning is logged
|
|
71
|
+
# and the download still succeeds.
|
|
57
72
|
#
|
|
58
73
|
# == Progress Monitoring
|
|
59
74
|
#
|
|
@@ -145,13 +160,20 @@ module Net; module SFTP; module Operations
|
|
|
145
160
|
# This will return immediately, and requires that the SSH event loop be
|
|
146
161
|
# run in order to effect the download. (See #wait.)
|
|
147
162
|
def initialize(sftp, local, remote, options={}, &progress)
|
|
163
|
+
local = local.to_path if local.is_a?(Pathname)
|
|
164
|
+
remote = remote.to_path if remote.is_a?(Pathname)
|
|
165
|
+
|
|
148
166
|
@sftp = sftp
|
|
149
167
|
@local = local
|
|
150
168
|
@remote = remote
|
|
151
169
|
@progress = progress || options[:progress]
|
|
152
170
|
@options = options
|
|
153
171
|
@active = 0
|
|
172
|
+
@aborted = false
|
|
173
|
+
@directories = []
|
|
154
174
|
@properties = options[:properties] || {}
|
|
175
|
+
requests
|
|
176
|
+
read_size
|
|
155
177
|
|
|
156
178
|
self.logger = sftp.logger
|
|
157
179
|
|
|
@@ -175,9 +197,10 @@ module Net; module SFTP; module Operations
|
|
|
175
197
|
@active > 0 || stack.any?
|
|
176
198
|
end
|
|
177
199
|
|
|
178
|
-
# Forces the transfer to stop.
|
|
200
|
+
# Forces the transfer to stop. Reads already sent are left to finish,
|
|
201
|
+
# then each open file is closed, so #wait returns once that is done.
|
|
179
202
|
def abort!
|
|
180
|
-
@
|
|
203
|
+
@aborted = true
|
|
181
204
|
@stack.clear
|
|
182
205
|
end
|
|
183
206
|
|
|
@@ -204,7 +227,8 @@ module Net; module SFTP; module Operations
|
|
|
204
227
|
|
|
205
228
|
# A simple struct for encapsulating information about a single remote
|
|
206
229
|
# file or directory that needs to be downloaded.
|
|
207
|
-
Entry = Struct.new(:remote, :local, :directory, :size, :handle, :offset, :sink
|
|
230
|
+
Entry = Struct.new(:remote, :local, :directory, :size, :handle, :offset, :sink,
|
|
231
|
+
:next_read, :pending, :window, :chunks, :eof, :attributes, :error)
|
|
208
232
|
|
|
209
233
|
#--
|
|
210
234
|
# "ruby -w" hates private attributes, so we have to do these longhand
|
|
@@ -222,13 +246,29 @@ module Net; module SFTP; module Operations
|
|
|
222
246
|
|
|
223
247
|
# The number of bytes to read at a time from remote files.
|
|
224
248
|
def read_size
|
|
225
|
-
|
|
249
|
+
@read_size ||= positive_option(:read_size, DEFAULT_READ_SIZE)
|
|
226
250
|
end
|
|
227
251
|
|
|
228
|
-
# The number of
|
|
229
|
-
# Defaults to 16 for recursive downloads.
|
|
252
|
+
# The number of simultaneous SFTP requests to use to effect the download.
|
|
253
|
+
# Defaults to 16 for recursive downloads and 64 for single files.
|
|
230
254
|
def requests
|
|
231
|
-
|
|
255
|
+
@requests ||= positive_option(:requests, recursive? ? 16 : 64)
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# The most reads a single file may have outstanding at once.
|
|
259
|
+
def read_window_limit
|
|
260
|
+
recursive? ? 1 : requests
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def preserve?
|
|
264
|
+
options[:preserve]
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def positive_option(name, default)
|
|
268
|
+
value = (options[name] || default).to_i
|
|
269
|
+
raise ArgumentError, ":#{name} must be positive" unless value > 0
|
|
270
|
+
|
|
271
|
+
value
|
|
232
272
|
end
|
|
233
273
|
|
|
234
274
|
# Enqueues as many files and directories from the stack as possible
|
|
@@ -240,7 +280,13 @@ module Net; module SFTP; module Operations
|
|
|
240
280
|
|
|
241
281
|
if entry.directory
|
|
242
282
|
update_progress(:mkdir, entry.local)
|
|
243
|
-
|
|
283
|
+
refuse_symlink!(entry.local) if server_named?(entry) && ::File.symlink?(entry.local)
|
|
284
|
+
created = !::File.directory?(entry.local)
|
|
285
|
+
::Dir.mkdir(entry.local, local_permissions(entry, 0777) | 0300) if created
|
|
286
|
+
if created || preserve?
|
|
287
|
+
@directories << entry
|
|
288
|
+
stat_directory(entry) unless entry.attributes
|
|
289
|
+
end
|
|
244
290
|
request = sftp.opendir(entry.remote, &method(:on_opendir))
|
|
245
291
|
request[:entry] = entry
|
|
246
292
|
else
|
|
@@ -248,7 +294,64 @@ module Net; module SFTP; module Operations
|
|
|
248
294
|
end
|
|
249
295
|
end
|
|
250
296
|
|
|
251
|
-
|
|
297
|
+
if !active?
|
|
298
|
+
set_directory_attributes
|
|
299
|
+
update_progress(:finish)
|
|
300
|
+
end
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
# Only entries listed by readdir come with attributes.
|
|
304
|
+
def stat_directory(entry)
|
|
305
|
+
@active += 1
|
|
306
|
+
sftp.stat(entry.remote) do |response|
|
|
307
|
+
@active -= 1
|
|
308
|
+
entry.attributes = remote_attributes(response, "stat #{entry.remote}")
|
|
309
|
+
process_next_entry
|
|
310
|
+
end
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
# Without the remote attributes the download goes ahead with default
|
|
314
|
+
# permissions and no times, as it would for a server that never sent any.
|
|
315
|
+
def remote_attributes(response, operation)
|
|
316
|
+
return response[:attrs] if response.ok?
|
|
317
|
+
|
|
318
|
+
debug { "#{operation} failed, using default attributes: #{response}" }
|
|
319
|
+
nil
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
# Deepest directories first, after their contents; setting attributes on
|
|
323
|
+
# a directory doesn't change its parent's modification time.
|
|
324
|
+
def set_directory_attributes
|
|
325
|
+
@directories.reverse_each do |entry|
|
|
326
|
+
next unless entry.attributes
|
|
327
|
+
|
|
328
|
+
permissions = local_permissions(entry, 0777)
|
|
329
|
+
permissions &= ~::File.umask unless preserve?
|
|
330
|
+
set_local_attributes(entry, permissions)
|
|
331
|
+
end
|
|
332
|
+
@directories.clear
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
# setuid, setgid and sticky bits are never applied, matching OpenSSH.
|
|
336
|
+
def local_permissions(entry, default)
|
|
337
|
+
permissions = entry.attributes && entry.attributes.permissions
|
|
338
|
+
permissions ? permissions & 0777 : default
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
# Like OpenSSH's sftp client, failing to set attributes only warns; the
|
|
342
|
+
# data has already been transferred.
|
|
343
|
+
def set_local_attributes(entry, permissions)
|
|
344
|
+
attempt_local("chmod", entry) { ::File.chmod(permissions, entry.local) } if permissions
|
|
345
|
+
|
|
346
|
+
attributes = entry.attributes
|
|
347
|
+
return unless preserve? && attributes && attributes.atime && attributes.mtime
|
|
348
|
+
attempt_local("utime", entry) { ::File.utime(attributes.atime, attributes.mtime, entry.local) }
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
def attempt_local(operation, entry)
|
|
352
|
+
yield
|
|
353
|
+
rescue SystemCallError => e
|
|
354
|
+
lwarn { "#{operation} #{entry.local} failed: #{e.message}" }
|
|
252
355
|
end
|
|
253
356
|
|
|
254
357
|
# Called when a remote directory is "opened" for reading, e.g. to
|
|
@@ -267,7 +370,7 @@ module Net; module SFTP; module Operations
|
|
|
267
370
|
# and #process_next_entry is called.
|
|
268
371
|
def on_readdir(response)
|
|
269
372
|
entry = response.request[:parent]
|
|
270
|
-
if response.eof?
|
|
373
|
+
if response.eof? || @aborted
|
|
271
374
|
request = sftp.close(entry.handle, &method(:on_closedir))
|
|
272
375
|
request[:parent] = entry
|
|
273
376
|
elsif !response.ok?
|
|
@@ -275,7 +378,10 @@ module Net; module SFTP; module Operations
|
|
|
275
378
|
else
|
|
276
379
|
response[:names].each do |item|
|
|
277
380
|
next if item.name == "." || item.name == ".."
|
|
278
|
-
|
|
381
|
+
validate_entry_name!(entry, item.name)
|
|
382
|
+
child = Entry.new(::File.join(entry.remote, item.name), ::File.join(entry.local, item.name), item.directory?, item.attributes.size)
|
|
383
|
+
child.attributes = item.attributes
|
|
384
|
+
stack << child
|
|
279
385
|
end
|
|
280
386
|
|
|
281
387
|
# take this opportunity to enqueue more requests
|
|
@@ -286,6 +392,38 @@ module Net; module SFTP; module Operations
|
|
|
286
392
|
end
|
|
287
393
|
end
|
|
288
394
|
|
|
395
|
+
# The name comes from the server, so anything but a single path component
|
|
396
|
+
# would let a malicious server write outside the target directory.
|
|
397
|
+
def validate_entry_name!(entry, name)
|
|
398
|
+
if name.empty? || name.include?("/") || name.include?("\0") ||
|
|
399
|
+
(::File::ALT_SEPARATOR && name.include?(::File::ALT_SEPARATOR))
|
|
400
|
+
raise Net::SFTP::Exception, "readdir #{entry.remote} returned an invalid file name: #{name.inspect}"
|
|
401
|
+
end
|
|
402
|
+
end
|
|
403
|
+
|
|
404
|
+
# The caller's own +local+ may be a symlink on purpose; anything below it
|
|
405
|
+
# was named by the server (net-sftp#162).
|
|
406
|
+
def server_named?(entry)
|
|
407
|
+
entry.local != local
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
# Like OpenSSH, the file is always created writable by its owner.
|
|
411
|
+
def open_local_file(entry)
|
|
412
|
+
permissions = local_permissions(entry, 0666) | 0200
|
|
413
|
+
return ::File.open(entry.local, "wb", permissions) unless server_named?(entry) && defined?(::File::NOFOLLOW)
|
|
414
|
+
|
|
415
|
+
begin
|
|
416
|
+
::File.open(entry.local, "wb", permissions, :flags => ::File::NOFOLLOW)
|
|
417
|
+
rescue SystemCallError
|
|
418
|
+
raise unless ::File.symlink?(entry.local)
|
|
419
|
+
refuse_symlink!(entry.local)
|
|
420
|
+
end
|
|
421
|
+
end
|
|
422
|
+
|
|
423
|
+
def refuse_symlink!(path)
|
|
424
|
+
raise Net::SFTP::Exception, "#{path} is a symlink, refusing to follow it"
|
|
425
|
+
end
|
|
426
|
+
|
|
289
427
|
# Called when a file is to be opened for reading from the remote server.
|
|
290
428
|
def open_file(entry)
|
|
291
429
|
update_progress(:open, entry)
|
|
@@ -301,55 +439,148 @@ module Net; module SFTP; module Operations
|
|
|
301
439
|
process_next_entry
|
|
302
440
|
end
|
|
303
441
|
|
|
304
|
-
# Called when a file has been opened. This will call #
|
|
442
|
+
# Called when a file has been opened. This will call #request_reads
|
|
305
443
|
# to initiate the data transfer.
|
|
306
444
|
def on_open(response)
|
|
307
445
|
entry = response.request[:entry]
|
|
308
446
|
raise StatusException.new(response, "open #{entry.remote}") unless response.ok?
|
|
309
447
|
|
|
310
448
|
entry.handle = response[:handle]
|
|
311
|
-
entry.
|
|
449
|
+
if @aborted || entry.attributes || entry.local.respond_to?(:write)
|
|
450
|
+
start_download(entry)
|
|
451
|
+
else
|
|
452
|
+
request = sftp.fstat(entry.handle, &method(:on_fstat))
|
|
453
|
+
request[:entry] = entry
|
|
454
|
+
end
|
|
455
|
+
end
|
|
456
|
+
|
|
457
|
+
# Called when the attributes of a file opened by #open_file arrive.
|
|
458
|
+
def on_fstat(response)
|
|
459
|
+
entry = response.request[:entry]
|
|
460
|
+
entry.attributes = remote_attributes(response, "fstat #{entry.remote}")
|
|
461
|
+
start_download(entry)
|
|
462
|
+
end
|
|
463
|
+
|
|
464
|
+
def start_download(entry)
|
|
465
|
+
return close_aborted_file(entry) if @aborted
|
|
466
|
+
|
|
467
|
+
entry.sink = entry.local.respond_to?(:write) ? entry.local : open_local_file(entry)
|
|
312
468
|
entry.offset = 0
|
|
469
|
+
entry.next_read = 0
|
|
470
|
+
entry.pending = 0
|
|
471
|
+
entry.window = 1
|
|
472
|
+
entry.chunks = {}
|
|
313
473
|
|
|
314
|
-
|
|
474
|
+
request_reads(entry)
|
|
315
475
|
end
|
|
316
476
|
|
|
317
|
-
#
|
|
318
|
-
|
|
319
|
-
|
|
477
|
+
# Keeps up to entry.window reads of #read_size bytes outstanding until
|
|
478
|
+
# the server reports EOF. The window starts at one and grows with each
|
|
479
|
+
# data reply, so small files don't cost a burst of reads past their end.
|
|
480
|
+
def request_reads(entry)
|
|
481
|
+
while !entry.eof && !@aborted && entry.pending < entry.window
|
|
482
|
+
read_range(entry, entry.next_read, read_size)
|
|
483
|
+
entry.next_read += read_size
|
|
484
|
+
end
|
|
485
|
+
end
|
|
486
|
+
|
|
487
|
+
def read_range(entry, offset, length)
|
|
488
|
+
entry.pending += 1
|
|
489
|
+
request = sftp.read(entry.handle, offset, length, &method(:on_read))
|
|
320
490
|
request[:entry] = entry
|
|
321
|
-
request[:offset] =
|
|
491
|
+
request[:offset] = offset
|
|
492
|
+
request[:length] = length
|
|
322
493
|
end
|
|
323
494
|
|
|
324
|
-
# Called when a read from a file finishes.
|
|
325
|
-
#
|
|
326
|
-
#
|
|
495
|
+
# Called when a read from a file finishes. Replies can arrive out of
|
|
496
|
+
# order, so data is buffered until everything before it has been
|
|
497
|
+
# written. Once the server has reported EOF or a read has failed, and
|
|
498
|
+
# no reads are left outstanding, the file is closed.
|
|
327
499
|
def on_read(response)
|
|
328
500
|
entry = response.request[:entry]
|
|
501
|
+
entry.pending -= 1
|
|
329
502
|
|
|
330
|
-
if
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
request[:entry] = entry
|
|
503
|
+
if entry.error
|
|
504
|
+
# The rest of the file is no longer wanted.
|
|
505
|
+
elsif response.eof?
|
|
506
|
+
entry.eof = true
|
|
335
507
|
elsif !response.ok?
|
|
336
|
-
|
|
508
|
+
entry.error = StatusException.new(response, "read #{entry.remote}")
|
|
509
|
+
elsif response[:data].empty? || response[:data].bytesize > response.request[:length]
|
|
510
|
+
entry.error = Net::SFTP::Exception.new("read #{entry.remote} returned #{response[:data].bytesize} bytes for a #{response.request[:length]} byte request")
|
|
511
|
+
else
|
|
512
|
+
offset = response.request[:offset]
|
|
513
|
+
length = response.request[:length]
|
|
514
|
+
data = response[:data]
|
|
515
|
+
|
|
516
|
+
# Servers may return less than asked for without being at EOF, and
|
|
517
|
+
# the reads after this one have already been sent.
|
|
518
|
+
read_range(entry, offset + data.bytesize, length - data.bytesize) if data.bytesize < length && !@aborted
|
|
519
|
+
|
|
520
|
+
entry.chunks[offset] = data
|
|
521
|
+
write_buffered_chunks(entry)
|
|
522
|
+
entry.window += 1 if entry.window < read_window_limit
|
|
523
|
+
end
|
|
524
|
+
|
|
525
|
+
if entry.error
|
|
526
|
+
close_failed_file(entry) if entry.pending.zero?
|
|
337
527
|
else
|
|
338
|
-
entry
|
|
339
|
-
|
|
340
|
-
entry.sink.write(response[:data])
|
|
341
|
-
download_next_chunk(entry)
|
|
528
|
+
request_reads(entry)
|
|
529
|
+
close_file(entry) if entry.pending.zero?
|
|
342
530
|
end
|
|
343
531
|
end
|
|
344
532
|
|
|
533
|
+
def write_buffered_chunks(entry)
|
|
534
|
+
while (data = entry.chunks.delete(entry.offset))
|
|
535
|
+
update_progress(:get, entry, entry.offset, data)
|
|
536
|
+
entry.sink.write(data)
|
|
537
|
+
entry.offset += data.bytesize
|
|
538
|
+
end
|
|
539
|
+
end
|
|
540
|
+
|
|
541
|
+
def close_file(entry)
|
|
542
|
+
if entry.chunks.any? && !@aborted
|
|
543
|
+
raise Net::SFTP::Exception, "read #{entry.remote} hit EOF at #{entry.offset} but the server returned data beyond it"
|
|
544
|
+
end
|
|
545
|
+
|
|
546
|
+
update_progress(:close, entry)
|
|
547
|
+
# The caller's IO stays open, but is flushed so its data is written when download! returns.
|
|
548
|
+
if entry.sink.equal?(local)
|
|
549
|
+
entry.sink.flush if entry.sink.respond_to?(:flush)
|
|
550
|
+
else
|
|
551
|
+
entry.sink.close
|
|
552
|
+
end
|
|
553
|
+
unless entry.local.respond_to?(:write)
|
|
554
|
+
set_local_attributes(entry, preserve? ? local_permissions(entry, nil) : nil)
|
|
555
|
+
end
|
|
556
|
+
request = sftp.close(entry.handle, &method(:on_close))
|
|
557
|
+
request[:entry] = entry
|
|
558
|
+
end
|
|
559
|
+
|
|
560
|
+
# The local file is left untouched: it may hold an earlier copy.
|
|
561
|
+
def close_aborted_file(entry)
|
|
562
|
+
update_progress(:close, entry)
|
|
563
|
+
request = sftp.close(entry.handle, &method(:on_close))
|
|
564
|
+
request[:entry] = entry
|
|
565
|
+
end
|
|
566
|
+
|
|
345
567
|
# Called when a file handle is closed.
|
|
346
568
|
def on_close(response)
|
|
347
569
|
@active -= 1
|
|
348
570
|
entry = response.request[:entry]
|
|
571
|
+
raise entry.error if entry.error
|
|
349
572
|
raise StatusException.new(response, "close #{entry.remote}") unless response.ok?
|
|
350
573
|
process_next_entry
|
|
351
574
|
end
|
|
352
575
|
|
|
576
|
+
# Waiting for every reply before raising keeps the remote handle from
|
|
577
|
+
# leaking and the error from being raised again by each later reply.
|
|
578
|
+
def close_failed_file(entry)
|
|
579
|
+
entry.sink.close unless entry.sink.equal?(local)
|
|
580
|
+
request = sftp.close(entry.handle, &method(:on_close))
|
|
581
|
+
request[:entry] = entry
|
|
582
|
+
end
|
|
583
|
+
|
|
353
584
|
# If a progress callback or object has been set, this will report
|
|
354
585
|
# the progress to that callback or object.
|
|
355
586
|
def update_progress(hook, *args)
|