smb 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/smb/vfs.rb ADDED
@@ -0,0 +1,215 @@
1
+ require 'smb'
2
+ require 'uri'
3
+ require 'io/nonblock'
4
+
5
+ module SMB
6
+ class Dir
7
+ include Enumerable
8
+ def each(&block)
9
+ while entry = read()
10
+ yield(entry)
11
+ end
12
+ end
13
+ def self.entries(url)
14
+ list = []
15
+ d = SMB::Dir.new(url)
16
+ begin
17
+ while entry = d.read
18
+ list << entry
19
+ end
20
+ ensure
21
+ d.close
22
+ end
23
+ list
24
+ end
25
+ def self.foreach(url)
26
+ d = SMB::Dir.new(url)
27
+ begin
28
+ while entry = d.read
29
+ yield(entry)
30
+ end
31
+ ensure
32
+ d.close
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ #module Ambit
39
+ module VFS
40
+ SCHEMES = {
41
+ 'smb'=> {
42
+ :dir_new => lambda { |uri| SMB::Dir.new(uri.to_s) },
43
+ :file_new => lambda { |uri,flags,mode| SMB::File.new(uri.to_s,flags,mode) },
44
+ :dir_entries => lambda { |uri| SMB::Dir.entries(uri.to_s) },
45
+ :dir_foreach => lambda { |uri,block| SMB::Dir.foreach(uri.to_s, &block) },
46
+ :mkdir => lambda { |uri,mode| SMB::Dir.mkdir(uri.to_s,mode) },
47
+ :rmdir => lambda { |uri| SMB::Dir.rmdir(uri.to_s) },
48
+ :stat => lambda { |uri| SMB::File.stat(uri.to_s) },
49
+ :unlink => lambda { |uri| SMB::File.unlink(uri.to_s) },
50
+ :file_read => lambda { |uri| SMB::File.read(uri.to_s) },
51
+
52
+
53
+ :init => lambda { |auth| SMB::init { |srv,shr,wrk,uid,pwd| auth.call(URI.parse("smb://#{srv}/#{shr}"),{:server=>srv, :share=>shr, :workgroup=>wrk, :username=>uid, :password=>pwd}) } },
54
+ },
55
+ 'file'=> {
56
+ :dir_new => lambda { |uri| ::Dir.new(unescape(uri.path)) },
57
+ :file_new => lambda { |uri,flags,mode| ::File.new(unescape(uri.path),flags,mode) },
58
+ :dir_entries => lambda { |uri| ::Dir.entries(unescape(uri.path)) },
59
+ :dir_foreach => lambda { |uri,block| ::Dir.foreach(unescape(uri.path), &block) },
60
+ :mkdir => lambda { |uri,mode| ::Dir.mkdir(unescape(uri.path),mode) },
61
+ :rmdir => lambda { |uri| ::Dir.rmdir(unescape(uri.path)) },
62
+ :stat => lambda { |uri| ::File.stat(unescape(uri.path)) },
63
+ :unlink => lambda { |uri| ::File.unlink(unescape(uri.path)) },
64
+ :file_read => lambda { |uri| ::File.read(unescape(uri.path)) },
65
+ }
66
+ }
67
+ def self.init(&auth_callback)
68
+ SCHEMES.each do |key,val|
69
+ if val[:init]
70
+ val[:init].call(auth_callback)
71
+ end
72
+ end
73
+ end
74
+ def self.escape(str)
75
+ str.gsub(/([^-A-Za-z0-9.\/])/) {|i| sprintf("%%%02X",i[0]) }
76
+ end
77
+ def self.unescape(str)
78
+ str.gsub(/(\%[A-Fa-f0-9]{2})/) {|i| d=" "; d[0]=Integer("0x"+i[1..-1]); d }
79
+ end
80
+ def self.path2uri(path)
81
+ "file://" + escape(if path[0] == ?/
82
+ path
83
+ else
84
+ ::File::expand_path(path)
85
+ end)
86
+ end
87
+ def self.io_copy(in_fp, out_fp, block_size = (4096 * 16))
88
+ rfp, wfp = IO::pipe
89
+
90
+ reader = fork {
91
+ rfp.close()
92
+ while str = in_fp.read(block_size)
93
+ wfp.write(str)
94
+ break if in_fp.respond_to?(:eof?) and in_fp.eof?
95
+ end
96
+ wfp.close
97
+ exit(1)
98
+ }
99
+ writer = fork {
100
+ wfp.close()
101
+ while str = rfp.read(block_size)
102
+ out_fp.write(str)
103
+ break if rfp.closed? or rfp.eof? or str.empty?
104
+ end
105
+ rfp.close
106
+ exit(1)
107
+ }
108
+ wfp.close()
109
+ rfp.close()
110
+
111
+ ar = aw = nil
112
+ until ar and aw
113
+ ar = Process::waitpid2(reader, Process::WNOHANG) unless ar
114
+ aw = Process::waitpid2(writer, Process::WNOHANG) unless aw
115
+ end
116
+ end
117
+
118
+ module Dir
119
+ def self.open(uri)
120
+ uri = URI.parse(uri)
121
+ SCHEMES[uri.scheme][:dir_new][uri]
122
+ end
123
+ def self.entries(uri)
124
+ uri = URI.parse(uri)
125
+ SCHEMES[uri.scheme][:dir_entries][uri]
126
+ end
127
+ def self.mkdir(uri,mode=0777)
128
+ uri = URI.parse(uri)
129
+ SCHEMES[uri.scheme][:mkdir][uri,mode]
130
+ end
131
+ def self.rmdir(uri)
132
+ uri = URI.parse(uri)
133
+ SCHEMES[uri.scheme][:rmdir][uri]
134
+ end
135
+ def self.foreach(uri, &block)
136
+ uri = URI.parse(uri)
137
+ SCHEMES[uri.scheme][:dir_foreach][uri,block]
138
+ end
139
+ def self.copy(from_uri, to_uri)
140
+ File::mkpath(to_uri)
141
+ Dir.foreach(from_uri) do |file|
142
+ next if file[0] == ?.
143
+ s = File::stat(furi = File::join(from_uri, VFS::escape(file)))
144
+ if s.directory?
145
+ Dir::copy(furi, File::join(to_uri, VFS::escape(file)))
146
+ elsif s.file?
147
+ File::copy(furi, File::join(to_uri, VFS::escape(file)))
148
+ end
149
+ end
150
+ end
151
+ end
152
+ module FileTest
153
+ def self.exists?(uri)
154
+ begin
155
+ File::stat(uri)
156
+ true
157
+ rescue Errno::ENOENT, Errno::EINVAL
158
+ false
159
+ end
160
+ end
161
+ def self.directory?(uri)
162
+ File::stat(uri).directory?
163
+ end
164
+ end
165
+ module File
166
+ def self.open(uri, flags=nil, mode=nil)
167
+ uri = URI.parse(uri)
168
+ mode ||= 0644
169
+ unless flags.kind_of?(Integer)
170
+ flags = {
171
+ 'w' => ::File::WRONLY|::File::CREAT|::File::TRUNC,
172
+ 'r' => ::File::RDONLY,
173
+ 'a' => ::File::RDWR|::File::CREAT|::File::APPEND
174
+ }[flags && flags[0...1]] || ::File::RDONLY
175
+ end
176
+ SCHEMES[uri.scheme][:file_new][uri, flags, mode]
177
+ end
178
+
179
+ def self.copy(in_uri,out_uri)
180
+ in_f, out_f = File.open(in_uri), File.open(out_uri,'w')
181
+ VFS::io_copy(in_f, out_f)
182
+ end
183
+ def self.mkpath(uri)
184
+ if FileTest.exists?(uri)
185
+ false
186
+ else
187
+ dir_uri, base = File::split(uri)
188
+ mkpath(dir_uri)
189
+ Dir::mkdir(uri,0777)
190
+ true
191
+ end
192
+ end
193
+ def self.stat(uri)
194
+ uri = URI.parse(uri)
195
+ SCHEMES[uri.scheme][:stat][uri]
196
+ end
197
+ def self.unlink(uri)
198
+ uri = URI.parse(uri)
199
+ SCHEMES[uri.scheme][:unlinks][uri]
200
+ end
201
+ def self.read(uri)
202
+ uri = URI.parse(uri)
203
+ SCHEMES[uri.scheme][:file_read][uri]
204
+ end
205
+ def self.join(*args)
206
+ ::File::join(*args)
207
+ end
208
+ def self.split(*args)
209
+ ::File::split(*args)
210
+ end
211
+ end
212
+ end
213
+ #end
214
+
215
+
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smb
3
+ version: !ruby/object:Gem::Version
4
+ hash: 11
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 5
9
+ - 0
10
+ version: 0.5.0
11
+ platform: ruby
12
+ authors:
13
+ - Geoff Youngs
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2013-06-17 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 61
28
+ segments:
29
+ - 0
30
+ - 0
31
+ - 17
32
+ version: 0.0.17
33
+ type: :runtime
34
+ name: rubber-generate
35
+ version_requirements: *id001
36
+ description: |
37
+ smb
38
+
39
+ email: git@intersect-uk.co.uk
40
+ executables: []
41
+
42
+ extensions:
43
+ - ext/smb/extconf.rb
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - ext/smb/smb.c
48
+ - ext/smb/smb.cr
49
+ - ext/smb/smb.rd
50
+ - Rakefile
51
+ - README.md
52
+ - lib/smb.rb
53
+ - lib/smb/version.rb
54
+ - lib/smb/vfs.rb
55
+ - ext/smb/extconf.rb
56
+ homepage: http://github.com/geoffyoungs/smb-ruby
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options: []
61
+
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 3
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ requirements: []
83
+
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.24
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: libsmbclient bindings for ruby
89
+ test_files: []
90
+