diff mbox series

[02/10] Make use of introduced swupdate_temporary_(u)mount

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

Commit Message

Stefano Babic Oct. 30, 2024, 10:15 a.m. UTC
Replaced duplicated code and use the common functions.

Signed-off-by: Stefano Babic <stefano.babic@swupdate.org>
---
 handlers/archive_handler.c | 11 ++++-------
 handlers/btrfs_handler.c   | 11 ++++-------
 handlers/delta_handler.c   |  9 ++++-----
 handlers/raw_handler.c     |  9 ++++-----
 handlers/rdiff_handler.c   |  7 +++----
 5 files changed, 19 insertions(+), 28 deletions(-)

--
2.34.1
diff mbox series

Patch

diff --git a/handlers/archive_handler.c b/handlers/archive_handler.c
index f973a270..73ce73a3 100644
--- a/handlers/archive_handler.c
+++ b/handlers/archive_handler.c
@@ -244,9 +244,7 @@  static int install_archive_image(struct img_type *img,
 		return -EINVAL;
 	}

-	if ((asprintf(&DATADST_DIR, "%s%s", get_tmpdir(), DATADST_DIR_SUFFIX) ==
-		ENOMEM_ASPRINTF) ||
-		(asprintf(&FIFO, "%s%s", get_tmpdir(), FIFO_FILE_NAME) ==
+	if ((asprintf(&FIFO, "%s%s", get_tmpdir(), FIFO_FILE_NAME) ==
 		ENOMEM_ASPRINTF)) {
 		ERROR("Path too long: %s", get_tmpdir());
 		exitval = -ENOMEM;
@@ -257,8 +255,8 @@  static int install_archive_image(struct img_type *img,
 	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

 	if (use_mount) {
-		ret = swupdate_mount(img->device, DATADST_DIR, img->filesystem);
-		if (ret) {
+		DATADST_DIR = swupdate_temporary_mount(MNT_DATA, img->device, img->filesystem);
+		if (!DATADST_DIR) {
 			ERROR("Device %s with filesystem %s cannot be mounted",
 				img->device, img->filesystem);
 			exitval = -EINVAL;
@@ -380,14 +378,13 @@  out:
 		unlink(FIFO);

 	if (is_mounted) {
-		ret = swupdate_umount(DATADST_DIR);
+		ret = swupdate_temporary_umount(DATADST_DIR);
 		if (ret) {
 			TRACE("Failed to unmount directory %s", DATADST_DIR);
 		}
 	}

 	sync();
-	free(DATADST_DIR);
 	free(FIFO);

 	return exitval;
diff --git a/handlers/btrfs_handler.c b/handlers/btrfs_handler.c
index f00ca759..e7470a7e 100644
--- a/handlers/btrfs_handler.c
+++ b/handlers/btrfs_handler.c
@@ -48,17 +48,14 @@  static int btrfs(struct img_type *img,
 			ERROR("btrfs must be mounted, no device set");
 			return -EINVAL;
 		}
-		globalpath = alloca(strlen(get_tmpdir()) +
-				strlen(DATADST_DIR_SUFFIX) + strlen(subvol_path) + 2);
-		sprintf(globalpath, "%s%s", get_tmpdir(), DATADST_DIR_SUFFIX);
-		mountpoint = strdupa(globalpath);
 		DEBUG("Try to mount %s as BTRFS", mountpoint);
-		ret = swupdate_mount(img->device, mountpoint, "btrfs");
-		if (ret) {
+		mountpoint = swupdate_temporary_mount(MNT_DATA, img->device, "btrfs");
+		if (!mountpoint) {
 			ERROR("%s cannot be mounted with btrfs", img->device);
 			return -1;
 		}
-		globalpath = strcat(globalpath, subvol_path);
+		globalpath = alloca(strlen(mountpoint) + strlen(subvol_path) + 2);
+		globalpath = strcat(mountpoint, subvol_path);
 	} else
 		globalpath = subvol_path;

diff --git a/handlers/delta_handler.c b/handlers/delta_handler.c
index a14181f1..0c782f3d 100644
--- a/handlers/delta_handler.c
+++ b/handlers/delta_handler.c
@@ -940,17 +940,16 @@  static int install_delta(struct img_type *img,
 		char *filesystem = diskformat_fs_detect(priv->srcdev);
 		if (filesystem) {
 			char* DATADST_DIR;
-			if (asprintf(&DATADST_DIR, "%s%s", get_tmpdir(), DATADST_DIR_SUFFIX) != -1)  {
-				if (!swupdate_mount(priv->srcdev, DATADST_DIR, filesystem)) {
+
+			DATADST_DIR = swupdate_temporary_mount(MNT_DATA, priv->srcdev, filesystem);
+			if (DATADST_DIR) {
 					struct statvfs vfs;
 					if (!statvfs(DATADST_DIR, &vfs)) {
 						TRACE("Detected filesystem %s, block size : %lu, %lu blocks =  %lu size",
 						       filesystem, vfs.f_frsize, vfs.f_blocks, vfs.f_frsize * vfs.f_blocks);
 						priv->srcsize = vfs.f_frsize * vfs.f_blocks;
 					}
-					swupdate_umount(DATADST_DIR);
-				}
-				free(DATADST_DIR);
+					swupdate_temporary_umount(DATADST_DIR);
 			}
 			free(filesystem);
 		}
diff --git a/handlers/raw_handler.c b/handlers/raw_handler.c
index 00297622..93aa29e4 100644
--- a/handlers/raw_handler.c
+++ b/handlers/raw_handler.c
@@ -155,8 +155,7 @@  static int install_raw_file(struct img_type *img,
 	int ret = -1;
 	int cleanup_ret = 0;
 	int use_mount = (strlen(img->device) && strlen(img->filesystem)) ? 1 : 0;
-	char* DATADST_DIR = alloca(strlen(get_tmpdir())+strlen(DATADST_DIR_SUFFIX)+1);
-	sprintf(DATADST_DIR, "%s%s", get_tmpdir(), DATADST_DIR_SUFFIX);
+	char* DATADST_DIR = NULL;

 	if (strlen(img->path) == 0) {
 		ERROR("Missing path attribute");
@@ -164,8 +163,8 @@  static int install_raw_file(struct img_type *img,
 	}

 	if (use_mount) {
-		ret = swupdate_mount(img->device, DATADST_DIR, img->filesystem);
-		if (ret) {
+		DATADST_DIR = swupdate_temporary_mount(MNT_DATA, img->device, img->filesystem);
+		if (!DATADST_DIR) {
 			ERROR("Device %s with filesystem %s cannot be mounted: %s",
 				img->device, img->filesystem, strerror(errno));
 			return -1;
@@ -245,7 +244,7 @@  cleanup:
 		close(fdout);

 	if (use_mount) {
-		cleanup_ret = swupdate_umount(DATADST_DIR);
+		cleanup_ret = swupdate_temporary_umount(DATADST_DIR);
 		if (cleanup_ret)
 			WARN("Can't unmount path %s: %s", DATADST_DIR, strerror(errno));
 	}
diff --git a/handlers/rdiff_handler.c b/handlers/rdiff_handler.c
index 3b01519c..e399171f 100644
--- a/handlers/rdiff_handler.c
+++ b/handlers/rdiff_handler.c
@@ -281,10 +281,9 @@  static int apply_rdiff_patch(struct img_type *img,

 		base_file_filename = img->path;
 		if (use_mount) {
-			mountpoint = alloca(strlen(get_tmpdir()) + strlen(DATADST_DIR_SUFFIX) + 1);
-			sprintf(mountpoint, "%s%s", get_tmpdir(), DATADST_DIR_SUFFIX);
+			mountpoint = swupdate_temporary_mount(MNT_DATA, img->device, img->filesystem);

-			if (swupdate_mount(img->device, mountpoint, img->filesystem) != 0) {
+			if (!mountpoint) {
 				ERROR("Device %s with filesystem %s cannot be mounted",
 					  img->device, img->filesystem);
 				ret = -1;
@@ -418,7 +417,7 @@  cleanup:
 			      dest_file_filename, strerror(errno));
 		}
 		if (use_mount == true) {
-			swupdate_umount(mountpoint);
+			swupdate_temporary_umount(mountpoint);
 		}
 	}
 	return ret;