diff mbox series

[01/10] util: introduce generic mount helpers

Message ID 20241030101550.21014-2-stefano.babic@swupdate.org
State New
Headers show
Series Introduce BTRFS Snapshot Handler | expand

Commit Message

Stefano Babic Oct. 30, 2024, 10:15 a.m. UTC
Functions asking to mount / umount filesystems must prepare the same
paths for the mountpoints. Factorize this into two new funtions:

	swupdate_temporary_mount()
	swupdate_temporary_umount()

If the mopunt part isn successful, it returns a generated path (not
fixed anymore) as mountpoint. This should be passed to the corresponding
umount(). that becomes responsible to free memory.

Signed-off-by: Stefano Babic <stefano.babic@swupdate.org>
---
 core/util.c       | 63 +++++++++++++++++++++++++++++++++++++++++++++++
 include/globals.h |  6 +++++
 include/util.h    |  2 ++
 3 files changed, 71 insertions(+)

--
2.34.1
diff mbox series

Patch

diff --git a/core/util.c b/core/util.c
index 33a9af71..1e50d948 100644
--- a/core/util.c
+++ b/core/util.c
@@ -852,6 +852,69 @@  int swupdate_umount(const char *dir)
 #endif
 }

+static const char *mount_points[] = {
+	[MNT_SCRIPTS] = SCRIPTS_DIR_SUFFIX,
+	[MNT_DATA] = DATADST_DIR_SUFFIX,
+	[MNT_BOOT_SCRIPTS] = BOOT_SCRIPT_SUFFIX
+};
+
+char *swupdate_temporary_mount(tmp_mountpoint_t type, const char *device, const char *fstype)
+{
+	char *mountpoint;
+	const char *dir;
+	int ret = 0;
+
+	if (type != MNT_SCRIPTS && type != MNT_DATA && type != MNT_BOOT_SCRIPTS)
+		return NULL;
+
+	if (!device || !strlen(device)) {
+		ERROR("Mount requested without Device");
+		return NULL;
+	}
+
+	dir = mount_points[type];
+	if (asprintf(&mountpoint, "%s%sXXXXXX", get_tmpdir(), dir) == -1) {
+		ERROR("Unable to allocate memory");
+		return NULL;
+	}
+
+	if (!mkdtemp(mountpoint)) {
+		TRACE("Unable to create a unique temporary directory %s: %s",
+			mountpoint, strerror(errno));
+		free(mountpoint);
+		return NULL;
+	}
+
+	ret = swupdate_mount(device, mountpoint, fstype);
+	if (ret) {
+		TRACE("Device %s with filesystem %s cannot be mounted: %s",
+			device, fstype, strerror(errno));
+		rmdir(mountpoint);
+		free(mountpoint);
+		return NULL;
+	}
+
+	return mountpoint;
+}
+
+int swupdate_temporary_umount(char *mountpoint)
+{
+	int ret = 0;
+	if (swupdate_umount(mountpoint)) {
+		TRACE("Unable to unmount %s: %s", mountpoint, strerror(errno));
+		ret = -EFAULT;
+	}
+	if (ret == 0 && rmdir(mountpoint) == -1) {
+		if (errno != EROFS) {
+			TRACE("Unable to remove directory %s: %s", mountpoint, strerror(errno));
+			ret = -EROFS;
+		}
+	}
+	free(mountpoint);
+
+	return ret;
+}
+
 /*
  * Date time in SWUpdate
  * @return : date in ISO8601 (it must be freed by caller)
diff --git a/include/globals.h b/include/globals.h
index 8aeb3f4d..a1d6b597 100644
--- a/include/globals.h
+++ b/include/globals.h
@@ -30,3 +30,9 @@ 
 #define SCRIPTS_DIR_SUFFIX	"scripts/"
 #define DATADST_DIR_SUFFIX	"datadst/"
 #define BOOT_SCRIPT_SUFFIX	"boot-script"
+
+typedef enum {
+	MNT_SCRIPTS,
+	MNT_DATA,
+	MNT_BOOT_SCRIPTS
+} tmp_mountpoint_t;
diff --git a/include/util.h b/include/util.h
index 17035142..1ac108be 100644
--- a/include/util.h
+++ b/include/util.h
@@ -290,6 +290,8 @@  int swupdate_remove_directory(const char* path);

 int swupdate_mount(const char *device, const char *dir, const char *fstype);
 int swupdate_umount(const char *dir);
+char *swupdate_temporary_mount(tmp_mountpoint_t type, const char *device, const char *fstype);
+int swupdate_temporary_umount(char *mountpoint);

 /* Date / Time utilities */
 char *swupdate_time_iso8601(struct timeval *tv);