Skip to content

Commit

Permalink
[FileCache#find] N to 1 calls for regex matching
Browse files Browse the repository at this point in the history
Signed-off-by: David Crosby <[email protected]>
  • Loading branch information
dafyddcrosby committed May 16, 2024
1 parent 4abc625 commit f48c75d
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/chef/file_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,23 @@ def list
# [String] - An array of file cache keys matching the glob
def find(glob_pattern)
keys = []
Dir[File.join(Chef::Util::PathHelper.escape_glob_dir(file_cache_path), glob_pattern)].each do |f|
file_cache_dir = Chef::Util::PathHelper.escape_glob_dir(file_cache_path)
first_filename = Dir[file_cache_dir].first
return keys unless first_filename

# TODO: The usage of Regexp.escape and the match here is likely
# vestigial, but since it's only getting called once per method, the
# effort needed to confirm that its removal won't break something else
# isn't worth it. A task for a brave soul ;-)
regexp_pattern = /^(#{Regexp.escape(first_filename) + File::Separator}).+/

files = Dir[File.join(file_cache_dir, glob_pattern)]
until files.empty?
f = files.shift
if File.file?(f)
keys << f[/^#{Regexp.escape(Dir[Chef::Util::PathHelper.escape_glob_dir(file_cache_path)].first) + File::Separator}(.+)/, 1]
path_to_remove ||= f[regexp_pattern, 1]
f.delete_prefix!(path_to_remove)
keys << f
end
end
keys
Expand Down

0 comments on commit f48c75d

Please sign in to comment.