file-find 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/CHANGES +4 -0
  2. data/lib/file/find.rb +46 -4
  3. data/test/test_file_find.rb +15 -1
  4. metadata +3 -3
data/CHANGES CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.2.4 - 10-Dec-2008
2
+ * Added support for symbolic permissions. Thanks go to Bill Kleb for the
3
+ suggestion and to Hal Fulton for providing the solution.
4
+
1
5
  == 0.2.3 - 25-Nov-2008
2
6
  * Added mtime support. My previous concerns, which I believe stemmed from
3
7
  the find(2) man page on Solaris 10 with regards to atime checks modifying
data/lib/file/find.rb CHANGED
@@ -6,7 +6,7 @@ include Sys
6
6
 
7
7
  class File::Find
8
8
  # The version of this library
9
- VERSION = '0.2.3'
9
+ VERSION = '0.2.4'
10
10
 
11
11
  # :stopdoc:
12
12
  VALID_OPTIONS = %w/
@@ -88,6 +88,8 @@ class File::Find
88
88
  # group, and world settings are used. Do not use a leading 0 in the values
89
89
  # that you supply, e.g. use 755 not 0755.
90
90
  #
91
+ # You may optionally use symbolic permissions, e.g. "g+rw", "u=rwx", etc.
92
+ #
91
93
  # Not currently supported on MS Windows.
92
94
  #
93
95
  attr_accessor :perm
@@ -144,10 +146,10 @@ class File::Find
144
146
  @size = nil
145
147
  @user = nil
146
148
 
147
- validate_and_set_options(options) unless options.empty?
148
-
149
149
  @previous = nil
150
150
 
151
+ validate_and_set_options(options) unless options.empty?
152
+
151
153
  @path ||= Dir.pwd
152
154
  @name ||= '*'
153
155
  end
@@ -265,7 +267,11 @@ class File::Find
265
267
  # return the proper value.
266
268
  #
267
269
  if @perm
268
- next unless sprintf("%o", stat_info.mode & 07777) == @perm.to_s
270
+ if @perm.is_a?(String)
271
+ next unless stat_info.mode & sym2oct(@perm) == sym2oct(@perm)
272
+ else
273
+ next unless sprintf("%o", stat_info.mode & 07777) == @perm.to_s
274
+ end
269
275
  end
270
276
 
271
277
  # Allow plain numbers, or strings for comparison operators.
@@ -325,4 +331,40 @@ class File::Find
325
331
  send("#{key}=", value)
326
332
  end
327
333
  end
334
+
335
+ # Converts a symoblic permissions mode into its octal equivalent.
336
+ #--
337
+ # Taken almost entirely from ruby-talk: 96956 (Hal Fulton).
338
+ #
339
+ def sym2oct(str)
340
+ left = {'u' => 0700, 'g' => 0070, 'o' => 0007, 'a' => 0777}
341
+ right = {'r' => 0444, 'w' => 0222, 'x' => 0111}
342
+ regex = /([ugoa]+)([+-=])([rwx]+)/
343
+
344
+ cmds = str.split(',')
345
+
346
+ perm = 0
347
+
348
+ cmds.each do |cmd|
349
+ match = cmd.match(regex)
350
+ raise "Invalid symbolic permissions: '#{str}'" if match.nil?
351
+
352
+ junk, who, what, how = match.to_a
353
+
354
+ who = who.split(//).inject(num=0) { |num,b| num |= left[b]; num }
355
+ how = how.split(//).inject(num=0) { |num,b| num |= right[b]; num }
356
+ mask = who & how
357
+
358
+ case what
359
+ when '+'
360
+ perm = perm | mask
361
+ when '-'
362
+ perm = perm & ~mask
363
+ when '='
364
+ perm = mask
365
+ end
366
+ end
367
+
368
+ perm
369
+ end
328
370
  end
@@ -51,7 +51,7 @@ class TC_File_Find < Test::Unit::TestCase
51
51
  end
52
52
 
53
53
  def test_version
54
- assert_equal('0.2.3', File::Find::VERSION)
54
+ assert_equal('0.2.4', File::Find::VERSION)
55
55
  end
56
56
 
57
57
  def test_path
@@ -189,6 +189,20 @@ class TC_File_Find < Test::Unit::TestCase
189
189
  assert_equal('test1.rb', File.basename(results.first))
190
190
  end
191
191
 
192
+ def test_perm_with_symbolic_permissions
193
+ omit_if(@@windows, 'symbolic perm test skipped on MS Windows')
194
+
195
+ File.chmod(0664, @file1) # test1.rb
196
+ File.chmod(0644, @file2) # test1.txt
197
+ results1 = File::Find.new(:name => "test1*", :perm => "g=rw").find
198
+ results2 = File::Find.new(:name => "test1*", :perm => "u=rw").find
199
+
200
+ assert_equal(1, results1.length)
201
+ assert_equal(2, results2.length)
202
+ assert_equal('test1.rb', File.basename(results1.first))
203
+ assert_equal(['test1.rb', 'test1.txt'], results2.map{ |e| File.basename(e) })
204
+ end
205
+
192
206
  def test_prune_basic
193
207
  assert_respond_to(@rule1, :prune)
194
208
  assert_respond_to(@rule1, :prune=)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file-find
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Berger
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-11-25 00:00:00 -07:00
12
+ date: 2008-12-10 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -71,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
71
  requirements: []
72
72
 
73
73
  rubyforge_project: shards
74
- rubygems_version: 1.3.1
74
+ rubygems_version: 1.2.0
75
75
  signing_key:
76
76
  specification_version: 2
77
77
  summary: A better way to find files