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.
@@ -12,6 +12,11 @@ module Net; module SFTP; module Operations
12
12
  # puts file.gets
13
13
  # file.close
14
14
  class File
15
+ # The largest amount of data sent in a single write request. Servers drop
16
+ # the connection on oversized packets (OpenSSH's limit is 256 KiB including
17
+ # headers), and the SFTP drafts only guarantee packets of about 34000 bytes.
18
+ MAX_WRITE_SIZE = 32 * 1024
19
+
15
20
  # A reference to the Net::SFTP::Session instance that drives this wrapper
16
21
  attr_reader :sftp
17
22
 
@@ -30,14 +35,14 @@ module Net; module SFTP; module Operations
30
35
  @pos = 0
31
36
  @real_pos = 0
32
37
  @real_eof = false
33
- @buffer = ""
38
+ @buffer = +""
34
39
  end
35
40
 
36
41
  # Repositions the file pointer to the given offset (relative to the
37
42
  # start of the file). This will also reset the EOF flag.
38
43
  def pos=(offset)
39
44
  @real_pos = @pos = offset
40
- @buffer = ""
45
+ @buffer = +""
41
46
  @real_eof = false
42
47
  end
43
48
 
@@ -63,15 +68,17 @@ module Net; module SFTP; module Operations
63
68
  #
64
69
  # This will advance the file pointer (#pos).
65
70
  def read(n=nil)
71
+ raise ArgumentError, "negative length #{n} given" if n && n < 0
72
+
66
73
  loop do
67
74
  break if n && @buffer.length >= n
68
75
  break unless fill
69
76
  end
70
77
 
71
78
  if n
72
- result, @buffer = @buffer[0,n], (@buffer[n..-1] || "")
79
+ result, @buffer = @buffer[0,n], (@buffer[n..-1] || +"")
73
80
  else
74
- result, @buffer = @buffer, ""
81
+ result, @buffer = @buffer, +""
75
82
  end
76
83
 
77
84
  @pos += result.length
@@ -92,6 +99,7 @@ module Net; module SFTP; module Operations
92
99
  sep_string = sep_or_limit
93
100
  lim = limit
94
101
  end
102
+ lim = Float::INFINITY if lim.nil? || lim < 0
95
103
 
96
104
  delim = if sep_string && sep_string.length == 0
97
105
  "#{$/}#{$/}"
@@ -99,22 +107,26 @@ module Net; module SFTP; module Operations
99
107
  sep_string
100
108
  end
101
109
 
110
+ search_from = 0
102
111
  loop do
103
- at = @buffer.index(delim) if delim
112
+ at = @buffer.index(delim, search_from) if delim
104
113
  if at
105
114
  offset = [at + delim.length, lim].min
106
115
  @pos += offset
107
116
  line, @buffer = @buffer[0,offset], @buffer[offset..-1]
108
117
  return line
109
- elsif lim < @buffer.length
118
+ elsif lim <= @buffer.length
110
119
  @pos += lim
111
120
  line, @buffer = @buffer[0,lim], @buffer[lim..-1]
112
121
  return line
113
- elsif !fill
114
- return nil if @buffer.empty?
115
- @pos += @buffer.length
116
- line, @buffer = @buffer, ""
117
- return line
122
+ else
123
+ search_from = [@buffer.length - delim.length + 1, 0].max if delim
124
+ unless fill
125
+ return nil if @buffer.empty?
126
+ @pos += @buffer.length
127
+ line, @buffer = @buffer, +""
128
+ return line
129
+ end
118
130
  end
119
131
  end
120
132
  end
@@ -128,13 +140,21 @@ module Net; module SFTP; module Operations
128
140
  end
129
141
 
130
142
  # Writes the given data to the stream, incrementing the file position and
131
- # returning the number of bytes written.
143
+ # returning the number of bytes written. Data larger than MAX_WRITE_SIZE
144
+ # is sent as several sequential write requests; if one fails, the
145
+ # position reflects the bytes written before it.
132
146
  def write(data)
133
147
  data = data.to_s
