aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEwoud Kohl van Wijngaarden <ewoud@kohlvanwijngaarden.nl>2018-12-08 13:42:23 +0100
committerEwoud Kohl van Wijngaarden <ewoud@kohlvanwijngaarden.nl>2020-05-06 14:00:21 +0200
commit196dea8289d126aaf9a011a15b348fde0d5159d5 (patch)
treed4fd1507999a0d61b2edbc89f5381048470afb1b
parentc5367aa3508b49ccb19ede20967ecfff8fe6e124 (diff)
downloadpuppet-cron_core-196dea8289d126aaf9a011a15b348fde0d5159d5.tar.gz
puppet-cron_core-196dea8289d126aaf9a011a15b348fde0d5159d5.tar.bz2
(MODULES-8603) Ignore .keep_* files
On Gentoo there's always a .keep_<package>-<slot> (e.g., .keep_cronbase-0) file inside the cron directory to ensure it's not removed with rmdir. Since usernames are very unlikely to start with .keep_, we can safely filter out these hidden files.
-rw-r--r--lib/puppet/provider/cron/crontab.rb16
-rw-r--r--spec/unit/provider/cron/crontab_spec.rb39
2 files changed, 52 insertions, 3 deletions
diff --git a/lib/puppet/provider/cron/crontab.rb b/lib/puppet/provider/cron/crontab.rb
index bfbdc30..9144725 100644
--- a/lib/puppet/provider/cron/crontab.rb
+++ b/lib/puppet/provider/cron/crontab.rb
@@ -273,17 +273,27 @@ Puppet::Type.type(:cron).provide(:crontab, parent: Puppet::Provider::ParsedFile,
'/var/spool/cron'
end
+ # Return the directory holding crontab files stored on the local system.
+ #
+ # @api private
+ def self.crontab_dir
+ CRONTAB_DIR
+ end
+
# Yield the names of all crontab files stored on the local system.
#
- # @note Ignores files that are not writable for the puppet process.
+ # @note Ignores files that are not writable for the puppet process and hidden
+ # files that start with .keep
#
# @api private
def self.enumerate_crontabs
Puppet.debug "looking for crontabs in #{CRONTAB_DIR}"
return unless File.readable?(CRONTAB_DIR)
Dir.foreach(CRONTAB_DIR) do |file|
- path = "#{CRONTAB_DIR}/#{file}"
- yield(file) if File.file?(path) && File.writable?(path)
+ path = File.join(CRONTAB_DIR, file)
+ # Gentoo creates .keep_PACKAGE-SLOT files to make sure the directory is not
+ # removed
+ yield(file) if File.file?(path) && File.writable?(path) && !file.start_with?('.keep_')
end
end
diff --git a/spec/unit/provider/cron/crontab_spec.rb b/spec/unit/provider/cron/crontab_spec.rb
index 031b3ae..796a4c2 100644
--- a/spec/unit/provider/cron/crontab_spec.rb
+++ b/spec/unit/provider/cron/crontab_spec.rb
@@ -202,4 +202,43 @@ describe Puppet::Type.type(:cron).provider(:crontab) do
end
end
end
+
+ context '#enumerate_crontabs' do
+ before(:each) do
+ File.expects(:readable?).with(subject.crontab_dir).returns(true)
+ Dir.expects(:foreach).with(subject.crontab_dir).multiple_yields(*files)
+ end
+
+ context 'only a hidden file' do
+ let(:files) { ['.keep_cronbase-0'] }
+
+ before(:each) do
+ files.each do |filename|
+ path = File.join(subject.crontab_dir, filename)
+ File.expects(:file?).with(path).returns(true)
+ File.expects(:writable?).with(path).returns(true)
+ end
+ end
+
+ it 'ignores .keep_* files' do
+ expect { |b| described_class.enumerate_crontabs(&b) }.not_to yield_control
+ end
+ end
+
+ context 'multiple files' do
+ let(:files) { ['myuser', '.keep_cronbase-0'] }
+
+ before(:each) do
+ files.each do |filename|
+ path = File.join(subject.crontab_dir, filename)
+ File.expects(:file?).with(path).returns(true)
+ File.expects(:writable?).with(path).returns(true)
+ end
+ end
+
+ it 'ignores .keep_* files' do
+ expect { |b| described_class.enumerate_crontabs(&b) }.to yield_control.once
+ end
+ end
+ end
end