From eca33e1e9e9012ff301a980760bc0850cac81e8f Mon Sep 17 00:00:00 2001 From: Silvan Calarco Date: Mon, 21 Oct 2013 21:48:37 +0200 Subject: [PATCH] distroquery: fixes for query_path search --- src/distroquery.c | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/distroquery.c b/src/distroquery.c index 204253c..d79fe0e 100644 --- a/src/distroquery.c +++ b/src/distroquery.c @@ -91,7 +91,7 @@ char *reply_xmltag = "queryreply"; int reply_plain = 0; char *lang = ""; int query_archs[ARCHS_MAX] = { 1, 0, 0, 0, 0 }; -char *file_browser = NULL; +char *query_path = NULL; struct configTag *query_repositories[100]; @@ -317,30 +317,37 @@ void printInputForm() { } } -int findFileIdFromPath(sqlite3 *db, char* path) { +int findFileIdFromPath(sqlite3 *db, char** path) { int frompos = 1, topos; int parent = -1; char* pt; char buffer[PATH_MAX]; char sql[PATH_MAX]; + char* linkpath = calloc(strlen(*path),0); sqlite3_stmt* stmt1; - printf("[ROOT]", + printf("[ROOT]", 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("/%s", - query_repository, query_arch, buffer, buffer); + while (frompos < strlen(*path)) { + pt = strchr(*path + frompos, '/'); + if (pt) topos = pt - *path; else topos = strlen(*path); + strncpy(buffer, *path + frompos, topos - frompos); + buffer[topos-frompos]=0; 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); + strcat(linkpath, "/"); + strncat(linkpath, buffer, strlen(*path) - strlen(linkpath)); + printf("/%s", + query_repository, query_arch, linkpath, buffer); } frompos = topos + 1; } + /* return clean path */ + //free(path); + *path = linkpath; return parent; } @@ -358,7 +365,7 @@ void printFileBrowser() { 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); + startid = findFileIdFromPath(dbf, &query_path); snprintf(sql, PATH_MAX, "SELECT * FROM files WHERE" " parent=%d" " ORDER BY name", startid); @@ -379,13 +386,13 @@ void printFileBrowser() { printf("%s%s" "%s" "" - "%s", + "%s", 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, + query_path, sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "name")), sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "name"))); } else { @@ -406,11 +413,11 @@ void printFileBrowser() { } if (cnt == 0) { printf("" - "%s" + "%s" "", query_repository, query_arch, - file_browser, + query_path, sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "name")), sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "name"))); } @@ -1172,8 +1179,8 @@ void parse_request_variables(char *data) { reply_plain = strstr(valuetok, "true") == valuetok; } else if (!strcmp(vartok, "query_compact")) { query_compact = strstr(valuetok, "true") == valuetok; - } else if (!strcmp(vartok, "file_browser")) { - file_browser = valuetok; + } else if (!strcmp(vartok, "path")) { + query_path = valuetok; } else { /* fields to make reusable query string for next pages */ if (!strcmp(vartok, "query")) { @@ -1263,7 +1270,7 @@ main(int argc, char *argv[]) } else if (query && strlen(query)) { printQueryResponse(); responsed = 1; - } else if (file_browser) { + } else if (query_path) { printFileBrowser(); }