sambal 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sambal.rb CHANGED
@@ -39,9 +39,9 @@ module Sambal
39
39
  end
40
40
 
41
41
  class Client
42
-
42
+
43
43
  attr_reader :connected
44
-
44
+
45
45
  def initialize(options={})
46
46
  begin
47
47
  options = {domain: 'WORKGROUP', host: '127.0.0.1', share: '', user: 'guest', password: '--no-pass', port: 445}.merge(options)
@@ -64,7 +64,7 @@ module Sambal
64
64
  true
65
65
  end
66
66
  end
67
-
67
+
68
68
  unless @connected
69
69
  close if @pid
70
70
  exit(1)
@@ -99,19 +99,20 @@ module Sambal
99
99
  end
100
100
  end
101
101
  end
102
-
102
+
103
103
  def ls(qualifier = '*')
104
104
  parse_files(ask_wrapped('ls', qualifier))
105
105
  end
106
-
106
+
107
107
  def cd(dir)
108
- if response = ask("cd #{dir}")
109
- Response.new(response, true)
110
- else
108
+ response = ask("cd \"#{dir}\"")
109
+ if response.split("\r\n").join('') =~ /NT_STATUS_OBJECT_NAME_NOT_FOUND/
111
110
  Response.new(response, false)
111
+ else
112
+ Response.new(response, true)
112
113
  end
113
114
  end
114
-
115
+
115
116
  def get(file, output)
116
117
  begin
117
118
  file_context(file) do |file|
@@ -126,7 +127,7 @@ module Sambal
126
127
  Response.new(e.message, false)
127
128
  end
128
129
  end
129
-
130
+
130
131
  def put(file, destination)
131
132
  response = ask_wrapped 'put', [file, destination]
132
133
  if response =~ /^putting\sfile.*$/
@@ -137,7 +138,7 @@ module Sambal
137
138
  rescue InternalError => e
138
139
  Response.new(e.message, false)
139
140
  end
140
-
141
+
141
142
  def put_content(content, destination)
142
143
  t = Tempfile.new("upload-smb-content-#{destination}")
143
144
  File.open(t.path, 'w') do |f|
@@ -156,7 +157,8 @@ module Sambal
156
157
  end
157
158
 
158
159
  def rmdir(dir)
159
- cd dir
160
+ response = cd dir
161
+ return response if response.failure?
160
162
  begin
161
163
  ls.each do |name, meta|
162
164
  if meta[:type]==:file
@@ -178,7 +180,7 @@ module Sambal
178
180
  Response.new(e.message, false)
179
181
  end
180
182
  end
181
-
183
+
182
184
  def del(file)
183
185
  begin
184
186
  file_context(file) do |file|
@@ -222,12 +224,12 @@ module Sambal
222
224
  # subdirs.times { cd '..' }
223
225
  # end
224
226
  end
225
-
227
+
226
228
  def close
227
229
  @i.printf("quit\n")
228
230
  @connected = false
229
231
  end
230
-
232
+
231
233
  def ask(cmd)
232
234
  @i.printf("#{cmd}\n")
233
235
  response = @o.expect(/^smb:.*\\>/,10)[0] rescue nil
@@ -238,22 +240,23 @@ module Sambal
238
240
  response
239
241
  end
240
242
  end
241
-
243
+
242
244
  def ask_wrapped(cmd,filenames)
243
245
  ask wrap_filenames(cmd,filenames)
244
246
  end
245
-
247
+
246
248
  def wrap_filenames(cmd,filenames)
247
249
  filenames = [filenames] unless filenames.kind_of?(Array)
248
250
  filenames.map!{ |filename| '"' + filename + '"' }
249
251
  [cmd,filenames].flatten.join(' ')
250
252
  end
251
-
253
+
252
254
  def parse_files(str)
253
255
  files = {}
254
256
  str.each_line do |line|
255
257
  if line =~ /\s+([\w\.\d\-\_\?\!\s]+)\s+([DAH]?)\s+(\d+)\s+(.+)$/
256
- lsplit = line.split(/\s+/)
258
+ lsplit = line.split(/\s{2,}/)
259
+ #$stderr.puts "lsplit: #{lsplit}"
257
260
  lsplit.shift ## remove first empty string
258
261
  name = lsplit.shift#$1
259
262
  if lsplit.first =~ /^[A-Za-z]+$/
@@ -273,6 +276,6 @@ module Sambal
273
276
  end
274
277
  files
275
278
  end
276
-
279
+
277
280
  end
278
281
  end
@@ -1,5 +1,5 @@
1
1
  # coding: UTF-8
2
2
 
