net-sftp 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
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,120 @@
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
+ $:.unshift "../lib"
18
+ require 'net/sftp'
19
+
20
+ Net::SFTP.start( 'localhost',
21
+ :registry_options => { :logs => { :levels => { "sftp.*" => :debug } } }
22
+ ) do |sftp|
23
+ puts "----------------------------------------------"
24
+ puts "getting handle..."
25
+ handle = sftp.open( "temp/out" )
26
+ puts "got handle: #{handle.inspect}"
27
+ puts "reading..."
28
+ data = sftp.read( handle )
29
+ puts "got data: #{data.length} bytes"
30
+ sftp.close_handle( handle )
31
+
32
+ puts "----------------------------------------------"
33
+ puts "opening handle for writing..."
34
+ handle = sftp.open( "temp/blah", IO::WRONLY | IO::CREAT )
35
+ puts "got handle: #{handle.inspect}"
36
+ data = "1234567890" * 100
37
+ puts "writing #{data.length} bytes..."
38
+ p sftp.write( handle, data ).code
39
+ sftp.close_handle( handle )
40
+
41
+ puts "----------------------------------------------"
42
+ puts "opening handle for writing..."
43
+ handle = sftp.open( "temp/blah", IO::WRONLY | IO::CREAT )
44
+ puts "got handle: #{handle.inspect}"
45
+ data = "1234567890" * 100000
46
+ puts "writing #{data.length} bytes..."
47
+ p sftp.write( handle, data ).code
48
+ sftp.close_handle( handle )
49
+
50
+ puts "----------------------------------------------"
51
+ puts "opening directory handle..."
52
+ handle = sftp.opendir( "/usr/lib" )
53
+ puts "got handle: #{handle.inspect}"
54
+ puts "reading directory..."
55
+ items = sftp.readdir( handle )
56
+ puts "got #{items.length} entries"
57
+ sftp.close_handle( handle )
58
+
59
+ puts "----------------------------------------------"
60
+ puts "removing file..."
61
+ p sftp.remove( "temp/blah" ).code
62
+
63
+ puts "----------------------------------------------"
64
+ puts "getting attributes (stat)..."
65
+ p sftp.stat( "temp/out" )
66
+
67
+ puts "----------------------------------------------"
68
+ puts "getting attributes (lstat)..."
69
+ p sftp.lstat( "temp/out" )
70
+
71
+ puts "----------------------------------------------"
72
+ puts "getting handle..."
73
+ handle = sftp.open( "temp/out" )
74
+ puts "getting attributes (fstat)..."
75
+ p sftp.fstat( handle )
76
+ sftp.close_handle( handle )
77
+
78
+ puts "----------------------------------------------"
79
+ puts "setting attributes (setstat)..."
80
+ p sftp.setstat( "temp/out", :permissions => 0777 )
81
+ puts "getting attributes (stat)..."
82
+ p sftp.stat( "temp/out" )
83
+
84
+ puts "----------------------------------------------"
85
+ puts "getting handle..."
86
+ handle = sftp.open( "temp/out" )
87
+ puts "setting attributes (fsetstat)..."
88
+ p sftp.fsetstat( handle, :permissions => 0660 )
89
+ sftp.close_handle( handle )
90
+ puts "getting attributes (stat)..."
91
+ p sftp.stat( "temp/out" )
92
+
93
+ puts "----------------------------------------------"
94
+ puts "mkdir..."
95
+ p sftp.mkdir( "temp/test_dir", :permissions => 0500 )
96
+ puts "getting attributes (stat)..."
97
+ p sftp.stat( "temp/test_dir" )
98
+
99
+ puts "----------------------------------------------"
100
+ puts "rmdir..."
101
+ p sftp.rmdir( "temp/test_dir" )
102
+
103
+ puts "----------------------------------------------"
104
+ puts "realpath..."
105
+ p sftp.realpath( "." )
106
+
107
+ # 'rename' is only available from protocol version 2+.
108
+ if sftp.support?( :rename )
109
+ puts "----------------------------------------------"
110
+ puts "rename..."
111
+ p sftp.rename( "temp/out", "temp/out2" )
112
+ puts "getting realpath..."
113
+ p sftp.realpath( "temp/out2" )
114
+ puts "restoring name..."
115
+ p sftp.rename( "temp/out2", "temp/out" )
116
+ end
117
+
118
+ puts "----------------------------------------------"
119
+ puts "done!"
120
+ end
data/lib/net/sftp.rb ADDED
@@ -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/ssh'
18
+ require 'net/sftp/session'
19
+
20
+ module Net ; module SFTP
21
+
22
+ # A convenience method for starting a standalone SFTP session. It will
23
+ # start up an SSH session using the given arguments (see the documentation
24
+ # for Net::SSH::Session for details), and will then start a new SFTP session
25
+ # with the SSH session. If a block is given, it will be passed to the SFTP
26
+ # session.
27
+ def start( *args, &block )
28
+ session = Net::SSH.start( *args )
29
+ Net::SFTP::Session.new( session, &block )
30
+ ensure
31
+ session.close if session && block_given?
32
+ end
33
+ module_function :start
34
+
35
+ end ; end
36
+
37
+ Net::SSH.register_service( :sftp ) do |c,p|
38
+ Net::SFTP::Session.new( c[:session] )
39
+ end
@@ -0,0 +1,25 @@
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
18
+
19
+ # The base exception class for the SFTP system.
20
+ class Exception < ::Exception; end
21
+
22
+ # An exception class representing a bug condition.
23
+ class Bug < Exception; end
24
+
25
+ end ; end
@@ -0,0 +1,103 @@
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/operations/errors'
18
+
19
+ module Net ; module SFTP ; module Operations
20
+
21
+ # The base class for all SFTP operations. Subclasses must implement a
22
+ # +perform+ method, which accepts the arguments expected by the corresponding
23
+ # method of the driver. Subclasses _may_ also override the default
24
+ # implementations of +do_status+, +do_data+, +do_name+, +do_handle+, and
25
+ # +do_attrs+, as necessary.
26
+ class Abstract
27
+ include Constants
28
+
29
+ # A structure for reporting status information.
30
+ Status = Struct.new( :code, :message, :language )
31
+
32
+ # A constant for representing the commonly-used FX_OK status.
33
+ OK = Status.new( FX_OK, "Success", "" )
34
+
35
+ # Create a new operation with the given logger instance, which will
36
+ # operate atop the given session, using the given driver to format and
37
+ # send the requests to the server.
38
+ def initialize( log, session, driver )
39
+ @log = log
40
+ @session = session
41
+ @driver = driver
42
+ end
43
+
44
+ # Execute the operation. If a callback is given, the operation will be
45
+ # performed asynchronously with the callback being invoked when the
46
+ # operation completes. If a callback is not given, the operation will
47
+ # be performed synchronously, with the expected value being returned.
48
+ def execute( *args, &callback )
49
+ @log.debug "executing" if @log.debug?
50
+
51
+ unless block_given?
52
+ callback = Proc.new do |status, *pargs|
53
+ @session.status = status
54
+ case
55
+ when pargs.empty? then return @session.status
56
+ when pargs.length == 1 then return pargs.first
57
+ else return pargs
58
+ end
59
+ end
60
+ end
61
+
62
+ @callback = callback
63
+ @id = perform *args
64
+ @log.debug "received request id #{@id}"
65
+ @session.register( @id, self )
66
+
67
+ @session.loop unless block_given?
68
+ end
69
+
70
+ # A callback for SFTP status packets. By default, raises an exception unless
71
+ # the status is FX_OK, in which case the registered callback is invoked.
72
+ def do_status( code, message, language )
73
+ raise StatusException.new( code, message, language ) unless code == FX_OK
74
+ @callback[ Status.new( code, message, language ) ]
75
+ end
76
+
77
+ # A callback for SFTP handle packets. By default, invokes the registered
78
+ # callback, passing an OK status and the handle.
79
+ def do_handle( handle )
80
+ @callback[ OK, handle ]
81
+ end
82
+
83
+ # A callback for SFTP data packets. By default, invokes the registered
84
+ # callback, passing an OK status and the data.
85
+ def do_data( data )
86
+ @callback[ OK, data ]
87
+ end
88
+
89
+ # A callback for SFTP name packets. By default, invokes the registered
90
+ # callback, passing an OK status and the list of items.
91
+ def do_name( items )
92
+ @callback[ OK, items ]
93
+ end
94
+
95
+ # A callback for SFTP attrs packets. By default, invokes the registered
96
+ # callback, passing an OK status and the attributes object.
97
+ def do_attrs( attributes )
98
+ @callback[ OK, attributes ]
99
+ end
100
+
101
+ end
102
+
103
+ end ; end ; end
@@ -0,0 +1,31 @@
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/operations/abstract'
18
+
19
+ module Net ; module SFTP ; module Operations
20
+
21
+ # The implementation of the close operation, for closing an open handle.
22
+ class Close < Abstract
23
+
24
+ # Closes the given handle.
25
+ def perform( handle )
26
+ @driver.close_handle( nil, handle )
27
+ end
28
+
29
+ end
30
+
31
+ end ; end ; end
@@ -0,0 +1,76 @@
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/errors'
18
+
19
+ module Net ; module SFTP ; module Operations
20
+
21
+ module Constants
22
+ FX_OK = 0
23
+ FX_EOF = 1
24
+ FX_NO_SUCH_FILE = 2
25
+ FX_PERMISSION_DENIED = 3
26
+ FX_FAILURE = 4
27
+ FX_BAD_MESSAGE = 5
28
+ FX_NO_CONNECTION = 6
29
+ FX_CONNECTION_LOST = 7
30
+ FX_OP_UNSUPPORTED = 8
31
+ FX_INVALID_HANDLE = 9
32
+ FX_NO_SUCH_PATH = 10
33
+ FX_FILE_ALREADY_EXISTS = 11
34
+ FX_WRITE_PROTECT = 12
35
+ FX_NO_MEDIA = 13
36
+ FX_NO_SPACE_ON_FILESYSTEM = 14
37
+ FX_QUOTA_EXCEEDED = 15
38
+ FX_UNKNOWN_PRINCIPLE = 16
39
+ FX_LOCK_CONFlICT = 17
40
+ FX_DIR_NOT_EMPTY = 18
41
+ FX_NOT_A_DIRECTORY = 19
42
+ FX_INVALID_FILENAME = 20
43
+ FX_LINK_LOOP = 21
44
+ end
45
+
46
+ # A exception class for reporting a non-success result of an operation.
47
+ class StatusException < Net::SFTP::Exception
48
+
49
+ # The error code (numeric)
50
+ attr_reader :code
51
+
52
+ # The description of the error
53
+ attr_reader :description
54
+
55
+ # The language in which the description is being reported (usually the
56
+ # empty string)
57
+ attr_reader :language
58
+
59
+ # Create a new status exception that reports the given code and
60
+ # description.
61
+ def initialize( code, description, language )
62
+ @code = code
63
+ @description = description
64
+ @language = language
65
+ end
66
+
67
+ # Override the default message format, to include the code and
68
+ # description.
69
+ def message
70
+ m = super
71
+ m << " (#{code}, #{description.inspect})"
72
+ end
73
+
74
+ end
75
+
76
+ end ; end ; end
@@ -0,0 +1,36 @@
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/operations/abstract'
18
+
19
+ module Net ; module SFTP ; module Operations
20
+
21
+ # The implementation of the +fsetstat+ operation.
22
+ class Fsetstat < Abstract
23
+
24
+ # Performs the fsetstat operation on the given handle. The +hash+
25
+ # parameter is a Hash of attribute name/value pairs, from which an
26
+ # attributes object will be created and sent to the server. It
27
+ # represents the attributes to set on the file/directory represented
28
+ # by the handle.
29
+ def perform( handle, hash )
30
+ attrs = @driver.attr_factory.from_hash( hash )
31
+ @driver.fsetstat( nil, handle, attrs )
32
+ end
33
+
34
+ end
35
+
36
+ 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/operations/abstract'
18
+
19
+ module Net ; module SFTP ; module Operations
20
+
21
+ # The implementation of the +fstat+ operation.
22
+ class Fstat < Abstract
23
+
24
+ # Ask the server for the attributes of the file/directory represented by
25
+ # the given handle.
26
+ def perform( handle )
27
+ @driver.fstat( nil, handle )
28
+ end
29
+
30
+ end
31
+
32
+ end ; end ; end