diff --git a/lib/net/sftp/protocol/04/name.rb b/lib/net/sftp/protocol/04/name.rb index 3e6044d..d27d669 100644 --- a/lib/net/sftp/protocol/04/name.rb +++ b/lib/net/sftp/protocol/04/name.rb @@ -38,6 +38,7 @@ def file? # used by the unix "ls" utility. def longname @longname ||= begin + permissions = attributes.permissions || 0 longname = if directory? "d" elsif symlink? @@ -46,22 +47,26 @@ def longname "-" end - longname << (attributes.permissions & 0400 != 0 ? "r" : "-") - longname << (attributes.permissions & 0200 != 0 ? "w" : "-") - longname << (attributes.permissions & 0100 != 0 ? "x" : "-") - longname << (attributes.permissions & 0040 != 0 ? "r" : "-") - longname << (attributes.permissions & 0020 != 0 ? "w" : "-") - longname << (attributes.permissions & 0010 != 0 ? "x" : "-") - longname << (attributes.permissions & 0004 != 0 ? "r" : "-") - longname << (attributes.permissions & 0002 != 0 ? "w" : "-") - longname << (attributes.permissions & 0001 != 0 ? "x" : "-") + longname << (permissions & 0400 != 0 ? "r" : "-") + longname << (permissions & 0200 != 0 ? "w" : "-") + longname << (permissions & 0100 != 0 ? "x" : "-") + longname << (permissions & 0040 != 0 ? "r" : "-") + longname << (permissions & 0020 != 0 ? "w" : "-") + longname << (permissions & 0010 != 0 ? "x" : "-") + longname << (permissions & 0004 != 0 ? "r" : "-") + longname << (permissions & 0002 != 0 ? "w" : "-") + longname << (permissions & 0001 != 0 ? "x" : "-") - longname << (" %-8s %-8s %8d " % [attributes.owner, attributes.group, attributes.size]) + longname << (" %-8s %-8s %8s " % [attributes.owner, attributes.group, attributes.size || "-"]) - longname << Time.at(attributes.mtime).strftime("%b %e %H:%M ") + longname << if attributes.mtime + Time.at(attributes.mtime).strftime("%b %e %H:%M ") + else + " " * 13 + end longname << name end end end -end; end; end; end \ No newline at end of file +end; end; end; end diff --git a/test/protocol/04/test_name.rb b/test/protocol/04/test_name.rb index c4b1d34..4f9c980 100644 --- a/test/protocol/04/test_name.rb +++ b/test/protocol/04/test_name.rb @@ -50,4 +50,13 @@ def test_longname_for_file_should_format_as_file assert_equal "-rwxr-xr-x jamis users 10240 Mar 12 03:40 test", @file.longname end + + def test_longname_should_tolerate_omitted_optional_attributes + sparse = Net::SFTP::Protocol::V04::Name.new( + "test", + Net::SFTP::Protocol::V04::Attributes.new(:type => 1) + ) + + assert_equal "---------- - test", sparse.longname + end end