From 54b3bbc00f354a929f277405d6cf7fd9d5e088ae Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Wed, 11 Apr 2018 13:30:32 -0400 Subject: [PATCH] sysroot: Split out a helper function to delete a deployment dir Prep for staged deployments. Closes: #1535 Approved by: jlebon --- src/libostree/ostree-sysroot-cleanup.c | 70 +++++++++++++++----------- src/libostree/ostree-sysroot-private.h | 6 +++ 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/libostree/ostree-sysroot-cleanup.c b/src/libostree/ostree-sysroot-cleanup.c index 2e7cd44b..1d46222b 100644 --- a/src/libostree/ostree-sysroot-cleanup.c +++ b/src/libostree/ostree-sysroot-cleanup.c @@ -239,6 +239,44 @@ cleanup_other_bootversions (OstreeSysroot *self, return TRUE; } +/* Delete a deployment directory */ +gboolean +_ostree_sysroot_rmrf_deployment (OstreeSysroot *self, + OstreeDeployment *deployment, + GCancellable *cancellable, + GError **error) +{ + g_autofree char *origin_relpath = ostree_deployment_get_origin_relpath (deployment); + g_autofree char *deployment_path = ostree_sysroot_get_deployment_dirpath (self, deployment); + struct stat stbuf; + glnx_autofd int deployment_fd = -1; + + if (!glnx_opendirat (self->sysroot_fd, deployment_path, TRUE, + &deployment_fd, error)) + return FALSE; + + if (!glnx_fstat (deployment_fd, &stbuf, error)) + return FALSE; + + /* This shouldn't happen, because higher levels should + * disallow having the booted deployment not in the active + * deployment list, but let's be extra safe. */ + if (stbuf.st_dev == self->root_device && + stbuf.st_ino == self->root_inode) + return TRUE; + + /* This deployment wasn't referenced, so delete it */ + if (!_ostree_linuxfs_fd_alter_immutable_flag (deployment_fd, FALSE, + cancellable, error)) + return FALSE; + if (!glnx_shutil_rm_rf_at (self->sysroot_fd, origin_relpath, cancellable, error)) + return FALSE; + if (!glnx_shutil_rm_rf_at (self->sysroot_fd, deployment_path, cancellable, error)) + return FALSE; + + return TRUE; +} + /* As the bootloader configuration changes, we will have leftover deployments * on disk. This function deletes all deployments which aren't actively * referenced. @@ -279,36 +317,12 @@ cleanup_old_deployments (OstreeSysroot *self, { OstreeDeployment *deployment = all_deployment_dirs->pdata[i]; g_autofree char *deployment_path = ostree_sysroot_get_deployment_dirpath (self, deployment); - g_autofree char *origin_relpath = ostree_deployment_get_origin_relpath (deployment); - if (!g_hash_table_lookup (active_deployment_dirs, deployment_path)) - { - struct stat stbuf; - glnx_autofd int deployment_fd = -1; + if (g_hash_table_lookup (active_deployment_dirs, deployment_path)) + continue; - if (!glnx_opendirat (self->sysroot_fd, deployment_path, TRUE, - &deployment_fd, error)) - return FALSE; - - if (!glnx_fstat (deployment_fd, &stbuf, error)) - return FALSE; - - /* This shouldn't happen, because higher levels should - * disallow having the booted deployment not in the active - * deployment list, but let's be extra safe. */ - if (stbuf.st_dev == root_stbuf.st_dev && - stbuf.st_ino == root_stbuf.st_ino) - continue; - - /* This deployment wasn't referenced, so delete it */ - if (!_ostree_linuxfs_fd_alter_immutable_flag (deployment_fd, FALSE, - cancellable, error)) - return FALSE; - if (!glnx_shutil_rm_rf_at (self->sysroot_fd, origin_relpath, cancellable, error)) - return FALSE; - if (!glnx_shutil_rm_rf_at (self->sysroot_fd, deployment_path, cancellable, error)) - return FALSE; - } + if (!_ostree_sysroot_rmrf_deployment (self, deployment, cancellable, error)) + return FALSE; } /* Clean up boot directories */ diff --git a/src/libostree/ostree-sysroot-private.h b/src/libostree/ostree-sysroot-private.h index b2776e8d..01b370e8 100644 --- a/src/libostree/ostree-sysroot-private.h +++ b/src/libostree/ostree-sysroot-private.h @@ -112,6 +112,12 @@ _ostree_sysroot_get_origin_relpath (GFile *path, GCancellable *cancellable, GError **error); +gboolean +_ostree_sysroot_rmrf_deployment (OstreeSysroot *sysroot, + OstreeDeployment *deployment, + GCancellable *cancellable, + GError **error); + char *_ostree_sysroot_join_lines (GPtrArray *lines); gboolean _ostree_sysroot_query_bootloader (OstreeSysroot *sysroot,