1 #define _POSIX_C_SOURCE 200809L
11 #include "lib/jsw/jsw_atree.h"
13 #define DMARK fprintf(stderr, "mark %s %s:%d\n", __FILE__, __func__, __LINE__)
16 * flags 0 = find installed
17 * flags 1 = skip installed
18 * flags 2 = skip not installed
21 struct zpm_package *zpm_package_alloc(struct zpm *zpm) {
22 struct zpm_package *pkg;
24 pkg = malloc(sizeof *pkg);
32 void zpm_sqlite_error(struct zpm *zpm) {
34 if (zpm->errmsg) free(zpm->errmsg);
35 zpm->errmsg = strdup((const char *)sqlite3_errmsg(zpm->db));
38 char *zpm_findlib(struct zpm *zpm, char *soname, char *where) {
40 char *query, *pkgid = 0;
42 char *select = "select printf('%%s-%%s-%%s',P.package,P.version,P.release) as pkgid, P.package, P.version, P.release\
44 join elflibraries EL on EL.file = PF.hash\
45 join packages P on P.package = PF.package and P.version = PF.version and P.release = PF.release";
46 char *order = "order by P.package, P.version, cast(P.release as integer)";
48 /* null pkgstr find "best" package
49 * best is shortest complete package if any are complete
50 * shortest incomplete if any are incomplete
51 * where more than one version is in those sets, best is
52 * latest as determined by vercmp
55 sql = sqlite3_str_new(zpm->db);
57 sqlite3_str_appendall(sql, select);
58 sqlite3_str_appendf(sql, " where P.hash is not null and EL.soname = %Q", soname);
61 sqlite3_str_appendall(sql, " and ");
62 sqlite3_str_appendall(sql, where);
65 sqlite3_str_appendall(sql, " ");
66 sqlite3_str_appendall(sql, order);
67 sqlite3_str_appendall(sql, " limit 1;");
69 if (sqlite3_str_errcode(sql)) {
71 sqlite3_free(sqlite3_str_finish(sql));
75 query = sqlite3_str_finish(sql);
77 pkgid = zpm_db_string(zpm, query);;
84 int zpm_libraries_needed(struct zpm *zpm, char *pkgid, jsw_atree_t *list) {
85 char *pkglibsneeded = "\
87 select distinct EN.needed as soname, PF.pkgid\
89 join packagefiles_pkgid PF on PF.hash = EN.file\
92 select distinct EL.soname, PF.pkgid\
94 join packagefiles_pkgid PF on PF.hash = EL.file\
96 select distinct PL.soname, PP.soname is not null as selfsatisfied\
98 left join pkgprovides PP on PL.pkgid = PP.pkgid and PL.soname = PP.soname\
106 st = zpm_dbquery(zpm, pkglibsneeded, pkgid);
108 while ((rv = sqlite3_step(st)) == SQLITE_ROW) {
112 soname = (char *)sqlite3_column_text(st, 0);
113 /* TODO check and skip self sats */
114 self = sqlite3_column_int(st,1);
120 /* if it's in needed, we've already looked at this one */
121 if (list && !jsw_afind(list, soname)) {
122 jsw_ainsert(list, soname);
126 sqlite3_finalize(st);
130 char *zpm_findpkg(struct zpm *zpm, char *pkgstr, char *where) {
131 char *select = "select pkgid, package, version, release from packages_pkgid";
133 char *group = "group by package having max( version||'-'||release collate vercmp) order by length(package), package, version||'-'||release collate vercmp";
135 char *group = "order by package, version collate vercmp desc, cast(release as integer)";
136 //group = "order by package, version , cast(release as integer)";
139 char *query, *pkgid = 0;
141 /* null pkgstr find "best" package
142 * best is shortest complete package if any are complete
143 * shortest incomplete if any are incomplete
144 * where more than one version is in those sets, best is
145 * latest as determined by vercmp
148 sql = sqlite3_str_new(zpm->db);
150 sqlite3_str_appendall(sql, select);
151 sqlite3_str_appendall(sql, " where true");
158 zpm_parse_package(pkgstr, package, version, &release);
160 sqlite3_str_appendf(sql, " and package = %Q",
164 sqlite3_str_appendf(sql, " and version = %Q",
168 sqlite3_str_appendf(sql, " and release = %d",
174 sqlite3_str_appendall(sql, " and ");
175 sqlite3_str_appendall(sql, where);
179 sqlite3_str_appendall(sql, " ");
180 sqlite3_str_appendall(sql, group);
181 sqlite3_str_appendall(sql, " limit 1;");
183 if (sqlite3_str_errcode(sql)) {
185 sqlite3_free(sqlite3_str_finish(sql));
189 query = sqlite3_str_finish(sql);
191 pkgid = zpm_db_string(zpm, query);;