From 8cb06cda6a801521db2d672a01bb8817124f0329 Mon Sep 17 00:00:00 2001 From: Ivan Shapovalov Date: Sun, 23 Jul 2023 20:28:21 +0400 Subject: [PATCH 1/3] Do not remove the target directory on --force-clean; only clear it If `--force-clean` is passed, clear (remove the contents of) the target (app) directory without deleting the directory itself. This is useful if the app directory is a symlink, e. g. to a faster disk or a scratch location. Submodule subprojects/libglnx 07e3e49d..1a503dcc: > shutil: add glnx_shutil_rm_rf_children_at() > shutil: specify symlink behavior of glnx_shutil_rm_rf_at(), add tests > tests: add a macro to check for positive errno --- src/builder-flatpak-utils.c | 10 ++++++++++ src/builder-flatpak-utils.h | 4 ++++ src/builder-main.c | 2 +- subprojects/libglnx | 2 +- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/builder-flatpak-utils.c b/src/builder-flatpak-utils.c index ef45e89d..e270bdbf 100644 --- a/src/builder-flatpak-utils.c +++ b/src/builder-flatpak-utils.c @@ -980,6 +980,16 @@ flatpak_rm_rf (GFile *dir, cancellable, error); } +gboolean +flatpak_rm_rf_children (GFile *dir, + GCancellable *cancellable, + GError **error) +{ + return glnx_shutil_rm_rf_children_at (AT_FDCWD, + flatpak_file_get_path_cached (dir), + cancellable, error); +} + gboolean flatpak_file_rename (GFile *from, GFile *to, GCancellable *cancellable, diff --git a/src/builder-flatpak-utils.h b/src/builder-flatpak-utils.h index eba6d715..a12823ed 100644 --- a/src/builder-flatpak-utils.h +++ b/src/builder-flatpak-utils.h @@ -237,6 +237,10 @@ gboolean flatpak_rm_rf (GFile *dir, GCancellable *cancellable, GError **error); +gboolean flatpak_rm_rf_children (GFile *dir, + GCancellable *cancellable, + GError **error); + static inline void flatpak_temp_dir_destroy (void *p) { diff --git a/src/builder-main.c b/src/builder-main.c index 42f4784e..2f26b7e0 100644 --- a/src/builder-main.c +++ b/src/builder-main.c @@ -782,7 +782,7 @@ main (int argc, if (opt_force_clean) { g_print ("Emptying app dir '%s'\n", app_dir_path); - if (!flatpak_rm_rf (app_dir, NULL, &error)) + if (!flatpak_rm_rf_children (app_dir, NULL, &error)) { g_printerr ("Couldn't empty app dir '%s': %s\n", app_dir_path, error->message); diff --git a/subprojects/libglnx b/subprojects/libglnx index 07e3e49d..1a503dcc 160000 --- a/subprojects/libglnx +++ b/subprojects/libglnx @@ -1 +1 @@ -Subproject commit 07e3e49d3e47dfd4265ffb5495111439131715ca +Subproject commit 1a503dcc07d57a8b7112d7991501595794397a10 From 471ebadc6e794a57b1ec09268b940732636d512b Mon Sep 17 00:00:00 2001 From: Ivan Shapovalov Date: Sun, 23 Jul 2023 20:32:59 +0400 Subject: [PATCH 2/3] Fix same-filesystem check on the state and app directories We only care about the app (== target) directory, not its parent. This breaks if the target directory is a symlink, like this: ``` $ pwd /path/to/code $ realpath state /path/to/scratch/state $ realpath build /path/to/scratch/target $ flatpak-builder --state-dir=state build org.some.App.yaml The state dir (/path/to/scratch/state) is not on the same filesystem as the target dir (/path/to/code) ``` --- src/builder-main.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/builder-main.c b/src/builder-main.c index 2f26b7e0..1fc7e133 100644 --- a/src/builder-main.c +++ b/src/builder-main.c @@ -802,15 +802,14 @@ main (int argc, if (!opt_download_only) { g_autofree char *state_path = g_file_get_path (builder_context_get_state_dir (build_context)); - g_autoptr(GFile) app_parent = g_file_get_parent (builder_context_get_app_dir (build_context)); - g_autofree char *app_parent_path = g_file_get_path (app_parent); + g_autofree char *app_path = g_file_get_path (builder_context_get_app_dir (build_context)); struct stat buf1, buf2; - if (stat (app_parent_path, &buf1) == 0 && stat (state_path, &buf2) == 0 && + if (stat (app_path, &buf1) == 0 && stat (state_path, &buf2) == 0 && buf1.st_dev != buf2.st_dev) { g_printerr ("The state dir (%s) is not on the same filesystem as the target dir (%s)\n", - state_path, app_parent_path); + state_path, app_path); return 1; } } From 15cfe113403156ab551062014302899c247ef665 Mon Sep 17 00:00:00 2001 From: Ivan Shapovalov Date: Mon, 24 Jul 2023 03:22:27 +0400 Subject: [PATCH 3/3] Do not remove the target directory in builder_cache_checkout(); only clear it --- src/builder-cache.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/builder-cache.c b/src/builder-cache.c index 1cec86fa..7f09f161 100644 --- a/src/builder-cache.c +++ b/src/builder-cache.c @@ -309,7 +309,7 @@ builder_cache_checkout (BuilderCache *self, const char *commit, gboolean delete_ if (delete_dir) { - if (!g_file_delete (self->app_dir, NULL, &my_error) && + if (!flatpak_rm_rf_children (self->app_dir, NULL, &my_error) && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) { g_propagate_error (error, g_steal_pointer (&my_error));