134
- sftp.write!(handle, @real_pos, data)
135
- @real_pos += data.bytes.length
136
- @pos = @real_pos
137
- data.bytes.length
148
+ size = data.bytesize
149
+ written = 0
150
+ loop do
151
+ chunk = data.byteslice(written, MAX_WRITE_SIZE)
152
+ sftp.write!(handle, @pos, chunk)
153
+ written += chunk.bytesize
154
+ self.pos = @pos + chunk.bytesize unless chunk.empty?
155
+ break if written >= size
156
+ end
157
+ size
138
158
  end
139
159
 
140
160
  # Writes each argument to the stream. If +$\+ is set, it will be written
@@ -155,16 +175,12 @@ module Net; module SFTP; module Operations
155
175
  end
156
176
 
157
177
  # Writes each argument to the stream, appending a newline to any item
158
- # that does not already end in a newline. Array arguments are flattened.
178
+ # that does not already end in a newline, like IO#puts. Array arguments
179
+ # are flattened, and with no arguments a single newline is written.
159
180
  def puts(*items)
160
- items.each do |item|
161
- if Array === item
162
- puts(*item)
163
- else
164
- write(item)
165
- write("\n") unless item[-1] == ?\n
166
- end
167
- end
181
+ items = [""] if items.empty?
182
+ recursion = {}.compare_by_identity
183
+ items.each { |item| write_puts_item(item, recursion) }
168
184
  nil
169
185
  end
170
186
 
@@ -178,12 +194,30 @@ module Net; module SFTP; module Operations
178
194
 
179
195
  private
180
196
 
197
+ def write_puts_item(item, recursion)
198
+ array = Array.try_convert(item)
199
+ if array
200
+ if recursion.key?(item)
201
+ item = "[...]"
202
+ else
203
+ recursion[item] = true
204
+ array.each { |child| write_puts_item(child, recursion) }
205
+ recursion.delete(item)
206
+ return
207
+ end
208
+ end
209
+
210
+ item = item.to_s
211
+ write(item) unless item.empty?
212
+ write("\n") unless item[-1] == ?\n
213
+ end
214
+
181
215
  # Fills the buffer. Returns +true+ if it succeeded, and +false+ if
182
216
  # EOF was encountered before any data was read.
183
217
  def fill
184
218
  data = sftp.read!(handle, @real_pos, 8192)
185
219
 
186
- if data.nil?
220
+ if data.nil? || data.empty?
187
221
  @real_eof = true
188
222
  return false
189
223
  else
@@ -1,3 +1,4 @@
1
+ require 'pathname'
1
2
  require 'net/ssh/loggable'
2
3
 
3
4
  module Net; module SFTP; module Operations
@@ -46,13 +47,25 @@ module Net; module SFTP; module Operations
46
47
  # callback. See the discussion of "progress monitoring" below.
47
48
  # * <tt>:requests</tt> - the number of pending SFTP requests to allow at
48
49
  # any given time. When uploading an entire directory tree recursively,
49
- # this will default to 16, otherwise it will default to 2. Setting this
50
- # higher might improve throughput. Reducing it will reduce throughput.
50
+ # this will default to 16, otherwise it will default to 64, like OpenSSH's
51
+ # sftp client. Setting this higher might improve throughput. Reducing it
52
+ # will reduce throughput.
51
53
  # * <tt>:read_size</tt> - the maximum number of bytes to read at a time
52
54
  # from the source. Increasing this value might improve throughput. It
53
- # defaults to 32,000 bytes.
55
+ # defaults to 32,000 bytes. Each chunk is sent as one write request, so
56
+ # it must stay within the server's packet limit (OpenSSH drops the
57
+ # connection on packets over 256 KiB).
54
58
  # * <tt>:name</tt> - the filename to report to the progress monitor when
55
59
  # an IO object is given as +local+. This defaults to "<memory>".
