* **`persistent`**
Enable on-disk persistence of both the hash table and the crawl checkpoint.
* `yes` (default) — bees reads `beeshash.dat` and `beespoint.ini` at startup
- (creating them if necessary, subject to `state.hash.create`) and writes
+ (creating them if necessary, subject to `state.create`) and writes
them back as the daemon runs.
* `no` — bees runs entirely from an in-memory hash table and starts every
crawl from the beginning. No state files are read or written. Combine
* When `persistent = no`, the keys in `[state.hash]` and `[state.point]`
have no effect.
+* **`create`**
+ Auto-create `path.home` and `beeshash.dat` if they do not already exist.
+ * `yes` (default) — bees creates the `path.home` directory at startup
+ if it is missing. It first attempts `BTRFS_IOC_SUBVOL_CREATE` so
+ `path.home` becomes its own subvolume, with independent
+ snapshot/quota lifecycle from the surrounding filesystem. On a
+ non-btrfs parent (or older kernels that reject the ioctl) it falls
+ back to a plain `mkdir()`. Only the final path component is
+ created — the parent of `path.home` must already exist. Once
+ `path.home` exists, `beeshash.dat` is created on first run, sized
+ to `state.hash.size`.
+ * `no` — startup fails if `path.home` or `beeshash.dat` is missing.
+ Useful to detect a misconfigured `path.home` before bees writes a
+ fresh empty hash table to the wrong location.
+ * Has no effect when `state.persistent = no` (no on-disk state is
+ read or written) or when `path.home` already exists (the create
+ step is idempotent).
+
## [state.hash] section
The `[state.hash]` section configures `beeshash.dat`: its size, whether bees
(combined with `state.hash.resize = yes`) rather than editing the
config file in place.
-* **`create`**
- Create `beeshash.dat` if it does not already exist.
- * `yes` (default) — bees creates the file at startup, sized to
- `state.hash.size`.
- * `no` — startup fails if the file is missing. Useful to detect a
- misconfigured `path.home` before bees writes a fresh empty hash table
- to the wrong location.
- * Has no effect when `state.persistent = no`.
-
* **`resize`**
Resize the on-disk hash table when `state.hash.size` does not match the
existing file size.
# hash table.
persistent = yes
+ # Auto-create path.home and beeshash.dat if they do not exist.
+ # When yes, bees creates the path.home directory at startup if
+ # it is missing. When no, startup fails if path.home or
+ # beeshash.dat is missing.
+ # Has no effect when state.persistent = no.
+ create = yes
+
# The [state.hash] section controls hash table file options.
[state.hash]
# other substitutions (see docs/config-format.md).
size = min(${RAM_BYTES} / 16, ${FS_BYTES} / 16384)
- # Create beeshash.dat if it does not exist.
- # Set to no to require the file to be present at startup.
- create = yes
-
# Resize the on-disk hash table to state.hash.size on startup.
# Uses reverse-LRU rebuild to preserve the most-recently-used entries.
# When no, a size mismatch is logged as a warning and the existing
BEESTRACE("Opening home directory");
const auto home_dir = m_config->get_trusted("path.home", [](const string &s) { return s; } );
BEESTRACE("Opening home directory '" << home_dir << "'");
- if (home_dir.at(0) == '/') {
- // Absolute paths use plain openat (dirfd is ignored
- // for absolute paths, symlinks are allowed).
- // Use an anonymous Fd here because the absolute/relative Fd path
- // code will be confused if it's not under the root
- m_home_fd = openat(root_fd(), home_dir.c_str(), FLAGS_OPEN_DIR);
- if (!m_home_fd) {
- THROW_ERRNO("openat: " << home_dir);
+ const bool persistent = m_config->get_trusted("state.persistent", bees_parse_bool);
+ const bool create = m_config->get_trusted("state.create", bees_parse_bool);
+
+ const bool absolute = (home_dir.at(0) == '/');
+
+ // First attempt: open paths.home if it already exists.
+ // Absolute paths use plain openat (dirfd is ignored for absolute
+ // paths, symlinks are allowed); relative paths use bees_openat,
+ // which tries openat2(RESOLVE_BENEATH | RESOLVE_NO_SYMLINKS |
+ // RESOLVE_NO_XDEV) and silently falls back to openat on kernels
+ // that predate openat2 (< 5.6). Returns the raw file descriptor
+ // and the errno from the failing open so the caller can inspect
+ // errno without worrying about Fd construction clobbering it.
+ auto try_open = [&](int &out_errno) -> Fd {
+ const int raw = absolute
+ ? openat(root_fd(), home_dir.c_str(), FLAGS_OPEN_DIR)
+ : bees_openat(root_fd(), home_dir.c_str(), FLAGS_OPEN_DIR);
+ out_errno = (raw < 0) ? errno : 0;
+ if (absolute) {
+ // Use an anonymous Fd here because the absolute/relative
+ // Fd path code will be confused if it's not under the root.
+ return Fd(raw);
}
- } else {
- // Relative paths use bees_openat, which tries openat2(RESOLVE_BENEATH |
- // RESOLVE_NO_SYMLINKS | RESOLVE_NO_XDEV) and silently falls back to
- // openat on kernels that predate openat2 (< 5.6).
- m_home_fd = Fd(bees_openat(root_fd(), home_dir.c_str(), FLAGS_OPEN_DIR), home_dir, m_root_fd);
+ return Fd(raw, home_dir, m_root_fd);
+ };
+
+ int open_errno = 0;
+ m_home_fd = try_open(open_errno);
+ if (!m_home_fd && open_errno == ENOENT) {
+ if (!persistent) {
+ // state.persistent=no: paths.home is optional. Run
+ // inert: BeesStringFile reads return "" and writes
+ // no-op when m_dir_fd is empty. Logged once at
+ // INFO so it's audible without paying every reconnect.
+ BEESLOGINFO("paths.home '" << home_dir << "' does not exist; running inert (state.persistent=no)");
+ return m_home_fd;
+ }
+ if (!create) {
+ THROW_ERROR(runtime_error, "paths.home '" << home_dir << "' does not exist and state.create=no");
+ }
+
+ // state.persistent=yes && state.create=yes: create paths.home.
+ // Try BTRFS_IOC_SUBVOL_CREATE first (so paths.home is its own
+ // subvolume with independent snapshot/quota lifecycle), then
+ // fall back to mkdir(). Subvol creation only applies when the
+ // parent is root_fd() and the name is a single path component;
+ // for absolute paths or paths with embedded '/', skip straight
+ // to mkdir().
+ const bool single_component = !absolute && home_dir.find('/') == string::npos;
+ bool created = false;
+ if (single_component) {
+ BEESLOGINFO("paths.home '" << home_dir << "' missing; creating subvolume");
+ created = btrfs_subvol_create_at(root_fd(), home_dir);
+ }
+ if (!created) {
+ BEESLOGINFO("paths.home '" << home_dir << "' missing; creating directory");
+ const int mkdir_rv = absolute
+ ? mkdir(home_dir.c_str(), 0700)
+ : mkdirat(root_fd(), home_dir.c_str(), 0700);
+ if (mkdir_rv != 0) {
+ THROW_ERRNO("mkdir: " << home_dir);
+ }
+ }
+ // Retry the open now that paths.home exists.
+ m_home_fd = try_open(open_errno);
if (!m_home_fd) {
- THROW_ERRNO("openat: " << name_fd(root_fd()) << "/" << home_dir);
+ errno = open_errno;
+ THROW_ERRNO("openat after create: " << home_dir);
}
+ } else if (!m_home_fd) {
+ // Real error (EACCES, EROFS, ENOTDIR, etc.) — surface it.
+ errno = open_errno;
+ THROW_ERRNO("openat: " << (absolute ? string() : (name_fd(root_fd()) + "/")) << home_dir);
}
BEESLOGDEBUG("opened home dir '" << home_dir << "' at '" << name_fd(m_home_fd) << "'");
return m_home_fd;