From: Michael Tremer Date: Thu, 9 Mar 2023 13:33:12 +0000 (+0000) Subject: repos: Try to hardlink packages when possible X-Git-Tag: 0.9.29~347 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dd8866ccd3aa079c89dc248affde76b2890374ea;p=pakfire.git repos: Try to hardlink packages when possible 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 --- diff --git a/src/libpakfire/archive.c b/src/libpakfire/archive.c index db9d3ace4..8ecde7c90 100644 --- a/src/libpakfire/archive.c +++ b/src/libpakfire/archive.c @@ -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; diff --git a/src/libpakfire/include/pakfire/archive.h b/src/libpakfire/include/pakfire/archive.h index 44eb04dce..811f78274 100644 --- a/src/libpakfire/include/pakfire/archive.h +++ b/src/libpakfire/include/pakfire/archive.h @@ -58,6 +58,7 @@ int pakfire_archive_make_package(struct pakfire_archive* archive, #include 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);