60
+ # * <tt>:preserve</tt> - if true, the remote files and directories get the
61
+ # access and modification times of the local ones, and their permissions
62
+ # are set even on files that already exist remotely.
63
+ #
64
+ # Like OpenSSH's sftp client, new remote files and directories are created
65
+ # with the permissions of the local ones, without setuid, setgid and sticky
66
+ # bits and subject to the server's umask. Nothing is set when +local+ is an
67
+ # IO object. If the server refuses to set the attributes after the data has
68
+ # been written, a warning is logged and the upload still succeeds.
56
69
  #
57
70
  # == Progress Monitoring
58
71
  #
@@ -141,6 +154,9 @@ module Net; module SFTP; module Operations
141
154
  # This will return immediately, and requires that the SSH event loop be
142
155
  # run in order to effect the upload. (See #wait.)
143
156
  def initialize(sftp, local, remote, options={}, &progress) #:nodoc:
157
+ local = local.to_path if local.is_a?(Pathname)
158
+ remote = remote.to_path if remote.is_a?(Pathname)
159
+
144
160
  @sftp = sftp
145
161
  @local = local
146
162
  @remote = remote
@@ -148,11 +164,15 @@ module Net; module SFTP; module Operations
148
164
  @options = options
149
165
  @properties = options[:properties] || {}
150
166
  @active = 0
167
+ @aborted = false
168
+ @directories = []
151
169
 
152
170
  self.logger = sftp.logger
153
171
 
154
172
  @uploads = []
155
173
  @recursive = local.respond_to?(:read) ? false : ::File.directory?(local)
174
+ requests
175
+ read_size
156
176
 
157
177
  if recursive?
158
178
  @stack = [entries_for(local)]
@@ -161,10 +181,10 @@ module Net; module SFTP; module Operations
161
181
 
162
182
  @active += 1
163
183
  if @options[:mkdir]
164
- sftp.mkdir(remote) do |response|
184
+ sftp.mkdir(remote, directory_attributes(local, remote)) do |response|
165
185
  @active -= 1
166
186
  raise StatusException.new(response, "mkdir `#{remote}'") unless response.ok?
167
- (options[:requests] || RECURSIVE_READERS).to_i.times do
187
+ requests.times do
168
188
  break unless process_next_entry
169
189
  end
170
190
  end
@@ -191,11 +211,11 @@ module Net; module SFTP; module Operations
191
211
  @active > 0 || @stack.any?
192
212
  end
193
213
 
194
- # Forces the transfer to stop.
214
+ # Forces the transfer to stop. Writes already sent are left to finish,
215
+ # then each open file is closed, so #wait returns once that is done.
195
216
  def abort!
196
- @active = 0
217
+ @aborted = true
197
218
  @stack.clear
198
- @uploads.clear
199
219
  end
200
220
 
201
221
  # Blocks until the upload has completed.
@@ -225,15 +245,34 @@ module Net; module SFTP; module Operations
225
245
  # The progress handler for this instance. Possibly nil.
226
246
  def progress; @progress; end
227
247
 
248
+ def requests
249
+ @requests ||= positive_option(:requests, recursive? ? RECURSIVE_READERS : SINGLE_FILE_READERS)
250
+ end
251
+
252
+ def read_size
253
+ @read_size ||= positive_option(:read_size, DEFAULT_READ_SIZE)
254
+ end
255
+
256
+ def positive_option(name, default)
257
+ value = (options[name] || default).to_i
258
+ raise ArgumentError, ":#{name} must be positive" unless value > 0
259
+
260
+ value
261
+ end
262
+
263
+ def preserve?
264
+ options[:preserve]
265
+ end
266
+
228
267
  # A simple struct for recording metadata about the file currently being
229
268
  # uploaded.
230
- LiveFile = Struct.new(:local, :remote, :io, :size, :handle)
269
+ LiveFile = Struct.new(:local, :remote, :io, :size, :handle, :attributes, :pending, :error)
231
270
 
232
271
  # The default # of bytes to read from disk at a time.
233
272
  DEFAULT_READ_SIZE = 32_000
234
273
 
235
274
  # The number of readers to use when uploading a single file.
