Red Hat Bugzilla – Bug 91096
Add option so that tmpwatch can handle removing empty directories
Last modified: 2007-11-30 17:10:31 EST
Description of problem:
Tmpwatch does not delete empty directories. I suspect it is because something
updates each directory's access time. A fix would be to automatically delete
empty directories, no matter when they were accessed last (provided no user had
the directory open).
Previous bug reports have claimed that tmpwatch itself was changing each
directory's access time, thus hosing itself, but RedHat QA has been unable to
replicate the bug. Could be that slocate is changing access time, and this is
why tmpwatch deletes old files but not empty directories.
Version-Release number of selected component (if applicable):
Steps to Reproduce:
1. Create an empty directory.
2. Run slocate with -U or -u option.
3. Check the directory's access time.
4. Run tmpwatch to check the directory.
tmpwatch will not delete the empty directory.
I did not really expect it to because the access time was recent. However, I
wish for a new option so that tmpwatch will delete empty directories no matter
when they were last accessed.
For the impatient, here is a script that will remove all empty directories found
in a tree:
for D; do
echo "Checking: $D"
# Locate all subdirectories, depth first, and try to rmdir.
# rmdir will fail if the directory is not empty.
find "$D" -depth -type d -print0 |
xargs -0 rmdir --ignore-fail-on-non-empty
When running from a cron job (which is the expected usage model
of tmpwatch), removing all empty directories can lead to race
conditions between tmpwatch and other programs that create
a temporary directory and then attempt to store files in it.
Even "keeping the directory open" by chdir () to the directory
doesn't remove the race condition in the time between mkdir () and
If that race condition would trigger, it would lead to mysterious
program failures that would be virtually impossible to reproduce.
Reopening as per discussion on fedora-devel-list.
As it stands, tmpwatch basically never removes directories unless given --ctime
or --mtime. However, it would often be desirable to use --mtime for directories
and --atime for regular files (as suggested in bug #14930 by Aleksey Nogin), and
there's currently not an option that allows that.
Removing directories could still be a little dangerous -- they could be being
used for simple inter-process communication, for example. However, there already
is the -d/--nodirs option to not remove directories. Therefore, one approach
would be to make --atime always use mtime for directories, and use -d in the
standard cron scripts.
That would introduce a change in behavior which might bite people, however. So,
it's probably best to keep current behavior as-is and add a new option --
something like --mtime-dirs (no idea what a good short option would be, since -d
is already taken).
Or, we could totally change the ui, and add options like this:
but maybe that's getting silly.
tmpwatch-2.9.4 has --dirmtime.
The current state (--[acm]time --dirmtime) is as complicated as the options can
get; I can see no good reason to discriminate between regular files and other
non-directories; atime for directories is used if atime is used for other files,
ctime doesn't make sense for directories because even removing a file from a
directory updates the ctime.
Thanks for proposing this alternative again.