net-sftp 0.5.0

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.
Files changed (77) hide show
  1. data/doc/LICENSE-BSD +27 -0
  2. data/doc/LICENSE-GPL +280 -0
  3. data/doc/LICENSE-RUBY +56 -0
  4. data/examples/asynchronous.rb +57 -0
  5. data/examples/ssh-service.rb +31 -0
  6. data/examples/synchronous.rb +120 -0
  7. data/lib/net/sftp.rb +39 -0
  8. data/lib/net/sftp/errors.rb +25 -0
  9. data/lib/net/sftp/operations/abstract.rb +103 -0
  10. data/lib/net/sftp/operations/close.rb +31 -0
  11. data/lib/net/sftp/operations/errors.rb +76 -0
  12. data/lib/net/sftp/operations/fsetstat.rb +36 -0
  13. data/lib/net/sftp/operations/fstat.rb +32 -0
  14. data/lib/net/sftp/operations/lstat.rb +31 -0
  15. data/lib/net/sftp/operations/mkdir.rb +33 -0
  16. data/lib/net/sftp/operations/open.rb +32 -0
  17. data/lib/net/sftp/operations/opendir.rb +32 -0
  18. data/lib/net/sftp/operations/read.rb +84 -0
  19. data/lib/net/sftp/operations/readdir.rb +55 -0
  20. data/lib/net/sftp/operations/realpath.rb +37 -0
  21. data/lib/net/sftp/operations/remove.rb +31 -0
  22. data/lib/net/sftp/operations/rename.rb +32 -0
  23. data/lib/net/sftp/operations/rmdir.rb +31 -0
  24. data/lib/net/sftp/operations/services.rb +42 -0
  25. data/lib/net/sftp/operations/setstat.rb +33 -0
  26. data/lib/net/sftp/operations/stat.rb +31 -0
  27. data/lib/net/sftp/operations/write.rb +63 -0
  28. data/lib/net/sftp/protocol/01/attributes.rb +146 -0
  29. data/lib/net/sftp/protocol/01/impl.rb +251 -0
  30. data/lib/net/sftp/protocol/01/packet-assistant.rb +82 -0
  31. data/lib/net/sftp/protocol/01/services.rb +47 -0
  32. data/lib/net/sftp/protocol/02/impl.rb +39 -0
  33. data/lib/net/sftp/protocol/02/packet-assistant.rb +32 -0
  34. data/lib/net/sftp/protocol/02/services.rb +44 -0
  35. data/lib/net/sftp/protocol/03/impl.rb +42 -0
  36. data/lib/net/sftp/protocol/03/packet-assistant.rb +35 -0
  37. data/lib/net/sftp/protocol/03/services.rb +44 -0
  38. data/lib/net/sftp/protocol/04/attributes.rb +227 -0
  39. data/lib/net/sftp/protocol/04/impl.rb +134 -0
  40. data/lib/net/sftp/protocol/04/packet-assistant.rb +51 -0
  41. data/lib/net/sftp/protocol/04/services.rb +44 -0
  42. data/lib/net/sftp/protocol/05/services.rb +44 -0
  43. data/lib/net/sftp/protocol/constants.rb +60 -0
  44. data/lib/net/sftp/protocol/driver.rb +232 -0
  45. data/lib/net/sftp/protocol/packet-assistant.rb +84 -0
  46. data/lib/net/sftp/protocol/services.rb +55 -0
  47. data/lib/net/sftp/session.rb +215 -0
  48. data/lib/net/sftp/version.rb +25 -0
  49. data/test/ALL-TESTS.rb +21 -0
  50. data/test/operations/tc_abstract.rb +124 -0
  51. data/test/operations/tc_close.rb +40 -0
  52. data/test/operations/tc_fsetstat.rb +48 -0
  53. data/test/operations/tc_fstat.rb +40 -0
  54. data/test/operations/tc_lstat.rb +40 -0
  55. data/test/operations/tc_mkdir.rb +48 -0
  56. data/test/operations/tc_open.rb +42 -0
  57. data/test/operations/tc_opendir.rb +40 -0
  58. data/test/operations/tc_read.rb +103 -0
  59. data/test/operations/tc_readdir.rb +88 -0
  60. data/test/operations/tc_realpath.rb +54 -0
  61. data/test/operations/tc_remove.rb +40 -0
  62. data/test/operations/tc_rmdir.rb +40 -0
  63. data/test/operations/tc_setstat.rb +48 -0
  64. data/test/operations/tc_stat.rb +40 -0
  65. data/test/operations/tc_write.rb +91 -0
  66. data/test/protocol/01/tc_attributes.rb +138 -0
  67. data/test/protocol/01/tc_impl.rb +294 -0
  68. data/test/protocol/01/tc_packet_assistant.rb +81 -0
  69. data/test/protocol/02/tc_impl.rb +41 -0
  70. data/test/protocol/02/tc_packet_assistant.rb +31 -0
  71. data/test/protocol/03/tc_impl.rb +48 -0
  72. data/test/protocol/03/tc_packet_assistant.rb +34 -0
  73. data/test/protocol/04/tc_attributes.rb +174 -0
  74. data/test/protocol/04/tc_impl.rb +102 -0
  75. data/test/protocol/04/tc_packet_assistant.rb +41 -0
  76. data/test/protocol/tc_driver.rb +219 -0
  77. metadata +137 -0