236
- SINGLE_FILE_READERS = 2
275
+ SINGLE_FILE_READERS = 64
237
276
 
238
277
  # The number of readers to use when uploading a directory.
239
278
  RECURSIVE_READERS = 16
@@ -245,7 +284,11 @@ module Net; module SFTP; module Operations
245
284
  if @uploads.any?
246
285
  write_next_chunk(@uploads.first)
247
286
  elsif !active?
248
- update_progress(:finish)
287
+ if @directories.any?
288
+ set_directory_attributes
289
+ else
290
+ update_progress(:finish)
291
+ end
249
292
  end
250
293
  return false
251
294
  elsif @stack.last.empty?
@@ -265,7 +308,7 @@ module Net; module SFTP; module Operations
265
308
 
266
309
  @active += 1
267
310
  update_progress(:mkdir, rpath)
268
- request = sftp.mkdir(rpath, &method(:on_mkdir))
311
+ request = sftp.mkdir(rpath, directory_attributes(lpath, rpath), &method(:on_mkdir))
269
312
  request[:dir] = rpath
270
313
  else
271
314
  open_file(lpath, rpath)
@@ -294,10 +337,11 @@ module Net; module SFTP; module Operations
294
337
  size = file.size
295
338
  end
296
339
 
297
- metafile = LiveFile.new(name, remote, file, size)
340
+ attributes = local.respond_to?(:read) ? {} : attributes_for(file.stat)
341
+ metafile = LiveFile.new(name, remote, file, size, nil, attributes, 0)
298
342
  update_progress(:open, metafile)
299
343
 
300
- request = sftp.open(remote, "w", &method(:on_open))
344
+ request = sftp.open(remote, "w", attributes, &method(:on_open))
301
345
  request[:file] = metafile
302
346
  end
303
347
 
@@ -326,18 +370,29 @@ module Net; module SFTP; module Operations
326
370
  write_next_chunk(file)
327
371
 
328
372
  if !recursive?
329
- (options[:requests] || SINGLE_FILE_READERS).to_i.times { write_next_chunk(file) }
373
+ (requests - 1).times { write_next_chunk(file) }
330
374
  end
331
375
  end
332
376
 
333
- # Called when a +write+ request finishes. Raises StatusException if the
334
- # write failed, otherwise it calls #write_next_chunk to continue the
335
- # write.
377
+ # Called when a +write+ request finishes. The first failed write stops
378
+ # the file from being read further and is recorded, to be raised once
379
+ # the file is closed. The file is closed once the whole file has been
380
+ # sent or a write has failed, and no writes are left outstanding.
336
381
  def on_write(response)
337
382
  @active -= 1
338
383
  file = response.request[:file]
339
- raise StatusException.new(response, "write #{file.remote}") unless response.ok?
340
- write_next_chunk(file)
384
+ file.pending -= 1
385
+
386
+ if !response.ok? && !file.error
387
+ file.error = StatusException.new(response, "write #{file.remote}")
388
+ stop_reading(file)
389
+ end
390
+
391
+ if file.io.nil? && file.pending.zero?
392
+ close_file(file)
393
+ elsif !file.error
394
+ write_next_chunk(file)
395
+ end
341
396
  end
342
397
 
343
398
  # Called when a +close+ request finishes. Raises a StatusException if the
@@ -346,27 +401,72 @@ module Net; module SFTP; module Operations
346
401
  def on_close(response)
347
402
  @active -= 1
348
403
  file = response.request[:file]
404
+ raise file.error if file.error
349
405
  raise StatusException.new(response, "close #{file.remote}") unless response.ok?
350
406
  process_next_entry
351
407
  end
352
408
 
