sambal 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 38f975857d880b0980139c775c4209198a0fc61d
4
- data.tar.gz: 736a0bff136bf70413f17df9e54a691b38e26a3a
3
+ metadata.gz: ec4f0b1b79fd466d3545c06238d4685e02d690cd
4
+ data.tar.gz: 8036f666109f31542df162291523a7742af88d81
5
5
  SHA512:
6
- metadata.gz: c64dd602dc5e03c8c5d4b2f72664e0fd2edf37fca88b795ee0f1ebb9d977d483c0e071728d187f45ce10bbbf4cb94f20b4fc7fe62a2eea031d11909201b07fad
7
- data.tar.gz: 16d10c22578db5eb2ac0b30835d892b41588cf4a79b953d9ad3231e0c3735a8808001edde69f8d1631470d9ff41a997fe5aacc9017efd66f34c2fb9c8cb20be6
6
+ metadata.gz: 73f4147a17833f4d2c308c60a0b66a1687d2e98eb5ab384d60bfb1442d9e92f922515fd8c1f62a4514aa54153342a12edaa1c8906da0e64e3d36127b6d6bba6d
7
+ data.tar.gz: a5407cf3408aa99a2b8887b8863c62713b35f602b637303160e740033711c86e0d2542d5395897e6b7ca066f463b3c712e023e3cdb9bae4c077ac19da0e02c5b
data/README.md CHANGED
@@ -24,13 +24,13 @@ Or install it yourself as:
24
24
 
25
25
  A working installation of samba, specifically the "smbclient" command line utility. See http://www.samba.org for more information.
26
26
  On a mac this can be installed through homebrew https://github.com/mxcl/homebrew, like this:
27
-
27
+
28
28
  brew install samba
29
29
 
30
30
  On the Mac it can probably also be installed both through Fink and MacPorts.
31
31
 
32
32
  On Linux (Ubuntu) it's as easy as:
33
-
33
+
34
34
  apt-get install smbclient
35
35
 
36
36
  It should be available in a similar way on all major Linux distributions.
@@ -40,9 +40,11 @@ It should be available in a similar way on all major Linux distributions.
40
40
  client = Sambal::Client.new(domain: 'WORKGROUP', host: '127.0.0.1', share: '', user: 'guest', password: '--no-pass', port: 445)
41
41
  client.ls # returns hash of files
42
42
  client.put("local_file.txt","remote_file.txt") # uploads file to server
43
+ client.exists?("remote_file.txt") # checks if file is on server
43
44
  client.put_content("My content here", "remote_file") # uploads content to a file on server
44
45
  client.get("remote_file.txt", "local_file.txt") # downloads file from server
45
46
  client.del("remote_file.txt") # deletes files from server
47
+ client.exists?("some_directory") # checks if directory is on server
46
48
  client.cd("some_directory") # changes directory on server
47
49
  client.close # closes connection
48
50
 
data/lib/sambal/client.rb CHANGED
@@ -9,12 +9,34 @@ module Sambal
9
9
 
10
10
  attr_reader :connected
11
11
 
12
- def initialize(options={})
12
+ def parsed_options(user_options)
13
+ default_options = {
14
+ domain: 'WORKGROUP',
15
+ host: '127.0.0.1',
16
+ share: '',
17
+ user: 'guest',
18
+ password: '--no-pass',
19
+ port: 445,
20
+ timeout: 10,
21
+ columns: 80
22
+ }
23
+
24
+ options = default_options.merge(user_options)
25
+ options[:ip_address] ||= options[:host] if options[:host] == default_options[:host]
26
+ options
27
+ end
28
+
29
+ def initialize(user_options={})
13
30
  begin
14
- options = {domain: 'WORKGROUP', host: '127.0.0.1', share: '', user: 'guest', password: '--no-pass', port: 445, timeout: 10, columns: 80}.merge(options)
31
+ options = parsed_options(user_options)
15
32
  @timeout = options[:timeout].to_i
