diff --git a/src/distroquery.c b/src/distroquery.c index 68fa384..4d6f9a5 100644 --- a/src/distroquery.c +++ b/src/distroquery.c @@ -81,8 +81,8 @@ const char* ARCHS[ARCHS_MAX] = { "i586", "x86_64", "arm", "", "" }; static struct configTag *firstconfigtag = NULL; char *query = ""; char *query_package; -char *query_repository; -char *query_arch = NULL; +char *query_repository = "milestone2"; +char *query_arch = "i586"; int query_compact = 0; int query_limit = 10; int query_offset = 0; @@ -229,6 +229,22 @@ char* resolveFilePath(sqlite3 *db, long id, char *buffer) { return buffer; } +void attachCtDatabases(struct configTag* ct, sqlite3 *db, char* arch) { + char dbname[PATH_MAX]; + int i=0; + char sql[PATH_MAX]; + 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); + } + i++; + } +} + void printInputForm() { struct configTag *ct = firstconfigtag; @@ -294,7 +310,6 @@ void printInputForm() { ct = ct->next; }*/ if (!reply_plain) printf("]]>"); - if (!reply_plain) { printf("<![CDATA["); printf("%s :: %s", _("Search software packages"), ct->configdefaults->distribution_name); @@ -302,11 +317,113 @@ void printInputForm() { } } +int findFileIdFromPath(sqlite3 *db, char* path) { + int frompos = 1, topos; + int parent = -1; + char* pt; + char buffer[PATH_MAX]; + char sql[PATH_MAX]; + sqlite3_stmt* stmt1; + + printf("[<a href='javascript:distroquery_request(\"repository=%s&query_arch=%s&file_browser=/\")'>ROOT</a>]", + query_repository, query_arch); + while (frompos < strlen(path)) { + pt = strchr(path + frompos, '/'); + if (pt) topos = pt - path; else topos = strlen(path); + strncpy(buffer, path + frompos, topos - frompos); + printf("/<a href='javascript:distroquery_request(\"repository=%s&query_arch=%s&file_browser=/%s\")'>%s</a>", + query_repository, query_arch, buffer, buffer); + snprintf(sql, PATH_MAX, "SELECT id FROM files WHERE parent=%d" + " AND name='%s'", parent, buffer); + if (sqlite3_prepare_v2(db, sql, strlen(sql), &stmt1, NULL) == SQLITE_OK && sqlite3_step(stmt1) == SQLITE_ROW) { + parent = sqlite3_column_int(stmt1,0); + sqlite3_finalize(stmt1); + } + frompos = topos + 1; + } + return parent; +} + void printFileBrowser() { + char dbname[PATH_MAX]; + char sql[PATH_MAX]; + sqlite3 *dbf; + sqlite3_stmt *stmt1, *stmt2; + int flags, cnt, startid; + struct configTag* ct = findRepositoryByTag(query_repository); if (!reply_plain) printf("<%s><![CDATA[", reply_xmltag); - if (!reply_plain) printf("]]></%s>", reply_xmltag); + printf("<hr>Repository %s, %s: ", query_repository, _("current path")); + + snprintf(dbname, PATH_MAX, "%s%s-%s-files.db", ct->repository_dir, ct->tag, query_arch); + if (!sqlite3_open_v2(dbname, &dbf, SQLITE_OPEN_READONLY, NULL)) { + startid = findFileIdFromPath(dbf, file_browser); + snprintf(sql, PATH_MAX, "SELECT * FROM files WHERE" + " parent=%d" + " ORDER BY name", startid); + printf("<table>"); + if (sqlite3_prepare_v2(dbf, sql, strlen(sql), &stmt1, NULL) == SQLITE_OK) { + while (sqlite3_step(stmt1) == SQLITE_ROW) { + snprintf(sql, PATH_MAX, "SELECT * FROM packages_files_rel,fileusers,filegroups WHERE" + " packages_files_rel.id_file=%d AND" + " packages_files_rel.id_user=fileusers.id AND " + " packages_files_rel.id_group=filegroups.id", + sqlite3_column_int(stmt1,sqlite3_find_column_id(stmt1, NULL, "id"))); + cnt = 0; + if (sqlite3_prepare_v2(dbf, sql, strlen(sql), &stmt2, NULL) == SQLITE_OK) { + if (sqlite3_step(stmt2) == SQLITE_ROW) { + cnt++; + flags = sqlite3_column_int(stmt2,sqlite3_find_column_id(stmt2, "packages_files_rel", "flags")); + if (flags >> 12 & 4) { + printf("<tr><td style=\"font-family:courier\">%s</td><td style=\"font-family:courier\">%s</td>" + "<td style=\"font-family:courier\">%s</td>" + "<td width=50%% style=\"font-family:courier\">" + "<a href='javascript:distroquery_request(\"repository=%s&query_arch=%s&file_browser=%s/%s\")'>%s</a></td>", + sqlite3_column_text(stmt2,sqlite3_find_column_id(stmt2, "fileusers", "name")), + sqlite3_column_text(stmt2,sqlite3_find_column_id(stmt2, "filegroups", "name")), + expandFileFlags(flags, sql), + query_repository, + query_arch, + file_browser, + sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "name")), + sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "name"))); + } else { + printf("<tr><td style=\"font-family:courier\">%s</td><td style=\"font-family:courier\">%s</td>" + "<td style=\"font-family:courier\">%s</td><td width=50%% style=\"font-family:courier\">%s</td>", + sqlite3_column_text(stmt2,sqlite3_find_column_id(stmt2, "fileusers", "name")), + sqlite3_column_text(stmt2,sqlite3_find_column_id(stmt2, "filegroups", "name")), + expandFileFlags(flags, sql), + sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "name"))); + } + printf("<td nowrap>%s: %s", _("Provider(s)"), sqlite3_column_text(stmt2,sqlite3_find_column_id(stmt2, "packages_files_rel", "name"))); + while (sqlite3_step(stmt2) == SQLITE_ROW) { + printf(" %s", sqlite3_column_text(stmt2,sqlite3_find_column_id(stmt2, "packages_files_rel", "name"))); + } + printf("</td></tr>"); + } + sqlite3_finalize(stmt2); + } + if (cnt == 0) { + printf("<tr><td></td><td></td><td></td>" + "<td width=50%% style=\"font-family:courier\"><a href='javascript:distroquery_request(\"repository=%s&query_arch=%s&file_browser=%s/%s\")'>%s</a></td>" + "<td></td></tr>", + query_repository, + query_arch, + file_browser, + sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "name")), + sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "name"))); + } + } + sqlite3_finalize(stmt1); + } + sqlite3_close(dbf); + printf("</table>"); + } + + if (!reply_plain) printf("]]></%s>", reply_xmltag); + + if (!reply_plain) printf("<querystatus><![CDATA[ ]]></querystatus>"); } @@ -631,21 +748,6 @@ void printQueryResponse() { } } -void attachCtDatabases(struct configTag* ct, sqlite3 *db) { - char dbname[PATH_MAX]; - int i=0; - char sql[PATH_MAX]; - char *errmsg; - - while (ct->repository[i]) { - snprintf(dbname, PATH_MAX, "%s%s-%s.db", ct->repository[i]->repository_dir, ct->repository[i]->tag, query_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); - } - i++; - } -} void printPackageData() { @@ -667,7 +769,7 @@ void printPackageData() { return; } - attachCtDatabases(ct, db); + attachCtDatabases(ct, db, query_arch); if (!reply_plain) printf("<%s><![CDATA[", reply_xmltag); @@ -1163,7 +1265,6 @@ main(int argc, char *argv[]) responsed = 1; } else if (file_browser) { printFileBrowser(); - responsed = 1; } if (!responsed || (responsed && searchbox)) printInputForm();