409
+ # setuid, setgid and sticky bits are never sent, matching OpenSSH.
410
+ def attributes_for(stat)
411
+ attributes = { :permissions => stat.mode & 0777 }
412
+ attributes.update(:atime => stat.atime.to_i, :mtime => stat.mtime.to_i) if preserve?
413
+ attributes
414
+ end
415
+
416
+ # The directory is created writable and searchable by its owner so its
417
+ # contents can be uploaded; its real attributes are set once everything
418
+ # else is done (see #set_directory_attributes).
419
+ def directory_attributes(local, remote)
420
+ stat = ::File.stat(local)
421
+ @directories << [remote, attributes_for(stat)]
422
+ { :permissions => (stat.mode & 0777) | 0300 }
423
+ end
424
+
425
+ # Deepest directories first; setting attributes on a directory doesn't
426
+ # change its parent's modification time.
427
+ def set_directory_attributes
428
+ @directories.reverse_each do |path, attributes|
429
+ @active += 1
430
+ sftp.setstat(path, attributes) do |response|
431
+ @active -= 1
432
+ warn_unless_ok(response, "setstat #{path}")
433
+ process_next_entry
434
+ end
435
+ end
436
+ @directories.clear
437
+ end
438
+
439
+ # Called when the +fsetstat+ for :preserve finishes.
440
+ def on_fsetstat(response)
441
+ @active -= 1
442
+ file = response.request[:file]
443
+ warn_unless_ok(response, "fsetstat #{file.remote}")
444
+ end
445
+
446
+ # Like OpenSSH's sftp client, failing to set attributes only warns; the
447
+ # data has already been transferred.
448
+ def warn_unless_ok(response, operation)
449
+ lwarn { "#{operation} failed: #{response}" } unless response.ok?
450
+ end
451
+
353
452
  # Attempts to send the next chunk from the given file (where +file+ is
354
453
  # a LiveFile instance).
355
454
  def write_next_chunk(file)
356
455
  if file.io.nil?
357
456
  process_next_entry
358
457
  else
359
- @active += 1
360
458
  offset = file.io.pos
361
- data = file.io.read(options[:read_size] || DEFAULT_READ_SIZE)
459
+ data = file.io.read(read_size) unless @aborted
362
460
  if data.nil?
363
- update_progress(:close, file)
364
- request = sftp.close(file.handle, &method(:on_close))
365
- request[:file] = file
366
- file.io.close
367
- file.io = nil
368
- @uploads.delete(file)
461
+ stop_reading(file)
462
+ if file.pending.zero?
463
+ close_file(file)
464
+ else
465
+ process_next_entry
466
+ end
369
467
  else
468
+ @active += 1
469
+ file.pending += 1
370
470
  update_progress(:put, file, offset, data)
371
471
  request = sftp.write(file.handle, offset, data, &method(:on_write))
372
472
  request[:file] = file
@@ -374,6 +474,30 @@ module Net; module SFTP; module Operations
374
474
  end
375
475
  end
376
476
 
477
+ def stop_reading(file)
478
+ # An IO passed in by the caller is theirs to close.
479
+ file.io.close unless file.io.equal?(local)
480
+ file.io = nil
481
+ @uploads.delete(file)
482
+ end
483
+
484
+ # Like OpenSSH's sftp client, CLOSE waits for every write to be
485
+ # acknowledged, so a failed write is raised once and the remote handle
486
+ # is not leaked.
487
+ def close_file(file)
488
+ @active += 1
489
+ unless file.error
490
+ update_progress(:close, file)
491
+ if preserve? && file.attributes.any?
492
+ @active += 1
493
+ request = sftp.fsetstat(file.handle, file.attributes, &method(:on_fsetstat))
494
+ request[:file] = file
495
+ end
496
+ end
497
+ request = sftp.close(file.handle, &method(:on_close))
498
+ request[:file] = file
499
+ end
500
+
377
501
  # Returns all directory entries for the given path, removing the '.'
378
502
  # and '..' relative paths.
379
503
  def entries_for(local)
@@ -1,4 +1,5 @@
1
1
  require 'net/ssh/buffer'
2
+ require 'net/sftp/protocol/bounded_read'
2
3
 
3
4
  module Net; module SFTP; module Protocol; module V01
4
5
 
@@ -39,6 +40,8 @@ module Net; module SFTP; module Protocol; module V01
39
40
  T_BLOCK_DEVICE = 8
40
41
  T_FIFO = 9
