]> git.ipfire.org Git - pakfire.git/commitdiff
libpakfire: Parse dependency data from packages
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 Jun 2019 14:35:23 +0000 (15:35 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 Jun 2019 14:35:23 +0000 (15:35 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/archive.c
src/libpakfire/include/pakfire/relation.h
src/libpakfire/include/pakfire/relationlist.h
src/libpakfire/include/pakfire/util.h
src/libpakfire/libpakfire.sym
src/libpakfire/package.c
src/libpakfire/relation.c
src/libpakfire/relationlist.c
src/libpakfire/util.c

index 81d01c0c29ac1988b6daea56f99a55cd96e97469..f233e7a5f05b782b42453eb114085d7abc1e14d5 100644 (file)
@@ -1207,5 +1207,57 @@ PAKFIRE_EXPORT PakfirePackage pakfire_archive_make_package(PakfireArchive archiv
                pakfire_package_set_buildtime(pkg, t);
        }
 
+       // Relations
+
+       PakfireRelationList l;
+
+       char* prerequires = pakfire_archive_get(archive, "dependencies.prerequires");
+       if (prerequires) {
+               l = pakfire_relationlist_create_from_string(archive->pakfire, prerequires);
+               pakfire_package_set_prerequires(pkg, l);
+               pakfire_relationlist_unref(l);
+               pakfire_free(prerequires);
+       }
+
+       char* requires = pakfire_archive_get(archive, "dependencies.requires");
+       if (requires) {
+               l = pakfire_relationlist_create_from_string(archive->pakfire, requires);
+               pakfire_package_set_requires(pkg, l);
+               pakfire_relationlist_unref(l);
+               pakfire_free(requires);
+       }
+
+       char* provides = pakfire_archive_get(archive, "dependencies.provides");
+       if (provides) {
+               l = pakfire_relationlist_create_from_string(archive->pakfire, provides);
+               pakfire_package_set_provides(pkg, l);
+               pakfire_relationlist_unref(l);
+               pakfire_free(provides);
+       }
+
+       char* obsoletes = pakfire_archive_get(archive, "dependencies.obsoletes");
+       if (obsoletes) {
+               l = pakfire_relationlist_create_from_string(archive->pakfire, obsoletes);
+               pakfire_package_set_obsoletes(pkg, l);
+               pakfire_relationlist_unref(l);
+               pakfire_free(obsoletes);
+       }
+
+       char* recommends = pakfire_archive_get(archive, "dependencies.recommends");
+       if (recommends) {
+               l = pakfire_relationlist_create_from_string(archive->pakfire, recommends);
+               pakfire_package_set_recommends(pkg, l);
+               pakfire_relationlist_unref(l);
+               pakfire_free(recommends);
+       }
+
+       char* suggests = pakfire_archive_get(archive, "dependencies.suggests");
+       if (suggests) {
+               l = pakfire_relationlist_create_from_string(archive->pakfire, suggests);
+               pakfire_package_set_suggests(pkg, l);
+               pakfire_relationlist_unref(l);
+               pakfire_free(suggests);
+       }
+
        return pkg;
 }
index 1e6b7b258b9ae0148eb9e65ed7625b5d6f7f0b18..d76bcf4cd7b20ac29e7715647dea054da02026f3 100644 (file)
@@ -29,6 +29,7 @@
 
 PakfireRelation pakfire_relation_create(Pakfire pakfire, const char* name, int cmp_type, const char* evr);
 PakfireRelation pakfire_relation_create_from_id(Pakfire pakfire, Id id);
+PakfireRelation pakfire_relation_create_from_string(Pakfire pakfire, const char* s);
 
 PakfireRelation pakfire_relation_ref(PakfireRelation relation);
 PakfireRelation pakfire_relation_unref(PakfireRelation relation);
index 5788411b799d357e928cb085874de2620e2f30cc..f6f1e728852e27d4bba653bfcda01fd00469e1bc 100644 (file)
@@ -24,6 +24,7 @@
 #include <pakfire/types.h>
 
 PakfireRelationList pakfire_relationlist_create(Pakfire pakfire);
+PakfireRelationList pakfire_relationlist_create_from_string(Pakfire pakfire, const char* s);
 
 PakfireRelationList pakfire_relationlist_ref(PakfireRelationList relationlist);
 PakfireRelationList pakfire_relationlist_unref(PakfireRelationList relationlist);
index c100d3af01259d0788fe16560ffc00f6b273e17f..3447d5e5f48db06a8b209ca01c28bdea9b66b2ca 100644 (file)
@@ -60,5 +60,6 @@ int pakfire_read_file_into_buffer(FILE* f, char** buffer, size_t* len);
 
 size_t pakfire_string_to_size(const char* s);
 char** pakfire_split_string(const char* s, char delim);
+void pakfire_partition_string(const char* s, const char* delim, char** s1, char** s2);
 
 #endif /* PAKFIRE_UTIL_H */
index d2d0aa655c2c5bd33feb4d512efde6999a53b76d..e882f75cb4efbf3f265b2d850b0a34cccc9013e2 100644 (file)
@@ -285,6 +285,7 @@ global:
        pakfire_relationlist_add;
        pakfire_relationlist_count;
        pakfire_relationlist_create;
+       pakfire_relationlist_create_from_string;
        pakfire_relationlist_get_clone;
        pakfire_relationlist_ref;
        pakfire_relationlist_unref;
@@ -353,6 +354,7 @@ global:
        pakfire_access;
        pakfire_free;
        pakfire_get_errno;
+       pakfire_partition_string;
        pakfire_path_join;
        pakfire_read_file_into_buffer;
        pakfire_split_string;
index 562c427863325bd26bcf79f0c1d35e9a8875f46d..f2c3b53610cdf65e6ea2d5247acd25899ba2e8b2 100644 (file)
@@ -575,6 +575,10 @@ PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_prerequires(PakfirePackag
        return NULL;
 }
 
+PAKFIRE_EXPORT void pakfire_package_set_prerequires(PakfirePackage pkg, PakfireRelationList relationlist) {
+       #warning TODO
+}
+
 PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_requires(PakfirePackage pkg) {
        return pakfire_package_get_relationlist(pkg, SOLVABLE_REQUIRES);
 }
index 7b71c2113ddca2b3ca89875ac0ba846b72d29d6f..5ca6310b26063aae5e023491924b58c182e33ee4 100644 (file)
@@ -38,6 +38,29 @@ struct _PakfireRelation {
        int nrefs;
 };
 
+const char* delimiters[] = {
+       ">=", ">", "<=", "<", "=", NULL,
+};
+
+static int cmp2type(const char* s) {
+       if (strcmp(s, ">=") == 0)
+               return PAKFIRE_GE;
+
+       if (strcmp(s, ">") == 0)
+               return PAKFIRE_GT;
+
+       if (strcmp(s, "<=") == 0)
+               return PAKFIRE_LE;
+
+       if (strcmp(s, "<") == 0)
+               return PAKFIRE_LT;
+
+       if (strcmp(s, "=") == 0)
+               return PAKFIRE_EQ;
+
+       return 0;
+}
+
 static int cmptype2relflags(int type) {
        int flags = 0;
 
@@ -83,6 +106,36 @@ PAKFIRE_EXPORT PakfireRelation pakfire_relation_create_from_id(Pakfire pakfire,
        return relation;
 }
 
+PAKFIRE_EXPORT PakfireRelation pakfire_relation_create_from_string(Pakfire pakfire, const char* s) {
+       DEBUG(pakfire, "Parsing relation from string: %s\n", s);
+
+       char* name;
+       char* evr;
+
+       const char** delim = delimiters;
+       while (*delim) {
+               pakfire_partition_string(s, *delim, &name, &evr);
+
+               // Nothing to do for no match
+               if (!name && !evr) {
+                       delim++;
+                       continue;
+               }
+
+               // Create new relation object
+               PakfireRelation rel = pakfire_relation_create(pakfire, name, cmp2type(*delim), evr);
+
+               // Cleanup
+               pakfire_free(name);
+               pakfire_free(evr);
+
+               return rel;
+       }
+
+       // No delimiter was found
+       return pakfire_relation_create(pakfire, s, 0, NULL);
+}
+
 PAKFIRE_EXPORT PakfireRelation pakfire_relation_ref(PakfireRelation relation) {
        relation->nrefs++;
 
index 1a43d4e47e3169e1a21fd68e6d773bc91aa09c15..65618ea3ec01393d48488106ec41707b63e639d5 100644 (file)
@@ -48,6 +48,33 @@ PAKFIRE_EXPORT PakfireRelationList pakfire_relationlist_create(Pakfire pakfire)
        return relationlist;
 }
 
+PAKFIRE_EXPORT PakfireRelationList pakfire_relationlist_create_from_string(Pakfire pakfire, const char* s) {
+       PakfireRelationList relationlist = pakfire_relationlist_create(pakfire);
+       if (!relationlist)
+               return NULL;
+
+       // Split input by newline
+       char** list = pakfire_split_string(s, '\n');
+       if (!list)
+               return relationlist;
+
+       char** item = list;
+       while (*item) {
+               PakfireRelation rel = pakfire_relation_create_from_string(relationlist->pakfire, *item);
+               if (rel) {
+                       pakfire_relationlist_add(relationlist, rel);
+                       pakfire_relation_unref(rel);
+               }
+
+               //pakfire_free(*item);
+               item++;
+       }
+
+       pakfire_free(list);
+
+       return relationlist;
+}
+
 PAKFIRE_EXPORT PakfireRelationList pakfire_relationlist_ref(PakfireRelationList relationlist) {
        relationlist->nrefs++;
 
index db665ff7c992f02833fa07c0897ec4574eb2eefa..529d0cb10c14c533ebaa582315a98a16303743e1 100644 (file)
@@ -361,3 +361,23 @@ PAKFIRE_EXPORT char** pakfire_split_string(const char* s, char delim) {
 
        return ret;
 }
+
+PAKFIRE_EXPORT void pakfire_partition_string(const char* s, const char* delim, char** s1, char** s2) {
+       char* p = strstr(s, delim);
+
+       // Delim was not found
+       if (!p) {
+               *s1 = NULL;
+               *s2 = NULL;
+               return;
+       }
+
+       // Length of string before delim
+       size_t l = p - s;
+       printf("%zu\n", l);
+
+       *s1 = pakfire_malloc(l);
+       snprintf(*s1, l, "%s", s);
+
+       *s2 = pakfire_strdup(p + strlen(delim));
+}