]> git.hungrycats.org Git - bees/commitdiff
util: throw on inert BeesStringFile use; callers bail on !home_fd()
authorZygo Blaxell <bees@furryterror.org>
Mon, 27 Apr 2026 01:11:26 +0000 (21:11 -0400)
committerZygo Blaxell <bees@furryterror.org>
Tue, 6 Oct 2026 05:51:40 +0000 (01:51 -0400)
The previous approach made BeesStringFile::read() return an empty string
and BeesStringFile::write() log + no-op when m_dir_fd was empty and the
name was relative.  That silently swallowed a programming error: a
caller had constructed expensive content destined for a file that was
never going to be written.  Replace the silent no-op with a runtime_error
throw so the misuse is operator-visible.

The contract is now: a BeesStringFile bound to home_fd() with a relative
name is only valid when home_fd() is a real fd.  If home_fd() is empty
(state.persistent=no with no on-disk home directory in a follow-up
commit) callers must check !home_fd() upfront and skip the read/write
before constructing content.  The absolute-path branch (m_name starts
with '/') is unchanged: bees-config.cc still passes a Fd() with an
absolute path through AT_FDCWD for the global config file.

Update the three call sites that bind BeesStringFile to home_fd() with
relative names:

  - BeesHashTable stats dump (bees-hash.cc): the entire graph_blob
    construction continues to run unconditionally because the result
    is also written to the log via BEESLOGINFO.  Only the file-write
    half is guarded by if (m_ctx->home_fd()) { ... } so the log copy
    keeps firing when the on-disk state file is unavailable.

  - BeesRoots::snapshot_point() and ::flush_pending_checkpoints()
    (bees-roots.cc): bail at the top when !home_fd().  snapshot_point
    avoids iterating crawlers and serialising the INI string;
    flush_pending_checkpoints is defence-in-depth — with snapshot_point
    bailing the queue stays empty so the loop is a no-op anyway.

  - BeesRoots::import_legacy() (bees-roots.cc): bail at the top when
    !home_fd().  There is no on-disk legacy beescrawl.dat to migrate
    when paths.home is missing, so the BeesStringFile open is skipped
    entirely.

Assisted-by: Claude-Code:claude-opus-4-7
Signed-off-by: Zygo Blaxell <bees@furryterror.org>
src/bees-hash.cc
src/bees-roots.cc
src/bees-util.cc

index f92f45d0b7ebc75882647e1ba3853190847c23a4..10da343c9569ecfc6dffa53faa56a13c9d26dd08 100644 (file)
@@ -392,9 +392,16 @@ BeesHashTable::prefetch_loop()
                graph_blob << m_ctx->get_progress();
 
                BEESLOGINFO(graph_blob.str());
-               catch_all([&]() {
-                       m_stats_file.write(graph_blob.str());
-               });
+               // Skip the on-disk stats file when paths.home is unavailable
+               // (state.persistent=no with no home directory).  The log copy
+               // above still fires so operators see the same content; only
+               // the file write is suppressed to avoid throwing from the
+               // inert BeesStringFile handle.
+               if (m_ctx->home_fd()) {
+                       catch_all([&]() {
+                               m_stats_file.write(graph_blob.str());
+                       });
+               }
 
                if (not_locked && !m_stop_requested) {
                        // Always do the mlock, whether shared or not
index 93125bfed8eac130cd80305631fb134de03bf407..faf2a1f24748a04656d173a29dfccebc86255af7 100644 (file)
@@ -1699,6 +1699,13 @@ BeesRoots::populate_from_ini(const Innie &ini)
 void
 BeesRoots::import_legacy()
 {
+       // No on-disk home means no legacy beescrawl.dat to migrate.  Bail
+       // before opening BeesStringFile against an empty home_fd(), which
+       // would throw on the inert read() below.
+       if (!m_ctx->home_fd()) {
+               return;
+       }
+
        BEESNOTE("importing beescrawl.dat into beespoint.ini");
        BEESLOGINFO("Importing beescrawl.dat into beespoint.ini");
 
@@ -1795,6 +1802,13 @@ BeesRoots::snapshot_point()
 {
        // Must already be holding m_mutex.
 
+       // No on-disk home means no beespoint.ini to write.  Bail before
+       // iterating crawlers and serialising the INI string — the work is
+       // wasted and m_point_file.write() would throw on its inert handle.
+       if (!m_ctx->home_fd()) {
+               return;
+       }
+
        if (m_crawl_clean == m_crawl_dirty) {
                BEESLOGINFO("Nothing to save");
                return;
@@ -1864,6 +1878,13 @@ BeesRoots::snapshot_point()
 void
 BeesRoots::flush_pending_checkpoints(bool force)
 {
+       // Defence in depth: snapshot_point() already bails when home_fd()
+       // is empty so the queue stays empty, but bail here too so the
+       // queue-drain loop never attempts an inert write.
+       if (!m_ctx->home_fd()) {
+               return;
+       }
+
        while (true) {
                string write_ini;
                uint64_t write_seq = 0;
index 1b52f96cfe894a16491a0f817e6a103b0ad4d1b0..0bde460f536101d84955ed8810a4a98d68715ad5 100644 (file)
@@ -221,6 +221,16 @@ BeesStringFile::allow_symlink(bool do_allow)
 string
 BeesStringFile::read()
 {
+       // Inert handle: relative name with no dir fd is a programming error.
+       // Callers that bind a BeesStringFile to home_fd() must check
+       // !home_fd() upfront and skip read() before getting here.  The
+       // absolute-path branch (m_name starts with '/') stays supported below
+       // via AT_FDCWD; only the relative-name + empty-dir-fd combination
+       // throws.
+       if (!m_dir_fd && !m_name.empty() && m_name.at(0) != '/') {
+               THROW_ERROR(runtime_error, "BeesStringFile read on inert handle: m_dir_fd unset and relative name '" << m_name << "'");
+       }
+
        BEESNOTE("opening " << m_name << " in " << name_fd(m_dir_fd));
        Fd fd;
        const auto fdcwd = (!m_dir_fd) ? AT_FDCWD : m_dir_fd.operator int();
@@ -316,6 +326,14 @@ bees_fsync(int const fd)
 void
 BeesStringFile::write(string contents)
 {
+       // Inert handle: symmetric to read().  A relative name with no dir fd
+       // means the caller built content destined for an unbound BeesStringFile
+       // without checking home_fd() first.  Throw rather than silently dropping
+       // the write so the bug is operator-visible.
+       if (!m_dir_fd && !m_name.empty() && m_name.at(0) != '/') {
+               THROW_ERROR(runtime_error, "BeesStringFile write on inert handle: m_dir_fd unset and relative name '" << m_name << "'");
+       }
+
        // Path components not allowed - this code can't implement m_allow_symlink with them
        THROW_CHECK1(invalid_argument, m_name, m_name.find_first_of("/") == string::npos);