From 956895430edd4a4ea2963798c3485ab227ea525c Mon Sep 17 00:00:00 2001
From: Tavian Barnes <tavianator@tavianator.com>
Date: Thu, 12 May 2022 14:52:52 -0400
Subject: stat: Pass AT_NO_AUTOMOUNT if possible

Previously bfs would trigger automounts even when it didn't descend into
the mount directory, while GNU find does not.
---
 src/stat.c               |  4 ++++
 tests.sh                 | 21 +++++++++++++++++++++
 tests/test_automount.out |  1 +
 3 files changed, 26 insertions(+)
 create mode 100644 tests/test_automount.out

diff --git a/src/stat.c b/src/stat.c
index c3ee1ad..ad57e65 100644
--- a/src/stat.c
+++ b/src/stat.c
@@ -310,6 +310,10 @@ int bfs_stat(int at_fd, const char *at_path, enum bfs_stat_flags flags, struct b
 		at_flags |= AT_SYMLINK_NOFOLLOW;
 	}
 
+#ifdef AT_NO_AUTOMOUNT
+	at_flags |= AT_NO_AUTOMOUNT;
+#endif
+
 	int x_flags = 0;
 #ifdef AT_STATX_DONT_SYNC
 	if (flags & BFS_STAT_NOSYNC) {
diff --git a/tests.sh b/tests.sh
index 25fda36..f6b7326 100755
--- a/tests.sh
+++ b/tests.sh
@@ -827,6 +827,8 @@ sudo_tests=(
     test_inum_bind_mount
     test_type_bind_mount
     test_xtype_bind_mount
+
+    test_automount
 )
 
 case "$UNAME" in
@@ -3019,6 +3021,25 @@ function test_xtype_bind_mount() {
     return $ret
 }
 
+function test_automount() {
+    # bfs shouldn't trigger automounts unless it descends into them
+
+    skip_if fail command -v systemd-mount &>/dev/null
+
+    rm -rf scratch/*
+    mkdir scratch/{foo,mnt}
+    quiet sudo systemd-mount -A -o bind basic scratch/mnt
+
+    local before=$(inum scratch/mnt)
+    bfs_diff scratch -inum "$before" -prune
+    local ret=$?
+    local after=$(inum scratch/mnt)
+
+    quiet sudo systemd-umount scratch/mnt
+
+    ((ret == 0 && before == after))
+}
+
 function set_acl() {
     case "$UNAME" in
         Darwin)
diff --git a/tests/test_automount.out b/tests/test_automount.out
new file mode 100644
index 0000000..99c7511
--- /dev/null
+++ b/tests/test_automount.out
@@ -0,0 +1 @@
+scratch/mnt
-- 
cgit v1.2.3