distromatic/src/distroquery.c

1031 lines
49 KiB
C
Raw Normal View History

/*
* distroquery - tool for querying data generated by distromatic
*
* Copyright (C) 2013 by Silvan Calarco <silvan.calarco@mambasoft.it>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of version 2 of the GNU General Public License as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY, to the extent permitted by law; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <getopt.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <libintl.h>
#include <locale.h>
#define _(x) gettext(x)
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <dirent.h>
#include <sys/stat.h>
#if HAVE_STRING_H
# if !STDC_HEADERS && HAVE_MEMORY_H
# include <memory.h>
# endif
/* Tell glibc's <string.h> to provide a prototype for strndup() */
# ifndef __USE_GNU
# define __USE_GNU
# endif
# include <string.h>
#endif
#if HAVE_STRINGS_H
# include <strings.h>
#endif
/* Tell glibc's <time.h> to provide a prototype for strptime() */
#ifndef __USE_XOPEN
# define __USE_XOPEN
#endif
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#if !HAVE_STRCHR
# define strchr index
#endif
#include <sqlite3.h>
#include "distromatic.h"
#include "functions.h"
// must be as big as ARCHS_MAX (5)
const char* ARCHS[ARCHS_MAX] = { "i586", "x86_64", "arm", "", "" };
static struct configTag *firstconfigtag = NULL, *configtag = NULL;
char *query = "";
char *query_package;
char *query_repository;
char *query_arch = NULL;
int query_limit = 10;
int query_offset = 0;
char query_next[PATH_MAX] = "";
char *lang = "";
int query_archs[ARCHS_MAX] = { 1, 0, 0, 0, 0 };
struct configTag *query_repositories[100];
int search_milestone1 = 0, search_milestone2 = 1, search_devel = 1;
int search_sources = 0, searchbox = 0;
int search_files = 0;
/* Converts a hex character to its integer value */
char from_hex(char ch) {
return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
}
/* Converts an integer value to its hex character*/
char to_hex(char code) {
static char hex[] = "0123456789abcdef";
return hex[code & 15];
}
/* Returns a url-encoded version of str */
/* IMPORTANT: be sure to free() the returned string after use */
char *url_encode(char *str) {
char *pstr = str, *buf = malloc(strlen(str) * 3 + 1), *pbuf = buf;
while (*pstr) {
if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~')
*pbuf++ = *pstr;
else if (*pstr == ' ')
*pbuf++ = '+';
else
*pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
pstr++;
}
*pbuf = '\0';
return buf;
}
/* Returns a url-decoded version of str */
/* IMPORTANT: be sure to free() the returned string after use */
char *url_decode(char *str) {
char *pstr = str, *buf = malloc(strlen(str) + 1), *pbuf = buf;
while (*pstr) {
if (*pstr == '%') {
if (pstr[1] && pstr[2]) {
*pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
pstr += 2;
}
} else if (*pstr == '+') {
*pbuf++ = ' ';
} else {
*pbuf++ = *pstr;
}
pstr++;
}
*pbuf = '\0';
return buf;
}
int sqlite3_find_column_id(sqlite3_stmt *stmt, const char* table, const char* name) {
const char* colname;
const char* tablename;
int id = 0;
while (colname = sqlite3_column_name(stmt, id)) {
if (!strcmp(colname, name)) {
if (table) {
if (!strcmp(sqlite3_column_table_name(stmt, id), table)) return id;
} else {
return id;
}
}
id++;
}
return -1;
}
int find_query_arch(char* arch) {
int i;
for (i = 0; i < ARCHS_MAX; i++) {
if (!strcmp(arch, ARCHS[i]) && query_archs[i]) return i;
}
return -1;
}
void printRPMFlags(int flags) {
if (flags & RPMSENSE_LESS) printf("<");
if (flags & RPMSENSE_GREATER) printf(">");
if (flags & RPMSENSE_EQUAL) printf("=");
}
void printTagsLine(const char* arch, const char* tag, const char* group, const char* license) {
if (!arch)
printf("<div style='display:inline;color:white;background-color:red'> %s </div>&nbsp;", _("Source"));
else
printf("<div style='display:inline;color:white;background-color:green'> %s </div>&nbsp;", arch);
printf("<div style='display:inline;color:black;background-color:yellow'> %s </div>&nbsp;"
"<div style='display:inline;color:black;background-color:lightblue'>&nbsp%s&nbsp;</div>&nbsp;"
"<div style='display:inline;color:black;background-color:orange'>&nbsp%s&nbsp;</div>",
tag, group, license);
}
char* resolveFilePath(sqlite3 *db, long id, char *buffer) {
long currid = id;
sqlite3_stmt *stmt;
char sql[PATH_MAX];
buffer[0]='\0';
do {
snprintf(sql, PATH_MAX, "SELECT * FROM files WHERE id=%d", currid);
if (!sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL) == SQLITE_OK) return NULL;
if (sqlite3_step(stmt) != SQLITE_ROW) return NULL;
snprintf(sql, PATH_MAX, "/%s%s", sqlite3_column_text(stmt,1), buffer);
snprintf(buffer, PATH_MAX, "%s", sql);
currid = sqlite3_column_int(stmt,4); /* parent */
} while (currid >= 0);
return buffer;
}
void printInputForm() {
struct configTag *ct = firstconfigtag;
const char ajax_call[] = "distroquery_request("
// "'repository='+getElementById('repository').value"
"'query='+getElementById('query').value+"
"'&search_devel='+getElementById('search_devel').checked+"
"'&search_milestone2='+getElementById('search_milestone2').checked+"
"'&search_milestone1='+getElementById('search_milestone1').checked+"
"'&search_i586='+getElementById('search_i586').checked+"
"'&search_x86_64='+getElementById('search_x86_64').checked+"
"'&search_arm='+getElementById('search_arm').checked+"
"'&search_sources='+getElementById('search_sources').checked+"
"'&search_files='+getElementById('search_files').checked"
")";
printf("<queryform><![CDATA[");
printf("<input id=query onkeyup=if(checkMinLength(this.value,3))%s value=\"%s\">", ajax_call, query);
printf("<button onclick=%s>%s</button>", ajax_call, _("Search"));
/* printf("&nbsp;Repository: <select id=repository>");
while (ct) {
printf("<option>%s</option>",ct->tag);
ct = ct->next;
}
printf("</select>");*/
printf("<input type=checkbox id=\"search_devel\" ");
if (search_devel) printf("checked=checked ");
printf("onclick=%s>devel&nbsp;", ajax_call);
printf("<input type=checkbox id=\"search_milestone2\" ");
if (search_milestone2) printf("checked=checked ");
printf("onclick=%s>milestone2&nbsp;", ajax_call);
printf("<input type=checkbox id=\"search_milestone1\" ");
if (search_milestone1) printf("checked=checked ");
printf("onclick=%s>milestone1&nbsp;", ajax_call);
printf("&nbsp;<input type=checkbox id=\"search_i586\" ");
if (query_archs[0]) printf("checked=checked ");
printf("onclick=%s>i586&nbsp;", ajax_call);
printf("<input type=checkbox id=\"search_x86_64\" ");
if (query_archs[1]) printf("checked=checked ");
printf("onclick=%s>x86_64&nbsp;", ajax_call);
printf("<input type=checkbox id=\"search_arm\" ");
if (query_archs[2]) printf("checked=checked ");
printf("onclick=%s>arm&nbsp;", ajax_call);
printf("<input type=checkbox id=\"search_sources\" ");
if (search_sources) printf("checked=checked ");
printf("onclick=%s>%s&nbsp;", ajax_call, _("sources"));
printf("<input type=checkbox id=\"search_files\" ");
if (search_files) printf("checked=checked ");
printf("onclick=%s>%s&nbsp;", ajax_call, _("files"));
printf("<br>");
/* ct = firstconfigtag;
while (ct) {
printf("<div width=150px style=\"display:inline\"><input name=\"%s\" type=checkbox value=\"%s\">%s</div>",ct->tag, ct->tag, ct->tag);
ct = ct->next;
}*/
printf("]]></queryform>");
printf("<title><![CDATA[%s :: %s]]></title>",
_("Search software packages"),
ct->configdefaults->distribution_name);
}
void printQueryData() {
int a, i, j, k, numresults = 0, otherresults = 0, localresults, localprovidesresults, localsourceresults;
char dbname[PATH_MAX];
sqlite3 *dbf;
sqlite3_stmt *statement, *stmt1;
char sql[PATH_MAX];
char buffer[PATH_MAX];
char queryenc[PATH_MAX];
printf("<queryreply><![CDATA[");
strncpy(queryenc, query, PATH_MAX);
for (i = 0; i < strlen(queryenc); i++) {
if (queryenc[i] == ' ') queryenc[i] = '%';
}
/* for (i = 0; query_repositories[i] != NULL; i++) {
for (a = 0; a < ARCHS_MAX && query_repositories[i]->arch[a]; a++) {
if (find_query_arch(query_repositories[i]->arch[a]) >= 0) {
snprintf(dbname, PATH_MAX, "%sdistromatic.db", query_repositories[i]->repository_dir);
if (sqlite3_open_v2(dbname, (sqlite3**)&query_repositories[i]->db, SQLITE_OPEN_READONLY, NULL)) {
if (query_repositories[i]->db) sqlite3_close(query_repositories[i]->db);
query_repositories[i]->db = NULL;
fprintf(stderr, "ERROR: unable to open sqlite3 db %s; aborting.\n", dbname);
}
}*/
for (i = 0; query_repositories[i] != NULL; i++) {
for (a = 0; a < ARCHS_MAX && query_repositories[i]->arch[a]; a++) {
if (find_query_arch(query_repositories[i]->arch[a]) >= 0) {
if (!query_repositories[i]->db[a]) {
snprintf(dbname, PATH_MAX, "%s%s-%s.db",
query_repositories[i]->repository_dir, query_repositories[i]->tag, query_repositories[i]->arch[a]);
if (sqlite3_open_v2(dbname, (sqlite3**)&query_repositories[i]->db[a], SQLITE_OPEN_READONLY, NULL)) {
if (query_repositories[i]->db) sqlite3_close(query_repositories[i]->db[a]);
query_repositories[i]->db[a] = NULL;
fprintf(stderr, "ERROR: unable to open sqlite3 db %s; ignoring.\n", dbname);
}
}
if (!query_repositories[i]->db[ARCHS_MAX]) {
snprintf(dbname, PATH_MAX, "%s%s-sources.db",
query_repositories[i]->repository_dir, query_repositories[i]->tag);
if (sqlite3_open_v2(dbname, (sqlite3**)&query_repositories[i]->db[ARCHS_MAX], SQLITE_OPEN_READONLY, NULL)) {
query_repositories[i]->db[ARCHS_MAX] = NULL;
fprintf(stderr, "ERROR: unable to open sqlite3 db %s; ignoring.\n", dbname);
}
}
if (query_repositories[i]->db[a]) {
2013-10-14 12:45:06 +02:00
localresults = 0;
snprintf(sql, PATH_MAX,
"SELECT * FROM packages WHERE name LIKE '%%%s%%' OR summary LIKE '%%%s%%' OR description LIKE '%%%s%%' OR version LIKE '%%%s%%' "
"ORDER BY name = '%s' DESC, name LIKE '%s%%' DESC, name LIKE '%%%s%%' DESC, "
"groupdescr LIKE 'Graphical Desktop/%%' DESC, groupdescr LIKE 'Applications/%%' DESC, "
"groupdescr LIKE 'Development/%%' ASC, groupdescr LIKE 'Documentation%%' ASC, groupdescr LIKE 'System/Libraries%%' ASC, "
"name LIKE '%%-devel' ASC, name LIKE '%%-debug' ASC "
"LIMIT %d OFFSET %d", queryenc, queryenc, queryenc, queryenc, queryenc, queryenc, queryenc, query_limit, query_offset);
if (sqlite3_prepare_v2((sqlite3*)query_repositories[i]->db[a], sql, strlen(sql), &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
numresults++;
2013-10-14 12:45:06 +02:00
localresults++;
snprintf(sql, PATH_MAX, "SELECT * FROM sources WHERE id=%d",
sqlite3_column_int(statement,sqlite3_find_column_id(statement, NULL, "id_source")));
if (sqlite3_prepare_v2((sqlite3*)query_repositories[i]->db[ARCHS_MAX], sql, strlen(sql), &stmt1, NULL) == SQLITE_OK) {
sqlite3_step(stmt1);
get_favicon_from_url(sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "url")),buffer,PATH_MAX);
printf("<div style='position:relative'><hr><br>");
printTagsLine(query_repositories[i]->arch[a],
query_repositories[i]->tag,
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "groupdescr")),
sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "license")));
printf("<br><img src=\"%s\" width\"16\" height=\"16\">&nbsp;", buffer);
sqlite3_finalize(stmt1);
}
printf("<b>%s</b> %s-%s<br>%s",
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "name")),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "version")),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "release")),
htmlcleanNoBr(sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "summary")),buffer,PATH_MAX));
printf("<br><br><div style='text-align:justify;text-justify:inter-word;font-style:italic'>%s</div>",
htmlcleanNoBr(sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "description")),buffer,PATH_MAX));
/* install */
/* printf("<div align=right style='position:absolute;top:5px;right:0;padding:5px;'>"
"<div><object type=\"application/x-packagekit-plugin\" width=\"400\" height=\"50\">"
"<param name=\"displayname\" value=\"%s - %s\" />"
"<param name=\"packagenames\" value=\"%s\" /></object></div>",
sqlite3_column_text(statement,1),
sqlite3_column_text(statement,6),
sqlite3_column_text(statement,1));*/
/* download */
printf("<div style='display:inline;background-color:green'>"
"&nbsp;<a href=\"%s%s/SRPMS.base/%s-%s-%s.src.rpm\" style=\"color:white\">%s</a>&nbsp;</div>",
query_repositories[i]->download_prefix,
query_repositories[i]->download_dir,
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "name")),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "version")),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "release")),
_("Download"));
/* details */
printf("&nbsp;<div style='display:inline;background-color:lightblue;font-weight:strong'>"
"&nbsp;<a href='javascript:distroquery_request("
"\"repository=%s&package=%s&arch=%s\")' style=\"color:black\">%s</a>&nbsp;</div>",
query_repositories[i]->tag,
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "name")),
query_repositories[i]->arch[a],
_("Details"));
printf("</div></div><br>");
}
}
sqlite3_finalize(statement);
2013-10-14 12:45:06 +02:00
localprovidesresults = 0;
snprintf(sql, PATH_MAX,
"SELECT * FROM packages,provides,provided WHERE "
"(provides.id_package=packages.id AND provided.id=provides.id_provided) "
"AND provided.name LIKE '%%%s%%' "
"ORDER BY provided.name = '%s' DESC, provided.name LIKE '%s%%' DESC, provided.name LIKE '%%%s%%' DESC "
"LIMIT %d OFFSET %d", queryenc, queryenc, queryenc, queryenc, query_limit, query_offset);
2013-10-14 12:45:06 +02:00
if (sqlite3_prepare_v2((sqlite3*)query_repositories[i]->db[a], sql, strlen(sql), &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
numresults++;
if (++localprovidesresults == 1) {
printf("<div style='position:relative'><hr><b>");
if (localresults > 0) printf(_("Other results in "));
else printf(_("Results in"));
printf(" %s %s %s:</b><br>",
query_repositories[i]->tag,
_("for arch "),
query_repositories[i]->arch[a]);
}
printf("<a href='javascript:distroquery_request("
"\"repository=%s&package=%s&arch=%s\")' style=\"color:black;background-color:lightblue\">%s</a> %s %s<br>",
query_repositories[i]->tag,
sqlite3_column_text(statement,sqlite3_find_column_id(statement, "packages", "name")),
query_repositories[i]->arch[a],
sqlite3_column_text(statement,sqlite3_find_column_id(statement, "packages", "name")),
_("provides"),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, "provided", "name")));
}
}
/* files search */
if (search_files) {
snprintf(dbname, PATH_MAX, "%s%s-%s-files.db",
query_repositories[i]->repository_dir,
query_repositories[i]->tag,
query_repositories[i]->arch[a]);
if (!sqlite3_open_v2(dbname, &dbf, SQLITE_OPEN_READONLY, NULL)) {
snprintf(sql, PATH_MAX, "SELECT * FROM files,packages_files_rel "
"WHERE files.name LIKE '%%%s%%' AND "
"packages_files_rel.id_file = files.id "
"ORDER BY files.name = '%s' DESC, files.name LIKE '%s%%' DESC, "
"files.name LIKE '%%%s%%' DESC LIMIT %d OFFSET %d",
queryenc, queryenc, queryenc, queryenc, query_limit, query_offset);
if (sqlite3_prepare_v2(dbf, sql, strlen(sql), &stmt1, NULL) == SQLITE_OK) {
while (sqlite3_step(stmt1) == SQLITE_ROW) {
numresults++;
if (++localprovidesresults == 1) {
printf("<div style='position:relative'><hr><b>");
if (localresults > 0) printf(_("Other results in "));
else printf(_("Results in"));
printf(" %s %s %s:</b><br>",
query_repositories[i]->tag,
_("for arch "),
query_repositories[i]->arch[a]);
}
printf("<a href='javascript:distroquery_request("
"\"repository=%s&package=%s&arch=%s\")' style=\"color:black;background-color:lightblue\">%s</a> %s %s<br>",
query_repositories[i]->tag,
sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, "packages_files_rel", "name")),
query_repositories[i]->arch[a],
sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, "packages_files_rel", "name")),
_("provides file "),
resolveFilePath(dbf, sqlite3_column_int(stmt1,sqlite3_find_column_id(stmt1, NULL, "id_file")), buffer));
}
sqlite3_finalize(stmt1);
}
sqlite3_close(dbf);
}
}
if (localprovidesresults > 0) printf("</div>");
sqlite3_finalize(statement);
}
if ((localresults >= query_limit) || (localprovidesresults >= query_limit)) otherresults = 1;
} /* if query_repository */
} /* archs loop */
} /* repositories loop */
if (search_sources) {
snprintf(sql, PATH_MAX,
2013-10-14 12:45:06 +02:00
"SELECT * FROM sources WHERE name LIKE '%%%s%%' OR summary LIKE '%%%s%%' OR description LIKE '%%%s%%' OR version LIKE '%%%s%%' "
"ORDER BY name = '%s' DESC, name LIKE '%s%%' DESC, name LIKE '%%%s%%' DESC LIMIT %d OFFSET %d",
queryenc, queryenc, queryenc, queryenc, queryenc, queryenc, queryenc, query_limit, query_offset);
for (i = 0; query_repositories[i] != NULL; i++) {
if (!query_repositories[i]->db[ARCHS_MAX]) {
snprintf(dbname, PATH_MAX, "%s%s-sources.db", query_repositories[i]->repository_dir, query_repositories[i]->tag);
if (sqlite3_open_v2(dbname, (sqlite3**)&query_repositories[i]->db[ARCHS_MAX], SQLITE_OPEN_READONLY, NULL)) {
if (query_repositories[i]->db) sqlite3_close(query_repositories[i]->db[a]);
query_repositories[i]->db[ARCHS_MAX] = NULL;
fprintf(stderr, "ERROR: unable to open sqlite3 db %s; ignoring.\n", dbname);
}
}
if (query_repositories[i]->db[ARCHS_MAX] &&
(sqlite3_prepare_v2((sqlite3*)query_repositories[i]->db[ARCHS_MAX], sql, strlen(sql), &statement, NULL) == SQLITE_OK)) {
localsourceresults = 0;
while (sqlite3_step(statement) == SQLITE_ROW) {
numresults++;
localsourceresults++;
get_favicon_from_url(sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "url")),buffer,PATH_MAX);
printf("<hr><br>");
printTagsLine(NULL,
query_repositories[i]->tag,
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "groupdescr")),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "license")));
printf("<br><img src=\"%s\" width\"16\" height=\"16\">&nbsp;", buffer);
printf("<b>%s</b> %s-%s<br>%s<br><br>",
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "name")),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "version")),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "release")),
htmlcleanNoBr(sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "summary")),buffer,PATH_MAX));
printf("<div style='text-align:justify;text-justify:inter-word;font-style:italic'>%s</div>",
htmlcleanNoBr(sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "description")),buffer,PATH_MAX));
printf("<div style='display:inline;background-color:lightblue;font-weight:strong'>"
"&nbsp;<a href='javascript:distroquery_request("
"\"repository=%s&package=%s\")' style=\"color:black\">Details</a>&nbsp;</div>",
query_repositories[i]->tag,
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "name")));
}
sqlite3_finalize(statement);
} else {
fprintf(stderr, "ERROR: SQLite: %s (%s)\n", sqlite3_errmsg(query_repositories[i]->db[ARCHS_MAX]), sql);
}
if (localsourceresults >= query_limit) otherresults = 1;
} /* repositories loop */
} /* search_sources */
printf("<hr>");
printf("]]></queryreply>");
k = query_offset / query_limit + 1; /* k = current shown page */
if (otherresults || k > 1)
printf("<querystatus><![CDATA[%d %s.", numresults, _("result(s) shown"));
else
printf("<querystatus><![CDATA[%d %s.", numresults, _("result(s) found"));
if (k < 7) j=1; else j=k-5; /* j = start pages list from */
if (!otherresults) {
/* current page is last available */
a=k;
j=a-10;
if (j <= 0) j=1;
} else a=j+9; /* a = number of page links displayed */
if (a > 1) {
printf("&nbsp;[&nbsp;");
for (i = j; i <= a; i++) {
if (i != k) printf("<a href='javascript:distroquery_request(\"%s&limit=%d&offset=%d\")'>",
query_next, query_limit, query_limit * (i-1));
printf("%d", i);
printf("</a>&nbsp");
}
printf("]");
}
printf("]]></querystatus>");
printf("<title><![CDATA[%s '%s' :: %s]]></title>",
_("Search results for"),
query,
firstconfigtag->configdefaults->distribution_name);
for (i = 0; query_repositories[i] != NULL; i++) {
for (a = 0; a <= ARCHS_MAX && query_repositories[i]->arch[a]; a++) {
if (query_repositories[i]->db[a]) {
sqlite3_close((sqlite3*)query_repositories[i]->db[a]);
query_repositories[i]->db[a] = NULL;
}
}
}
}
void printPackageData() {
int a, i, j, k;
char dbname[PATH_MAX];
sqlite3 *db, *dbs, *dbf;
sqlite3_stmt *statement, *stmt1, *stmt2;
char sql[PATH_MAX];
char buffer[PATH_MAX];
struct configTag* ct = findRepositoryByTag(query_repository);
humanDate strdate;
sizeString strsize;
FILE *file;
printf("<queryreply><![CDATA[");
snprintf(dbname, PATH_MAX, "%s%s-%s.db", ct->repository_dir, ct->tag, query_arch);
if (sqlite3_open_v2(dbname, &db, SQLITE_OPEN_READONLY, NULL)) {
if (db) sqlite3_close(db);
db = NULL;
fprintf(stderr, "ERROR: unable to open sqlite3 db %s; aborting.\n", dbname);
return;
}
snprintf(sql, PATH_MAX, "SELECT * FROM packages WHERE name = '%s'", query_package);
if (db &&
(sqlite3_prepare_v2(db, sql, strlen(sql), &statement, NULL) == SQLITE_OK)) {
if (sqlite3_step(statement) == SQLITE_ROW) {
snprintf(dbname, PATH_MAX, "%s%s-sources.db", ct->repository_dir, ct->tag);
if (sqlite3_open_v2(dbname, &dbs, SQLITE_OPEN_READONLY, NULL)) {
if (dbs) sqlite3_close(dbs);
dbs = NULL;
fprintf(stderr, "ERROR: unable to open sqlite3 db %s; aborting.\n", dbname);
return;
}
snprintf(sql, PATH_MAX,
"SELECT * FROM sources,packagers WHERE sources.id=%d AND sources.id_packager=packagers.id",
sqlite3_column_int(statement,sqlite3_find_column_id(statement, NULL, "id_source")));
if (!sqlite3_prepare_v2(dbs, sql, strlen(sql), &stmt1, NULL) == SQLITE_OK) {
fprintf(stderr, "ERROR: unable to prepare statement for %s; aborting.\n", sql);
return;
}
sqlite3_step(stmt1);
printf("<hr><br>");
printTagsLine(query_arch,
ct->tag,
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "groupdescr")),
sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "license")));
get_favicon_from_url(sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "url")),buffer,PATH_MAX);
printf("<br><br>"
"<h1><img src=\"%s\" width\"16\" height=\"16\">&nbsp;"
"%s - %s</h1>%s: %s-%s<br>URL: <a href=\"%s\" target=_new>%s</a><br>%s: %s",
buffer,
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "name")),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "summary")),
_("Version"),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "version")),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "release")),
sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "url")),
sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "url")),
_("Size"),
humanSize(sqlite3_column_int(statement,sqlite3_find_column_id(statement, NULL, "size")), &strsize));
printf("<br>%s:", _("Related packages"));
snprintf(sql, PATH_MAX, "SELECT * FROM packages WHERE id_source=%d AND NOT id=%d",
sqlite3_column_int(statement,sqlite3_find_column_id(statement, NULL, "id_source")),
sqlite3_column_int(statement,sqlite3_find_column_id(statement, NULL, "id")));
if (sqlite3_prepare_v2(db, sql, strlen(sql), &stmt2, NULL) == SQLITE_OK) {
while (sqlite3_step(stmt2) == SQLITE_ROW)
printf(" <a href='javascript:distroquery_request(\"repository=%s&package=%s&arch=%s\")'>%s(%s)</a>",
ct->tag,
sqlite3_column_text(stmt2,sqlite3_find_column_id(stmt2, NULL, "name")),
query_arch,
sqlite3_column_text(stmt2,sqlite3_find_column_id(stmt2, NULL, "name")),
query_arch);
sqlite3_finalize(stmt2);
}
printf("<br><br><div style='text-align:justify;text-justify:inter-word;font-style:italic'>%s</div><br>",
htmlclean(sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "description")),buffer,PATH_MAX));
/* download */
printf("<div style='display:inline;background-color:green'>"
"&nbsp;<a href=\"%s%s/RPMS.%s/%s-%s-%s.%s.rpm\" style=\"color:white\">%s</a>&nbsp;</div>",
ct->download_prefix,
ct->download_dir,
query_arch,
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "name")),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "version")),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "release")),
query_arch,
_("Download"));
printf("<hr><br><b>%s:</b>",_("Developers details"));
printf("<br>%s:", _("Source package"));
printf(" <a href='javascript:distroquery_request(\"repository=%s&package=%s\")'>%s</a>",
ct->tag,
sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "name")),
sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "name")));
snprintf(buffer, PATH_MAX, "%s/specs/%s.spec", ct->repository_dir, query_package);
if (file = fopen(buffer, "r")) {
fclose(file);
snprintf(buffer, PATH_MAX, "%s%s/specs/%s.spec", ct->showfile_prefix, ct->download_dir, query_package);
printf("<br>%s: <a href=\"%s\" target=_blank>%s.spec</a>", _("Specfile"), buffer, query_package);
} else {
printf("<br>%s: %s.spec", _("Specfile"), query_package);
}
printf("<br>%s: %s", _("Maintainer"), sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, "packagers", "name")));
printf("<br>%s: %s", _("Build date"),
simpleTimeToHuman(sqlite3_column_int(stmt1,sqlite3_find_column_id(stmt1, NULL, "buildtime")), (humanDate *) & strdate));
printf("<br><br>%s:", _("Obsoletes"));
snprintf(sql, PATH_MAX, "SELECT * FROM obsoletes WHERE id_package=%d",
sqlite3_column_int(statement,sqlite3_find_column_id(statement, NULL, "id")));
sqlite3_finalize(stmt1);
if (sqlite3_prepare_v2(db, sql, strlen(sql), &stmt1, NULL) == SQLITE_OK) {
while (sqlite3_step(stmt1) == SQLITE_ROW) {
printf(" %s", sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "obsoletename")));
if (sqlite3_column_int(stmt1,sqlite3_find_column_id(stmt1, NULL, "obsoleteversion"))) {
printRPMFlags(sqlite3_column_int(stmt1,sqlite3_find_column_id(stmt1, NULL, "obsoleteflags")));
printf("%s", sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "obsoleteversion")));
}
}
sqlite3_finalize(stmt1);
}
printf("<br><br>%s:",_("Provides"));
snprintf(sql, PATH_MAX, "SELECT * FROM provides,provided "
"WHERE provides.id_package=%d AND provided.id=provides.id_provided "
"ORDER BY provided.name",
sqlite3_column_int(statement,sqlite3_find_column_id(statement, NULL, "id")));
if (sqlite3_prepare_v2(db, sql, strlen(sql), &stmt1, NULL) == SQLITE_OK) {
while (sqlite3_step(stmt1) == SQLITE_ROW) {
printf(" %s", sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "name")));
if (sqlite3_column_int(stmt1,sqlite3_find_column_id(stmt1, NULL, "provideversion"))) {
printRPMFlags(sqlite3_column_int(stmt1,sqlite3_find_column_id(stmt1, NULL, "provideflags")));
printf("%s", sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "provideversion")));
}
}
sqlite3_finalize(stmt1);
}
printf("<br><br>%s:", _("Requires"));
snprintf(sql, PATH_MAX, "SELECT * FROM requires,provided "
"WHERE requires.id_package=%d AND provided.id=requires.id_provided "
"ORDER BY provided.name",
sqlite3_column_int(statement,sqlite3_find_column_id(statement, NULL, "id")));
if (sqlite3_prepare_v2(db, sql, strlen(sql), &stmt1, NULL) == SQLITE_OK) {
while (sqlite3_step(stmt1) == SQLITE_ROW) {
printf(" %s", sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "name")));
if (sqlite3_column_int(stmt1,sqlite3_find_column_id(stmt1, NULL, "requireversion"))) {
printRPMFlags(sqlite3_column_int(stmt1,sqlite3_find_column_id(stmt1, NULL, "requireflags")));
printf("%s", sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "requireversion")));
}
}
sqlite3_finalize(stmt1);
}
/* files list */
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)) {
printf("<br><br>%s:<br>", _("Files list"));
snprintf(sql, PATH_MAX, "SELECT * FROM packages_files_rel WHERE name='%s'", query_package);
if (sqlite3_prepare_v2(dbf, sql, strlen(sql), &stmt1, NULL) == SQLITE_OK) {
while (sqlite3_step(stmt1) == SQLITE_ROW) {
printf(" %s<br>", resolveFilePath(dbf, sqlite3_column_int(stmt1,sqlite3_find_column_id(stmt1, NULL, "id_file")), buffer));
}
sqlite3_finalize(stmt1);
}
sqlite3_close(dbf);
}
sqlite3_close(dbs);
}
snprintf(buffer, PATH_MAX, "%s - %s",
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "name")),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "summary")));
sqlite3_finalize(statement);
}
printf("]]></queryreply>");
printf("<title><![CDATA[%s :: %s]]></title>",
buffer,
firstconfigtag->configdefaults->distribution_name);
sqlite3_close(db);
db = NULL;
}
void printSourcePackageData() {
int a, i, j, k;
char dbname[PATH_MAX];
sqlite3 *db, *dba;
sqlite3_stmt *statement, *stmt1;
char sql[PATH_MAX];
char buffer[PATH_MAX];
struct configTag* ct = findRepositoryByTag(query_repository);
humanDate strdate;
sizeString strsize;
FILE *file;
printf("<queryreply><![CDATA[");
snprintf(dbname, PATH_MAX, "%s%s-sources.db", ct->repository_dir, ct->tag);
if (sqlite3_open_v2(dbname, &db, SQLITE_OPEN_READONLY, NULL)) {
if (db) sqlite3_close(db);
db = NULL;
fprintf(stderr, "ERROR: unable to open sqlite3 db %s; aborting.\n", dbname);
return;
}
snprintf(sql, PATH_MAX,
"SELECT * FROM sources,packagers WHERE sources.name = '%s' AND sources.id_packager=packagers.id", query_package);
if (db &&
(sqlite3_prepare_v2(db, sql, strlen(sql), &statement, NULL) == SQLITE_OK)) {
if (sqlite3_step(statement) == SQLITE_ROW) {
printf("<hr><br>");
printTagsLine(NULL,
ct->tag,
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "groupdescr")),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "license")));
get_favicon_from_url(sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "url")),buffer,PATH_MAX);
printf("<br><br>"
"<h1><img src=\"%s\" width\"16\" height=\"16\">&nbsp;"
"%s - %s</h1>%s: %s-%s<br>URL: <a href=\"%s\" target=_new>%s</a><br>Size: %s",
buffer,
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "name")),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "summary")),
_("Version"),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "version")),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "release")),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "url")),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "url")),
humanSize(sqlite3_column_int(statement,sqlite3_find_column_id(statement, NULL, "size")), &strsize));
printf("<br>%s:", _("Built packages"));
for (a = 0; a < ARCHS_MAX && ct->arch[a]; a++) {
snprintf(dbname, PATH_MAX, "%s%s-%s.db", ct->repository_dir, ct->tag, ct->arch[a]);
if (!sqlite3_open_v2(dbname, &dba, SQLITE_OPEN_READONLY, NULL)) {
snprintf(sql, PATH_MAX, "SELECT * FROM packages WHERE id_source=%d",
sqlite3_column_int(statement,sqlite3_find_column_id(statement, NULL, "id")));
if (sqlite3_prepare_v2(dba, sql, strlen(sql), &stmt1, NULL) == SQLITE_OK) {
while (sqlite3_step(stmt1) == SQLITE_ROW)
printf(" <a href='javascript:distroquery_request(\"repository=%s&package=%s&arch=%s\")'>%s(%s)</a>",
ct->tag,
sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "name")),
ct->arch[a],
sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "name")),
ct->arch[a]);
sqlite3_finalize(stmt1);
}
sqlite3_close(dba);
}
}
printf("<br><br><div style='text-align:justify;text-justify:inter-word;font-style:italic'>%s</div><br>",
htmlclean(sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "description")),buffer,PATH_MAX));
/* download */
printf("<div style='display:inline;background-color:red'>"
"&nbsp;<a href=\"%s%s/SRPMS.base/%s-%s-%s.src.rpm\" style=\"color:white\">%s</a>&nbsp;</div>",
ct->download_prefix,
ct->download_dir,
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "name")),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "version")),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "release")),
_("Download"));
printf("<hr><br><b>%s:</b>", _("Developers details"));
printf("<br>%s: %s", _("Maintainer"), sqlite3_column_text(statement,sqlite3_find_column_id(statement, "packagers", "name")));
printf("<br>%s: %s", _("Build date"),
simpleTimeToHuman(sqlite3_column_int(statement,sqlite3_find_column_id(statement, NULL, "buildtime")), (humanDate *) & strdate));
printf("<br><br>%s:", _("Source files"));
snprintf(sql, PATH_MAX,
"SELECT * FROM sources_source WHERE id_source=%d",
sqlite3_column_int(statement,sqlite3_find_column_id(statement, NULL, "id")));
if (sqlite3_prepare_v2(db, sql, strlen(sql), &stmt1, NULL) == SQLITE_OK) {
while (sqlite3_step(stmt1) == SQLITE_ROW) {
printf(" %s", sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "source")));
}
sqlite3_finalize(stmt1);
}
printf("<br><br>%s:", _("Patches"));
snprintf(sql, PATH_MAX,
"SELECT * FROM sources_patch WHERE id_source=%d",
sqlite3_column_int(statement,sqlite3_find_column_id(statement, NULL, "id")));
if (sqlite3_prepare_v2(db, sql, strlen(sql), &stmt1, NULL) == SQLITE_OK) {
while (sqlite3_step(stmt1) == SQLITE_ROW) {
printf(" %s", sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "patch")));
}
sqlite3_finalize(stmt1);
}
printf("<br><br>%s:", _("Build requirements"));
snprintf(sql, PATH_MAX, "SELECT * FROM buildrequires WHERE id_source=%d",
sqlite3_column_int(statement,sqlite3_find_column_id(statement, NULL, "id")));
if (sqlite3_prepare_v2(db, sql, strlen(sql), &stmt1, NULL) == SQLITE_OK) {
while (sqlite3_step(stmt1) == SQLITE_ROW) {
printf(" %s", sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "buildrequirename")));
if (strcmp("", sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "buildrequireversion")))) {
printRPMFlags(sqlite3_column_int(stmt1,sqlite3_find_column_id(stmt1, NULL, "buildrequireflags")));
printf("%s", sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "buildrequireversion")));
}
}
sqlite3_finalize(stmt1);
}
printf("<br><br>%s:<br>", _("Changelog"));
snprintf(sql, PATH_MAX,
"SELECT * FROM changelog,packagers WHERE id_source=%d AND changelog.id_packager=packagers.id",
sqlite3_column_int(statement,sqlite3_find_column_id(statement, NULL, "id")));
if (sqlite3_prepare_v2(db, sql, strlen(sql), &stmt1, NULL) == SQLITE_OK) {
while (sqlite3_step(stmt1) == SQLITE_ROW) {
printf("%s - %s (%s)<br><pre>%s</pre>",
simpleTimeToHuman(sqlite3_column_int(stmt1,sqlite3_find_column_id(stmt1, NULL, "time")), (humanDate *) & strdate),
sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, "packagers", "name")),
sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "release")),
sqlite3_column_text(stmt1,sqlite3_find_column_id(stmt1, NULL, "text")));
}
sqlite3_finalize(stmt1);
}
}
snprintf(buffer, PATH_MAX, "%s - %s",
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "name")),
sqlite3_column_text(statement,sqlite3_find_column_id(statement, NULL, "summary")));
sqlite3_finalize(statement);
}
printf("]]></queryreply>");
printf("<title><![CDATA[%s :: %s]]></title>",
buffer,
firstconfigtag->configdefaults->distribution_name);
sqlite3_close(db);
db = NULL;
}
void parse_request_variables(char *data) {
char *vartok, *valuetok;
struct configTag *ct = firstconfigtag;
int i;
while (1) {
vartok = (char *) strtok(data, "=");
if (data) data = NULL;
if (!vartok || (vartok[0] == '\0') ) break;
valuetok = (char *) strtok(NULL, "&");
if (!strcmp(vartok, "repository")) {
query_repository = valuetok;
} else if (!strcmp(vartok, "package")) {
query_package = valuetok;
} else if (!strcmp(vartok, "arch")) {
query_arch = valuetok;
} else if (!strcmp(vartok, "offset")) {
query_offset = atoi(url_decode(valuetok));
if (query_offset < 0) query_offset = 0;
} else if (!strcmp(vartok, "limit")) {
query_limit = atoi(url_decode(valuetok));
if (query_limit < 0) query_limit = 10;
else if (query_limit > 20) query_limit = 20;
} else if (!strcmp(vartok, "searchbox")) {
searchbox = strstr(valuetok, "false") != valuetok;
} else if (!strcmp(vartok, "lang")) {
lang = valuetok;
} else {
/* fields to make reusable query string for next pages */
if (!strcmp(vartok, "query")) {
query = url_decode(valuetok);
} else if (!strcmp(vartok, "search_milestone2")) {
search_milestone2 = strstr(valuetok, "false") != valuetok;
} else if (!strcmp(vartok, "search_milestone1")) {
search_milestone1 = strstr(valuetok, "false") != valuetok;
} else if (!strcmp(vartok, "search_devel")) {
search_devel = strstr(valuetok, "false") != valuetok;
} else if (!strcmp(vartok, "search_i586")) {
query_archs[0] = strstr(valuetok, "false") != valuetok;
} else if (!strcmp(vartok, "search_x86_64")) {
query_archs[1] = strstr(valuetok, "false") != valuetok;
} else if (!strcmp(vartok, "search_arm")) {
query_archs[2] = strstr(valuetok, "false") != valuetok;
} else if (!strcmp(vartok, "search_sources")) {
search_sources = strstr(valuetok, "false") != valuetok;
} else if (!strcmp(vartok, "search_files")) {
search_files = strstr(valuetok, "false") != valuetok;
}
if (vartok && valuetok) {
strcat(query_next, vartok);
strcat(query_next, "=");
strcat(query_next, valuetok);
strcat(query_next, "&");
}
}
}
if (query_next[strlen(query_next)] == '&') query_next[strlen(query_next)] = '\0';
ct = firstconfigtag;
i = 0;
while (ct) {
if ((strstr(ct->tag, "devel") == ct->tag) && search_devel) query_repositories[i++] = ct;
ct = ct->next;
}
ct = firstconfigtag;
while (ct) {
if ((strstr(ct->tag, "milestone2") == ct->tag) && search_milestone2) query_repositories[i++] = ct;
ct = ct->next;
}
ct = firstconfigtag;
while (ct) {
if ((strstr(ct->tag, "milestone1") == ct->tag) && search_milestone1) query_repositories[i++] = ct;
ct = ct->next;
}
query_repositories[i++] = NULL;
}
int
main(int argc, char *argv[])
{
char *repository_dir = NULL;
// char *configfile = NULL;
char *data;
// install backtrace handler
signal(SIGSEGV, backtraceHandler);
printf("Content-Type: text/xml;charset=utf-8\n\n<distroquery>");
firstconfigtag = read_configuration(DEFAULT_CONFIGFILE);
if (!firstconfigtag) {
fprintf(stderr, "Fatal error while parsing config file " DEFAULT_CONFIGFILE "; aborting.\n");
exit(1);
}
data = getenv("QUERY_STRING");
if (data && strlen(data)) {
parse_request_variables(data);
}
setlocale(LC_ALL, lang);
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
if (query_package && strlen(query_package)) {
if (query_arch)
printPackageData();
else
printSourcePackageData();
if (searchbox) printInputForm();
} else if (query && strlen(query)) {
printQueryData();
if (searchbox) printInputForm();
} else {
printInputForm();
}
printf("</distroquery>\n");
exit(0);
}