/* * sql.c: front-end for MySQL database */ #include "config.h" #include #include #include #include #ifdef HAVE_FCNTL_H #include #endif #include #include #include #ifdef HAVE_UNISTD_H #include #endif /******** #include ********/ #include #include "rpm2html.h" /******** #include "rpmdata.h" ********/ #include "sql.h" /************************************************************************ * * * Generic inititialisation/close of the DB * * * ************************************************************************/ static MYSQL *sql = NULL; int init_sql(const char *base, const char *user, const char *passwd) { if (base == NULL) base = "rpmfind"; if (passwd == NULL) passwd = getenv("MySQL_PASS"); if (user == NULL) user = getenv("MySQL_USER"); if (user == NULL) user = getenv("USER"); if (user == NULL) user = getenv("USERNAME"); if (user == NULL) user = getenv("LOGNAME"); sql = mysql_init(NULL); if (mysql_errno(sql)) { fprintf(stderr, "mysql_init failed: %s\n", mysql_error(sql)); return(-1); } mysql_real_connect(sql, "localhost", user, passwd, base, 0, NULL, 0); if (mysql_errno(sql)) { fprintf(stderr, "mysql: connect as %s to %s failed: %s\n", user, base, mysql_error(sql)); return(-1); } sql_check_tables(); return(0); } int close_sql(void) { mysql_close(sql); if (mysql_errno(sql)) { fprintf(stderr, "mysql_close failed: %s\n", mysql_error(sql)); return(-1); } return(0); } /************************************************************************ * * * Generic functions to access the tables * * * ************************************************************************/ int sql_update_id(const char *table, int id, const char *field, const char *value) { MYSQL_RES *result; char query[1000]; int nb_fields = 0; if ((table == NULL) || (field == NULL) || (value == NULL)) return(-1); snprintf(query, 999, "UPDATE %s SET %s='%s' WHERE ID=%d", table, field, value, id); query[999] = 0; if (mysql_query(sql, query)) { printf("sql_update_id: UPDATE %s %d failed: %s\n", table, id, mysql_error(sql)); return(-1); } result = mysql_store_result(sql); if (result != NULL) { nb_fields = mysql_num_fields(result); mysql_free_result(result); } else { nb_fields = 1; } if(mysql_errno(sql)) { fprintf(stderr, "sql_update_id UPDATE error: %s\n", mysql_error(sql)); return(-1); } return(nb_fields); } int sql_blind_insert(const char *table, const char *key, const char *value, int id) { MYSQL_RES *result; char query[1000]; if ((table == NULL) || (key == NULL) || (value == NULL)) return(-1); /* * Search first for the ID if it already exists */ if (id > 0) { snprintf(query, 999, "INSERT INTO %s (ID, %s) VALUES (%d, '%s')", table, key, id, value); } else { snprintf(query, 999, "INSERT INTO %s (%s) VALUES ('%s')", table, key, value); } query[999] = 0; if (mysql_query(sql,query)) { return(-1); } result = mysql_store_result(sql); if (result) { mysql_free_result(result); return(1); } if(mysql_errno(sql)) { fprintf(stderr, "sql_blind_insert Error: %s\n", mysql_error(sql)); return(-1); } return(0); } int sql_update(const char *table, const char *name, const char *field, const char *value) { MYSQL_RES *result; MYSQL_ROW row; char query[1000]; int id; int nb_fields = 0; if ((name == NULL) || (table == NULL) || (field == NULL) || (value == NULL)) return(-1); /* * Search first for the ID if it already exists */ snprintf(query, 999, "SELECT ID FROM %s WHERE Name='%s'", table, name); query[999] = 0; if (mysql_query(sql,query)) { printf("sql_update: SELECT failed\n"); return(-1); } result = mysql_use_result(sql); if (result) { while((row = mysql_fetch_row(result))) { if (row[0] == NULL) { printf("sql_update: select ID for %s returns NULL !\n", name); return(-1); } if (sscanf(row[0], "%d", &id) != 1) { printf("sql_update: ID non numeric %s\n", row[0]); return(-1); } snprintf(query, 999, "UPDATE %s SET %s='%s' WHERE ID=%d", table, field, value, id); query[999] = 0; mysql_free_result(result); if (mysql_query(sql, query)) { printf("sql_update: UPDATE failed: %s\n", mysql_error(sql)); return(-1); } result = mysql_store_result(sql); if (result != NULL) { nb_fields = mysql_num_fields(result); mysql_free_result(result); } else { return(1); } /* Do not loop ... only the first */ return(nb_fields); } mysql_free_result(result); if (nb_fields == 0) { /* * Propagate an insert */ snprintf(query, 999, "INSERT INTO %s (Name,%s) VALUES ('%s','%s')", table, field, name, value); query[999] = 0; mysql_free_result(result); if (mysql_query(sql, query)) { printf("sql_update: INSERT failed: %s\n", mysql_error(sql)); return(-1); } result = mysql_store_result(sql); if (result != NULL) { nb_fields = mysql_num_fields(result); mysql_free_result(result); } else { return(1); } } } if(mysql_errno(sql)) { fprintf(stderr, "sql_update Error: %s\n", mysql_error(sql)); return(-1); } return(nb_fields); } int sql_get_key(const char *table, const char *name) { int id; MYSQL_RES *result; MYSQL_ROW row; char query[200]; if ((table == NULL) || (name == NULL)) return(-1); /* * Search first for the ID if it already exists */ snprintf(query, 199, "SELECT ID FROM %s WHERE Name='%s'", table, name); query[199] = 0; if (mysql_query(sql,query)) { printf("sql_create: SELECT %s failed %s\n", name, mysql_error(sql)); return(-1); } result = mysql_use_result(sql); if (result) { while((row = mysql_fetch_row(result))) { /* * Lookup the first ID and return it */ if (row[0] == NULL) { mysql_free_result(result); printf("sql_create: select returns NULL !\n"); return(-1); } if (sscanf(row[0], "%d", &id) != 1) { mysql_free_result(result); printf("sql_create: ID non numeric %s\n", row[0]); return(-1); } mysql_free_result(result); return(id); } mysql_free_result(result); } if(mysql_errno(sql)) { fprintf(stderr, "Error: %s\n", mysql_error(sql)); return(-1); } /* * Do a creation */ snprintf(query, 199, "INSERT INTO %s (Name) VALUES ('%s')", table, name); query[199] = 0; if (mysql_query(sql,query)) { printf("sql_get_key: INSERT %s failed %s\n", name, mysql_error(sql)); return(-1); } id = mysql_insert_id(sql); result = mysql_store_result(sql); if (result != NULL) mysql_free_result(result); return(id); } int sql_read_key(const char *table, const char *name) { int id; MYSQL_RES *result; MYSQL_ROW row; char query[200]; if ((table == NULL) || (name == NULL)) return(-1); /* * Search for the ID it has to exist */ snprintf(query, 199, "SELECT ID FROM %s WHERE Name='%s'", table, name); query[199] = 0; if (mysql_query(sql,query)) { printf("sql_create: SELECT %s failed %s\n", name, mysql_error(sql)); return(-1); } result = mysql_use_result(sql); if (result) { while((row = mysql_fetch_row(result))) { /* * Lookup the first ID and return it */ if (row[0] == NULL) { mysql_free_result(result); printf("sql_create: select returns NULL !\n"); return(-1); } if (sscanf(row[0], "%d", &id) != 1) { mysql_free_result(result); printf("sql_create: ID non numeric %s\n", row[0]); return(-1); } mysql_free_result(result); return(id); } mysql_free_result(result); } if(mysql_errno(sql)) { fprintf(stderr, "Error: %s\n", mysql_error(sql)); return(-1); } return(-1); } int sql_read_info_key(const char *table, const char *name, const char *value) { int id; MYSQL_RES *result; MYSQL_ROW row; char query[200]; if ((table == NULL) || (name == NULL) || (value == NULL)) return(-1); /* * Search for the ID it has to exist */ snprintf(query, 199, "SELECT ID FROM %s WHERE %s='%s'", table, name, value); query[199] = 0; if (mysql_query(sql,query)) { printf("sql_create: SELECT %s failed %s\n", name, mysql_error(sql)); return(-1); } result = mysql_use_result(sql); if (result) { while((row = mysql_fetch_row(result))) { /* * Lookup the first ID and return it */ if (row[0] == NULL) { mysql_free_result(result); printf("sql_create: select returns NULL !\n"); return(-1); } if (sscanf(row[0], "%d", &id) != 1) { mysql_free_result(result); printf("sql_create: ID non numeric %s\n", row[0]); return(-1); } mysql_free_result(result); return(id); } mysql_free_result(result); } if(mysql_errno(sql)) { fprintf(stderr, "Error: %s\n", mysql_error(sql)); return(-1); } return(-1); } /************************************************************************ * * * Tables handling * * * ************************************************************************/ int sql_rebuild_config(void) { const char *query = "CREATE TABLE Config ( \n\ ID int(11) NOT NULL auto_increment, \n\ Name varchar(50) NOT NULL, \n\ Value varchar(255), \n\ PRIMARY KEY (ID), \n\ KEY Name (Name(10)) \n\ )"; if (mysql_query(sql,query)) { printf("sql_rebuild_config: CREATE TABLE Config failed %s\n", mysql_error(sql)); return(-1); } return(0); } int sql_rebuild_vendors(void) { const char *query = "CREATE TABLE Vendors ( \n\ ID int(11) NOT NULL auto_increment, \n\ Name varchar(255) NOT NULL, \n\ URL varchar(255), \n\ Key1 text, \n\ Key2 text, \n\ Key3 text, \n\ Description text, \n\ PRIMARY KEY (ID), \n\ KEY Name (Name(10)) \n\ )"; if (mysql_query(sql,query)) { printf("sql_rebuild_vendors: CREATE TABLE Vendors failed %s\n", mysql_error(sql)); return(-1); } return(0); } int sql_rebuild_mirrors(void) { const char *query = "CREATE TABLE Mirrors ( \n\ ID int(11), \n\ URL varchar(255) NOT NULL, \n\ Country int(11), \n\ UNIQUE(URL) \n\ )"; if (mysql_query(sql,query)) { printf("sql_rebuild_mirrors: CREATE TABLE Mirrors failed %s\n", mysql_error(sql)); return(-1); } return(0); } int sql_rebuild_metadata(void) { const char *query = "CREATE TABLE Metadata ( \n\ URL varchar(255) NOT NULL, \n\ Maintainer int(11), \n\ Country int(11), \n\ UNIQUE(URL) \n\ )"; if (mysql_query(sql,query)) { printf("sql_rebuild_metadata: CREATE TABLE Metadata failed %s\n", mysql_error(sql)); return(-1); } return(0); } int sql_rebuild_distribs(void) { const char *query = "CREATE TABLE Distribs ( \n\ ID int(11) NOT NULL auto_increment, \n\ Name varchar(255) NOT NULL, \n\ Vendor int(11), \n\ Directory varchar(255), \n\ Path varchar(100) NOT NULL, \n\ URL varchar(255), \n\ URLSrc varchar(255), \n\ Key1 text, \n\ Key2 text, \n\ Description text, \n\ PRIMARY KEY (ID), \n\ KEY Name (Name(10)) \n\ )"; if (mysql_query(sql,query)) { printf("sql_rebuild_distribs: CREATE TABLE Distribs failed %s\n", mysql_error(sql)); return(-1); } return(0); } int sql_rebuild_packages(void) { const char *query = "CREATE TABLE Packages ( \n\ ID int(11) NOT NULL auto_increment, \n\ filename varchar(255) NOT NULL, \n\ Name varchar(50) NOT NULL, \n\ Version varchar(50) NOT NULL, \n\ Release varchar(50) NOT NULL, \n\ Dist int(11), \n\ URL varchar(255), \n\ URLSrc varchar(255), \n\ Vendor int(11), \n\ Packager int(11), \n\ Category varchar(255), \n\ Summary varchar(255), \n\ Description text, \n\ Copyright varchar(255), \n\ PRIMARY KEY (ID), \n\ KEY Name (Name(10)) \n\ )"; if (mysql_query(sql,query)) { printf("sql_rebuild_packages: CREATE TABLE Packages failed %s\n", mysql_error(sql)); return(-1); } return(0); } int sql_rebuild_files(void) { const char *query = "CREATE TABLE Files ( \n\ ReleaseID int(11) NOT NULL, \n\ Path varchar(255) NOT NULL, \n\ KEY ReleaseID (ReleaseID), \n\ KEY Path (Path(20)) \n\ )"; if (mysql_query(sql,query)) { printf("sql_rebuild_files: CREATE TABLE Files failed %s\n", mysql_error(sql)); return(-1); } return(0); } int sql_check_tables(void) { const char *query = "SHOW TABLES"; MYSQL_RES *result; MYSQL_ROW row; int config = 0; int distribs = 0; int vendors = 0; int mirrors = 0; int metadata = 0; int packages = 0; int files = 0; int rebuilt = 0; if (mysql_query(sql,query)) { printf("sql_check_tables: SHOW TABLES failed %s\n", mysql_error(sql)); return(-1); } result = mysql_use_result(sql); if (result) { while((row = mysql_fetch_row(result))) { if (row[0] == NULL) { mysql_free_result(result); printf("sql_check_tables: SHOW TABLES returns NULL !\n"); return(-1); } if (!strcmp(row[0], "Config")) config = 1; if (!strcmp(row[0], "Distribs")) distribs = 1; if (!strcmp(row[0], "Vendors")) vendors = 1; if (!strcmp(row[0], "Mirrors")) mirrors = 1; if (!strcmp(row[0], "Metadata")) metadata = 1; if (!strcmp(row[0], "Packages")) packages = 1; if (!strcmp(row[0], "Files")) files = 1; } mysql_free_result(result); } if(mysql_errno(sql)) { fprintf(stderr, "Error: %s\n", mysql_error(sql)); return(-1); } if (!config) { fprintf(stderr, "Table Config disapeared: rebuilding it\n"); if (!sql_rebuild_config()) rebuilt++; } if (!vendors) { fprintf(stderr, "Table Vendors disapeared: rebuilding it\n"); if (!sql_rebuild_vendors()) rebuilt++; } if (!distribs) { fprintf(stderr, "Table Distribs disapeared: rebuilding it\n"); if (!sql_rebuild_distribs()) rebuilt++; } if (!mirrors) { fprintf(stderr, "Table Mirrors disapeared: rebuilding it\n"); if (!sql_rebuild_mirrors()) rebuilt++; } if (!metadata) { fprintf(stderr, "Table Metadata disapeared: rebuilding it\n"); if (!sql_rebuild_metadata()) rebuilt++; } if (!packages) { fprintf(stderr, "Table Packages disapeared: rebuilding it\n"); if (!sql_rebuild_packages()) rebuilt++; } if (!files) { fprintf(stderr, "Table Files disapeared: rebuilding it\n"); if (!sql_rebuild_files()) rebuilt++; } return(rebuilt); } /************************************************************************ * * * Specific rpm2html functions * * * ************************************************************************/ int add_dist_mirror(int distrib, const char *URL, int country) { if (URL == NULL) return(-1); if (distrib < 0) return(-1); return(sql_blind_insert("Mirrors", "URL", URL, distrib)); } int add_mirror(const char *Name, const char *URL, int country) { int distrib; if ((Name == NULL) || (URL == NULL)) return(-1); distrib = sql_read_key("Distribs", Name); if (distrib < 0) return(distrib); return(sql_blind_insert("Mirrors", "URL", URL, distrib)); } int add_package(const char *filename, const char *Name, const char *Version, const char *Release, int dist, const char *URL, const char *URLSrc, const char *Vendor, const char *Packager, const char *Category, const char *Summary, const char *Description, const char *Copyright) { int id; int vendor; /* int packager; */ int nb_fields = 0; char intStr[15]; if ((filename == NULL) || (URL == NULL) || (dist <= 0)) return(-1); if ((Name == NULL) || (Version == NULL) || (Release == NULL)) return(-1); id = sql_get_key("Packages", filename); if (id <= 0) return(-1); nb_fields = 1; if (Vendor != NULL) { vendor = sql_get_key("Vendors", Vendor); sprintf(intStr, "%d", vendor); nb_fields += sql_update_id("Packages", id, "Vendor", intStr); } /******* if (Packager != NULL) { packager = sql_get_key("People", Packager); sprintf(intStr, "%d", packager); nb_fields += sql_update_id("Distribs", id, "Packager", intStr); } *******/ if (Name != NULL) nb_fields += sql_update_id("Packages", id, "Package", Name); if (Version != NULL) nb_fields += sql_update_id("Packages", id, "Version", Version); if (Release != NULL) nb_fields += sql_update_id("Packages", id, "Release", Release); if (Category != NULL) nb_fields += sql_update_id("Packages", id, "Category", Category); if (URL != NULL) nb_fields += sql_update_id("Packages", id, "URL", URL); if (URLSrc != NULL) nb_fields += sql_update_id("Packages", id, "URLSrc", URLSrc); if (Summary != NULL) nb_fields += sql_update_id("Packages", id, "Summary", Summary); if (Description != NULL) nb_fields += sql_update_id("Packages", id, "Description", Description); if (Copyright != NULL) nb_fields += sql_update_id("Packages", id, "Copyright", Copyright); return(nb_fields); } int add_distrib(const char *Name, const char *Vendor, const char *Directory, const char *Path, const char *URL, const char *URLSrc, const char *Description) { int id, vendor; int nb_fields = 0; char VendorStr[15]; if (Name == NULL) return(-1); id = sql_get_key("Distribs", Name); nb_fields = 1; if (Vendor != NULL) { vendor = sql_get_key("Vendors", Vendor); sprintf(VendorStr, "%d", vendor); nb_fields += sql_update_id("Distribs", id, "Vendor", VendorStr); } if (Directory != NULL) nb_fields += sql_update_id("Distribs", id, "Directory", Directory); if (Path != NULL) nb_fields += sql_update_id("Distribs", id, "Path", Path); if (URL != NULL) nb_fields += sql_update_id("Distribs", id, "URL", URL); if (URLSrc != NULL) nb_fields += sql_update_id("Distribs", id, "URLSrc", URLSrc); if (Description != NULL) nb_fields += sql_update_id("Distribs", id, "Description", Description); return(nb_fields); } int add_vendor(const char *Name, const char *URL, const char *Description) { int id; int nb_fields = 0; if (Name == NULL) return(-1); id = sql_get_key("Vendors", Name); nb_fields = 1; if (URL != NULL) nb_fields += sql_update_id("Vendors", id, "URL", URL); if (Description != NULL) nb_fields += sql_update_id("Vendors", id, "Description", Description); return(nb_fields); } void add_config_info(const char *name, const char *value) { sql_update("Config", name, "Value", value); } void add_metadata_base(const char *URL) { sql_blind_insert("Metadata", "URL", URL, -1); } void show_vendors(void) { MYSQL_RES *result; MYSQL_ROW row; mysql_query(sql,"SELECT Name, URL FROM Vendors"); result = mysql_use_result(sql); while((row = mysql_fetch_row(result))) { if (row[0] == NULL) printf("NULL !\n"); else { if (row[1] == NULL) printf("%s : no url\n", row[0]); else printf("%s : %s\n", row[0], row[1]); } } if(mysql_errno(sql)) { fprintf(stderr, "Error: %s\n", mysql_error(sql)); } } void show_distribs(void) { MYSQL_RES *result; MYSQL_ROW row; mysql_query(sql,"SELECT Name, Path, URL FROM Distribs"); result = mysql_use_result(sql); while((row = mysql_fetch_row(result))) { if (row[0] == NULL) printf("NULL !\n"); else { if (row[1] == NULL) printf("%s : no Path\n", row[0]); else { if (row[2] == NULL) printf("%s : %s : no url\n", row[0], row[1]); else printf("%s : %s : %s\n", row[0], row[1], row[2]); } } } if(mysql_errno(sql)) { fprintf(stderr, "Error: %s\n", mysql_error(sql)); } } int show_table_stats(const char *table, const char *key) { char query[1000]; MYSQL_RES *result; MYSQL_ROW row; int res; /* * Search first for the ID if it already exists */ snprintf(query, 999, "SELECT COUNT(%s) FROM %s", key, table); query[999] = 0; if (mysql_query(sql,query)) { printf("show_table_stats: SELECT COUNT failed: %s\n", mysql_error(sql)); return(-1); } result = mysql_use_result(sql); if (result) { while((row = mysql_fetch_row(result))) { /* * Lookup the value and return it */ if (row[0] == NULL) { mysql_free_result(result); printf("show_table_stats: select count returns NULL !\n"); return(-1); } if (sscanf(row[0], "%d", &res) != 1) { mysql_free_result(result); printf("show_table_stats: value non numeric %s\n", row[0]); return(-1); } mysql_free_result(result); if (res <= 0) printf(" %s is empty\n", table); else printf(" %s contains %d records\n", table, res); return(res); } mysql_free_result(result); } if(mysql_errno(sql)) { fprintf(stderr, "show_stats error: %s\n", mysql_error(sql)); } return(-1); } int show_stats(void) { const char *query = "SHOW TABLES"; MYSQL_RES *result; MYSQL_ROW row; int tables = 0; int records = 0; if (mysql_query(sql,query)) { printf("sql_check_tables: SHOW TABLES failed %s\n", mysql_error(sql)); return(-1); } result = mysql_use_result(sql); if (result) { while((row = mysql_fetch_row(result))) { if (row[0] == NULL) { mysql_free_result(result); printf("sql_check_tables: SHOW TABLES returns NULL !\n"); return(-1); } tables++; } mysql_free_result(result); } if(mysql_errno(sql)) { fprintf(stderr, "show_stats error: %s\n", mysql_error(sql)); } printf("%d tables in use\n", tables); records += show_table_stats("Config", "Name"); records += show_table_stats("Distribs", "Name"); records += show_table_stats("Vendors", "Name"); records += show_table_stats("Mirrors", "URL"); records += show_table_stats("Metadata", "URL"); records += show_table_stats("Packages", "Name"); records += show_table_stats("Files", "Path"); printf("Total: %d records\n", records); return(records); } /************************************************************************ * * * rpm2html configuration functions * * * ************************************************************************/ #ifndef STANDALONE int readConfigSql(void) { char query[1000]; MYSQL_RES *result; MYSQL_ROW row; int dir = 0; /* * General configuration informations */ snprintf(query, 999, "SELECT Name,Value FROM Config"); query[999] = 0; if (mysql_query(sql,query)) { printf("sql_check_tables: SELECT Config failed %s\n", mysql_error(sql)); return(-1); } result = mysql_use_result(sql); if (result) { while((row = mysql_fetch_row(result))) { if ((row[0] == NULL) || (row[1] == NULL)) { fprintf(stderr, "readConfigSql : found NULL value\n"); continue; } if (!strcmp(row[0], "dir")) dir = 1; addConfigEntry(RPM2HTML_NAME, row[0], row[1]); } mysql_free_result(result); } if(mysql_errno(sql)) { fprintf(stderr, "show_stats error: %s\n", mysql_error(sql)); } if (dir == 0) { fprintf(stderr, "readConfigSql : no directory\n"); return(-1); } /* * The metadata mirror list. */ snprintf(query, 999, "SELECT URL FROM Metadata"); query[999] = 0; if (mysql_query(sql,query)) { printf("sql_check_tables: SELECT Metadata failed %s\n", mysql_error(sql)); return(-1); } result = mysql_use_result(sql); if (result) { while((row = mysql_fetch_row(result))) { if (row[0] == NULL) { fprintf(stderr, "readConfigSql : found NULL metadata\n"); continue; } addConfigEntry("metadata", "mirror", row[0]); } mysql_free_result(result); } if(mysql_errno(sql)) { fprintf(stderr, "show_stats error: %s\n", mysql_error(sql)); } /* * The distribution lists */ snprintf(query, 999, "SELECT Directory,Name,URL,URLSrc,Path FROM Distribs"); query[999] = 0; if (mysql_query(sql,query)) { printf("sql_check_tables: SELECT Distribs failed %s\n", mysql_error(sql)); return(-1); } result = mysql_use_result(sql); if (result) { while((row = mysql_fetch_row(result))) { if (row[0] == NULL) { fprintf(stderr, "readConfigSql : found NULL distro\n"); continue; } if (row[1] != NULL) addConfigEntry(row[0], "name", row[1]); if (row[2] != NULL) addConfigEntry(row[0], "ftp", row[2]); if (row[3] != NULL) addConfigEntry(row[0], "ftp", row[3]); if (row[4] != NULL) addConfigEntry(row[0], "subdir", row[4]); } mysql_free_result(result); } if(mysql_errno(sql)) { fprintf(stderr, "show_stats error: %s\n", mysql_error(sql)); } /* * The Mirrors */ snprintf(query, 999, "SELECT Distribs.Directory,Mirrors.URL FROM Distribs,Mirrors WHERE Distribs.ID = Mirrors.ID"); query[999] = 0; if (mysql_query(sql,query)) { printf("sql_check_tables: SELECT Distribs failed %s\n", mysql_error(sql)); return(-1); } result = mysql_use_result(sql); if (result) { while((row = mysql_fetch_row(result))) { if ((row[0] == NULL) || (row[1] == NULL)) { fprintf(stderr, "readConfigSql : found NULL mirror\n"); continue; } addConfigEntry(row[0], "mirror", row[1]); } mysql_free_result(result); } if(mysql_errno(sql)) { fprintf(stderr, "show_stats error: %s\n", mysql_error(sql)); } return(0); } void sqlConfigEntry(const char *rpmdir, const char *name, const char *value) { int distrib; if (rpm2htmlVerbose > 1) printf("sqlConfigEntry(\"%s\", \"%s\", \"%s\")\n", rpmdir, name, value); /* * case of global option for rpm2html. */ if (!strcasecmp(rpmdir, RPM2HTML_NAME)) { add_config_info(name, value); return; } /* * Options for the metadata mirrors. */ if (!strcasecmp(rpmdir, "metadata")) { if (!strcasecmp(name, "mirror")) { add_metadata_base(value); } else { printf("Config file : %s entry for [metadata] ignored\n", name); } return; } /* * option for a directory. */ if (!strcasecmp(name, "name")) { add_distrib(value, NULL, rpmdir, NULL, NULL, NULL, NULL); } else if (!strcasecmp(name, "subdir")) { distrib = sql_read_info_key("Distribs", "Directory", rpmdir); if (distrib > 0) sql_update_id("Distribs", distrib, "Path", value); } else if (!strcasecmp(name, "url")) { distrib = sql_read_info_key("Distribs", "Directory", rpmdir); if (distrib > 0) sql_update_id("Distribs", distrib, "URL", value); } else if (!strcasecmp(name, "ftp")) { distrib = sql_read_info_key("Distribs", "Directory", rpmdir); if (distrib > 0) sql_update_id("Distribs", distrib, "URL", value); } else if (!strcasecmp(name, "ftpsrc")) { distrib = sql_read_info_key("Distribs", "Directory", rpmdir); if (distrib > 0) sql_update_id("Distribs", distrib, "URLSrc", value); } else if (!strcasecmp(name, "mirror")) { distrib = sql_read_info_key("Distribs", "Directory", rpmdir); if (distrib > 0) add_dist_mirror(distrib, value, 0); } else { printf("Config file : %s entry for [%s] ignored\n", name, rpmdir); } } #endif /************************************************************************ * * * Main part when compiled standalone * * * ************************************************************************/ #ifdef STANDALONE void usage(const char *name) { printf("%s: usage\n", name); printf(" vendors: list the registered vendors\n"); printf(" distribs: list the registered distribs\n"); printf(" add distrib name [vendor [directory [path [url [ urlsrc [description]]]]]]\n"); printf(" add vendor name [url [description]]\n"); printf(" add mirror distrib url\n"); exit(1); } int main(int argc, char **argv) { int res; if (argc < 2) usage(argv[0]); if (init_sql(NULL, NULL, NULL) < 0) exit(1); res = sql_check_tables(); if (res > 0) { printf("rebuilt %d tables\n", res); } if (!strcmp(argv[1], "vendors")) show_vendors(); else if (!strcmp(argv[1], "distribs")) show_distribs(); else if (!strcmp(argv[1], "stats")) show_stats(); else if (!strcmp(argv[1], "add")) { if (argc < 5) usage(argv[0]); if (!strcmp(argv[2], "distrib")) { char *Name = NULL; char *Path = NULL; char *Directory = NULL; char *Vendor = NULL; char *URL = NULL; char *URLSrc = NULL; char *Description = NULL; if ((argc > 3) && (argv[3][0] != 0)) Name = argv[3]; if ((argc > 4) && (argv[4][0] != 0)) Vendor = argv[4]; if ((argc > 5) && (argv[5][0] != 0)) Directory = argv[5]; if ((argc > 6) && (argv[6][0] != 0)) Path = argv[6]; if ((argc > 7) && (argv[7][0] != 0)) URL = argv[7]; if ((argc > 8) && (argv[8][0] != 0)) URLSrc = argv[8]; if ((argc > 9) && (argv[9][0] != 0)) Description = argv[9]; res = add_distrib(Name, Vendor, Directory, Path, URL, URLSrc, Description); printf("updated %d fields\n", res); } else if (!strcmp(argv[2], "vendor")) { char *Name = NULL; char *URL = NULL; char *Description = NULL; if ((argc > 3) && (argv[3][0] != 0)) Name = argv[3]; if ((argc > 4) && (argv[4][0] != 0)) URL = argv[4]; if ((argc > 5) && (argv[5][0] != 0)) Description = argv[5]; res = add_vendor(Name, URL, Description); printf("updated %d fields\n", res); } else if (!strcmp(argv[2], "mirror")) { char *Name = NULL; char *URL = NULL; if ((argc > 3) && (argv[3][0] != 0)) Name = argv[3]; if ((argc > 4) && (argv[4][0] != 0)) URL = argv[4]; res = add_mirror(Name, URL, 0); printf("updated %d fields\n", res); } } if (close_sql() < 0) return(1); exit(0); } #endif