net-sftp 0.5.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/examples/get-put.rb +45 -0
- data/examples/ssh-service.rb +0 -1
- data/examples/synchronous.rb +16 -7
- data/lib/net/sftp/operations/abstract.rb +11 -6
- data/lib/net/sftp/session.rb +49 -1
- data/lib/net/sftp/version.rb +1 -1
- metadata +6 -4
data/examples/get-put.rb
ADDED
@@ -0,0 +1,45 @@
|
|
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
|
+
File.open( "test.data", "w" ) { |f| f.write "012345789"*10000 }
|
24
|
+
|
25
|
+
puts "putting local file to remote location..."
|
26
|
+
sftp.put_file "test.data", "temp/blah.data"
|
27
|
+
|
28
|
+
puts "getting remote file to local location..."
|
29
|
+
sftp.get_file "temp/blah.data", "new.data"
|
30
|
+
|
31
|
+
sftp.remove "temp/blah.data"
|
32
|
+
|
33
|
+
if !File.exist? "new.data"
|
34
|
+
warn "----\n" +
|
35
|
+
"something went wrong---'new.data' was apparently not created..." +
|
36
|
+
"----\n"
|
37
|
+
else
|
38
|
+
File.delete "new.data"
|
39
|
+
end
|
40
|
+
|
41
|
+
File.delete "test.data"
|
42
|
+
|
43
|
+
puts "----------------------------------------------"
|
44
|
+
puts "done!"
|
45
|
+
end
|
data/examples/ssh-service.rb
CHANGED
@@ -21,7 +21,6 @@ require 'net/sftp'
|
|
21
21
|
Net::SSH.start( 'localhost' ) do |session|
|
22
22
|
session.sftp.connect do |sftp|
|
23
23
|
h = sftp.opendir( "." )
|
24
|
-
session.loop { sftp.state != :open }
|
25
24
|
sftp.readdir( h ).sort { |a,b| a.filename <=> b.filename }.each do |item|
|
26
25
|
puts "%06o %6d %-20s" %
|
27
26
|
[ item.attributes.permissions, item.attributes.size, item.filename ]
|
data/examples/synchronous.rb
CHANGED
@@ -22,21 +22,30 @@ Net::SFTP.start( 'localhost',
|
|
22
22
|
) do |sftp|
|
23
23
|
puts "----------------------------------------------"
|
24
24
|
puts "getting handle..."
|
25
|
-
handle = sftp.
|
25
|
+
handle = sftp.open_handle( "temp/out" )
|
26
26
|
puts "got handle: #{handle.inspect}"
|
27
27
|
puts "reading..."
|
28
28
|
data = sftp.read( handle )
|
29
29
|
puts "got data: #{data.length} bytes"
|
30
30
|
sftp.close_handle( handle )
|
31
31
|
|
32
|
+
puts "----------------------------------------------"
|
33
|
+
puts "getting handle..."
|
34
|
+
sftp.open_handle( "temp/out" ) do |handle|
|
35
|
+
puts "got handle: #{handle.inspect}"
|
36
|
+
puts "reading..."
|
37
|
+
data = sftp.read( handle )
|
38
|
+
puts "got data: #{data.length} bytes"
|
39
|
+
end
|
40
|
+
|
32
41
|
puts "----------------------------------------------"
|
33
42
|
puts "opening handle for writing..."
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
43
|
+
sftp.open_handle( "temp/blah", "w" ) do |handle|
|
44
|
+
puts "got handle: #{handle.inspect}"
|
45
|
+
data = "1234567890" * 100
|
46
|
+
puts "writing #{data.length} bytes..."
|
47
|
+
p sftp.write( handle, data ).code
|
48
|
+
end
|
40
49
|
|
41
50
|
puts "----------------------------------------------"
|
42
51
|
puts "opening handle for writing..."
|
@@ -49,12 +49,13 @@ module Net ; module SFTP ; module Operations
|
|
49
49
|
@log.debug "executing" if @log.debug?
|
50
50
|
|
51
51
|
unless block_given?
|
52
|
+
status = result = nil
|
52
53
|
callback = Proc.new do |status, *pargs|
|
53
|
-
|
54
|
-
case
|
55
|
-
when pargs.empty? then
|
56
|
-
when pargs.length == 1 then
|
57
|
-
else
|
54
|
+
status = status
|
55
|
+
result = case
|
56
|
+
when pargs.empty? then @session.status
|
57
|
+
when pargs.length == 1 then pargs.first
|
58
|
+
else pargs
|
58
59
|
end
|
59
60
|
end
|
60
61
|
end
|
@@ -64,7 +65,11 @@ module Net ; module SFTP ; module Operations
|
|
64
65
|
@log.debug "received request id #{@id}"
|
65
66
|
@session.register( @id, self )
|
66
67
|
|
67
|
-
|
68
|
+
unless block_given?
|
69
|
+
@session.loop { status.nil? }
|
70
|
+
@session.status = status
|
71
|
+
return result
|
72
|
+
end
|
68
73
|
end
|
69
74
|
|
70
75
|
# A callback for SFTP status packets. By default, raises an exception unless
|
data/lib/net/sftp/session.rb
CHANGED
@@ -114,7 +114,7 @@ module Net ; module SFTP
|
|
114
114
|
# the SFTP protocol has not yet been negotiated and no underlying driver has
|
115
115
|
# been selected.
|
116
116
|
#
|
117
|
-
# If no block is given, it
|
117
|
+
# If no block is given, it returns +self+, so it can be chained easily to
|
118
118
|
# other method calls. If a block _is_ given, the session is yielded to the
|
119
119
|
# block as soon as the driver successfully reports it's state as +open+,
|
120
120
|
# with the session's channel being closed automatically when the block
|
@@ -143,6 +143,54 @@ module Net ; module SFTP
|
|
143
143
|
self
|
144
144
|
end
|
145
145
|
end
|
146
|
+
|
147
|
+
# Opens the given remote file and returns a handle to it, which may be used
|
148
|
+
# with other operations (read, write, etc.). If a block is given, the handle
|
149
|
+
# will be yielded to it and closed when the block finishes, otherwise the
|
150
|
+
# handle will be returned. If the flags parameter is a numeric value, it
|
151
|
+
# must be a combination of IO constants, otherwise, it should be a string
|
152
|
+
# such as given to File.open.
|
153
|
+
def open_handle( path, flags=IO::RDONLY, mode=0660 )
|
154
|
+
if String === flags
|
155
|
+
flags = case flags
|
156
|
+
when "r" then IO::RDONLY
|
157
|
+
when "r+" then IO:RDWR
|
158
|
+
when "w" then IO::WRONLY | IO::CREAT | IO::TRUNC
|
159
|
+
when "w+" then IO::RDWR | IO::CREAT | IO::TRUNC
|
160
|
+
when "a" then IO::APPEND | IO::CREAT
|
161
|
+
when "a+" then IO::APPEND | IO::CREAT
|
162
|
+
else IO::RDONLY
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
handle = self.open( path, flags, mode )
|
167
|
+
if block_given?
|
168
|
+
begin
|
169
|
+
yield handle
|
170
|
+
ensure
|
171
|
+
close_handle( handle )
|
172
|
+
end
|
173
|
+
else
|
174
|
+
return handle
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
# Retrieves the given remote file to the given local path. This will
|
179
|
+
# overwrite any file at the local path name. The remote file must
|
180
|
+
# exist.
|
181
|
+
def get_file( remote_path, local_path )
|
182
|
+
open_handle( remote_path ) do |handle|
|
183
|
+
contents = read( handle )
|
184
|
+
File.open( local_path, "w" ) { |f| f.write contents }
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
# This stores the given local file at the given remote path. This will
|
189
|
+
# overwrite any file at the remote path name. The local file must exist.
|
190
|
+
def put_file( local_path, remote_path )
|
191
|
+
contents = File.read( local_path )
|
192
|
+
open_handle( remote_path, "w" ) { |handle| write( handle, contents ) }
|
193
|
+
end
|
146
194
|
|
147
195
|
#--
|
148
196
|
# ====================================================================
|
data/lib/net/sftp/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.8.
|
2
|
+
rubygems_version: 0.8.4
|
3
3
|
specification_version: 1
|
4
4
|
name: net-sftp
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date:
|
6
|
+
version: 0.9.0
|
7
|
+
date: 2005-01-11
|
8
8
|
summary: Net::SFTP is a pure-Ruby implementation of the SFTP client protocol.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
11
|
-
author: Jamis Buck
|
12
11
|
email: jgb3@email.byu.edu
|
13
12
|
homepage: http://net-ssh.rubyforge.org/sftp
|
14
13
|
rubyforge_project:
|
@@ -25,6 +24,8 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
|
|
25
24
|
version: 0.0.0
|
26
25
|
version:
|
27
26
|
platform: ruby
|
27
|
+
authors:
|
28
|
+
- Jamis Buck
|
28
29
|
files:
|
29
30
|
- doc/LICENSE-RUBY
|
30
31
|
- doc/LICENSE-BSD
|
@@ -80,6 +81,7 @@ files:
|
|
80
81
|
- lib/net/sftp/protocol/04/services.rb
|
81
82
|
- lib/net/sftp/protocol/04/impl.rb
|
82
83
|
- lib/net/sftp/protocol/05/services.rb
|
84
|
+
- examples/get-put.rb
|
83
85
|
- examples/ssh-service.rb
|
84
86
|
- examples/synchronous.rb
|
85
87
|
- examples/asynchronous.rb
|