From 2707361aaad8717c392702bf94a07e56cd81ba80 Mon Sep 17 00:00:00 2001 From: Silvan Calarco Date: Fri, 25 Oct 2013 15:10:18 +0200 Subject: [PATCH] distroquery: implement specialQueryResponse function starting with %latest% special command --- src/distroquery.c | 112 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 97 insertions(+), 15 deletions(-) diff --git a/src/distroquery.c b/src/distroquery.c index 8dc4b6b..03b172c 100644 --- a/src/distroquery.c +++ b/src/distroquery.c @@ -130,6 +130,7 @@ char *url_encode(char *str) { /* Returns a url-decoded version of str */ /* IMPORTANT: be sure to free() the returned string after use */ char *url_decode(char *str) { + if (!str) { char *buf=malloc(1); buf[0]=0; return buf; } char *pstr = str, *buf = malloc(strlen(str) + 1), *pbuf = buf; while (*pstr) { if (*pstr == '%') { @@ -236,15 +237,18 @@ void attachCtDatabases(struct configTag* ct, sqlite3 *db, char* arch) { char *errmsg; while (ct->repository[i]) { - snprintf(dbname, PATH_MAX, "%s%s-%s.db", ct->repository[i]->repository_dir, ct->repository[i]->tag, arch); - snprintf(sql, PATH_MAX, "ATTACH DATABASE '%s' as '%s'", dbname, ct->repository[i]->tag); - if (sqlite3_exec(db, sql, NULL, NULL, &errmsg)) { - fprintf(stderr, "ERROR: unable to exec statement for %s: %s\n", sql, errmsg); - } - snprintf(dbname, PATH_MAX, "%s%s-sources.db", ct->repository[i]->repository_dir, ct->repository[i]->tag); - snprintf(sql, PATH_MAX, "ATTACH DATABASE '%s' as '%s_sources'", dbname, ct->repository[i]->tag); - if (sqlite3_exec(db, sql, NULL, NULL, &errmsg)) { - fprintf(stderr, "ERROR: unable to exec statement for %s: %s\n", sql, errmsg); + if (arch) { + snprintf(dbname, PATH_MAX, "%s%s-%s.db", ct->repository[i]->repository_dir, ct->repository[i]->tag, arch); + snprintf(sql, PATH_MAX, "ATTACH DATABASE '%s' as '%s'", dbname, ct->repository[i]->tag); + if (sqlite3_exec(db, sql, NULL, NULL, &errmsg)) { + fprintf(stderr, "ERROR: unable to exec statement for %s: %s\n", sql, errmsg); + } + } else { + snprintf(dbname, PATH_MAX, "%s%s-sources.db", ct->repository[i]->repository_dir, ct->repository[i]->tag); + snprintf(sql, PATH_MAX, "ATTACH DATABASE '%s' as '%s_sources'", dbname, ct->repository[i]->tag); + if (sqlite3_exec(db, sql, NULL, NULL, &errmsg)) { + fprintf(stderr, "ERROR: unable to exec statement for %s: %s\n", sql, errmsg); + } } i++; } @@ -260,13 +264,11 @@ void attachRepositoryDatabases(struct configTag* ct, sqlite3 *db, char* arch) { if (sqlite3_exec(db, sql, NULL, NULL, &errmsg)) { fprintf(stderr, "ERROR: unable to exec statement for %s: %s\n", sql, errmsg); } -fprintf(stderr,"ATTACHED DATABASE %s\n", ct->tag); snprintf(dbname, PATH_MAX, "%s%s-sources.db", ct->repository_dir, ct->tag); snprintf(sql, PATH_MAX, "ATTACH DATABASE '%s' as '%s_sources'", dbname, ct->tag); if (sqlite3_exec(db, sql, NULL, NULL, &errmsg)) { fprintf(stderr, "ERROR: unable to exec statement for %s: %s\n", sql, errmsg); } -fprintf(stderr,"ATTACHED DATABASE %s_sources\n", ct->tag); } void detachRepositoryDatabases(struct configTag* ct, sqlite3 *db, char* arch) { @@ -540,7 +542,7 @@ void printQueryResponse() { sqlite3_finalize(stmt1); } if (query_compact) { - printf("" "%s %s-%s - %s
", query_repositories[i]->tag, @@ -615,7 +617,7 @@ void printQueryResponse() { if (query_compact) { - printf("" "%s (%s-%s) %s %s
", query_repositories[i]->tag, @@ -730,7 +732,7 @@ void printQueryResponse() { printf("
 ", buffer); if (query_compact) { - printf("" "%s %s-%s - %s
", query_repositories[i]->tag, @@ -806,6 +808,83 @@ void printQueryResponse() { } } +void printSpecialQueryResponse() { + char *repository; + char sql[PATH_MAX]; + char dbname[PATH_MAX]; + char buffer[PATH_MAX]; + sqlite3 *db; + sqlite3_stmt *statement, *stmt1; + struct configTag* ct; + int i; + char updates[PATH_MAX]; + + if (strstr(query, "$latest$") == query) { + repository = query + 8; + ct = findRepositoryByTag(repository); + if (!ct) return; + /* latest packages in a repository */ + snprintf(dbname, PATH_MAX, "%s%s-sources.db", ct->repository_dir, ct->tag); + if (sqlite3_open_v2(dbname, (sqlite3**)&db, SQLITE_OPEN_READONLY, NULL)) { + if (db) sqlite3_close(db); + fprintf(stderr, "ERROR: unable to open sqlite3 db %s\n", dbname); + return; + } + attachCtDatabases(ct, db, NULL); + if (!reply_plain) printf("<%s>repository[i]) { + if (ct->repository[i] == ct) break; + snprintf(sql, PATH_MAX, "SELECT * FROM '%s_sources'.sources WHERE name='%s'", + ct->repository[i]->tag, + (const char*)sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "name"))); + if (sqlite3_prepare_v2(db, sql, strlen(sql), &stmt1, NULL) == SQLITE_OK) { + if (sqlite3_step(stmt1) == SQLITE_ROW) { + snprintf(updates, PATH_MAX, "%s %s(%s,%s-%s)", _("Updates"), + (const char*)sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "name")), + ct->repository[i]->tag, + (const char*)sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "version")), + (const char*)sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "release"))); + } + sqlite3_finalize(stmt1); + } + i++; + } + get_favicon_from_url((const char*)sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "url")),buffer,PATH_MAX); + if (updates[0] != 0) { + printf(" ", updates); + } else { + printf(" "); + } + printf(" ", buffer); + printf("" + "%s %s-%s
", + ct->tag, + (const char*)sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "name")), + htmlcleanNoBr((const char*)sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "summary")),buffer,PATH_MAX), + (const char*)sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "name")), + (const char*)sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "version")), + (const char*)sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "release"))); + } + sqlite3_finalize(statement); + } + + if (!reply_plain) printf("]]>", reply_xmltag); + if (!reply_plain) { + printf("<![CDATA["); + printf("%s :: %s", buffer, firstconfigtag->configdefaults->distribution_name); + printf("]]>"); + } + } else { + printQueryResponse(); + } +} + void printPackageData() { int i, j; @@ -1382,7 +1461,10 @@ main(int argc, char *argv[]) printSourcePackageData(); responsed = 1; } else if (query && strlen(query)) { - printQueryResponse(); + if (query[0] == '$') + printSpecialQueryResponse(); + else + printQueryResponse(); responsed = 1; } else if (query_path) { printFileBrowser();