3
3
  module Sambal
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
@@ -13,11 +13,16 @@ describe Sambal::Client do
13
13
  File.open("#{test_server.share_path}/#{testfile}", 'w') do |f|
14
14
  f << "Hello"
15
15
  end
16
+ FileUtils.mkdir_p "#{test_server.share_path}/#{test_directory_with_space_in_name}"
17
+ File.open("#{test_server.share_path}/#{test_directory_with_space_in_name}/#{test_file_in_directory_with_space_in_name}", 'w') do |f|
18
+ f << "Hello there"
19
+ end
16
20
  FileUtils.mkdir_p "#{test_server.share_path}/#{test_directory}"
17
21
  FileUtils.mkdir_p "#{test_server.share_path}/#{test_directory}/#{test_sub_directory}"
18
22
  File.open("#{test_server.share_path}/#{test_directory}/#{test_sub_directory}/#{testfile_sub}", 'w') do |f|
19
23
  f << "Hello"
20
24
  end
25
+ FileUtils.chmod 0775, "#{test_server.share_path}/#{test_directory_with_space_in_name}/#{test_file_in_directory_with_space_in_name}"
21
26
  FileUtils.chmod 0775, "#{test_server.share_path}/#{test_directory}/#{test_sub_directory}/#{testfile_sub}"
22
27
  FileUtils.chmod 0775, "#{test_server.share_path}/#{test_directory}/#{test_sub_directory}"
23
28
  FileUtils.chmod 0775, "#{test_server.share_path}/#{test_directory}"
@@ -37,6 +42,12 @@ describe Sambal::Client do
37
42
  t
38
43
  end
39
44
 
45
+ let(:test_directory_with_space_in_name) { 'my dir with spaces in name' }
46
+
47
+ let(:test_file_in_directory_with_space_in_name) { 'a_file_in_a_dir_with_spaces_in_name' }
48
+
49
+ let(:test_spaces_in_name_path) { "#{test_directory_with_space_in_name}/#{test_file_in_directory_with_space_in_name}" }
50
+
40
51
  let(:test_directory) do
41
52
  'testdir'
42
53
  end
@@ -74,7 +85,12 @@ describe Sambal::Client do
74
85
  FileUtils.cp "#{test_server.share_path}/#{testfile}", "#{test_server.share_path}/#{testfile2}"
75
86
  FileUtils.cp "#{test_server.share_path}/#{testfile}", "#{test_server.share_path}/#{testfile3}"
76
87
  end
77
-
88
+
89
+ it "should list files with spaces in their names" do
90
+ result = @sambal_client.ls
91
+ result.should have_key(test_directory_with_space_in_name)
92
+ end
93
+
78
94
  it "should list files on an smb server" do
79
95
  result = @sambal_client.ls
80
96
  result.should have_key(testfile)
@@ -96,6 +112,13 @@ describe Sambal::Client do
96
112
  File.size("/tmp/sambal_spec_testfile.txt").should == @sambal_client.ls[testfile][:size].to_i
97
113
  end
98
114
 
115
+ it "should get files in a dir with spaces in it's name from an smb server" do
116
+ @sambal_client.get(test_spaces_in_name_path, "/tmp/sambal_this_file_was_in_dir_with_spaces.txt").should be_successful
117
+ File.exists?("/tmp/sambal_this_file_was_in_dir_with_spaces.txt").should == true
118
+ @sambal_client.cd(test_directory_with_space_in_name)
119
+ File.size("/tmp/sambal_this_file_was_in_dir_with_spaces.txt").should == @sambal_client.ls[test_file_in_directory_with_space_in_name][:size].to_i
120
+ end
121
+
99
122
  it "should get files in a subdirectory while in a higher level directory from an smb server" do
100
123
  @sambal_client.get(testfile_sub_path, "/tmp/sambal_spec_testfile_sub.txt").should be_successful
101
124
  File.exists?("/tmp/sambal_spec_testfile_sub.txt").should == true
@@ -176,11 +199,11 @@ describe Sambal::Client do
176
199
  result = @sambal_client.put("jhfahsf iasifasifh", "jsfijsf ijidjag")
177
200
  result.should_not be_successful
178
201
  end
179
-
202
+
180
203
  it 'should create commands with one wrapped filename' do
181
204
  @sambal_client.wrap_filenames('cmd',['file1','file2']).should eq('cmd "file1" "file2"')
182
205
  end
183
-
206
+
184
207
  it 'should create commands with more than one wrapped filename' do
185
208
  @sambal_client.wrap_filenames('cmd',['file1','file2']).should eq('cmd "file1" "file2"')
186
209
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sambal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-21 00:00:00.000000000 Z
12
+ date: 2012-10-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec