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 <dcrosby@fb.com>
  • Loading branch information
dafyddcrosby committed May 8, 2024
1 parent cfbb21d commit b421935
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/chef/file_cache.rb
Expand Up @@ -159,9 +159,15 @@ 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

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

0 comments on commit b421935

Please sign in to comment.