@@ -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,10 @@ out:
unlink(FIFO);
if (is_mounted) {
- ret = swupdate_umount(DATADST_DIR);
- if (ret) {
- TRACE("Failed to unmount directory %s", DATADST_DIR);
- }
+ swupdate_temporary_umount(DATADST_DIR);
}
sync();
- free(DATADST_DIR);
free(FIFO);
return exitval;
@@ -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;
@@ -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);
}
@@ -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,9 +244,10 @@ cleanup:
close(fdout);
if (use_mount) {
- cleanup_ret = swupdate_umount(DATADST_DIR);
- if (cleanup_ret)
- WARN("Can't unmount path %s: %s", DATADST_DIR, strerror(errno));
+ /*
+ * error is already tracked by umount
+ */
+ swupdate_temporary_umount(DATADST_DIR);
}
return (ret ? ret : cleanup_ret);
@@ -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;
@@ -417,8 +416,8 @@ cleanup:
ERROR("Cannot delete temporary file %s, please clean up manually: %s",
dest_file_filename, strerror(errno));
}
- if (use_mount == true) {
- swupdate_umount(mountpoint);
+ if (use_mount == true && mountpoint) {
+ swupdate_temporary_umount(mountpoint);
}
}
return ret;
Replaced duplicated code and use the common functions. Signed-off-by: Stefano Babic <stefano.babic@swupdate.org> --- handlers/archive_handler.c | 14 ++++---------- handlers/btrfs_handler.c | 11 ++++------- handlers/delta_handler.c | 9 ++++----- handlers/raw_handler.c | 14 +++++++------- handlers/rdiff_handler.c | 9 ++++----- 5 files changed, 23 insertions(+), 34 deletions(-) -- 2.34.1