lib/file/find.rb in file-find-0.3.7 vs lib/file/find.rb in file-find-0.3.8
- old
+ new
@@ -1,21 +1,20 @@
require 'date'
+require 'sys/admin'
-# For alternate implementations of Ruby, such as JRuby, that cannot
-# build C extensions fall back to the Etc module.
begin
- require 'sys/admin'
+ require 'win32/file'
rescue LoadError
- require 'etc'
+ # Do nothing, not required, just nicer.
end
class File::Find
# The version of the file-find library
- VERSION = '0.3.7'
+ VERSION = '0.3.8'
# :stopdoc:
- VALID_OPTIONS = %w/
+ VALID_OPTIONS = %w[
atime
ctime
follow
ftype
inum
@@ -30,11 +29,11 @@
path
perm
prune
size
user
- /
+ ]
# :startdoc:
# The starting path(s) for the search. The default is the current directory.
# This can be a single path or an array of paths.
#
@@ -58,12 +57,10 @@
attr_accessor :ctime
# Limits searches to files that belong to a specific group, where the
# group can be either a group name or ID.
#
- # Not currently supported on MS Windows.
- #
attr_accessor :group
# An array of two element arrays for storing FileTest methods and their
# boolean value.
#
@@ -78,12 +75,11 @@
# Limits searches to specific types of files. The possible values here are
# those returned by the File.ftype method.
#
attr_accessor :ftype
- # Limits search to a file with a specific inode number. Ignored on MS
- # Windows.
+ # Limits search to a file with a specific inode number.
#
attr_accessor :inum
# Limits search to files with the specified number of links.
#
@@ -115,16 +111,15 @@
#
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.
+ # group, and world settings are used.
#
# You may optionally use symbolic permissions, e.g. "g+rw", "u=rwx", etc.
#
- # Not currently supported on MS Windows.
+ # MS Windows only recognizes two modes, 0644 and 0444.
#
attr_accessor :perm
# Skips files or directories that match the string provided as an argument.
#
@@ -138,12 +133,10 @@
attr_accessor :size
# Limits searches to files that belong to a specific user, where the user
# can be either a user name or an ID.
#
- # Not currently supported on MS Windows.
- #
attr_accessor :user
# The file that matched previously in the current search.
#
attr_reader :previous
@@ -337,32 +330,39 @@
next unless File.ftype(file) == @ftype
end
if @group
if @group.is_a?(String)
- next unless get_group(stat_info.gid).name == @group
+ if File::ALT_SEPARATOR
+ begin
+ next unless Sys::Admin.get_group(stat_info.gid, :LocalAccount => true).name == @group
+ rescue Sys::Admin::Error
+ next
+ end
+ else
+ begin
+ next unless Sys::Admin.get_group(stat_info.gid).name == @group
+ rescue Sys::Admin::Error
+ next
+ end
+ end
else
next unless stat_info.gid == @group
end
end
- unless File::ALT_SEPARATOR
- if @inum
- next unless stat_info.ino == @inum
- end
+ if @inum
+ next unless stat_info.ino == @inum
end
- # This currently doesn't work on MS Windows, even in limited
- # fashion for 0666 and 0664, because File.stat.mode doesn't
- # return the proper value.
- #
+ # Note that only 0644 and 0444 are supported on MS Windows.
if @perm
if @perm.is_a?(String)
octal_perm = sym2oct(@perm)
next unless stat_info.mode & octal_perm == octal_perm
else
- next unless sprintf("%o", stat_info.mode & 07777) == @perm.to_s
+ next unless sprintf("%o", stat_info.mode & 07777) == sprintf("%o", @perm)
end
end
# Allow plain numbers, or strings for comparison operators.
if @size
@@ -383,11 +383,23 @@
end
end
if @user
if @user.is_a?(String)
- next unless get_user(stat_info.uid).name == @user
+ if File::ALT_SEPARATOR
+ begin
+ next unless Sys::Admin.get_user(stat_info.uid, :LocalAccount => true).name == @user
+ rescue Sys::Admin::Error
+ next
+ end
+ else
+ begin
+ next unless Sys::Admin.get_user(stat_info.uid).name == @user
+ rescue Sys::Admin::Error
+ next
+ end
+ end
else
next unless stat_info.uid == @user
end
end
@@ -474,29 +486,7 @@
perm = mask
end
end
perm
- end
-
- # Returns the group object based on the group id. Implemented for the
- # sake of platforms that cannot build extensions, such as JRuby.
- #
- def get_group(gid)
- if defined? Sys::Admin
- Sys::Admin.get_group(gid)
- else
- Etc.getgrgid(gid)
- end
- end
-
- # Returns the user object based on the group id. Implemented for the
- # sake of platforms that cannot build extensions, such as JRuby.
- #
- def get_user(uid)
- if defined? Sys::Admin
- Sys::Admin.get_user(uid)
- else
- Etc.getpwuid(uid)
- end
end
end