From: Michael Tremer Date: Thu, 3 Jun 2021 14:11:43 +0000 (+0000) Subject: packager: Remove files after packaging them X-Git-Tag: 0.9.28~1285^2~37 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3fca50322e0201857610ba79bfa402b897d41d54;p=pakfire.git packager: Remove files after packaging them Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/build.c b/src/libpakfire/build.c index 56bdf4353..f26912022 100644 --- a/src/libpakfire/build.c +++ b/src/libpakfire/build.c @@ -279,6 +279,13 @@ static int pakfire_build_package_add_files(Pakfire pakfire, PakfireParser makefi goto ERROR; } + // Remove the file after it was packaged + r = pakfire_file_cleanup(file); + if (r) { + pakfire_file_unref(file); + goto ERROR; + } + pakfire_file_unref(file); } diff --git a/src/libpakfire/file.c b/src/libpakfire/file.c index 5670de1d7..4dbc316ea 100644 --- a/src/libpakfire/file.c +++ b/src/libpakfire/file.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include #include #include @@ -29,6 +30,7 @@ #include #include +#include #include #include #include @@ -256,3 +258,71 @@ PAKFIRE_EXPORT void pakfire_file_set_chksum(PakfireFile file, const char* chksum if (chksum) file->chksum = strdup(chksum); } + +static int pakfire_file_levels(PakfireFile file) { + if (!*file->path) + return 0; + + int levels = 0; + + for (char* p = file->path; *p; p++) { + if (*p == '/') + levels++; + } + + return levels; +} + +/* + This function tries to remove the file after it has been packaged. + + It will try to delete any parent directories as well and ignore if directories + cannot be deleted because they might contain other files +*/ +int pakfire_file_cleanup(PakfireFile file) { + char path[PATH_MAX]; + + if (!*file->abspath) { + errno = EINVAL; + return 1; + } + + DEBUG(file->pakfire, "Removing %s...\n", file->path); + + int r = remove(file->abspath); + if (r) { + // Ignore when we could not remove directories + if (errno == ENOTEMPTY) + return 0; + + ERROR(file->pakfire, "Could not remove %s (%s): %s\n", + file->path, file->abspath, strerror(errno)); + } + + // Create a working copy of abspath + r = pakfire_string_set(path, file->abspath); + if (r < 0) + return r; + + // See how many levels this file has + int levels = pakfire_file_levels(file); + + // Walk all the way up and remove all parent directories if possible + while (--levels) { + dirname(path); + + // Break if path is suddenly empty + if (!*path) + break; + + r = rmdir(path); + if (r) { + if (errno == ENOTEMPTY) + return 0; + + return r; + } + } + + return r; +} diff --git a/src/libpakfire/include/pakfire/file.h b/src/libpakfire/include/pakfire/file.h index 200b8b3bd..3dab78e32 100644 --- a/src/libpakfire/include/pakfire/file.h +++ b/src/libpakfire/include/pakfire/file.h @@ -73,6 +73,8 @@ int pakfire_file_copy_archive_entry(PakfireFile file, struct archive_entry* entr const char* pakfire_file_get_abspath(PakfireFile file); int pakfire_file_set_abspath(PakfireFile file, const char* path); +int pakfire_file_cleanup(PakfireFile file); + #endif #endif /* PAKFIRE_FILE_H */