#! /bin/bash # Test the "Age" parameter (with age-by) for systemd-tmpfiles. set -e set -x # Test directory structure looks like this: # /tmp/ageby/ # ├── d1 # │   ├── f1 # │   ├── f2 # │   ├── f3 # │   └── f4 # ├── d2 # │   ├── f1 # │   ├── f2 # ... export SYSTEMD_LOG_LEVEL="debug" rm -rf /tmp/ageby mkdir -p /tmp/ageby/d{1..4} # TODO: There is probably a better way to figure this out. # Test for [bB] age-by arguments only on filesystems that expose # the creation time. Note that this is _not_ an accurate way to # check if the filesystem or kernel version don't provide the # timestamp. But, if the timestamp is visible in "stat" it is a # good indicator that the test can be run. TEST_TMPFILES_AGEBY_BTIME=${TEST_TMPFILES_AGEBY_BTIME:-0} if stat --format "%w" /tmp/ageby 2>/dev/null | grep -qv '^[\?\-]$'; then TEST_TMPFILES_AGEBY_BTIME=1 fi touch -a --date "2 minutes ago" /tmp/ageby/d1/f1 touch -m --date "4 minutes ago" /tmp/ageby/d2/f1 # Create a bunch of other files. touch /tmp/ageby/d{1,2}/f{2..4} # For "ctime". touch /tmp/ageby/d3/f1 chmod +x /tmp/ageby/d3/f1 sleep 1 # For "btime". touch /tmp/ageby/d4/f1 sleep 1 # More files with recent "{a,b}time" values. touch /tmp/ageby/d{3,4}/f{2..4} # Check for cleanup of "f1" in each of "/tmp/d{1..4}". systemd-tmpfiles --clean - <<-EOF d /tmp/ageby/d1 - - - a:1m - e /tmp/ageby/d2 - - - m:3m - D /tmp/ageby/d3 - - - c:2s - EOF for d in d{1..3}; do test ! -f "/tmp/ageby/${d}/f1" done if [[ $TEST_TMPFILES_AGEBY_BTIME -gt 0 ]]; then systemd-tmpfiles --clean - <<-EOF d /tmp/ageby/d4 - - - b:1s - EOF test ! -f "/tmp/ageby/d4/f1" else # Remove the file manually. rm "/tmp/ageby/d4/f1" fi # Check for an invalid "age" and "age-by" arguments. for a in ':' ':1s' '2:1h' 'nope:42h' '" :7m"' 'm:' '::' '"+r^w-x:2/h"' 'b ar::64'; do systemd-tmpfiles --clean - <&1 | grep -q -F 'Invalid age' d /tmp/ageby - - - ${a} - EOF done for d in d{1..4}; do for f in f{2..4}; do test -f "/tmp/ageby/${d}/${f}" done done # Check for parsing with whitespace, repeated values # for "age-by" (valid arguments). for a in '" a:24h"' 'cccaab:2h' '" aa : 4h"' '" a A B C c:1h"'; do systemd-tmpfiles --clean - <