From: Michael Tremer Date: Thu, 17 Oct 2024 17:36:15 +0000 (+0000) Subject: snapshot: Ensure we only umount once X-Git-Tag: 0.9.30~1032 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=33bd1318cd6074f9562e126b2c8e8cb1fea24318;p=pakfire.git snapshot: Ensure we only umount once Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/snapshot.c b/src/libpakfire/snapshot.c index dcfdd6140..ccab9fcc6 100644 --- a/src/libpakfire/snapshot.c +++ b/src/libpakfire/snapshot.c @@ -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;