16
- @o, @i, @pid = PTY.spawn("COLUMNS=#{options[:columns]} smbclient \"//#{options[:host]}/#{options[:share]}\" '#{options[:password]}' -W \"#{options[:domain]}\" -U \"#{options[:user]}\" -p #{options[:port]}")
17
- res = @o.expect(/(.*\n)?smb:.*\\>/, @timeout)[0] rescue nil
33
+
34
+ option_flags = "-W \"#{options[:domain]}\" -U \"#{options[:user]}\" -I #{options[:ip_address]} -p #{options[:port]}"
35
+ command = "COLUMNS=#{options[:columns]} smbclient \"//#{options[:host]}/#{options[:share]}\" '#{options[:password]}'"
36
+
37
+ @output, @input, @pid = PTY.spawn(command + ' ' + option_flags)
38
+
39
+ res = @output.expect(/(.*\n)?smb:.*\\>/, @timeout)[0] rescue nil
18
40
  @connected = case res
19
41
  when nil
20
42
  $stderr.puts "Failed to connect"
@@ -70,6 +92,10 @@ module Sambal
70
92
  parse_files(ask_wrapped('ls', qualifier))
71
93
  end
72
94
 
95
+ def exists?(path)
96
+ ls(path).key? File.basename(path)
97
+ end
98
+
73
99
  def cd(dir)
74
100
  response = ask("cd \"#{dir}\"")
75
101
  if response.split("\r\n").join('') =~ /NT_STATUS_OBJECT_NAME_NOT_FOUND/
@@ -79,9 +105,9 @@ module Sambal
79
105
  end
80
106
  end
81
107
 
82
- def get(file, output)
108
+ def get(filename, output)
83
109
  begin
84
- file_context(file) do |file|
110
+ file_context(filename) do |file|
85
111
  response = ask_wrapped 'get', [file, output]
86
112
  if response =~ /^getting\sfile.*$/
87
113
  Response.new(response, true)
@@ -157,9 +183,9 @@ module Sambal
157
183
  end
158
184
  end
159
185
 
160
- def del(file)
186
+ def del(filename)
161
187
  begin
162
- file_context(file) do |file|
188
+ file_context(filename) do |file|
163
189
  response = ask_wrapped 'del', file
164
190
  next_line = response.split("\n")[1]
165
191
  if next_line =~ /^smb:.*\\>/
@@ -202,15 +228,15 @@ module Sambal
202
228
  end
203
229
 
204
230
  def close
205
- @i.close
206
- @o.close
231
+ @input.close
232
+ @output.close
207
233
  Process.wait(@pid)
208
234
  @connected = false
209
235
  end
210
236
 
211
237
  def ask(cmd)
212
- @i.printf("#{cmd}\n")
213
- response = @o.expect(/^smb:.*\\>/,@timeout)[0] rescue nil
238
+ @input.printf("#{cmd}\n")
239
+ response = @output.expect(/^smb:.*\\>/,@timeout)[0] rescue nil
214
240
  if response.nil?
215
241
  $stderr.puts "Failed to do #{cmd}"
216
242
  raise Exception.new, "Failed to do #{cmd}"
@@ -229,30 +255,33 @@ module Sambal
229
255
  [cmd,filenames].flatten.join(' ')
230
256
  end
231
257
 
258
+ # Parse output from Client#ls
259
+ # Returns Hash of file names with meta information
232
260
  def parse_files(str)
233
- files = {}
234
- str.each_line do |line|
235
- if line =~ /\s+([\w\.\d\-\_\?\!\s]+)\s+([DAH]?)\s+(\d+)\s+(.+)$/
236
- lsplit = line.split(/\s{2,}/)
237
- #$stderr.puts "lsplit: #{lsplit}"
238
- lsplit.shift ## remove first empty string
239
- name = lsplit.shift#$1
240
- if lsplit.first =~ /^[A-Za-z]+$/
241
- type = lsplit.shift
242
- else
243
- type = ""
244
- end
245
- size = lsplit.shift#$3
246
- date = lsplit.join(' ')#$4
247
- name.gsub!(/\s+$/,'')
248
- files[name] = if type =~/^D.*$/
249
- {type: :directory, size: size, modified: (Time.parse(date) rescue "!!#{date}")}
250
- else
251
- {type: :file, size: size , modified: (Time.parse(date) rescue "!!#{date}")}
252
- end
261
+ listing = str.each_line.inject({}) do |files, line|
262
+ line.strip!
263
+ name = line[/.*(?=\b\s+[ABDHNRS]+\s+\d+)/]
264
+ name ||= line[/^\.\.|^\./]
265
+
266
+ if name
267
+ line.sub!(name, '')
268
+ line.strip!
269
+
270
+ type = line[0] == "D" ? :directory : :file
271
+ size = line[/\d+/]
272
+
273
+ date = line[/(?<=\d )\D.*$/]
274
+ modified = (Time.parse(date) rescue "!!#{date}")
275
+
276
+ files[name] = {
277
+ type: type,
278
+ size: size,
279
+ modified: modified
280
+ }
253
281
  end
