From: Steinar H. Gunderson Date: Fri, 5 Jan 2024 07:49:04 +0000 (+0100) Subject: Remove some globals from the bind mount code. X-Git-Tag: 1.1.21~6 X-Git-Url: https://git.sesse.net/?p=plocate;a=commitdiff_plain;h=3a6c45e1937c698579028b15d4a1c3cc8967172d Remove some globals from the bind mount code. --- diff --git a/bind-mount.cpp b/bind-mount.cpp index 1390a59..899bf3b 100644 --- a/bind-mount.cpp +++ b/bind-mount.cpp @@ -38,6 +38,7 @@ any later version. #include #include #include +#include using namespace std; @@ -54,10 +55,10 @@ struct mount { }; /* Path to mountinfo */ -static const char *mountinfo_path; -atomic mountinfo_updated{ false }; +static atomic mountinfo_updated{ false }; -multimap, mount> mount_entries; // Keyed by device major/minor. +// Keyed by device major/minor. +using MountEntries = multimap, mount>; /* Read a line from F. Return a string, or empty string on error. */ @@ -168,15 +169,15 @@ static bool read_mount_entry(FILE *f, mount *me) /* Read mount information from mountinfo_path, update mount_entries and num_mount_entries. - Return 0 if OK, -1 on error. */ -static int read_mount_entries(void) + Return std::nullopt on error. */ +static optional read_mount_entries(void) { - FILE *f = fopen(mountinfo_path, "r"); + FILE *f = fopen(MOUNTINFO_PATH, "r"); if (f == nullptr) { - return -1; + return {}; } - mount_entries.clear(); + MountEntries mount_entries; mount me; while (read_mount_entry(f, &me)) { @@ -189,7 +190,7 @@ static int read_mount_entries(void) mount_entries.emplace(make_pair(me.dev_major, me.dev_minor), me); } fclose(f); - return 0; + return mount_entries; } /* Bind mount path list maintenace and top-level interface. */ @@ -209,7 +210,8 @@ static void rebuild_bind_mount_paths(void) if (conf_debug_pruning) { fprintf(stderr, "Rebuilding bind_mount_paths:\n"); } - if (read_mount_entries() != 0) { + optional mount_entries = read_mount_entries(); + if (!mount_entries.has_value()) { return; } if (conf_debug_pruning) { @@ -218,8 +220,8 @@ static void rebuild_bind_mount_paths(void) bind_mount_paths.clear(); - for (const auto &[dev_id, me] : mount_entries) { - const auto &[first, second] = mount_entries.equal_range(make_pair(me.dev_major, me.dev_minor)); + for (const auto &[dev_id, me] : *mount_entries) { + const auto &[first, second] = mount_entries->equal_range(make_pair(me.dev_major, me.dev_minor)); for (auto it = first; it != second; ++it) { const mount &other = it->second; if (other.id == me.id) { @@ -265,10 +267,9 @@ bool is_bind_mount(const char *path) } /* Initialize state for is_bind_mount(), to read data from MOUNTINFO. */ -void bind_mount_init(const char *mountinfo) +void bind_mount_init() { - mountinfo_path = mountinfo; - mountinfo_fd = open(mountinfo_path, O_RDONLY); + mountinfo_fd = open(MOUNTINFO_PATH, O_RDONLY); if (mountinfo_fd == -1) return; rebuild_bind_mount_paths(); diff --git a/bind-mount.h b/bind-mount.h index b8c9e5f..b8d76b5 100644 --- a/bind-mount.h +++ b/bind-mount.h @@ -30,7 +30,7 @@ any later version. (Bind mounts "to self" are ignored.) */ extern bool is_bind_mount(const char *path); -/* Initialize state for is_bind_mount(), to read data from MOUNTINFO. */ -extern void bind_mount_init(const char *mountinfo); +/* Initialize state for is_bind_mount(), to read data from MOUNTINFO_PATH. */ +extern void bind_mount_init(); #endif diff --git a/updatedb.cpp b/updatedb.cpp index f4cfb02..cf19992 100644 --- a/updatedb.cpp +++ b/updatedb.cpp @@ -789,7 +789,7 @@ int main(int argc, char **argv) conf_prepare(argc, argv); if (conf_prune_bind_mounts) { - bind_mount_init(MOUNTINFO_PATH); + bind_mount_init(); } int fd = open(conf_output.c_str(), O_RDONLY);