41
42
 
43
+ extend Net::SFTP::Protocol::BoundedRead
44
+
42
45
  class <<self
43
46
  # Returns the array of attribute meta-data that defines the structure of
44
47
  # the attributes packet as described by this version of the protocol.
@@ -106,7 +109,8 @@ module Net; module SFTP; module Protocol; module V01
106
109
  # Parse the hash of extended data from the buffer.
107
110
  def parse_extended(buffer)
108
111
  extended = Hash.new
109
- buffer.read_long.times do
112
+ # Each pair is two strings, at least 8 bytes.
113
+ read_bounded_count!(buffer, 8, "extended attribute").times do
110
114
  extended[buffer.read_string] = buffer.read_string
111
115
  end
112
116
  extended
@@ -150,7 +154,7 @@ module Net; module SFTP; module Protocol; module V01
150
154
  # * :mtime:: the modification time of the file (integer, seconds since epoch)
151
155
  # * :extended:: a hash of name/value pairs identifying extended info
152
156
  def initialize(attributes={})
153
- @attributes = attributes
157
+ @attributes = attributes.dup
154
158
  end
155
159
 
156
160
  # Returns the user-id of the user that owns the file, or +nil+ if that
@@ -54,7 +54,8 @@ module Net; module SFTP; module Protocol; module V01
54
54
  def parse_name_packet(packet)
55
55
  names = []
56
56
 
57
- packet.read_long.times do
57
+ # Each entry is two strings and the attribute flags, at least 12 bytes.
58
+ read_bounded_count!(packet, 12, "name").times do
58
59
  filename = packet.read_string
59
60
  longname = packet.read_string
60
61
  attrs = attribute_factory.from_buffer(packet)
@@ -5,7 +5,7 @@ module Net; module SFTP; module Protocol; module V01
5
5
  # for use when displaying directory data, and has no specified format.
6
6
  class Name
7
7
  # The name of the item on the remote server.
8
- attr_reader :name
8
+ attr_accessor :name
9
9
 
10
10
  # The display-ready name of the item, possibly with other attributes.
11
11
  attr_reader :longname
@@ -18,6 +18,16 @@ module Net; module SFTP; module Protocol; module V03
18
18
  3
19
19
  end
20
20
 
21
+ # Version 3 adds a message and language tag to status responses.
22
+ def parse_status_packet(packet)
23
+ data = super
24
+ unless packet.eof?
25
+ data[:message] = packet.read_string
26
+ data[:language] = packet.read_string
27
+ end
28
+ data
29
+ end
30
+
21
31
  # Sends a FXP_READLINK packet to the server to request that the target of
22
32
  # the given symlink on the remote host (+path+) be returned.
23
33
  def readlink(path)
@@ -32,4 +42,4 @@ module Net; module SFTP; module Protocol; module V03
32
42
 
33
43
  end
34
44
 
35
- end; end; end; end
45
+ end; end; end; end
@@ -69,7 +69,8 @@ module Net; module SFTP; module Protocol; module V04
69
69
  def parse_acl(buffer)
70
70
  acl_buf = Net::SSH::Buffer.new(buffer.read_string)
71
71
  acl = []
72
- acl_buf.read_long.times do
72
+ # Each entry is three longs and a string, at least 16 bytes.
73
+ read_bounded_count!(acl_buf, 16, "ACL").times do
73
74
  acl << ACL.new(acl_buf.read_long, acl_buf.read_long, acl_buf.read_long, acl_buf.read_string)
74
75
  end
75
76
  acl
@@ -123,7 +124,7 @@ module Net; module SFTP; module Protocol; module V04
123
124
  # to T_REGULAR.
124
125
  def initialize(attributes={})
125
126
  super
126
- attributes[:type] ||= T_REGULAR
127
+ self.type ||= T_REGULAR
127
128
  end
128
129
 
129
130
  private
@@ -134,6 +135,15 @@ module Net; module SFTP; module Protocol; module V04
134
135
  # were given on instantiation
135
136
  owner
136
137
  group
