lib/file/find.rb in file-find-0.1.1 vs lib/file/find.rb in file-find-0.2.0
- old
+ new
@@ -1,10 +1,10 @@
require 'date'
class File::Find
# The version of this package
- VERSION = '0.1.1'
+ VERSION = '0.2.0'
# :stopdoc:
VALID_OPTIONS = %w/
atime
ctime
@@ -12,10 +12,12 @@
ftype
inum
group
name
path
+ perm
+ prune
size
user
/
# :startdoc:
@@ -64,10 +66,21 @@
# The name pattern used to limit file searches. The patterns that are legal
# for Dir.glob are legal here. The default is '*', i.e. everything.
#
attr_accessor :name
+ # Limits searches to files which have permissions that match the octal
+ # value that you provide. For purposes of this comparison, only the user,
+ # group, and world settings are used. Do not use a leading 0 in the values
+ # that you supply, e.g. use 755 not 0755.
+ #
+ attr_accessor :perm
+
+ # Skips files or directories that match the string provided as an argument.
+ #
+ attr_accessor :prune
+
# If the value passed is an integer, this option limits searches to files
# that match the size, in bytes, exactly. If a string is passed, you can
# use the standard comparable operators to match files, e.g. ">= 200" would
# limit searches to files greater than or equal to 200 bytes.
#
@@ -90,10 +103,12 @@
@ctime = nil
@ftype = nil
@group = nil
@follow = true
@inum = nil
+ @perm = nil
+ @prune = nil
@size = nil
@user = nil
validate_and_set_options(options) unless options.empty?
@@ -105,18 +120,29 @@
# In block form, yields each file in turn that matches the specified rules.
# In non-block form it will return an array of matches instead.
#
def find
results = [] unless block_given?
- paths = [@path]
+ paths = @path.to_a
+ if @prune
+ prune_regex = Regexp.new(@prune)
+ else
+ prune_regex = nil
+ end
+
catch(:loop) do
paths.each{ |path|
Dir.foreach(path){ |file|
next if file == '.'
next if file == '..'
+ if prune_regex
+ next if prune_regex.match(file)
+ end
+
+ orig = file.dup
file = File.join(path, file)
stat_method = @follow ? :lstat : :stat
# Skip files we cannot access, stale links, etc.
@@ -128,21 +154,24 @@
stat_method = :lstat # Handle recursive symlinks
retry
end
glob = File.join(File.dirname(file), @name)
- next unless Dir[glob].include?(file)
# Add directories back onto the list of paths to search unless
# they've already been added.
#
+ # TODO: Add max_depth support here
+ #
if stat_info.directory?
unless paths.include?(file)
paths << file
end
end
+ next unless Dir[glob].include?(file)
+
if @atime
date1 = Date.parse(Time.now.to_s)
date2 = Date.parse(stat_info.atime.to_s)
next unless (date1 - date2).numerator == @atime
end
@@ -163,9 +192,13 @@
unless RUBY_PLATFORM.match('mswin')
if @inum
next unless stat_info.ino == @inum
end
+ end
+
+ if @perm
+ next unless sprintf("%o", stat_info.mode & 07777) == @perm.to_s
end
# Allow plain numbers, or strings for comparison operators.
if @size
if @size.is_a?(String)