]> git.ipfire.org Git - pakfire.git/commitdiff
repos: Try to hardlink packages when possible
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 9 Mar 2023 13:33:12 +0000 (13:33 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 9 Mar 2023 13:44:41 +0000 (13:44 +0000)
Since we no longer change any packages when composing a repository (no
embedded signatures), we can try to hardlink to save disk space and IO.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/archive.c
src/libpakfire/include/pakfire/archive.h

index db9d3ace42ea8265242e43aebf8708f1103a1b92..8ecde7c9018d59d706a454396d66273c3b48ff71 100644 (file)
@@ -850,6 +850,49 @@ ERROR:
        return r;
 }
 
+static int pakfire_archive_link(struct pakfire_archive* archive, const char* path) {
+       int r;
+
+       // Check if path is set
+       if (!path) {
+               errno = EINVAL;
+               return 1;
+       }
+
+       DEBUG(archive->pakfire, "Linking %s to %s...\n", archive->path, path);
+
+       // Delete the destination file (if it exists)
+       unlink(path);
+
+       // Create the new link
+       r = link(archive->path, path);
+       if (r) {
+               DEBUG(archive->pakfire, "Could not create hardlink %s: %m\n", path);
+               return r;
+       }
+
+       return 0;
+}
+
+int pakfire_archive_link_or_copy(struct pakfire_archive* archive, const char* path) {
+       int r;
+
+       // Try to create a hardlink
+       r = pakfire_archive_link(archive, path);
+       if (r) {
+               switch (errno) {
+                       // Try to copy the file if we could not create a hardlink
+                       case EPERM:
+                               r = pakfire_archive_copy(archive, path);
+
+                       default:
+                               break;
+               }
+       }
+
+       return r;
+}
+
 static int __pakfire_archive_extract(struct pakfire_archive* archive, int flags) {
        struct pakfire_filelist* filelist = NULL;
        struct pakfire_package* pkg = NULL;
index 44eb04dcef8e274d5cd96d8cd6e5196827014277..811f78274db030d429dfb746040dfec1b3c97315 100644 (file)
@@ -58,6 +58,7 @@ int pakfire_archive_make_package(struct pakfire_archive* archive,
 #include <pakfire/pakfire.h>
 
 int pakfire_archive_copy(struct pakfire_archive* archive, const char* path);
+int pakfire_archive_link_or_copy(struct pakfire_archive* archive, const char* path);
 
 int pakfire_archive_check_digest(struct pakfire_archive* archive,
        const enum pakfire_digest_types type, const unsigned char* digest, const size_t length);