138
+
139
+ # The subsecond flag applies to every timestamp present in the packet.
140
+ subseconds = self.class.elements.select { |_name, _type, flags| flags & F_SUBSECOND_TIMES != 0 }
141
+ if subseconds.any? { |name, _type, _flags| attributes[name] }
142
+ subseconds.each do |name, _type, _flags|
143
+ seconds = name.to_s.sub(/_nseconds\z/, "").to_sym
144
+ attributes[name] ||= 0 if attributes[seconds]
145
+ end
146
+ end
137
147
  end
138
148
 
139
149
  # Performs protocol-version-specific encoding of the access control
@@ -28,7 +28,8 @@ module Net; module SFTP; module Protocol; module V04
28
28
  def parse_name_packet(packet)
29
29
  names = []
30
30
 
31
- packet.read_long.times do
31
+ # Each entry is a string, the attribute flags and the type, at least 9 bytes.
32
+ read_bounded_count!(packet, 9, "name").times do
32
33
  filename = packet.read_string
33
34
  attrs = attribute_factory.from_buffer(packet)
34
35
  names << name_factory.new(filename, attrs)
@@ -9,7 +9,7 @@ module Net; module SFTP; module Protocol; module V04
9
9
  # a directory listing.
10
10
  class Name
11
11
  # The name of the item on the remote server.
12
- attr_reader :name
12
+ attr_accessor :name
13
13
 
14
14
  # Attributes instance describing this item.
15
15
  attr_reader :attributes
@@ -44,24 +44,33 @@ module Net; module SFTP; module Protocol; module V04
44
44
  "l"
45
45
  else
46
46
  "-"
47
- end
47
+ end.dup
48
48
 
49
- longname << (attributes.permissions & 0400 != 0 ? "r" : "-")
50
- longname << (attributes.permissions & 0200 != 0 ? "w" : "-")
51
- longname << (attributes.permissions & 0100 != 0 ? "x" : "-")
52
- longname << (attributes.permissions & 0040 != 0 ? "r" : "-")
53
- longname << (attributes.permissions & 0020 != 0 ? "w" : "-")
54
- longname << (attributes.permissions & 0010 != 0 ? "x" : "-")
55
- longname << (attributes.permissions & 0004 != 0 ? "r" : "-")
56
- longname << (attributes.permissions & 0002 != 0 ? "w" : "-")
57
- longname << (attributes.permissions & 0001 != 0 ? "x" : "-")
49
+ # Fields the server omitted are shown as "?", like GNU ls -l does for entries it cannot stat.
50
+ if (permissions = attributes.permissions)
51
+ longname << (permissions & 0400 != 0 ? "r" : "-")
52
+ longname << (permissions & 0200 != 0 ? "w" : "-")
53
+ longname << (permissions & 0100 != 0 ? "x" : "-")
54
+ longname << (permissions & 0040 != 0 ? "r" : "-")
55
+ longname << (permissions & 0020 != 0 ? "w" : "-")
56
+ longname << (permissions & 0010 != 0 ? "x" : "-")
57
+ longname << (permissions & 0004 != 0 ? "r" : "-")
58
+ longname << (permissions & 0002 != 0 ? "w" : "-")
59
+ longname << (permissions & 0001 != 0 ? "x" : "-")
60
+ else
61
+ longname << "?" * 9
62
+ end
58
63
 
59
- longname << (" %-8s %-8s %8d " % [attributes.owner, attributes.group, attributes.size])
64
+ longname << (" %-8s %-8s %8s " % [attributes.owner || "?", attributes.group || "?", attributes.size || "?"])
60
65
 
61
- longname << Time.at(attributes.mtime).strftime("%b %e %H:%M ")
66
+ longname << if attributes.mtime
67
+ Time.at(attributes.mtime).strftime("%b %e %H:%M ")
68
+ else
69
+ "%12s " % "?"
70
+ end
62
71
  longname << name
63
72
  end
64
73
  end
65
74
  end
66
75
 
67
- end; end; end; end
76
+ end; end; end; end