]> git.sesse.net Git - plocate/commitdiff
Remove some globals from the bind mount code.
authorSteinar H. Gunderson <steinar+git@gunderson.no>
Fri, 5 Jan 2024 07:49:04 +0000 (08:49 +0100)
committerSteinar H. Gunderson <steinar+git@gunderson.no>
Fri, 5 Jan 2024 07:49:04 +0000 (08:49 +0100)
bind-mount.cpp
bind-mount.h
updatedb.cpp

index 1390a59fe5eeca7f4005177a891bef1fade9f23a..899bf3bd5f9a5496fbdb9d11e8e09d4a86493633 100644 (file)
@@ -38,6 +38,7 @@ any later version.
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <thread>
+#include <optional>
 
 using namespace std;
 
@@ -54,10 +55,10 @@ struct mount {
 };
 
 /* Path to mountinfo */
-static const char *mountinfo_path;
-atomic<bool> mountinfo_updated{ false };
+static atomic<bool> mountinfo_updated{ false };
 
-multimap<pair<int, int>, mount> mount_entries;  // Keyed by device major/minor.
+// Keyed by device major/minor.
+using MountEntries = multimap<pair<int, int>, 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<MountEntries> 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<MountEntries> 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();
index b8c9e5fc6a96b3183e67c846154d5d2c0109e12f..b8d76b56ce9d2b4ddfcfbb8043f70f7fe35d5b43 100644 (file)
@@ -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
index f4cfb020f131027913a14fd5e40cdfe5b4887bd6..cf19992ca3716feb5ac6d098d7007431af2a1676 100644 (file)
@@ -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);