]> git.ipfire.org Git - pakfire.git/commitdiff
snapshot: Ensure we only umount once
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 17 Oct 2024 17:36:15 +0000 (17:36 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 17 Oct 2024 17:36:15 +0000 (17:36 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/snapshot.c

index dcfdd614001fdb58133ef4d5402d6950c61f36bc..ccab9fcc6f22850b9f9dd09e69960113ac9107a1 100644 (file)
@@ -51,9 +51,17 @@ struct pakfire_snapshot {
 
        char upperdir[PATH_MAX];
        char workdir[PATH_MAX];
+
+       // State
+       enum pakfire_snapshot_state {
+               PAKFIRE_SNAPSHOT_INIT = 0,
+               PAKFIRE_SNAPSHOT_MOUNTED,
+               PAKFIRE_SNAPSHOT_UMOUNTED,
+       } state;
 };
 
 static void pakfire_snapshot_free(struct pakfire_snapshot* snapshot) {
+       // Ensure this is umounted
        pakfire_snapshot_umount(snapshot);
 
        if (snapshot->overlayfs.fd >= 0)
@@ -307,6 +315,9 @@ int pakfire_snapshot_mount(struct pakfire_snapshot* snapshot, const char* path)
        if (r < 0)
                goto ERROR;
 
+       // Mark as mounted
+       snapshot->state = PAKFIRE_SNAPSHOT_MOUNTED;
+
 ERROR:
        if (mountfd >= 0)
                close(mountfd);
@@ -319,22 +330,32 @@ ERROR:
 int pakfire_snapshot_umount(struct pakfire_snapshot* snapshot) {
        int r;
 
-       // Umount the overlayfs
-       if (*snapshot->overlayfs.path) {
-               r = umount(snapshot->overlayfs.path);
-               if (r < 0)
-                       return r;
-       }
-
-       // Umount the tmpfs
-       if (*snapshot->tmpfs.path) {
-               r = umount(snapshot->tmpfs.path);
-               if (r < 0)
-                       return r;
-
-               r = pakfire_rmtree(snapshot->tmpfs.path, 0);
-               if (r < 0)
-                       return r;
+       switch (snapshot->state) {
+               case PAKFIRE_SNAPSHOT_MOUNTED:
+                       // Umount the overlayfs
+                       if (*snapshot->overlayfs.path) {
+                               r = umount(snapshot->overlayfs.path);
+                               if (r < 0)
+                                       return r;
+                       }
+
+                       // Umount the tmpfs
+                       if (*snapshot->tmpfs.path) {
+                               r = umount(snapshot->tmpfs.path);
+                               if (r < 0)
+                                       return r;
+
+                               r = pakfire_rmtree(snapshot->tmpfs.path, 0);
+                               if (r < 0)
+                                       return r;
+                       }
+
+                       // We are now umounted
+                       snapshot->state = PAKFIRE_SNAPSHOT_UMOUNTED;
+                       break;
+
+               default:
+                       break;
        }
 
        return 0;