distroquery: convert from C to C++

This commit is contained in:
Silvan Calarco 2024-05-30 12:16:48 +02:00
parent 772fd4d42b
commit 659b2ba778
5 changed files with 52 additions and 19 deletions

View File

@ -65,8 +65,9 @@ target_include_directories(distromatic PUBLIC
# #
target_compile_options(distromatic PUBLIC -O2 -g -Wall -std=gnu11 -pedantic ${RPM_CFLAGS_OTHER}) target_compile_options(distromatic PUBLIC -O2 -g -Wall -std=gnu11 -pedantic ${RPM_CFLAGS_OTHER})
# -fno-toplevel-reorder # -fno-toplevel-reorder
add_executable(distroquery add_executable(distroquery
distroquery.c distroquery.cpp
functions.c functions.c
headerlist.c headerlist.c
requirelist.c requirelist.c
@ -85,6 +86,7 @@ target_link_libraries(distroquery
${LIBDW_LIBRARIES} ${LIBDW_LIBRARIES}
${LIBIBERTY} ${LIBIBERTY}
) )
target_include_directories(distroquery PUBLIC target_include_directories(distroquery PUBLIC
${RPM_INCLUDE_DIRS} ${RPM_INCLUDE_DIRS}
${LIBREPO_INCLUDE_DIRS} ${LIBREPO_INCLUDE_DIRS}

View File

@ -17,6 +17,8 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/ */
#include <distroquery.hpp>
#include <getopt.h> #include <getopt.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
@ -35,26 +37,23 @@
#include <string.h> #include <string.h>
#include <linux/limits.h> #include <linux/limits.h>
#include <sqlite3.h> #include <sqlite3.h>
#include "distromatic.h"
#include "config.h" #include "config.h"
#include "rpmfunctions.h"
#include "functions.h"
// must be as big as ARCHS_MAX (5) // must be as big as ARCHS_MAX (5)
const char* ARCHS[ARCHS_MAX] = { "x86_64", "aarch64", "i586", "arm", "" }; const char* ARCHS[ARCHS_MAX] = { "x86_64", "aarch64", "i586", "arm", "" };
static struct configTag *firstconfigtag = NULL; static struct configTag *firstconfigtag = NULL;
char *query = ""; char *query = (char*)"";
char *query_package; char *query_package;
char *query_repository = "base"; char *query_repository = (char*)"base";
char *query_arch = NULL; char *query_arch = NULL;
int query_compact = 0; int query_compact = 0;
int query_limit = 10; int query_limit = 10;
int query_offset = 0; int query_offset = 0;
char query_next[PATH_MAX] = ""; char query_next[PATH_MAX] = "";
char *reply_xmltag = "queryreply"; char *reply_xmltag = (char*)"queryreply";
int reply_plain = 0; int reply_plain = 0;
char *lang = ""; char *lang = (char*)"";
int query_archs[ARCHS_MAX] = { 1, 0, 0, 0, 0 }; int query_archs[ARCHS_MAX] = { 1, 0, 0, 0, 0 };
char *query_path = NULL; char *query_path = NULL;
@ -78,7 +77,7 @@ char to_hex(char code) {
/* Returns a url-encoded version of str */ /* Returns a url-encoded version of str */
/* IMPORTANT: be sure to free() the returned string after use */ /* IMPORTANT: be sure to free() the returned string after use */
char *url_encode(char *str) { char *url_encode(char *str) {
char *pstr = str, *buf = malloc(strlen(str) * 3 + 1), *pbuf = buf; char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf;
while (*pstr) { while (*pstr) {
if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~')
*pbuf++ = *pstr; *pbuf++ = *pstr;
@ -95,8 +94,8 @@ char *url_encode(char *str) {
/* Returns a url-decoded version of str */ /* Returns a url-decoded version of str */
/* IMPORTANT: be sure to free() the returned string after use */ /* IMPORTANT: be sure to free() the returned string after use */
char *url_decode(char *str) { char *url_decode(char *str) {
if (!str) { char *buf=malloc(1); buf[0]=0; return buf; } if (!str) { char *buf=(char*)malloc(1); buf[0]=0; return buf; }
char *pstr = str, *buf = malloc(strlen(str) + 1), *pbuf = buf; char *pstr = str, *buf = (char*)malloc(strlen(str) + 1), *pbuf = buf;
while (*pstr) { while (*pstr) {
if (*pstr == '%') { if (*pstr == '%') {
if (pstr[1] && pstr[2]) { if (pstr[1] && pstr[2]) {
@ -369,7 +368,7 @@ int findFileIdFromPath(sqlite3 *db, char** path) {
char* pt; char* pt;
char buffer[PATH_MAX]; char buffer[PATH_MAX];
char sql[PATH_MAX]; char sql[PATH_MAX];
char* linkpath = calloc(strlen(*path),1); char* linkpath = (char*)calloc(strlen(*path),1);
sqlite3_stmt* stmt1; sqlite3_stmt* stmt1;
printf("[<a href='javascript:distroquery_request(\"repository=%s&arch=%s&path=/\")'>ROOT</a>]", printf("[<a href='javascript:distroquery_request(\"repository=%s&arch=%s&path=/\")'>ROOT</a>]",
@ -512,7 +511,7 @@ void printQueryResponse() {
snprintf(dbname, PATH_MAX, "%s%s-%s.db", snprintf(dbname, PATH_MAX, "%s%s-%s.db",
query_repositories[i]->repository_dir, query_repositories[i]->tag, query_repositories[i]->arch[a]); 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 (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]); if (query_repositories[i]->db) sqlite3_close((sqlite3*)query_repositories[i]->db[a]);
query_repositories[i]->db[a] = NULL; query_repositories[i]->db[a] = NULL;
fprintf(stderr, "ERROR: unable to open sqlite3 db %s; ignoring.\n", dbname); fprintf(stderr, "ERROR: unable to open sqlite3 db %s; ignoring.\n", dbname);
} }
@ -740,7 +739,7 @@ void printQueryResponse() {
if (!query_repositories[i]->db[ARCHS_MAX]) { if (!query_repositories[i]->db[ARCHS_MAX]) {
snprintf(dbname, PATH_MAX, "%s%s-sources.db", query_repositories[i]->repository_dir, query_repositories[i]->tag); 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 (sqlite3_open_v2(dbname, (sqlite3**)&query_repositories[i]->db[ARCHS_MAX], SQLITE_OPEN_READONLY, NULL)) {
if (query_repositories[i]->db[ARCHS_MAX]) sqlite3_close(query_repositories[i]->db[ARCHS_MAX]); if (query_repositories[i]->db[ARCHS_MAX]) sqlite3_close((sqlite3*)query_repositories[i]->db[ARCHS_MAX]);
query_repositories[i]->db[ARCHS_MAX] = NULL; query_repositories[i]->db[ARCHS_MAX] = NULL;
fprintf(stderr, "ERROR: unable to open sqlite3 db %s; ignoring.\n", dbname); fprintf(stderr, "ERROR: unable to open sqlite3 db %s; ignoring.\n", dbname);
} }
@ -792,7 +791,7 @@ void printQueryResponse() {
} }
sqlite3_finalize(statement); sqlite3_finalize(statement);
} else { } else {
fprintf(stderr, "ERROR: SQLite: %s (%s)\n", sqlite3_errmsg(query_repositories[i]->db[ARCHS_MAX]), sql); fprintf(stderr, "ERROR: SQLite: %s (%s)\n", sqlite3_errmsg((sqlite3*)query_repositories[i]->db[ARCHS_MAX]), sql);
} }
if (localsourceresults >= query_limit) otherresults = 1; if (localsourceresults >= query_limit) otherresults = 1;
} /* repositories loop */ } /* repositories loop */

View File

@ -477,12 +477,12 @@ strip_separators(char *st, const char *sepstr)
} }
humanDate * humanDate *
simpleTimeToTemplate(long calendartime, const char* template, humanDate * strdate) simpleTimeToTemplate(long calendartime, const char* templ, humanDate * strdate)
{ {
struct tm *ltime; struct tm *ltime;
ltime = localtime((time_t *) & calendartime); ltime = localtime((time_t *) & calendartime);
strftime((char *) strdate, 16, template, ltime); strftime((char *) strdate, 16, templ, ltime);
return strdate; return strdate;
} }

View File

@ -0,0 +1,33 @@
/*
* distroquery - tool for querying data generated by distromatic
*
* Copyright (C) 2024 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.
*/
#define HDSIZE 16
#define SSSIZE 24
extern "C" typedef char humanDate[HDSIZE];
extern "C" typedef char sizeString[SSSIZE];
extern "C" char *humanSize(long long sbytes, sizeString *s);
extern "C" humanDate *simpleTimeToTemplate(long calendartime, const char* templ, humanDate * strdate);
extern "C" humanDate *simpleTimeToHuman(long calendartime, humanDate * strdate);
extern "C" const char *htmlclean(const char *source,char *dest,unsigned int max);
extern "C" const char *htmlcleanNoBr(const char *source,char *dest,unsigned int max);
extern "C" int get_favicon_from_url(const char* url,char *buf,int bufsize);
extern "C" void backtraceHandler(int sig);
extern "C" struct configTag* findRepositoryByTag(const char *tag);
extern "C" struct configTag* read_configuration(const char *confFile);

View File

@ -30,8 +30,7 @@ void *memndup(void *memp, size_t size);
void log_debug_set(int value); void log_debug_set(int value);
void logmsg(int level, const char *msg, ...); void logmsg(int level, const char *msg, ...);
char *strip_separators(char *st, const char *sepstr); char *strip_separators(char *st, const char *sepstr);
humanDate * humanDate *simpleTimeToTemplate(long calendartime, const char* templ, humanDate * strdate);
simpleTimeToTemplate(long calendartime, const char* template, humanDate * strdate);
humanDate *simpleTimeToHuman(long calendartime, humanDate * strdate); humanDate *simpleTimeToHuman(long calendartime, humanDate * strdate);
char *humanSize(long long sbytes, sizeString *s); char *humanSize(long long sbytes, sizeString *s);
void fprintf_depstable_filtered_var(FILE* f,char* st); void fprintf_depstable_filtered_var(FILE* f,char* st);