]> git.hungrycats.org Git - bees/commitdiff
bees: explain what --show-config ROOT_PATH requires when it is unusable
authorZygo Blaxell <bees@furryterror.org>
Sun, 6 Sep 2026 01:49:42 +0000 (21:49 -0400)
committerZygo Blaxell <bees@furryterror.org>
Sat, 10 Oct 2026 19:00:06 +0000 (15:00 -0400)
--show-config takes the filesystem root, not a configuration file, and
a wrong argument used to fail with whatever errno surfaced first from
context setup (realpath, or an O_NOATIME open refused with EPERM), with
no hint about what the option actually wanted.

Check the cheap requirements up front -- the path exists, is a
directory, and is on btrfs -- and name the failed one.  Permission
problems surface as the EACCES/EPERM from stat() or statfs(); bees
never reads the directory, so only search permission is needed.  Wrap
the remaining checks in set_root_path (subvol id 5, FS_INFO) the same
way.

Every failure ends with the same statement of why a filesystem is
needed at all: the merged configuration depends on it through the
${ROOT}/${UUID}/${LABEL}/${FS_BYTES} interpolations, the local
configuration filename, and limits validated against the filesystem
block size.  The statement is wrapped at 80 columns and written to
stderr directly, together with the specific failure in a single
insertion, so a command-line usage error is visible regardless of the
log configuration and its lines cannot interleave with other output.

Assisted-by: Claude-Code:claude-fable-5-1
src/bees.cc

index 7786bfbff41059de9b80ef64c480bdf20ec4c94c..2cbbad199078547c521939154a699e8cbe173ff9 100644 (file)
@@ -148,6 +148,56 @@ wait_for_signals()
        }
 }
 
+/// Why --show-config needs a real filesystem, for the diagnostics below.
+/// Wrapped at 80 columns; written to stderr directly so it is visible
+/// regardless of the log configuration.
+static const char *const show_config_root_reason =
+       "--show-config ROOT_PATH needs the filesystem bees would run on, not a\n"
+       "configuration file:  the merged configuration depends on it (${ROOT},\n"
+       "${UUID}, ${LABEL} and ${FS_BYTES} interpolation, the local configuration\n"
+       "filename, and limits checked against the filesystem block size).\n"
+       "ROOT_PATH must be an existing directory that is the root of a mounted\n"
+       "btrfs filesystem, searchable by this user.\n";
+
+/// Report a --show-config ROOT_PATH problem and the requirement behind it.
+static
+void
+show_config_root_fail(const string &what)
+{
+       cerr << "--show-config: " << what << "\n" << show_config_root_reason << flush;
+}
+
+/// Check the cheap, explainable requirements on a --show-config
+/// ROOT_PATH before opening it, so a wrong argument gets a message
+/// naming the requirement instead of a bare errno from deep inside
+/// context setup.  Returns false after reporting if the path cannot
+/// qualify.  Permission problems surface as stat/statfs errors, and
+/// only search permission is needed:  bees never reads the directory.
+static
+bool
+check_show_config_root(const string &path)
+{
+       struct stat st;
+       if (stat(path.c_str(), &st)) {
+               show_config_root_fail("cannot access '" + path + "': " + strerror(errno));
+               return false;
+       }
+       if (!S_ISDIR(st.st_mode)) {
+               show_config_root_fail("'" + path + "' is not a directory");
+               return false;
+       }
+       struct statfs sfs;
+       if (statfs(path.c_str(), &sfs)) {
+               show_config_root_fail("cannot statfs '" + path + "': " + strerror(errno));
+               return false;
+       }
+       if (sfs.f_type != BTRFS_SUPER_MAGIC) {
+               show_config_root_fail("'" + path + "' is not on a btrfs filesystem");
+               return false;
+       }
+       return true;
+}
+
 static
 int
 bees_main(int argc, char *argv[])
@@ -363,6 +413,9 @@ bees_main(int argc, char *argv[])
                        return EXIT_FAILURE;
                }
                root_path = show_config_root_path;
+               if (!check_show_config_root(root_path)) {
+                       return EXIT_FAILURE;
+               }
        } else if (optind + 1 == argc) {
                root_path = argv[optind++];
        } else {
@@ -416,7 +469,19 @@ bees_main(int argc, char *argv[])
        BEESTRACE("context constructed");
 
        // Set root path and open root FD
-       bc->set_root_path(root_path);
+       if (show_merged_config) {
+               // The remaining checks (btrfs, subvol id 5, FS_INFO) happen
+               // inside set_root_path; translate a failure into the same
+               // requirement statement as the pre-checks above.
+               try {
+                       bc->set_root_path(root_path);
+               } catch (const exception &e) {
+                       show_config_root_fail("'" + root_path + "' is not usable as ROOT_PATH: " + e.what());
+                       return EXIT_FAILURE;
+               }
+       } else {
+               bc->set_root_path(root_path);
+       }
 
        // Set configuration
        bc->set_config(bconfig);