Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FileCache#Find] N to 1 calls for file matching #14351

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 additions & 2 deletions lib/chef/file_cache.rb
Expand Up @@ -159,9 +159,24 @@ 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 # directory of the cache
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]
# We remove the cache directory from the string of each entry
path_to_remove ||= f[regexp_pattern, 1]
f.delete_prefix!(path_to_remove)
keys << f
end
end
keys
Expand Down
64 changes: 64 additions & 0 deletions spec/unit/file_cache_spec.rb
Expand Up @@ -94,7 +94,71 @@
it "searches for cached files by globbing" do
expect(Chef::FileCache.find("snappy/**/*")).to eq(%w{snappy/patter})
end
end

describe "#find handles regex-unsafe unix paths" do
before(:each) do
# Dir.mktmpdir doesn't allow regex unsafe characters (the nerve!), so we'll have to fake file lookups
@file_cache_path = "/tmp/[foo* ^bar]"
escaped_file_cache_path = '/tmp/\[foo\* ^bar\]'
Chef::Config[:file_cache_path] = @file_cache_path
allow(Chef::Util::PathHelper).to receive(:escape_glob_dir).and_return(escaped_file_cache_path)
allow(Dir).to receive(:[]).with(File.join(escaped_file_cache_path, "snappy/**/*")).and_return([
File.join(@file_cache_path, "snappy"),
File.join(@file_cache_path, "snappy", "patter"),
])
allow(Dir).to receive(:[]).with(escaped_file_cache_path).and_return([
@file_cache_path,
])
[
File.join(@file_cache_path, "snappy", "patter"),
File.join(@file_cache_path, "whiz", "bang"),
].each do |f|
allow(File).to receive(:file?).with(f).and_return(true)
end
[
File.join(@file_cache_path, "snappy"),
File.join(@file_cache_path, "whiz"),
].each do |f|
allow(File).to receive(:file?).with(f).and_return(false)
end
end

it "searches for cached files by globbing" do
expect(Chef::FileCache.find("snappy/**/*")).to eq(%w{snappy/patter})
end
end

describe "#find handles Windows paths" do
before(:each) do
allow(ChefUtils).to receive(:windows?).and_return(true)
@file_cache_path = 'C:\tmp\fakecache'
escaped_file_cache_path = "C:\\tmp\\fakecache"
Chef::Config[:file_cache_path] = @file_cache_path
allow(Chef::Util::PathHelper).to receive(:escape_glob_dir).and_return(escaped_file_cache_path)
allow(Dir).to receive(:[]).with(File.join(escaped_file_cache_path, "snappy/**/*")).and_return([
File.join(@file_cache_path, "snappy"),
File.join(@file_cache_path, "snappy", "patter"),
])
allow(Dir).to receive(:[]).with(escaped_file_cache_path).and_return([
@file_cache_path,
])
[
File.join(@file_cache_path, "snappy", "patter"),
File.join(@file_cache_path, "whiz", "bang"),
].each do |f|
allow(File).to receive(:file?).with(f).and_return(true)
end
[
File.join(@file_cache_path, "snappy"),
File.join(@file_cache_path, "whiz"),
].each do |f|
allow(File).to receive(:file?).with(f).and_return(false)
end
end
it "searches for cached files by globbing" do
expect(Chef::FileCache.find("snappy/**/*")).to eq(%w{snappy/patter})
end
end

describe "when checking for the existence of a file" do
Expand Down