@@ -0,0 +1,82 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # This source file is distributed as part of the Net::SFTP Secure FTP Client
7
+ # library for Ruby. This file (and the library as a whole) may be used only as
8
+ # allowed by either the BSD license, or the Ruby license (or, by association
9
+ # with the Ruby license, the GPL). See the "doc" subdirectory of the Net::SFTP
10
+ # distribution for the texts of these licenses.
11
+ # -----------------------------------------------------------------------------
12
+ # net-sftp website: http://net-ssh.rubyforge.org/sftp
13
+ # project website : http://rubyforge.org/projects/net-ssh
14
+ # =============================================================================
15
+ #++
16
+
17
+ require 'net/sftp/protocol/packet-assistant'
18
+
19
+ module Net ; module SFTP ; module Protocol ; module V_01
20
+
21
+ # Defines the packets available to version 1 of the SFTP protocol, as well
22
+ # as their formats. These packets are:
23
+ #
24
+ # * open( id, path, flags, attrs )
25
+ # * close( id, handle )
26
+ # * read( id, handle, offset, length )
27
+ # * write( id, handle, offset, data )
28
+ # * opendir( id, path )
29
+ # * readdir( id, handle )
30
+ # * remove( id, filename )
31
+ # * stat( id, path )
32
+ # * lstat( id, path )
33
+ # * fstat( id, handle )
34
+ # * setstat( id, path, attrs )
35
+ # * fsetstat( id, handle, attrs )
36
+ # * mkdir( id, path, attrs )
37
+ # * rmdir( id, path )
38
+ # * realpath( id, path )
39
+ class PacketAssistant < Protocol::PacketAssistant
40
+
41
+ packet :open, :string, # path
42
+ :long, # flags
43
+ :attrs # file attributes
44
+
45
+ packet :close, :string # handle
46
+
47
+ packet :read, :string, # handle
48
+ :int64, # offset
49
+ :long # length
50
+
51
+ packet :write, :string, # handle
52
+ :int64, # offset
53
+ :string # data
54
+
55
+ packet :opendir, :string # path
56
+
57
+ packet :readdir, :string # handle
58
+
59
+ packet :remove, :string # filename
60
+
61
+ packet :stat, :string # path
62
+
63
+ packet :lstat, :string # path
64
+
65
+ packet :fstat, :string # handle
66
+
67
+ packet :setstat, :string, # path
68
+ :attrs # attributes to set
69
+
70
+ packet :fsetstat, :string, # handle
71
+ :attrs # attributes to set
72
+
73
+ packet :mkdir, :string, # path
74
+ :attrs # directory attributes
75
+
76
+ packet :rmdir, :string # path
77
+
78
+ packet :realpath, :string # path
79
+
80
+ end
81
+
82
+ end ; end ; end ; end
@@ -0,0 +1,47 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # This source file is distributed as part of the Net::SFTP Secure FTP Client
7
+ # library for Ruby. This file (and the library as a whole) may be used only as
8
+ # allowed by either the BSD license, or the Ruby license (or, by association
9
+ # with the Ruby license, the GPL). See the "doc" subdirectory of the Net::SFTP
10
+ # distribution for the texts of these licenses.
11
+ # -----------------------------------------------------------------------------
12
+ # net-sftp website: http://net-ssh.rubyforge.org/sftp
13
+ # project website : http://rubyforge.org/projects/net-ssh
14
+ # =============================================================================
15
+ #++
16
+
17
+ module Net ; module SFTP ; module Protocol ; module V_01
18
+
19
+ def register_services( container )
20
+ container.namespace_define :v_01 do |ns|
21
+
22
+ # The packet assistant to use for formatting SFTP packets.
23
+ ns.packet_assistant do |c,|
24
+ require 'net/sftp/protocol/01/packet-assistant'
25
+ PacketAssistant.new( c[:transport][:buffers],
26
+ c[:driver] )
27
+ end
28
+
29
+ # The attribute factory to use to obtain attribute object instances.
30
+ ns.attr_factory do |c,|
31
+ require 'net/sftp/protocol/01/attributes'
32
+ Attributes.init( c[:transport][:buffers] )
33
+ end
34
+
35
+ # The version implementation to use.
36
+ ns.impl do |c,|
37
+ require 'net/sftp/protocol/01/impl'
38
+ Impl.new( c[:transport][:buffers],
39
+ c[:driver], c[:packet_assistant],
40
+ c[:attr_factory] )
41
+ end
42
+
43
+ end
44
+ end
45
+ module_function :register_services
46
+
47
+ end ; end ; end ; end
@@ -0,0 +1,39 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # This source file is distributed as part of the Net::SFTP Secure FTP Client
7
+ # library for Ruby. This file (and the library as a whole) may be used only as
8
+ # allowed by either the BSD license, or the Ruby license (or, by association
9
+ # with the Ruby license, the GPL). See the "doc" subdirectory of the Net::SFTP
10
+ # distribution for the texts of these licenses.
11
+ # -----------------------------------------------------------------------------
12
+ # net-sftp website: http://net-ssh.rubyforge.org/sftp
13
+ # project website : http://rubyforge.org/projects/net-ssh
14
+ # =============================================================================
15
+ #++
16
+
17
+ require 'net/sftp/protocol/01/impl'
18
+
19
+ module Net ; module SFTP ; module Protocol ; module V_02
20
+
21
+ # The implementation of the operations defined by version 2 of the SFTP
22
+ # protocol. This simply adds support for the RENAME operation, otherwise
23
+ # extending the version 1 implementation, unaltered.
24
+ class Impl < V_01::Impl
25
+
26
+ operation :rename
27
+
28
+ alias :rename_raw :rename
29
+
30
+ # This operation is special since a later version of the protocol adds
31
+ # support for flags. In this version, the flags parameter is ignored,
32
+ # but it exists for compatibility with later versions.
33
+ def rename( id, name, new_name, flags=nil )
34
+ rename_raw id, name, new_name
35
+ end
36
+
37
+ end
38
+
39
+ end ; end ; end ; end
@@ -0,0 +1,32 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # This source file is distributed as part of the Net::SFTP Secure FTP Client
7
+ # library for Ruby. This file (and the library as a whole) may be used only as
8
+ # allowed by either the BSD license, or the Ruby license (or, by association
9
+ # with the Ruby license, the GPL). See the "doc" subdirectory of the Net::SFTP
10
+ # distribution for the texts of these licenses.
11
+ # -----------------------------------------------------------------------------
12
+ # net-sftp website: http://net-ssh.rubyforge.org/sftp
13
+ # project website : http://rubyforge.org/projects/net-ssh
14
+ # =============================================================================
15
+ #++
16
+
17
+ require 'net/sftp/protocol/01/packet-assistant'
18
+
19
+ module Net ; module SFTP ; module Protocol ; module V_02
20
+
21
+ # The packet assistant, used to format packets for version 2 of the SFTP
22
+ # protocol. This simply adds support for the RENAME packet:
23
+ #
24
+ # * rename( id, old, new )
25
+ class PacketAssistant < V_01::PacketAssistant
26
+
27
+ packet :rename, :string, # old name
28
+ :string # new name
29
+
30
+ end
31
+
32
+ end ; end ; end ; end
@@ -0,0 +1,44 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # This source file is distributed as part of the Net::SFTP Secure FTP Client
7
+ # library for Ruby. This file (and the library as a whole) may be used only as
8
+ # allowed by either the BSD license, or the Ruby license (or, by association
9
+ # with the Ruby license, the GPL). See the "doc" subdirectory of the Net::SFTP
10
+ # distribution for the texts of these licenses.
11
+ # -----------------------------------------------------------------------------
12
+ # net-sftp website: http://net-ssh.rubyforge.org/sftp
13
+ # project website : http://rubyforge.org/projects/net-ssh
14
+ # =============================================================================
15
+ #++
16
+
17
+ module Net ; module SFTP ; module Protocol ; module V_02
18
+
19
+ def register_services( container )
20
+ container.namespace_define :v_02 do |ns|
21
+
22
+ ns.packet_assistant do |c,|
23
+ require 'net/sftp/protocol/02/packet-assistant'
24
+ PacketAssistant.new( c[:transport][:buffers],
25
+ c[:driver] )
26
+ end
27
+
28
+ ns.attr_factory do |c,|
29
+ require 'net/sftp/protocol/01/attributes'
30
+ V_01::Attributes.init( c[:transport][:buffers] )
31
+ end
32
+
33
+ ns.impl do |c,|
34
+ require 'net/sftp/protocol/02/impl'
35
+ Impl.new( c[:transport][:buffers],
36
+ c[:driver], c[:packet_assistant],
37
+ c[:attr_factory] )
38
+ end
39
+
40
+ end
41
+ end
42
+ module_function :register_services
43
+
44
+ end ; end ; end ; end
@@ -0,0 +1,42 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # This source file is distributed as part of the Net::SFTP Secure FTP Client
7
+ # library for Ruby. This file (and the library as a whole) may be used only as
8
+ # allowed by either the BSD license, or the Ruby license (or, by association
9
+ # with the Ruby license, the GPL). See the "doc" subdirectory of the Net::SFTP
10
+ # distribution for the texts of these licenses.
11
+ # -----------------------------------------------------------------------------
12
+ # net-sftp website: http://net-ssh.rubyforge.org/sftp
13
+ # project website : http://rubyforge.org/projects/net-ssh
14
+ # =============================================================================
15
+ #++
16
+
17
+ require 'net/sftp/protocol/02/impl'
18
+
19
+ module Net ; module SFTP ; module Protocol ; module V_03
20
+
21
+ # The implementation of the operations for version 3 of the SFTP protocol.
22
+ # This adds support for the readlink and symlink operations.
23
+ class Impl < V_02::Impl
24
+
25
+ operation :readlink,
26
+ :symlink
27
+
28
+ # The #status message changed in this version. It adds +message+ and
29
+ # +language+ fields, so the callback now receives +driver+, +id+, +code+,
30
+ # +message+, and +language+ as parameters.
31
+ def do_status( channel, content )
32
+ return unless has_on_status?
33
+ id = content.read_long
34
+ code = content.read_long
35
+ message = content.read_string
36
+ language = content.read_string
37
+ call_on_status( driver, id, code, message, language )
38
+ end
39
+
40
+ end
41
+
42
+ end ; end ; end ; end
@@ -0,0 +1,35 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # This source file is distributed as part of the Net::SFTP Secure FTP Client
7
+ # library for Ruby. This file (and the library as a whole) may be used only as
8
+ # allowed by either the BSD license, or the Ruby license (or, by association
9
+ # with the Ruby license, the GPL). See the "doc" subdirectory of the Net::SFTP
10
+ # distribution for the texts of these licenses.
11
+ # -----------------------------------------------------------------------------
12
+ # net-sftp website: http://net-ssh.rubyforge.org/sftp
13
+ # project website : http://rubyforge.org/projects/net-ssh
14
+ # =============================================================================
15
+ #++
16
+
17
+ require 'net/sftp/protocol/02/packet-assistant'
18
+
19
+ module Net ; module SFTP ; module Protocol ; module V_03
20
+
21
+ # The packet assistant which formats packets for version 3 of the SFTP
22
+ # protocol. This adds support for the readlink and symlink packets:
23
+ #
24
+ # * readlink( id, path )
25
+ # * symlink( id, link, target )
26
+ class PacketAssistant < V_02::PacketAssistant
27
+
28
+ packet :readlink, :string # path
29
+
30
+ packet :symlink, :string, # link-path
31
+ :string # target-path
32
+
33
+ end
34
+
35
+ end ; end ; end ; end
@@ -0,0 +1,44 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # This source file is distributed as part of the Net::SFTP Secure FTP Client
7
+ # library for Ruby. This file (and the library as a whole) may be used only as
8
+ # allowed by either the BSD license, or the Ruby license (or, by association
9
+ # with the Ruby license, the GPL). See the "doc" subdirectory of the Net::SFTP
10
+ # distribution for the texts of these licenses.
11
+ # -----------------------------------------------------------------------------
12
+ # net-sftp website: http://net-ssh.rubyforge.org/sftp
13
+ # project website : http://rubyforge.org/projects/net-ssh
14
+ # =============================================================================
15
+ #++
16
+
17
+ module Net ; module SFTP ; module Protocol ; module V_03
18
+
19
+ def register_services( container )
20
+ container.namespace_define :v_03 do |ns|
21
+
22
+ ns.packet_assistant do |c,|
23
+ require 'net/sftp/protocol/03/packet-assistant'
24
+ PacketAssistant.new( c[:transport][:buffers],
25
+ c[:driver] )
26
+ end
27
+
28
+ ns.attr_factory do |c,|
29
+ require 'net/sftp/protocol/01/attributes'
30
+ V_01::Attributes.init( c[:transport][:buffers] )
31
+ end
32
+
33
+ ns.impl do |c,|
34
+ require 'net/sftp/protocol/03/impl'
35
+ Impl.new( c[:transport][:buffers],
36
+ c[:driver], c[:packet_assistant],
37
+ c[:attr_factory] )
38
+ end
39
+
40
+ end
41
+ end
42
+ module_function :register_services
43
+
44
+ end ; end ; end ; end
@@ -0,0 +1,227 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # This source file is distributed as part of the Net::SFTP Secure FTP Client
7
+ # library for Ruby. This file (and the library as a whole) may be used only as
8
+ # allowed by either the BSD license, or the Ruby license (or, by association
9
+ # with the Ruby license, the GPL). See the "doc" subdirectory of the Net::SFTP
10
+ # distribution for the texts of these licenses.
11
+ # -----------------------------------------------------------------------------
12
+ # net-sftp website: http://net-ssh.rubyforge.org/sftp
13
+ # project website : http://rubyforge.org/projects/net-ssh
14
+ # =============================================================================
15
+ #++
16
+
17
+ module Net ; module SFTP ; module Protocol ; module V_04
18
+
19
+ # Version 4 of the SFTP protocol made some pretty significant alterations to
20
+ # the File Attributes data type. This encapsulates those changes.
21
+ class Attributes
22
+
23
+ F_SIZE = 0x00000001
24
+ F_PERMISSIONS = 0x00000004
25
+ F_ACCESSTIME = 0x00000008
26
+ F_CREATETIME = 0x00000010
27
+ F_MODIFYTIME = 0x00000020
28
+ F_ACL = 0x00000040
29
+ F_OWNERGROUP = 0x00000080
30
+ F_SUBSECOND_TIMES = 0x00000100
31
+ F_EXTENDED = 0x80000000
32
+
33
+ attr_accessor :type
34
+ attr_accessor :size
35
+ attr_accessor :owner
36
+ attr_accessor :group
37
+ attr_accessor :permissions
38
+ attr_accessor :atime
39
+ attr_accessor :atime_nseconds
40
+ attr_accessor :ctime
41
+ attr_accessor :ctime_nseconds
42
+ attr_accessor :mtime
43
+ attr_accessor :mtime_nseconds
44
+ attr_accessor :acl
45
+ attr_accessor :extended
46
+
47
+ ACL = Struct.new( :type, :flag, :mask, :who )
48
+
49
+ # An initializer for specifying the buffer factory that should be used by
50
+ # instances of this class.
51
+ def self.init( buffers )
52
+ @buffers = buffers
53
+ self
54
+ end
55
+
56
+ # Return a reference to the buffer factory in use by this class.
57
+ def self.buffers
58
+ @buffers
59
+ end
60
+
61
+ # A convenience for obtaining a reference to the buffer factory used by
62
+ # this instance's class.
63
+ def buffers
64
+ self.class.buffers
65
+ end
66
+
67
+ # Return an empty Attributes instance.
68
+ def self.empty
69
+ new
70
+ end
71
+
72
+ # Return an Attributes instance initialized from the given buffer.
73
+ def self.from_buffer( buffer )
74
+ flags = buffer.read_long
75
+
76
+ type = buffer.read_byte
77
+ size = buffer.read_int64 if ( flags & F_SIZE ) != 0
78
+ owner = buffer.read_string if ( flags & F_OWNERGROUP ) != 0
79
+ group = buffer.read_string if ( flags & F_OWNERGROUP ) != 0
80
+ permissions = buffer.read_long if ( flags & F_PERMISSIONS ) != 0
81
+ if ( flags & F_ACCESSTIME ) != 0
82
+ atime = buffer.read_int64
83
+ atime_nseconds = buffer.read_long if ( flags & F_SUBSECOND_TIMES ) != 0
84
+ end
85
+ if ( flags & F_CREATETIME ) != 0
86
+ ctime = buffer.read_int64
87
+ ctime_nseconds = buffer.read_long if ( flags & F_SUBSECOND_TIMES ) != 0
88
+ end
89
+ if ( flags & F_MODIFYTIME ) != 0
90
+ mtime = buffer.read_int64
91
+ mtime_nseconds = buffer.read_long if ( flags & F_SUBSECOND_TIMES ) != 0
92
+ end
93
+ if ( flags & F_ACL ) != 0
94
+ acl_buf = buffers.reader( buffer.read_string )
95
+ acl = []
96
+ acl_buf.read_long.times do
97
+ acl << ACL.new( acl_buf.read_long,
98
+ acl_buf.read_long,
99
+ acl_buf.read_long,
100
+ acl_buf.read_string )
101
+ end
102
+ end
103
+
104
+ if ( flags & F_EXTENDED ) != 0
105
+ extended = Hash.new
106
+ buffer.read_long.times do
107
+ extended[ buffer.read_string ] = buffer.read_string
108
+ end
109
+ end
110
+
111
+ new( type, size, owner, group, permissions, atime, atime_nseconds,
112
+ ctime, ctime_nseconds, mtime, mtime_nseconds, acl, extended )
113
+ end
114
+
115
+ # Create a new attributes object, initialized from the given hash. The
116
+ # :uid and :gid attributes are treated specially; they are not actually
117
+ # supported by this version of the protocol, but are instead converted
118
+ # by this method to their corresponding names, and assigned (respectively)
119
+ # to :owner and :group.
120
+ def self.from_hash( hash )
121
+ if hash.has_key?(:uid)
122
+ require 'etc'
123
+ hash[:owner] = Etc.getpwuid( hash[:uid] ).name
124
+ end
125
+
126
+ if hash.has_key?(:gid)
127
+ require 'etc'
128
+ hash[:group] = Etc.getgrgid( hash[:gid] ).name
129
+ end
130
+
131
+ new hash[:type] || T_REGULAR, hash[:size], hash[:owner], hash[:group],
132
+ hash[:permissions], hash[:atime], hash[:atime_nseconds],
133
+ hash[:ctime], hash[:ctime_nseconds], hash[:mtime],
134
+ hash[:mtime_nseconds], hash[:acl], hash[:extended]
135
+ end
136
+
137
+ private_class_method :new
138
+
139
+ T_REGULAR = 1
140
+ T_DIRECTORY = 2
141
+ T_SYMLINK = 3
142
+ T_SPECIAL = 4
143
+ T_UNKNOWN = 5
144
+ T_SOCKET = 6
145
+ T_CHAR_DEVICE = 7
146
+ T_BLOCK_DEVICE = 8
147
+ T_FIFO = 9
148
+
149
+ # Create a new Attributes instance with the given values.
150
+ def initialize( type=T_REGULAR, size=nil, owner=nil, group=nil,
151
+ permissions=nil, atime=nil, atime_nseconds=nil, ctime=nil,
152
+ ctime_nseconds=nil, mtime=nil, mtime_nseconds=nil, acl=nil,
153
+ extended=nil )
154
+ # begin
155
+ @type = type
156
+ @size = size
157
+ @owner = owner
158
+ @group = group
159
+ @permissions = permissions
160
+ @atime = atime
161
+ @atime_nseconds = atime_nseconds
162
+ @ctime = ctime
163
+ @ctime_nseconds = ctime_nseconds
164
+ @mtime = mtime
165
+ @mtime_nseconds = mtime_nseconds
166
+ @acl = acl
167
+ @extended = extended
168
+ end
169
+
170
+ # Convert this object to a string, suitable for inclusion in an SFTP
171
+ # packet (protocol version 4+).
172
+ def to_s
173
+ flags = 0
174
+
175
+ flags |= F_SIZE if @size
176
+ flags |= F_OWNERGROUP if @owner && @group
177
+ flags |= F_PERMISSIONS if @permissions
178
+ flags |= F_ACCESSTIME if @atime
179
+ flags |= F_CREATETIME if @ctime
180
+ flags |= F_MODIFYTIME if @mtime
181
+ if @atime_nseconds && @ctime_nseconds && @mtime_nseconds
182
+ flags |= F_SUBSECOND_TIMES
183
+ end
184
+ flags |= F_ACL if @acl
185
+ flags |= F_EXTENDED if @extended
186
+
187
+ buffer = buffers.writer
188
+ buffer.write_long flags
189
+ buffer.write_byte @type
190
+ buffer.write_int64 @size if @size
191
+ buffer.write_string @owner, @group if @owner && @group
192
+ buffer.write_long @permissions if @permissions
193
+
194
+ if @atime
195
+ buffer.write_int64 @atime
196
+ buffer.write_long @atime_nseconds if ( flags & F_SUBSECOND_TIMES != 0 )
197
+ end
198
+ if @ctime
199
+ buffer.write_int64 @ctime
200
+ buffer.write_long @ctime_nseconds if ( flags & F_SUBSECOND_TIMES != 0 )
201
+ end
202
+ if @mtime
203
+ buffer.write_int64 @mtime
204
+ buffer.write_long @mtime_nseconds if ( flags & F_SUBSECOND_TIMES != 0 )
205
+ end
206
+
207
+ if @acl
208
+ acl_buf = buffers.writer
209
+ acl_buf.write_long @acl.length
210
+ @acl.each do |item|
211
+ acl_buf.write_long item.type, item.flag, item.mask
212
+ acl_buf.write_string item.who
213
+ end
214
+ buffer.write_string acl_buf.to_s
215
+ end
216
+
217
+ if @extended
218
+ buffer.write_long @extended.length
219
+ @extended.each { |k,v| buffer.write_string k, v }
220
+ end
221
+
222
+ buffer.to_s
223
+ end
224
+
225
+ end
226
+
227
+ end ; end ; end ; end