282
+ files
254
283
  end
255
- files
284
+ Hash[listing.sort]
256
285
  end
257
286
  end
258
287
  end
@@ -1,5 +1,5 @@
1
1
  # coding: UTF-8
2
2
 
3
3
  module Sambal
4
- VERSION = "0.1.7"
4
+ VERSION = "0.1.8"
5
5
  end
@@ -6,6 +6,7 @@ require 'tempfile'
6
6
  describe Sambal::Client do
7
7
 
8
8
  TEST_DIRECTORY_WITH_SPACE_IN_NAME = 'my dir with spaces in name'
9
+ TEST_DIRECTORY_WITH_CONSECUTIVE_SPACES_IN_NAME = 'my dir with consecutive spaces in name'
9
10
  TEST_FILE_IN_DIRECTORY_WITH_SPACE_IN_NAME = 'a_file_in_a_dir_with_spaces_in_name'
10
11
  TEST_SPACES_IN_NAME_PATH = "#{TEST_DIRECTORY_WITH_SPACE_IN_NAME}/#{TEST_FILE_IN_DIRECTORY_WITH_SPACE_IN_NAME}"
11
12
  TEST_DIRECTORY = 'testdir'
@@ -26,6 +27,7 @@ describe Sambal::Client do
26
27
  f << "Hello"
27
28
  end
28
29
  FileUtils.mkdir_p "#{test_server.share_path}/#{TEST_DIRECTORY_WITH_SPACE_IN_NAME}"
30
+ FileUtils.mkdir_p "#{test_server.share_path}/#{TEST_DIRECTORY_WITH_CONSECUTIVE_SPACES_IN_NAME}"
29
31
  File.open("#{test_server.share_path}/#{TEST_DIRECTORY_WITH_SPACE_IN_NAME}/#{TEST_FILE_IN_DIRECTORY_WITH_SPACE_IN_NAME}", 'w') do |f|
30
32
  f << "Hello there"
31
33
  end
@@ -67,6 +69,7 @@ describe Sambal::Client do
67
69
  it "should list files with spaces in their names" do
68
70
  result = @sambal_client.ls
69
71
  expect(result).to have_key(TEST_DIRECTORY_WITH_SPACE_IN_NAME)
72
+ expect(result).to have_key(TEST_DIRECTORY_WITH_CONSECUTIVE_SPACES_IN_NAME)
70
73
  end
71
74
 
72
75
  it "should list files on an smb server" do
@@ -84,6 +87,20 @@ describe Sambal::Client do
84
87
  end
85
88
  end
86
89
 
90
+ describe 'exists?' do
91
+ it "returns true if a file or directory exists at a given path" do
92
+ expect(@sambal_client.exists?(TESTFILE)).to eq(true)
93
+ expect(@sambal_client.exists?(TESTFILE_SUB_PATH)).to eq(true)
94
+ expect(@sambal_client.exists?(TEST_DIRECTORY)).to eq(true)
95
+ expect(@sambal_client.exists?(SUB_DIRECTORY_PATH)).to eq(true)
96
+ end
97
+
98
+ it "returns false if nothing exists at a given path" do
99
+ expect(@sambal_client.exists?('non_existing_file.txt')).to eq(false)
100
+ expect(@sambal_client.exists?('non_existing_directory')).to eq(false)
101
+ end
102
+ end
103
+
87
104
  describe 'mkdir' do
88
105
  before(:all) do
89
106
  @sambal_client.cd('/')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sambal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Axel Eriksson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-19 00:00:00.000000000 Z
11
+ date: 2017-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -66,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
66
  version: '0'
67
67
  requirements: []
68
68
  rubyforge_project:
69
- rubygems_version: 2.2.3
69
+ rubygems_version: 2.5.2
70
70
  signing_key:
71
71
  specification_version: 4
72
72
  summary: Ruby Samba Client