Skip to content

Commit fb943ab

Browse files
andy5995claude
andcommitted
test: use hard-link probe instead of stat for cross-device detection
stat flag syntax differs between GNU (-c) and BSD/macOS (-f), making the previous approach unreliable. Hard links only succeed within the same filesystem, so attempting ln across /tmp and $HOME gives a portable, unambiguous cross-device check on Linux, macOS, and FreeBSD. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent bffc6e4 commit fb943ab

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

test/test_media_root.sh

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ else
2121
fi
2222

2323
# This test needs a file and waste folder on the same device, but a different
24-
# device than $HOME. Check whether /tmp qualifies.
25-
tmp_dev=$(stat -c %d /tmp 2>/dev/null || stat -f %d /tmp 2>/dev/null || echo "")
26-
home_dev=$(stat -c %d "$RMW_FAKE_HOME" 2>/dev/null || stat -f %d "$RMW_FAKE_HOME" 2>/dev/null || echo "")
27-
28-
if [ -z "$tmp_dev" ] || [ "$tmp_dev" = "$home_dev" ]; then
29-
echo "/tmp is on the same device as home, or stat is unavailable; skipping"
24+
# device than $HOME. Use a hard-link probe to detect this portably: hard links
25+
# only succeed within the same filesystem, so a failure means different devices.
26+
mkdir -p "$RMW_FAKE_HOME"
27+
probe="/tmp/.rmw-media-root-probe"
28+
touch "$probe"
29+
if ln "$probe" "$RMW_FAKE_HOME/.rmw-probe" 2>/dev/null; then
30+
rm -f "$probe" "$RMW_FAKE_HOME/.rmw-probe"
31+
echo "/tmp is on the same device as home; skipping"
3032
exit 0
3133
fi
34+
rm -f "$probe"
3235

3336
# /tmp is on a different device — use it as the simulated media root.
3437
TRASH_DIR="/tmp/.Trash-$(id -u)"
@@ -40,7 +43,6 @@ test_file_path="$TEST_DIR/$test_file"
4043
rm -rf "$TRASH_DIR" "$TEST_DIR"
4144

4245
# Create test config pointing the waste folder to /tmp
43-
mkdir -p "$RMW_FAKE_HOME"
4446
TEST_CONFIG="$RMW_FAKE_HOME/media-root.testrc"
4547
printf 'WASTE = /tmp/.Trash-%s, removable\nexpire_age = 90\n' "$(id -u)" > "$TEST_CONFIG"
4648

0 commit comments

Comments
 (0)