]> git.ipfire.org Git - pakfire.git/commitdiff
dist: Add source files to packages
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 10 Mar 2021 14:57:30 +0000 (14:57 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 10 Mar 2021 14:57:30 +0000 (14:57 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/dist.c

index 0059aec234fcbd4d84a554fd94ec6aa5ee3008fe..38b97a6748410f7e5e62ecb34f4937afd6a48a5e 100644 (file)
 #include <pakfire/types.h>
 #include <pakfire/util.h>
 
+static int pakfire_dist_download_source(Pakfire pakfire, const char* filename,
+               const char* cache_path) {
+       DEBUG(pakfire, "Downloading %s...\n", filename);
+
+       return 1;
+}
+
+static int pakfire_dist_add_source(Pakfire pakfire, struct pakfire_packager* packager,
+               PakfirePackage pkg, const char* filename) {
+       int r;
+       char archive_path[PATH_MAX];
+       char path[PATH_MAX];
+
+       const char* name = pakfire_package_get_name(pkg);
+
+       snprintf(path, sizeof(path) - 1, "sources/%s/%s", name, filename);
+       snprintf(archive_path, sizeof(archive_path) - 1, "files/%s", filename);
+
+       // Download the file if it does not exist in the cache
+       if (pakfire_cache_access(pakfire, path, R_OK)) {
+               r = pakfire_dist_download_source(pakfire, filename, path);
+               if (r)
+                       return r;
+       }
+
+       // Make path in cache
+       char* cache_abspath = pakfire_get_cache_path(pakfire, path);
+       if (!cache_abspath)
+               return 1;
+
+       // Add file to package
+       r = pakfire_packager_add(packager, cache_abspath, archive_path);
+       if (r)
+               goto ERROR;
+
+       // Success
+       r = 0;
+
+ERROR:
+       free(cache_abspath);
+
+       return r;
+}
+
+static int pakfire_dist_add_sources(Pakfire pakfire, struct pakfire_packager* packager,
+               PakfirePackage pkg, PakfireParser makefile) {
+       int r;
+
+       char* sources = pakfire_parser_get(makefile, NULL, "sources");
+       if (!sources)
+               return 1;
+
+       // Nothing to do if there are no sources
+       if (!*sources) {
+               free(sources);
+               return 0;
+       }
+
+       // Split sources by space
+       char** sourceslist = pakfire_split_string(sources, ' ');
+       if (!sourceslist) {
+               ERROR(pakfire, "Could not split sources: %s\n", strerror(errno));
+               r = 1;
+               goto ERROR;
+       }
+
+       // Add all files one by one
+       for (char** source = sourceslist; *source; source++) {
+               DEBUG(pakfire, "Adding source file %s\n", *source);
+
+               r = pakfire_dist_add_source(pakfire, packager, pkg, *source);
+               if (r) {
+                       ERROR(pakfire, "Could not add '%s' to package: %s\n", *source, strerror(errno));
+                       goto ERROR;
+               }
+       }
+
+       // Success
+       r = 0;
+
+ERROR:
+       if (sourceslist) {
+               for (char** source = sourceslist; *source; source++)
+                       free(*source);
+               free(sourceslist);
+       }
+
+       return r;
+}
+
 static int pakfire_dist_add_files(Pakfire pakfire, struct pakfire_packager* packager,
                const char* path) {
        int r = 1;
@@ -139,6 +229,11 @@ PAKFIRE_EXPORT int pakfire_dist(Pakfire pakfire, const char* path, const char* t
        if (r)
                goto ERROR;
 
+       // Add all source files (which might need to be downloaded)
+       r = pakfire_dist_add_sources(pakfire, packager, pkg, makefile);
+       if (r)
+               goto ERROR;
+
        snprintf(tempfile, PATH_MAX - 1, "%s/.pakfire-dist.XXXXXX", target);
 
        // Create a temporary result file