]> git.ipfire.org Git - pakfire.git/commitdiff
packager: Remove files after packaging them
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 3 Jun 2021 14:11:43 +0000 (14:11 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 3 Jun 2021 14:11:43 +0000 (14:11 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/build.c
src/libpakfire/file.c
src/libpakfire/include/pakfire/file.h

index 56bdf4353b5b1e6bc3a3465102e0de8fd9fd49ef..f269120228768c7e1968b3c45899de8c463316fb 100644 (file)
@@ -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);
        }
 
index 5670de1d7c616ad04f119bcbe68f0b9af1187a75..4dbc316ea8a294fb8a004c31da694919a0fa4f8a 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <errno.h>
+#include <libgen.h>
 #include <linux/limits.h>
 #include <stdlib.h>
 #include <string.h>
@@ -29,6 +30,7 @@
 
 #include <pakfire/constants.h>
 #include <pakfire/file.h>
+#include <pakfire/logging.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/private.h>
 #include <pakfire/util.h>
@@ -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;
+}
index 200b8b3bdef5b6758bfcaec9fc8f1f4a143584a8..3dab78e3230fd22e7fbf70bd2be9554942cb1da6 100644 (file)
@@ -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 */