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,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 +lstat+ operation.
22
+ class Lstat < Abstract
23
+
24
+ # Request attributes for the file/directory at the given path.
25
+ def perform( path )
26
+ @driver.lstat( nil, path )
27
+ end
28
+
29
+ end
30
+
31
+ end ; end ; end
@@ -0,0 +1,33 @@
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 +mkdir+ operation.
22
+ class Mkdir < Abstract
23
+
24
+ # Attempt to create the directory at the given path, giving it the
25
+ # attributes specified by the hash.
26
+ def perform( path, hash )
27
+ attrs = @driver.attr_factory.from_hash( hash )
28
+ @driver.mkdir( nil, path, attrs )
29
+ end
30
+
31
+ end
32
+
33
+ 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
+ # Implements the +open+ operation, for obtaining a handle to an open file
22
+ # on the server.
23
+ class Open < Abstract
24
+
25
+ # Performs the operation.
26
+ def perform( path, flags=IO::RDONLY, mode=0660 )
27
+ @driver.open( nil, path, flags, mode )
28
+ end
29
+
30
+ end
31
+
32
+ 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
+ # Implements the +opendir+ operation, for obtaining a handle to an open
22
+ # directory on the server.
23
+ class Opendir < Abstract
24
+
25
+ # Performs the operation.
26
+ def perform( path )
27
+ @driver.opendir( nil, path )
28
+ end
29
+
30
+ end
31
+
32
+ end ; end ; end
@@ -0,0 +1,84 @@
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
+ # Implements the +read+ operation, which reads data from an open file handle.
22
+ # This also implements the common case of reading to the end of the file. In
23
+ # that case, the callback is guaranteed to receive the contents of the entire
24
+ # file in one chunk.
25
+ class Read < Abstract
26
+
27
+ # The maximum amount of data to read at once when reading an entire file.
28
+ # (Setting this to a larger value may cause problems, especially with
29
+ # very large files, so beware!)
30
+ CHUNK_SIZE = 64 * 1024
31
+
32
+ # Perform the operation. If length is less than 0 (the default), then the
33
+ # entire file (from the given offset) will be read and returned in "one
34
+ # fell swoop". Otherwise, the given length of data will be requested.
35
+ def perform( handle, offset=0, length=-1 )
36
+ @length = length
37
+ @handle = handle
38
+ @offset = offset
39
+ @data = ""
40
+
41
+ real_length = ( length < 0 ? CHUNK_SIZE : length )
42
+ @driver.read( nil, handle, offset, real_length )
43
+ end
44
+
45
+ # Invoked when a data packet is received from the server. If the original
46
+ # request was for an entire file, this will send another read request,
47
+ # offset to the end of the data that has been read so far. Otherwise, the
48
+ # callback will be invoked directly.
49
+ def do_data( data )
50
+ @log.debug "[#{@id}] got #{data.length} bytes" if @log.debug?
51
+
52
+ @data << data
53
+ if @length < 0 || @data.length < @length
54
+ if @length < 0
55
+ length = CHUNK_SIZE
56
+ else
57
+ length = @length - @data.length
58
+ length = length > CHUNK_SIZE ? CHUNK_SIZE : length
59
+ end
60
+
61
+ @log.debug "[#{@id}] requesting #{length} more bytes" if @log.debug?
62
+ @driver.read @id, @handle, @offset + @data.length, length
63
+ @session.register( @id, self )
64
+ else
65
+ @callback[ OK, @data ]
66
+ end
67
+ end
68
+
69
+ # Invoked when a status code is received from the server. If the code is
70
+ # FX_EOF, then no data could be read because the end of the file was
71
+ # reached. In this case, the callback is invoked with the data that has been
72
+ # read so far. Other status codes are handled by the superclass.
73
+ def do_status( code, message, language )
74
+ if code == FX_EOF
75
+ @log.debug "[#{@id}] got EOF" if @log.debug?
76
+ @callback[ OK, @data ]
77
+ else
78
+ super
79
+ end
80
+ end
81
+
82
+ end
83
+
84
+ end ; end ; end
@@ -0,0 +1,55 @@
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
+ # Implements the +readdir+ operation. This will handle the continuous calls
22
+ # to +readdir+ until the entire contents of the directory have been read,
23
+ # returning them in a single array.
24
+ class Readdir < Abstract
25
+
26
+ # Performs the operation.
27
+ def perform( handle )
28
+ @items = []
29
+ @handle = handle
30
+ @driver.readdir nil, @handle
31
+ end
32
+
33
+ # Invoked when the server returns a list of "names". Requests +readdir+
34
+ # again, automatically.
35
+ def do_name( items )
36
+ @log.debug "[#{@id}] got #{items.length} items" if @log.debug?
37
+ @items.concat items
38
+ @driver.readdir @id, @handle
39
+ @session.register @id, self
40
+ end
41
+
42
+ # Invoked when a status code is received from the server. If the code is
43
+ # FX_OK or FX_EOF then there is nothing left to read and the callback is
44
+ # invoked. Other status codes are handled by the superclass.
45
+ def do_status( code, message, language )
46
+ if code == FX_OK || code == FX_EOF
47
+ @callback[ OK, @items ]
48
+ else
49
+ super
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ end ; end ; end
@@ -0,0 +1,37 @@
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
+ # Implements the +realpath+ operation.
22
+ class Realpath < Abstract
23
+
24
+ # Perform the operation.
25
+ def perform( path )
26
+ @driver.realpath( nil, path )
27
+ end
28
+
29
+ # A convenience--returns the first (and only) element of the array of items
30
+ # that is returned, instead of returning the array itself.
31
+ def do_name( items )
32
+ @callback[ OK, items.first ]
33
+ end
34
+
35
+ end
36
+
37
+ 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
+ # Implements the +remove+ operation.
22
+ class Remove < Abstract
23
+
24
+ # Perform the operation.
25
+ def perform( filename )
26
+ @driver.remove( nil, filename )
27
+ end
28
+
29
+ end
30
+
31
+ 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
+ # Implements the +rename+ operation.
22
+ class Rename < Abstract
23
+
24
+ # Perform the operation. The flags parameter is ignored under earlier
25
+ # versions of the SFTP protocol (3 and under).
26
+ def perform( filename, new_name, flags=0 )
27
+ @driver.rename( nil, filename, new_name, flags )
28
+ end
29
+
30
+ end
31
+
32
+ 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
+ # Implements the +rmdir+ operation.
22
+ class Rmdir < Abstract
23
+
24
+ # Perform the operation.
25
+ def perform( path )
26
+ @driver.rmdir( nil, path )
27
+ end
28
+
29
+ end
30
+
31
+ 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
+ module Net ; module SFTP ; module Operations
18
+
19
+ # Register all services that implement an SFTP operation.
20
+ def register_services( container )
21
+ container.namespace :operations
22
+ container.operations.use :model => :prototype do |ns|
23
+ [
24
+ :open,
25
+ [ :close_handle, :close ],
26
+ :read, :write, :opendir, :readdir, :remove,
27
+ :stat, :lstat, :fstat, :setstat, :fsetstat,
28
+ :mkdir, :rmdir, :realpath, :rename
29
+ ].each do |op|
30
+ array = op.is_a?( Array )
31
+ ns.register( array ? op.first : op ) do |c,p|
32
+ require "net/sftp/operations/#{array ? op.last : op}"
33
+ klass_name = ( array ? op.last : op ).to_s
34
+ klass = const_get( klass_name[0,1].upcase + klass_name[1..-1] )
35
+ klass.new c[:log_for, p], c[:session], c[:protocol][:driver]
36
+ end
37
+ end
38
+ end
39
+ end
40
+ module_function :register_services
41
+
42
+ end ; end ; end