"Fossies" - the Fresh Open Source Software Archive

Member "rpm2html-1.11.2/sql.c" (5 Oct 2010, 84643 Bytes) of package /linux/privat/rpm2html-1.11.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "sql.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * sql.c: front-end for MySQL database
    3  */
    4 
    5 #include "config.h"
    6 
    7 #include <stdio.h>
    8 #include <stdlib.h>
    9 #include <sys/types.h>
   10 #include <sys/stat.h>
   11 #include <sys/wait.h>
   12 
   13 #ifdef HAVE_FCNTL_H
   14 #include <fcntl.h>
   15 #endif
   16 #include <stdio.h>
   17 #include <stdlib.h>
   18 #include <string.h>
   19 #include <time.h>
   20 #include <errno.h>
   21 #include <signal.h>
   22 #ifdef HAVE_UNISTD_H
   23 #include <unistd.h>
   24 #endif
   25 /********
   26 #include <rpm/rpmlib.h>
   27  ********/
   28 #include <zlib.h>
   29 
   30 #include <mysql/mysql.h>
   31 #include <mysql/errmsg.h>
   32 #include <zlib.h>
   33 #include "rpm2html.h"
   34 #include "rpmdata.h"
   35 /********
   36 #include "rpmdata.h"
   37  ********/
   38 #include "sql.h"
   39 #include "html.h"
   40 
   41 /* #define SQL_DEBUG_TIMING */
   42 #define SQL_DEBUG_MEM 
   43 
   44 #define SQL_MAX_DISTRIBS 500
   45 
   46 /************************************************************************
   47  *                                  *
   48  *      Generic inititialisation/close of the DB        *
   49  *                                  *
   50  ************************************************************************/
   51 
   52 static MYSQL *sql = NULL;
   53 static const char *myhost, *mybase, *myuser, *mypasswd;
   54 
   55 int init_sql(const char *host, const char *base, const char *user, const char *passwd) {
   56     int attempts = 0;
   57 
   58     /*
   59      * Close the previous connection if any
   60      */
   61     if (sql != NULL) {
   62     close_sql();
   63     if (myhost != NULL) free((char *) myhost);
   64     myhost = NULL;
   65     if (mybase != NULL) free((char *) mybase);
   66     mybase = NULL;
   67     if (myuser != NULL) free((char *) myuser);
   68     myuser = NULL;
   69     if (mypasswd != NULL) free((char *) mypasswd);
   70     mypasswd = NULL;
   71     }
   72 
   73     if (host == NULL)
   74     host = getenv("MySQL_HOST");
   75     if (host == NULL)
   76     host = "localhost";
   77     if (base == NULL)
   78     base = getenv("MySQL_BASE");
   79     if (base == NULL)
   80     base = "rpmfind";
   81     if (passwd == NULL)
   82     passwd = getenv("MySQL_PASS");
   83     if (user == NULL)
   84     user = getenv("MySQL_USER");
   85     if (user == NULL)
   86     user = getenv("USER");
   87     if (user == NULL)
   88     user = getenv("USERNAME");
   89     if (user == NULL)
   90     user = getenv("LOGNAME");
   91     sql = mysql_init(NULL);
   92     if (mysql_errno(sql)) {
   93     fprintf(stderr, "mysql_init failed: %s\n", mysql_error(sql));
   94     return(-1);
   95     }
   96     for (attempts = 0; ;attempts ++) {
   97     mysql_real_connect(sql, host, user, passwd, base, 0, NULL, 0);
   98     if (mysql_errno(sql)) {
   99         if ((attempts == 0) && (passwd == NULL)) {
  100         fprintf(stderr, "No passwd defined, use MySQL_PASS env\n");
  101         }
  102         if (attempts >= 20) {
  103         fprintf(stderr,
  104             "mysql: connect as %s to %s on host %s  failed: %s\n",
  105                 user, base, host, mysql_error(sql));
  106         return(-1);
  107         } else {
  108         fprintf(stderr,
  109             "mysql: retrying connect as %s to %s on host %s  failed: %s\n",
  110                 user, base, host, mysql_error(sql));
  111         }
  112     } else {
  113         break;
  114     }
  115     sleep(15);
  116     }
  117     myhost = strdup(host);
  118     mybase = strdup(base);
  119     myuser = strdup(user);
  120     mypasswd = strdup(passwd);
  121     sql_check_tables();
  122     return(0);
  123 }
  124 
  125 int close_sql(void) {
  126     mysql_close(sql);
  127     if (mysql_errno(sql)) {
  128     fprintf(stderr, "mysql_close failed: %s\n", mysql_error(sql));
  129     return(-1);
  130     }
  131     sql = NULL;
  132     return(0);
  133 }
  134 
  135 int restart_sql(void) {
  136     int attempts = 0;
  137 
  138     /*
  139      * Close the previous connection if any
  140      */
  141     if (sql != NULL) {
  142     close_sql();
  143     sql = NULL;
  144     }
  145 
  146     sql = mysql_init(NULL);
  147     if (mysql_errno(sql)) {
  148     fprintf(stderr, "mysql_init failed: %s\n", mysql_error(sql));
  149     return(-1);
  150     }
  151     for (attempts = 0; ;attempts ++) {
  152     mysql_real_connect(sql, myhost, myuser, mypasswd, mybase, 0, NULL, 0);
  153     if (mysql_errno(sql)) {
  154         if (attempts >= 20) {
  155         fprintf(stderr,
  156             "mysql: reconnect as %s to %s on host %s  failed: %s\n",
  157                 myuser, mybase, myhost, mysql_error(sql));
  158         return(-1);
  159         }
  160     } else {
  161         break;
  162     }
  163     sleep(15);
  164     }
  165     sql_check_tables();
  166     return(0);
  167 }
  168 
  169 /*
  170  * Handle disconnects by doing a reconnect and a retry.
  171  */
  172 int do_sql_query(const char *query, int len) {
  173     int res;
  174 
  175     if (sql == NULL) {
  176     res = restart_sql();
  177     if (res != 0)
  178         return(res);
  179     }
  180     res = mysql_real_query(sql, query, len);
  181     if ((res == CR_SERVER_GONE_ERROR) || (res == CR_SERVER_LOST)) {
  182     res = restart_sql();
  183     if (res != 0)
  184         return(res);
  185     res = mysql_real_query(sql, query, len);
  186     }
  187     return(res);
  188 }
  189 
  190 /************************************************************************
  191  *                                  *
  192  *      Generic functions to access the tables          *
  193  *                                  *
  194  ************************************************************************/
  195 
  196 #define MAX_QUERY 8000
  197 #define SMALL_QUERY 500
  198 
  199 int sql_update_id(const char *table, int id,
  200               const char *field, const char *value) {
  201     MYSQL_RES *result;
  202     char query[MAX_QUERY];
  203     int nb_fields = 0;
  204     int left = MAX_QUERY - 1;
  205     int len;
  206     char *end;
  207 
  208     if ((table == NULL) ||
  209     (field == NULL) || (value == NULL))
  210     return(-1);
  211 
  212     len = snprintf(query, left, "UPDATE %s SET %s='", table, field);
  213     if (len < 0) {
  214     fprintf(stderr, "sql_update_id : %s(%d).%s overflow\n",
  215         table, id, field);
  216     return(-1);
  217     }
  218     end = &query[len];
  219     left -= len;
  220     len = strlen(value);
  221     if (len * 2 >= left) {
  222     fprintf(stderr, "sql_update_id : %s(%d).%s overflow\n",
  223         table, id, field);
  224     return(-1);
  225     }
  226     len = mysql_escape_string(end, value, len);
  227     left -= len;
  228     if (left <= 0) {
  229     fprintf(stderr, "sql_update_id : %s(%d).%s overflow\n",
  230         table, id, field);
  231     return(-1);
  232     }
  233     end += len;
  234     len = snprintf(end, left, "' WHERE ID=%d", id);
  235     if (len < 0) {
  236     fprintf(stderr, "sql_update_id : %s(%d).%s overflow\n",
  237         table, id, field);
  238     return(-1);
  239     }
  240     end += len;
  241     query[MAX_QUERY - 1] = 0;
  242 
  243     if (do_sql_query(query, (unsigned int) (end - query))) {
  244     printf("sql_update_id: UPDATE %s %d failed: %s\n",
  245            table, id, mysql_error(sql));
  246     return(-1);
  247     }
  248     result = mysql_store_result(sql);
  249     if (result != NULL) {
  250     nb_fields = mysql_num_fields(result);
  251     mysql_free_result(result);
  252     } else {
  253     nb_fields = 1;
  254     }
  255     if(mysql_errno(sql)) {
  256     fprintf(stderr, "sql_update_id UPDATE error: %s\n",
  257          mysql_error(sql));
  258     return(-1);
  259     }
  260     return(nb_fields);
  261 }
  262 
  263 int sql_blind_insert(const char *table, const char *key,
  264                      const char *value, int id) {
  265     MYSQL_RES *result;
  266     char query[MAX_QUERY];
  267     int left = MAX_QUERY - 1;
  268     int len;
  269     char *end;
  270     int insert = -1;
  271 
  272     if ((table == NULL) ||
  273     (key == NULL) || (value == NULL))
  274     return(-1);
  275 
  276     /*
  277      * Search first for the ID if it already exists
  278      */
  279     if (id > 0) {
  280     len = snprintf(query, left, "INSERT INTO %s (ID, %s) VALUES (%d, '",
  281                table, key, id);
  282     if (len < 0) {
  283         fprintf(stderr, "sql_blind_insert : %s(%d).%s overflow\n",
  284             table, id, key);
  285         return(-1);
  286     }
  287     end = &query[len];
  288     left -= len;
  289     len = strlen(value);
  290     if (len * 2 >= left) {
  291         fprintf(stderr, "sql_blind_insert : %s(%d).%s overflow\n",
  292             table, id, key);
  293         return(-1);
  294     }
  295     len = mysql_escape_string(end, value, len);
  296     left -= len;
  297     if (left <= 0) {
  298         fprintf(stderr, "sql_blind_insert : %s(%d).%s overflow\n",
  299             table, id, key);
  300         return(-1);
  301     }
  302     end += len;
  303     len = snprintf(end, left, "')");
  304     if (len < 0) {
  305         fprintf(stderr, "sql_blind_insert : %s(%d).%s overflow\n",
  306             table, id, key);
  307         return(-1);
  308     }
  309     end += len;
  310     query[MAX_QUERY - 1] = 0;
  311 
  312     } else {
  313     len = snprintf(query, left, "INSERT INTO %s (%s) VALUES ('",
  314                table, key);
  315     if (len < 0) {
  316         fprintf(stderr, "sql_blind_insert : %s.%s overflow\n",
  317             table, key);
  318         return(-1);
  319     }
  320     end = &query[len];
  321     left -= len;
  322     len = strlen(value);
  323     if (len * 2 >= left) {
  324         fprintf(stderr, "sql_blind_insert : %s.%s overflow\n",
  325             table, key);
  326         return(-1);
  327     }
  328     len = mysql_escape_string(end, value, len);
  329     left -= len;
  330     if (left <= 0) {
  331         fprintf(stderr, "sql_blind_insert : %s.%s overflow\n",
  332             table, key);
  333         return(-1);
  334     }
  335     end += len;
  336     len = snprintf(end, left, "')");
  337     if (len < 0) {
  338         fprintf(stderr, "sql_blind_insert : %s.%s overflow\n",
  339             table, key);
  340         return(-1);
  341     }
  342     end += len;
  343     query[MAX_QUERY - 1] = 0;
  344 
  345     }
  346     query[MAX_QUERY - 1] = 0;
  347     if (do_sql_query(query, (unsigned int) (end - query))) {
  348 #ifdef SQL_DEBUG
  349     fprintf(stderr, "sql_blind_insert Error: %s\n", mysql_error(sql));
  350 #endif
  351     return(-1);
  352     }
  353     result = mysql_store_result(sql);
  354     insert = mysql_insert_id(sql);
  355     if (result) {
  356     mysql_free_result(result);
  357     return(insert);
  358     }
  359     if(mysql_errno(sql)) {
  360     fprintf(stderr, "sql_blind_insert Error: %s\n", mysql_error(sql));
  361     return(-1);
  362     }
  363     return(insert);
  364 }
  365 
  366 int sql_update(const char *table, const char *name,
  367            const char *field, const char *value) {
  368     MYSQL_RES *result;
  369     MYSQL_ROW row;
  370     char query[MAX_QUERY];
  371     int id;
  372     int nb_fields = 0;
  373     int left = MAX_QUERY - 1;
  374     int len;
  375     char *end;
  376 
  377     if ((name == NULL) || (table == NULL) ||
  378     (field == NULL) || (value == NULL))
  379     return(-1);
  380 
  381     /*
  382      * Search first for the ID if it already exists
  383      */
  384     snprintf(query, MAX_QUERY - 1, "SELECT ID FROM %s WHERE Name='%s'", table, name);
  385     query[MAX_QUERY - 1] = 0;
  386     if (mysql_query(sql,query)) {
  387     printf("sql_update: SELECT failed\n");
  388     return(-1);
  389     }
  390 
  391     result = mysql_use_result(sql);
  392     if (result) {
  393     while((row = mysql_fetch_row(result)))
  394     {
  395         if (row[0] == NULL) {
  396         printf("sql_update: select ID for %s returns NULL !\n", name);
  397         return(-1);
  398         }
  399         if (sscanf(row[0], "%d", &id) != 1) {
  400         printf("sql_update: ID non numeric %s\n", row[0]);
  401         return(-1);
  402         }
  403 
  404         left = MAX_QUERY - 1;
  405         len = snprintf(query, left, "UPDATE %s SET %s='", table, field);
  406         if (len < 0) {
  407         fprintf(stderr, "sql_update : %s(%d).%s overflow\n",
  408             table, id, field);
  409         return(-1);
  410         }
  411         end = &query[len];
  412         left -= len;
  413         len = strlen(value);
  414         if (len * 2 >= left) {
  415         fprintf(stderr, "sql_update : %s(%d).%s overflow\n",
  416             table, id, field);
  417         return(-1);
  418         }
  419         len = mysql_escape_string(end, value, len);
  420         left -= len;
  421         if (left <= 0) {
  422         fprintf(stderr, "sql_update : %s(%d).%s overflow\n",
  423             table, id, field);
  424         return(-1);
  425         }
  426         end += len;
  427         len = snprintf(end, left, "' WHERE ID=%d", id);
  428         if (len < 0) {
  429         fprintf(stderr, "sql_update : %s(%d).%s overflow\n",
  430             table, id, field);
  431         return(-1);
  432         }
  433         end += len;
  434         query[MAX_QUERY - 1] = 0;
  435 
  436         mysql_free_result(result);
  437         if (do_sql_query(query, (unsigned int) (end - query))) {
  438         printf("sql_update: UPDATE failed: %s\n", mysql_error(sql));
  439         return(-1);
  440         }
  441         result = mysql_store_result(sql);
  442         if (result != NULL) {
  443         nb_fields = mysql_num_fields(result);
  444         mysql_free_result(result);
  445         } else {
  446         return(1);
  447         }
  448         /* Do not loop ... only the first */
  449         return(nb_fields);
  450     }
  451     mysql_free_result(result);
  452     if (nb_fields == 0) {
  453         /*
  454          * Propagate an insert
  455          */
  456         snprintf(query, MAX_QUERY - 1,
  457             "INSERT INTO %s (Name,%s) VALUES ('%s','%s')",
  458              table, field, name, value);
  459         query[MAX_QUERY - 1] = 0;
  460         if (mysql_query(sql, query)) {
  461         printf("sql_update: INSERT failed: %s\n", mysql_error(sql));
  462         return(-1);
  463         }
  464         result = mysql_store_result(sql);
  465         if (result != NULL) {
  466         nb_fields = mysql_num_fields(result);
  467         mysql_free_result(result);
  468         } else {
  469         return(1);
  470         }
  471     }
  472     }
  473     if(mysql_errno(sql)) {
  474     fprintf(stderr, "sql_update Error: %s\n", mysql_error(sql));
  475     return(-1);
  476     }
  477     return(nb_fields);
  478 }
  479 
  480 int sql_get_key(const char *table, const char *name, const char *value) {
  481     int id;
  482     MYSQL_RES *result;
  483     MYSQL_ROW row;
  484     char query[MAX_QUERY];
  485     int left = MAX_QUERY - 1;
  486     int len;
  487     char *end;
  488 
  489     if ((table == NULL) || (name == NULL))
  490     return(-1);
  491 
  492 
  493     /*
  494      * Search first for the ID if it already exists
  495      */
  496     len = snprintf(query, left, "SELECT ID FROM %s WHERE %s='",
  497                table, name);
  498     if (len < 0) {
  499     fprintf(stderr, "sql_get_key : %s(%s).%s overflow\n",
  500         table, name, value);
  501     return(-1);
  502     }
  503     end = &query[len];
  504     left -= len;
  505     len = strlen(value);
  506     if (len * 2 >= left) {
  507     fprintf(stderr, "sql_get_key : %s(%s).%s overflow\n",
  508         table, name, value);
  509     return(-1);
  510     }
  511     len = mysql_escape_string(end, value, len);
  512     left -= len;
  513     if (left <= 0) {
  514     fprintf(stderr, "sql_get_key : %s(%s).%s overflow\n",
  515         table, name, value);
  516     return(-1);
  517     }
  518     end += len;
  519     len = snprintf(end, left, "'");
  520     if (len < 0) {
  521     fprintf(stderr, "sql_get_key : %s(%s).%s overflow\n",
  522         table, name, value);
  523     return(-1);
  524     }
  525     end += len;
  526     query[MAX_QUERY - 1] = 0;
  527 
  528     if (mysql_query(sql,query)) {
  529     printf("sql_get_key: SELECT %s failed %s\n", name, mysql_error(sql));
  530     return(-1);
  531     }
  532 
  533     result = mysql_use_result(sql);
  534     if (result) {
  535     while((row = mysql_fetch_row(result)))
  536     {
  537         /*
  538          * Lookup the first ID and return it
  539          */
  540         if (row[0] == NULL) {
  541         mysql_free_result(result);
  542         printf("sql_get_key: select returns NULL !\n");
  543         return(-1);
  544         }
  545         if (sscanf(row[0], "%d", &id) != 1) {
  546         mysql_free_result(result);
  547         printf("sql_get_key: ID non numeric %s\n", row[0]);
  548         return(-1);
  549         }
  550         mysql_free_result(result);
  551         return(id);
  552     }
  553     mysql_free_result(result);
  554     }
  555     if(mysql_errno(sql)) {
  556     fprintf(stderr, "Error: %s\n", mysql_error(sql));
  557     return(-1);
  558     }
  559 
  560     /*
  561      * Do a creation
  562      */
  563 
  564     query[SMALL_QUERY - 1] = 0;
  565     len = snprintf(query, left, "INSERT INTO %s (%s) VALUES ('",
  566                table, name);
  567     if (len < 0) {
  568     fprintf(stderr, "sql_get_key : %s(%s).%s overflow\n",
  569         table, name, value);
  570     return(-1);
  571     }
  572     end = &query[len];
  573     left -= len;
  574     len = strlen(value);
  575     if (len * 2 >= left) {
  576     fprintf(stderr, "sql_get_key : %s(%s).%s overflow\n",
  577         table, name, value);
  578     return(-1);
  579     }
  580     len = mysql_escape_string(end, value, len);
  581     left -= len;
  582     if (left <= 0) {
  583     fprintf(stderr, "sql_get_key : %s(%s).%s overflow\n",
  584         table, name, value);
  585     return(-1);
  586     }
  587     end += len;
  588     len = snprintf(end, left, "')");
  589     if (len < 0) {
  590     fprintf(stderr, "sql_get_key : %s(%s).%s overflow\n",
  591         table, name, value);
  592     return(-1);
  593     }
  594     end += len;
  595     query[MAX_QUERY - 1] = 0;
  596 
  597     if (mysql_query(sql,query)) {
  598     printf("sql_get_key: INSERT %s failed %s\n", name, mysql_error(sql));
  599     return(-1);
  600     }
  601     id = mysql_insert_id(sql);
  602     result = mysql_store_result(sql);
  603     if (result != NULL)
  604     mysql_free_result(result);
  605     return(id);
  606 }
  607 
  608 int sql_read_key(const char *table, const char *name) {
  609     int id;
  610     MYSQL_RES *result;
  611     MYSQL_ROW row;
  612     char query[SMALL_QUERY];
  613 
  614     if ((table == NULL) || (name == NULL))
  615     return(-1);
  616 
  617     /*
  618      * Search for the ID it has to exist
  619      */
  620     snprintf(query, SMALL_QUERY - 1, "SELECT ID FROM %s WHERE Name='%s'", table, name);
  621     query[SMALL_QUERY - 1] = 0;
  622     if (mysql_query(sql,query)) {
  623     printf("sql_read_key: SELECT %s failed %s\n", name, mysql_error(sql));
  624     return(-1);
  625     }
  626 
  627     result = mysql_use_result(sql);
  628     if (result) {
  629     while((row = mysql_fetch_row(result)))
  630     {
  631         /*
  632          * Lookup the first ID and return it
  633          */
  634         if (row[0] == NULL) {
  635         mysql_free_result(result);
  636         printf("sql_read_key: select returns NULL !\n");
  637         return(-1);
  638         }
  639         if (sscanf(row[0], "%d", &id) != 1) {
  640         mysql_free_result(result);
  641         printf("sql_read_key: ID non numeric %s\n", row[0]);
  642         return(-1);
  643         }
  644         mysql_free_result(result);
  645         return(id);
  646     }
  647     mysql_free_result(result);
  648     }
  649     if(mysql_errno(sql)) {
  650     fprintf(stderr, "Error: %s\n", mysql_error(sql));
  651     return(-1);
  652     }
  653     return(-1);
  654 }
  655 
  656 int sql_read_info_key(const char *table, const char *name, const char *value) {
  657     int id;
  658     MYSQL_RES *result;
  659     MYSQL_ROW row;
  660     char query[SMALL_QUERY];
  661 
  662     if ((table == NULL) || (name == NULL) || (value == NULL))
  663     return(-1);
  664 
  665     /*
  666      * Search for the ID it has to exist
  667      */
  668     snprintf(query, SMALL_QUERY - 1, "SELECT ID FROM %s WHERE %s='%s'", table, name, value);
  669     query[SMALL_QUERY - 1] = 0;
  670     if (mysql_query(sql,query)) {
  671     printf("sql_read_info_key: SELECT %s failed %s\n", name, mysql_error(sql));
  672     return(-1);
  673     }
  674 
  675     result = mysql_use_result(sql);
  676     if (result) {
  677     while((row = mysql_fetch_row(result)))
  678     {
  679         /*
  680          * Lookup the first ID and return it
  681          */
  682         if (row[0] == NULL) {
  683         mysql_free_result(result);
  684         printf("sql_read_info_key: select returns NULL !\n");
  685         return(-1);
  686         }
  687         if (sscanf(row[0], "%d", &id) != 1) {
  688         mysql_free_result(result);
  689         printf("sql_read_info_key: ID non numeric %s\n", row[0]);
  690         return(-1);
  691         }
  692         mysql_free_result(result);
  693         return(id);
  694     }
  695     mysql_free_result(result);
  696     }
  697     if(mysql_errno(sql)) {
  698     fprintf(stderr, "Error: %s\n", mysql_error(sql));
  699     return(-1);
  700     }
  701     return(-1);
  702 }
  703 
  704 /************************************************************************
  705  *                                  *
  706  *              Tables handling             *
  707  *                                  *
  708  ************************************************************************/
  709 
  710 int sql_rebuild_config(void) {
  711     const char *query =
  712 "CREATE TABLE Config ( \n\
  713     ID int(11) NOT NULL auto_increment, \n\
  714     Name varchar(50) NOT NULL, \n\
  715     Value varchar(255), \n\
  716     PRIMARY KEY (ID), \n\
  717     KEY Name (Name(10)) \n\
  718 )";
  719 
  720     if (mysql_query(sql,query)) {
  721     printf("sql_rebuild_config: CREATE TABLE Config failed %s\n",
  722            mysql_error(sql));
  723     return(-1);
  724     }
  725     return(0);
  726 }
  727 
  728 int sql_rebuild_vendors(void) {
  729     const char *query =
  730 "CREATE TABLE Vendors ( \n\
  731     ID int(11) NOT NULL auto_increment, \n\
  732     Name varchar(255) NOT NULL, \n\
  733     URL varchar(255), \n\
  734     Key1 text, \n\
  735     Key2 text, \n\
  736     Key3 text, \n\
  737     Description text, \n\
  738     PRIMARY KEY (ID), \n\
  739     KEY Name (Name(10)) \n\
  740 )";
  741 
  742     if (mysql_query(sql,query)) {
  743     printf("sql_rebuild_vendors: CREATE TABLE Vendors failed %s\n",
  744            mysql_error(sql));
  745     return(-1);
  746     }
  747     return(0);
  748 }
  749 
  750 int sql_rebuild_distributions(void) {
  751     const char *query =
  752 "CREATE TABLE Distributions ( \n\
  753     ID int(11) NOT NULL auto_increment, \n\
  754     Name varchar(255) NOT NULL, \n\
  755     URL varchar(255), \n\
  756     Key1 text, \n\
  757     Key2 text, \n\
  758     Key3 text, \n\
  759     Description text, \n\
  760     PRIMARY KEY (ID), \n\
  761     KEY Name (Name(10)) \n\
  762 )";
  763 
  764     if (mysql_query(sql,query)) {
  765     printf("sql_rebuild_ditributions: CREATE TABLE Distributions failed %s\n",
  766            mysql_error(sql));
  767     return(-1);
  768     }
  769     return(0);
  770 }
  771 
  772 int sql_rebuild_searches(void) {
  773     const char *query =
  774 "CREATE TABLE Searches ( \n\
  775     URL varchar(255) NOT NULL, \n\
  776     name varchar(255) NOT NULL, \n\
  777     active int, \n\
  778     UNIQUE(URL), \n\
  779     UNIQUE(name) \n\
  780 )";
  781 
  782     if (mysql_query(sql,query)) {
  783     printf("sql_rebuild_searches: CREATE TABLE Searches failed %s\n",
  784            mysql_error(sql));
  785     return(-1);
  786     }
  787     return(0);
  788 }
  789 
  790 int sql_rebuild_mirrors(void) {
  791     const char *query =
  792 "CREATE TABLE Mirrors ( \n\
  793     ID int(11), \n\
  794     URL varchar(255) NOT NULL, \n\
  795     Country int(11), \n\
  796     UNIQUE(URL) \n\
  797 )";
  798 
  799     if (mysql_query(sql,query)) {
  800     printf("sql_rebuild_mirrors: CREATE TABLE Mirrors failed %s\n",
  801            mysql_error(sql));
  802     return(-1);
  803     }
  804     return(0);
  805 }
  806 
  807 int sql_rebuild_metadata(void) {
  808     const char *query =
  809 "CREATE TABLE Metadata ( \n\
  810     URL varchar(255) NOT NULL, \n\
  811     Maintainer int(11), \n\
  812     Country int(11), \n\
  813     UNIQUE(URL) \n\
  814 )";
  815 
  816     if (mysql_query(sql,query)) {
  817     printf("sql_rebuild_metadata: CREATE TABLE Metadata failed %s\n",
  818            mysql_error(sql));
  819     return(-1);
  820     }
  821     return(0);
  822 }
  823 
  824 int sql_rebuild_distribs(void) {
  825     const char *query =
  826 "CREATE TABLE Distribs ( \n\
  827     ID int(11) NOT NULL auto_increment, \n\
  828     Name varchar(255) NOT NULL, \n\
  829     Vendor int(11), \n\
  830     Directory varchar(255), \n\
  831     Path varchar(100) NOT NULL, \n\
  832     URL varchar(255), \n\
  833     URLSrc varchar(255), \n\
  834     Html varchar(8), \n\
  835     Color varchar(10), \n\
  836     Key1 text, \n\
  837     Key2 text, \n\
  838     Description text, \n\
  839     PRIMARY KEY (ID), \n\
  840     KEY Name (Name(10)) \n\
  841 )";
  842 
  843     if (mysql_query(sql,query)) {
  844     printf("sql_rebuild_distribs: CREATE TABLE Distribs failed %s\n",
  845            mysql_error(sql));
  846     return(-1);
  847     }
  848     return(0);
  849 }
  850 
  851 int sql_rebuild_packages(void) {
  852     const char *query =
  853 "CREATE TABLE Packages ( \n\
  854 ID int(11) NOT NULL auto_increment, \n\
  855 filename varchar(255) NOT NULL, \n\
  856 Name varchar(50) NOT NULL, \n\
  857 Version varchar(50) NOT NULL, \n\
  858 Release varchar(50) NOT NULL, \n\
  859 Arch varchar(15) NOT NULL, \n\
  860 Dist int(11), \n\
  861 URL varchar(255), \n\
  862 URLSrc varchar(255), \n\
  863 Vendor int(11), \n\
  864 Packager int(11), \n\
  865 Category varchar(255), \n\
  866 Summary varchar(255), \n\
  867 Description text, \n\
  868 Copyright varchar(255), \n\
  869 Date int(11), \n\
  870 Size int(11), \n\
  871 Os varchar(12), \n\
  872 PRIMARY KEY (ID), \n\
  873 KEY filename (filename(80)), \n\
  874 KEY Name (Name(15)) \n\
  875 )";
  876 
  877     if (mysql_query(sql,query)) {
  878     printf("sql_rebuild_packages: CREATE TABLE Packages failed %s\n",
  879            mysql_error(sql));
  880     return(-1);
  881     }
  882     return(0);
  883 }
  884 
  885 int sql_rebuild_files(void) {
  886     const char *query =
  887 "CREATE TABLE Files ( \n\
  888     ID int(11) NOT NULL, \n\
  889     Path varchar(35) NOT NULL, \n\
  890     UNIQUE KEY id (ID,Path(35)), \n\
  891     INDEX (ID), \n\
  892     INDEX (Path) \n\
  893 )";
  894 
  895     if (mysql_query(sql,query)) {
  896     printf("sql_rebuild_files: CREATE TABLE Files failed %s\n",
  897            mysql_error(sql));
  898     return(-1);
  899     }
  900     return(0);
  901 }
  902 
  903 int sql_rebuild_provides(void) {
  904     const char *query =
  905 "CREATE TABLE Provides ( \n\
  906     ID int(11) NOT NULL, \n\
  907     Resource varchar(35) NOT NULL, \n\
  908     UNIQUE KEY id (ID,Resource(35)), \n\
  909     INDEX (ID), \n\
  910     INDEX (Resource) \n\
  911 )";
  912 
  913     if (mysql_query(sql,query)) {
  914     printf("sql_rebuild_provides: CREATE TABLE Provides failed %s\n",
  915            mysql_error(sql));
  916     return(-1);
  917     }
  918     return(0);
  919 }
  920 
  921 int sql_rebuild_requires(void) {
  922     const char *query =
  923 "CREATE TABLE Requires ( \n\
  924     ID int(11) NOT NULL, \n\
  925     Resource varchar(35) NOT NULL, \n\
  926     Rel char(2), \n\
  927     Value varchar(20), \n\
  928     UNIQUE KEY id (ID,Resource(35)), \n\
  929     INDEX (ID), \n\
  930     INDEX (Resource) \n\
  931 )";
  932 
  933     if (mysql_query(sql,query)) {
  934     printf("sql_rebuild_requires: CREATE TABLE Requires failed %s\n",
  935            mysql_error(sql));
  936     return(-1);
  937     }
  938     return(0);
  939 }
  940 
  941 int sql_rebuild_queries(void) {
  942     const char *query =
  943 "CREATE TABLE Queries ( \n\
  944     ID int(11) NOT NULL auto_increment,\n\
  945     Value varchar(50) NOT NULL, \n\
  946     Count int(11) NOT NULL, \n\
  947     Results int(11) NOT NULL, \n\
  948     UNIQUE KEY id (ID,Value(35)), \n\
  949     INDEX (ID), \n\
  950     INDEX (Value) \n\
  951 )";
  952 
  953     if (mysql_query(sql,query)) {
  954     printf("sql_rebuild_queries: CREATE TABLE Queries failed %s\n",
  955            mysql_error(sql));
  956     return(-1);
  957     }
  958     return(0);
  959 }
  960 
  961 
  962 int sql_check_tables(void) {
  963     const char *query = "SHOW TABLES";
  964     MYSQL_RES *result;
  965     MYSQL_ROW row;
  966     int config = 0;
  967     int distribs = 0;
  968     int requires = 0;
  969     int provides = 0;
  970     int vendors = 0;
  971     int mirrors = 0;
  972     int metadata = 0;
  973     int packages = 0;
  974     int searches = 0;
  975     int files = 0;
  976     int queries = 0;
  977     int distributions = 0;
  978 
  979     int rebuilt = 0;
  980 
  981     if (mysql_query(sql,query)) {
  982     printf("sql_check_tables: SHOW TABLES failed %s\n",
  983            mysql_error(sql));
  984     return(-1);
  985     }
  986 
  987     result = mysql_use_result(sql);
  988     if (result) {
  989     while((row = mysql_fetch_row(result)))
  990     {
  991         if (row[0] == NULL) {
  992         mysql_free_result(result);
  993         printf("sql_check_tables: SHOW TABLES returns NULL !\n");
  994         return(-1);
  995         }
  996         if (!strcmp(row[0], "Config"))
  997         config = 1;
  998         if (!strcmp(row[0], "Distribs"))
  999         distribs = 1;
 1000         if (!strcmp(row[0], "Vendors"))
 1001         vendors = 1;
 1002         if (!strcmp(row[0], "Mirrors"))
 1003         mirrors = 1;
 1004         if (!strcmp(row[0], "Metadata"))
 1005         metadata = 1;
 1006         if (!strcmp(row[0], "Packages"))
 1007         packages = 1;
 1008         if (!strcmp(row[0], "Files"))
 1009         files = 1;
 1010         if (!strcmp(row[0], "Requires"))
 1011         requires = 1;
 1012         if (!strcmp(row[0], "Provides"))
 1013         provides = 1;
 1014         if (!strcmp(row[0], "Queries"))
 1015         queries = 1;
 1016         if (!strcmp(row[0], "Distributions"))
 1017         distributions = 1;
 1018         if (!strcmp(row[0], "Searches"))
 1019         searches = 1;
 1020     }
 1021     mysql_free_result(result);
 1022     }
 1023     if(mysql_errno(sql)) {
 1024     fprintf(stderr, "Error: %s\n", mysql_error(sql));
 1025     return(-1);
 1026     }
 1027 
 1028     if (!config) {
 1029     fprintf(stderr, "Table Config disapeared: rebuilding it\n");
 1030     if (!sql_rebuild_config())
 1031         rebuilt++;
 1032     }
 1033     if (!vendors) {
 1034     fprintf(stderr, "Table Vendors disapeared: rebuilding it\n");
 1035     if (!sql_rebuild_vendors())
 1036         rebuilt++;
 1037     }
 1038     if (!distribs) {
 1039     fprintf(stderr, "Table Distribs disapeared: rebuilding it\n");
 1040     if (!sql_rebuild_distribs())
 1041         rebuilt++;
 1042     }
 1043     if (!mirrors) {
 1044     fprintf(stderr, "Table Mirrors disapeared: rebuilding it\n");
 1045     if (!sql_rebuild_mirrors())
 1046         rebuilt++;
 1047     }
 1048     if (!metadata) {
 1049     fprintf(stderr, "Table Metadata disapeared: rebuilding it\n");
 1050     if (!sql_rebuild_metadata())
 1051         rebuilt++;
 1052     }
 1053     if (!packages) {
 1054     fprintf(stderr, "Table Packages disapeared: rebuilding it\n");
 1055     if (!sql_rebuild_packages())
 1056         rebuilt++;
 1057     }
 1058     if (!files) {
 1059     fprintf(stderr, "Table Files disapeared: rebuilding it\n");
 1060     if (!sql_rebuild_files())
 1061         rebuilt++;
 1062     }
 1063     if (!requires) {
 1064     fprintf(stderr, "Table Requires disapeared: rebuilding it\n");
 1065     if (!sql_rebuild_requires())
 1066         rebuilt++;
 1067     }
 1068     if (!provides) {
 1069     fprintf(stderr, "Table Provides disapeared: rebuilding it\n");
 1070     if (!sql_rebuild_provides())
 1071         rebuilt++;
 1072     }
 1073     if (!queries) {
 1074     fprintf(stderr, "Table Queries disapeared: rebuilding it\n");
 1075     if (!sql_rebuild_queries())
 1076         rebuilt++;
 1077     }
 1078     if (!distributions) {
 1079     fprintf(stderr, "Table Distributions disapeared: rebuilding it\n");
 1080     if (!sql_rebuild_distributions())
 1081         rebuilt++;
 1082     }
 1083     if (!searches) {
 1084     fprintf(stderr, "Table Searches disapeared: rebuilding it\n");
 1085     if (!sql_rebuild_searches())
 1086         rebuilt++;
 1087     }
 1088     return(rebuilt);
 1089 }
 1090 
 1091 /************************************************************************
 1092  *                                  *
 1093  *          Specific rpm2html functions         *
 1094  *                                  *
 1095  ************************************************************************/
 1096 
 1097 int sql_add_dist_mirror(int distrib, const char *URL, int country) {
 1098     if (URL == NULL)
 1099     return(-1);
 1100     if (distrib < 0)
 1101     return(-1);
 1102     return(sql_blind_insert("Mirrors", "URL", URL, distrib));
 1103 }
 1104 
 1105 int sql_add_mirror(const char *Name, const char *URL, int country) {
 1106     int distrib;
 1107 
 1108     if ((Name == NULL) || (URL == NULL))
 1109     return(-1);
 1110     distrib = sql_read_key("Distribs", Name);
 1111     if (distrib < 0)
 1112     return(distrib);
 1113     return(sql_blind_insert("Mirrors", "URL", URL, distrib));
 1114 }
 1115 
 1116 int sql_add_file(const char *filename, int package) {
 1117     if ((filename == NULL) || (package <= 0))
 1118     return(-1);
 1119     if (strlen(filename) > 35)
 1120     return(0);
 1121 
 1122     return(sql_blind_insert("Files", "Path", filename, package));
 1123 }
 1124 
 1125 int sql_add_provides(int package, const char *resource) {
 1126     if ((resource == NULL) || (package <= 0))
 1127     return(-1);
 1128     if (strlen(resource) > 35)
 1129     return(0);
 1130 
 1131     return(sql_blind_insert("Provides", "Resource", resource, package));
 1132 }
 1133 
 1134 int sql_add_requires(int package, const char *resource, rpm_dep_flag rel,
 1135                  const char *value) {
 1136     int record;
 1137     if ((resource == NULL) || (package <= 0))
 1138     return(-1);
 1139     if (strlen(resource) > 35)
 1140     return(0);
 1141 
 1142     record = sql_blind_insert("Requires", "Resource", resource, package);
 1143     if ((rel != RPM2HTML_REQ_NONE) && (value != NULL) &&
 1144     (strlen(value) <= 50)) {
 1145     char query[SMALL_QUERY];
 1146 
 1147     switch (rel) {
 1148         case RPM2HTML_REQ_LT:
 1149         snprintf(query, SMALL_QUERY - 1,
 1150              "UPDATE Requires SET Rel='<',Value='%s' WHERE ID=%d",
 1151              value, record);
 1152             break;
 1153         case RPM2HTML_REQ_LEQ:
 1154         snprintf(query, SMALL_QUERY - 1,
 1155              "UPDATE Requires SET Rel='<=',Value='%s' WHERE ID=%d",
 1156              value, record);
 1157             break;
 1158         case RPM2HTML_REQ_GT:
 1159         snprintf(query, SMALL_QUERY - 1,
 1160              "UPDATE Requires SET Rel='>',Value='%s' WHERE ID=%d",
 1161              value, record);
 1162             break;
 1163         case RPM2HTML_REQ_GEQ:
 1164         snprintf(query, SMALL_QUERY - 1,
 1165              "UPDATE Requires SET Rel='>=',Value='%s' WHERE ID=%d",
 1166              value, record);
 1167             break;
 1168         case RPM2HTML_REQ_EQU:
 1169         snprintf(query, SMALL_QUERY - 1,
 1170              "UPDATE Requires SET Rel='=',Value='%s' WHERE ID=%d",
 1171              value, record);
 1172             break;
 1173         case RPM2HTML_REQ_NONE:
 1174         query[0] = 0;
 1175     }
 1176     query[SMALL_QUERY - 1] = 0;
 1177     if (mysql_query(sql,query)) {
 1178         printf("sql_add_requires: UPDATE Requires %d failed %s\n",
 1179            record, mysql_error(sql));
 1180         return(record);
 1181     }
 1182     }
 1183     return(record);
 1184 }
 1185 
 1186 int sql_add_vendor(const char *Name, const char *URL, const char *Description) {
 1187     int id;
 1188 
 1189     if (Name == NULL)
 1190     return(-1);
 1191 
 1192     id = sql_get_key("Vendors", "Name", Name);
 1193     if (id <= 0) {
 1194     id = sql_blind_insert("Vendors", "Name", Name, 0);
 1195     if (id <= 0)
 1196         return(id);
 1197     }
 1198     if (URL != NULL)
 1199     sql_update_id("Vendors", id, "URL", URL);
 1200     if (Description != NULL)
 1201     sql_update_id("Vendors", id,
 1202                    "Description", Description);
 1203     
 1204     return(id);
 1205 }
 1206 
 1207 int sql_add_distribution(const char *Name, const char *URL, const char *Description) {
 1208     int id;
 1209 
 1210     if (Name == NULL)
 1211     return(-1);
 1212 
 1213     id = sql_get_key("Distributions", "Name", Name);
 1214     if (id <= 0) {
 1215     id = sql_blind_insert("Distributions", "Name", Name, 0);
 1216     if (id <= 0)
 1217         return(id);
 1218     }
 1219     if (URL != NULL)
 1220     sql_update_id("Distributions", id, "URL", URL);
 1221     if (Description != NULL)
 1222     sql_update_id("Distributions", id,
 1223                    "Description", Description);
 1224     
 1225     return(id);
 1226 }
 1227 
 1228 int sql_get_package_id(const char *filename) {
 1229     int id;
 1230     MYSQL_RES *result;
 1231     MYSQL_ROW row;
 1232     char query[MAX_QUERY];
 1233     int left = MAX_QUERY - 1;
 1234     int len;
 1235     char *end;
 1236     static int queries = 0;
 1237 
 1238     if (filename == NULL)
 1239     return(0);
 1240 
 1241     queries++;
 1242 
 1243     /*
 1244      * Search first for the ID if it already exists
 1245      */
 1246     len = snprintf(query, left, "SELECT ID FROM Packages WHERE filename='%s'",
 1247                filename);
 1248     if (len < 0) {
 1249     fprintf(stderr, "sql_get_package_id : %s overflow\n",
 1250         filename);
 1251     return(0);
 1252     }
 1253     query[len] = 0;
 1254 
 1255     if (mysql_query(sql,query)) {
 1256     printf("sql_get_package_id: SELECT %s failed %s\n", filename,
 1257            mysql_error(sql));
 1258     return(0);
 1259     }
 1260 
 1261     result = mysql_use_result(sql);
 1262     if (result) {
 1263     if ((row = mysql_fetch_row(result)))
 1264     {
 1265         /*
 1266          * Lookup the first ID and return it
 1267          */
 1268         if (row[0] == NULL) {
 1269         mysql_free_result(result);
 1270         printf("sql_get_package_id: select returns NULL !\n");
 1271         return(0);
 1272         }
 1273         if (sscanf(row[0], "%d", &id) != 1) {
 1274         mysql_free_result(result);
 1275         printf("sql_get_package_id: ID non numeric %s\n", row[0]);
 1276         return(0);
 1277         }
 1278         mysql_free_result(result);
 1279         return(id);
 1280     }
 1281     mysql_free_result(result);
 1282     }
 1283     if (mysql_errno(sql)) {
 1284     fprintf(stderr, "Error: %s\n", mysql_error(sql));
 1285     return(-1);
 1286     }
 1287 
 1288     printf("%d New package %s\n", queries, filename);
 1289     return(0);
 1290 }
 1291 
 1292 int sql_get_top_queries(int count) {
 1293     int id;
 1294     MYSQL_RES *result;
 1295     MYSQL_ROW row;
 1296     char query[MAX_QUERY];
 1297     int left = MAX_QUERY - 1;
 1298     int len;
 1299     char *end;
 1300     static int queries = 0;
 1301 
 1302     if (count <= 0)
 1303     count = 100;
 1304 
 1305     queries++;
 1306 
 1307     /*
 1308      * Search first for the ID if it already exists
 1309      */
 1310     len = snprintf(query, left,
 1311         "select Value, Count from Queries order by count desc limit %d",
 1312                count);
 1313     if (len < 0) {
 1314     fprintf(stderr, "sql_get_top_queries : overflow\n");
 1315     return(0);
 1316     }
 1317     query[len] = 0;
 1318 
 1319     if (mysql_query(sql,query)) {
 1320     printf("sql_get_top_queries : SELECT failed %s\n", mysql_error(sql));
 1321     return(0);
 1322     }
 1323 
 1324     result = mysql_use_result(sql);
 1325     if (result) {
 1326     printf("<queries>\n");
 1327     while ((row = mysql_fetch_row(result)))
 1328     {
 1329         /*
 1330          * Lookup the first ID and return it
 1331          */
 1332         if ((row[0] == NULL) || (row[1] == NULL)) {
 1333         mysql_free_result(result);
 1334         fprintf(stderr, "sql_get_top_queries: select returns NULL !\n");
 1335         break;
 1336         }
 1337         printf("<query occur='%s'>%s</query>\n", row[1], row[0]);
 1338     }
 1339     printf("</queries>\n");
 1340     mysql_free_result(result);
 1341     }
 1342     if (mysql_errno(sql)) {
 1343     fprintf(stderr, "Error: %s\n", mysql_error(sql));
 1344     return(-1);
 1345     }
 1346     return(0);
 1347 }
 1348 int sql_add_package(const char *filename,
 1349     const char *Name, const char *Version, const char *Release,
 1350     const char *Arch,
 1351     int dist, const char *URL, const char *URLSrc, int vendor,
 1352     const char *Packager, const char *Category, const char *Summary,
 1353     const char *Description, const char *Copyright, int Date, int Size,
 1354     const char *Os, const char *Distribution, const char *Vendor) {
 1355     int id;
 1356     int nb_fields = 0;
 1357     char installed_filename[500];
 1358 
 1359     if (filename == NULL) {
 1360     sprintf(installed_filename, "localbase/%s-%s-%s.%s.rpm",
 1361         Name,Version,Release,Arch);
 1362     filename = installed_filename;
 1363     }
 1364 
 1365     if (dist < 0)
 1366     return(-1);
 1367     if ((Name == NULL) || (Version == NULL) || (Release == NULL) ||
 1368     (Arch == NULL))
 1369     return(-1);
 1370 
 1371     id = sql_get_key("Packages", "filename", filename);
 1372     if (id <= 0)
 1373     return(-1);
 1374     nb_fields = 1;
 1375 
 1376     if (rpm2htmlVerbose > 1)
 1377     printf("Adding %s ID %d\n", filename, id);
 1378 
 1379     if (Name != NULL)
 1380     nb_fields += sql_update_id("Packages", id, "Name", Name);
 1381     if (Version != NULL)
 1382     nb_fields += sql_update_id("Packages", id, "Version", Version);
 1383     if (Release != NULL)
 1384     nb_fields += sql_update_id("Packages", id, "Release", Release);
 1385     if (Release != NULL)
 1386     nb_fields += sql_update_id("Packages", id, "Arch", Arch);
 1387     if (Category != NULL)
 1388     nb_fields += sql_update_id("Packages", id, "Category", Category);
 1389     if (URL != NULL)
 1390     nb_fields += sql_update_id("Packages", id, "URL", URL);
 1391     if (URLSrc != NULL)
 1392     nb_fields += sql_update_id("Packages", id, "URLSrc", URLSrc);
 1393     if (dist >= 0) {
 1394     char str[30];
 1395     snprintf(str, 30, "%d", dist);
 1396     str[29] = 0;
 1397     nb_fields += sql_update_id("Packages", id, "Dist", str);
 1398     }
 1399     if (Summary != NULL) {
 1400     nb_fields += sql_update_id("Packages", id, "Summary", Summary);
 1401     }
 1402     if (Description != NULL) {
 1403     nb_fields += sql_update_id("Packages", id,
 1404                    "Description", Description);
 1405     }
 1406     if (Copyright != NULL)
 1407     nb_fields += sql_update_id("Packages", id, "Copyright", Copyright);
 1408     if (Date != 0) {
 1409     char str[30];
 1410     snprintf(str, 30, "%d", Date);
 1411     str[29] = 0;
 1412     nb_fields += sql_update_id("Packages", id, "Date", str);
 1413     }
 1414     if (Size != 0) {
 1415     char str[30];
 1416     snprintf(str, 30, "%d", Size);
 1417     str[29] = 0;
 1418     nb_fields += sql_update_id("Packages", id, "Size", str);
 1419     }
 1420     if (Os != NULL) {
 1421     nb_fields += sql_update_id("Packages", id, "Os", Os);
 1422     }
 1423     if (Packager != NULL) {
 1424     char str[30];
 1425     int packager = sql_add_vendor(Packager, NULL, NULL);
 1426 
 1427     if (packager > 0) {
 1428         snprintf(str, 30, "%d", packager);
 1429         str[29] = 0;
 1430         nb_fields += sql_update_id("Packages", id, "Packager", str);
 1431     }
 1432     }
 1433     if (Distribution != NULL) {
 1434     char str[30];
 1435     int packager = sql_add_distribution(Distribution, NULL, NULL);
 1436 
 1437     if (packager > 0) {
 1438         snprintf(str, 30, "%d", packager);
 1439         str[29] = 0;
 1440         nb_fields += sql_update_id("Packages", id, "Vendor", str);
 1441     }
 1442     }
 1443     /***************
 1444     if (vendor > 0) {
 1445     char str[30];
 1446     snprintf(str, 30, "%d", vendor);
 1447     str[29] = 0;
 1448     nb_fields += sql_update_id("Packages", id, "Vendor", str);
 1449     }
 1450      ***************/
 1451     
 1452     return(id);
 1453 }
 1454 
 1455 int sql_get_distrib_by_name(const char *Name) {
 1456     int ret;
 1457 
 1458     if (Name == NULL)
 1459     return(-1);
 1460     ret = sql_read_key("Distribs", Name);
 1461 
 1462 #ifdef SQL_DEBUG
 1463 fprintf(stderr, "sql_get_distrib_by_name(%s) => %d\n", Name, ret);
 1464 #endif
 1465 
 1466     return(ret);
 1467 }
 1468 
 1469 int sql_get_distrib_by_directory(const char *Directory) {
 1470     int ret;
 1471 
 1472     if (Directory == NULL)
 1473     return(-1);
 1474 
 1475     ret = sql_get_key("Distribs", "Directory", Directory);
 1476 
 1477 #ifdef SQL_DEBUG
 1478 fprintf(stderr, "sql_get_distrib_by_directory(%s) => %d\n", Directory, ret);
 1479 #endif
 1480 
 1481     return(ret);
 1482 }
 1483 
 1484 int sql_add_distrib(const char *Name, const char *Vendor,
 1485     const char *Directory, const char *Path, const char *URL,
 1486     const char *URLSrc, const char *Description,
 1487     const char *Html, const char *Color) {
 1488     int id, vendor;
 1489     int nb_fields = 0;
 1490     char VendorStr[15];
 1491 
 1492     if (Name == NULL)
 1493     return(-1);
 1494 
 1495     id = sql_get_key("Distribs", "Name", Name);
 1496     nb_fields = 1;
 1497     if (Vendor != NULL) {
 1498     vendor = sql_get_key("Vendors", "Name", Vendor);
 1499     sprintf(VendorStr, "%d", vendor);
 1500     nb_fields += sql_update_id("Distribs", id, "Vendor", VendorStr);
 1501     }
 1502     if (Directory != NULL)
 1503     nb_fields += sql_update_id("Distribs", id, "Directory", Directory);
 1504     if (Path != NULL)
 1505     nb_fields += sql_update_id("Distribs", id, "Path", Path);
 1506     if (URL != NULL)
 1507     nb_fields += sql_update_id("Distribs", id, "URL", URL);
 1508     if (URLSrc != NULL)
 1509     nb_fields += sql_update_id("Distribs", id, "URLSrc", URLSrc);
 1510     if (Html != NULL)
 1511     nb_fields += sql_update_id("Distribs", id, "Html", Html);
 1512     if (Color != NULL)
 1513     nb_fields += sql_update_id("Distribs", id, "Color", Color);
 1514     if (Description != NULL)
 1515     nb_fields += sql_update_id("Distribs", id,
 1516                    "Description", Description);
 1517     
 1518     return(nb_fields);
 1519 }
 1520 
 1521 void sql_add_config_info(const char *name, const char *value) {
 1522     sql_update("Config", name, "Value", value);
 1523 }
 1524 
 1525 void sql_add_metadata_base(const char *URL) {
 1526     sql_blind_insert("Metadata", "URL", URL, -1);
 1527 }
 1528 
 1529 /************************************************************************
 1530  *                                  *
 1531  *          Cleanup functions               *
 1532  *                                  *
 1533  ************************************************************************/
 1534 
 1535 int sql_remove_package(int id) {
 1536     char query[SMALL_QUERY];
 1537 
 1538     if (id <= 0)
 1539     return(-1);
 1540 
 1541     /*
 1542      * remove the ID from the package list
 1543      */
 1544     snprintf(query, SMALL_QUERY - 1, "DELETE FROM Packages WHERE ID=%d", id);
 1545     query[SMALL_QUERY - 1] = 0;
 1546     if (mysql_query(sql,query)) {
 1547     printf("sql_remove_package: DELETE package %d failed %s\n", id, mysql_error(sql));
 1548     return(-1);
 1549     }
 1550     if(mysql_errno(sql)) {
 1551     fprintf(stderr, "Error: %s\n", mysql_error(sql));
 1552     return(-1);
 1553     }
 1554 
 1555     /*
 1556      * remove the associated files from the Files list
 1557      */
 1558     snprintf(query, SMALL_QUERY - 1, "DELETE FROM Files WHERE ID=%d", id);
 1559     query[SMALL_QUERY - 1] = 0;
 1560     if (mysql_query(sql,query)) {
 1561     printf("sql_remove_package: DELETE Files %d failed %s\n", id, mysql_error(sql));
 1562     return(-1);
 1563     }
 1564     if(mysql_errno(sql)) {
 1565     fprintf(stderr, "Error: %s\n", mysql_error(sql));
 1566     return(-1);
 1567     }
 1568 
 1569     /*
 1570      * remove the associated Provides entries
 1571      */
 1572     snprintf(query, SMALL_QUERY - 1, "DELETE FROM Provides WHERE ID=%d", id);
 1573     query[SMALL_QUERY - 1] = 0;
 1574     if (mysql_query(sql,query)) {
 1575     printf("sql_remove_package: DELETE Provides %d failed %s\n", id, mysql_error(sql));
 1576     return(-1);
 1577     }
 1578     if(mysql_errno(sql)) {
 1579     fprintf(stderr, "Error: %s\n", mysql_error(sql));
 1580     return(-1);
 1581     }
 1582 
 1583     /*
 1584      * remove the associated Requires entries
 1585      */
 1586     snprintf(query, SMALL_QUERY - 1, "DELETE FROM Requires WHERE ID=%d", id);
 1587     query[SMALL_QUERY - 1] = 0;
 1588     if (mysql_query(sql,query)) {
 1589     printf("sql_remove_package: DELETE Requires %d failed %s\n", id, mysql_error(sql));
 1590     return(-1);
 1591     }
 1592     if(mysql_errno(sql)) {
 1593     fprintf(stderr, "Error: %s\n", mysql_error(sql));
 1594     return(-1);
 1595     }
 1596 
 1597     /*
 1598      * Remove the RDF and HTML files.
 1599      * TODO
 1600      */
 1601     return(-1);
 1602 }
 1603 
 1604 int sql_check_packages(void) {
 1605     MYSQL_RES *result;
 1606     MYSQL_ROW row;
 1607     char *query = "SELECT filename,ID FROM Packages";
 1608     struct stat buf;
 1609     int id;
 1610     int ids[2500]; /* Do not remove more than 2500 package per run */
 1611     int index;
 1612     int total = 0;
 1613 
 1614     if (rpm2htmlVerbose)
 1615     printf("Database cleanup\n");
 1616     sqlInitSqlDistributionList();
 1617     index = 0;
 1618     /*
 1619      * Search first for the ID if it already exists
 1620      */
 1621     if (mysql_query(sql,query)) {
 1622     printf("sql_check_packages: SELECT from Packages failed: %s\n",
 1623            mysql_error(sql));
 1624     return(-1);
 1625     }
 1626 
 1627     result = mysql_use_result(sql);
 1628     if (result) {
 1629     while((row = mysql_fetch_row(result)))
 1630     {
 1631         if ((row[0] == NULL) || (row[1] == NULL)) {
 1632         printf("sql_check_packages: Path or ID is NULL !\n");
 1633         continue;
 1634         }
 1635         if (!strncmp(row[0], "localbase", 9))
 1636         continue;
 1637 
 1638         if ((stat(row[0], &buf) < 0) || (buf.st_size < 50)) {
 1639         /*
 1640          * Need to remove the package from the database.
 1641          */
 1642         if (sscanf(row[1], "%d", &id) != 1) {
 1643             printf("sql_check_packages: ID non numeric %s\n", row[1]);
 1644             continue;
 1645         }
 1646         ids[index++] = id;
 1647         if (rpm2htmlVerbose)
 1648             printf("Removing %s ID %d\n", row[0], id);
 1649         if (index >= 2500)
 1650             break;
 1651         }
 1652     }
 1653     mysql_free_result(result);
 1654     }
 1655     if(mysql_errno(sql)) {
 1656     fprintf(stderr, "sql_update Error: %s\n", mysql_error(sql));
 1657     return(-1);
 1658     }
 1659 
 1660     /*
 1661      * Do the cleanup.
 1662      */
 1663     if (rpm2htmlVerbose)
 1664     printf("Database cleanup : removing %d entries...\n", index);
 1665     for (id = 0;id < index;id++) {
 1666     sql_remove_package(ids[id]);
 1667     }
 1668 
 1669     printf("Database cleanup : removed %d entries\n", index);
 1670     return(total);
 1671 }
 1672 
 1673 /************************************************************************
 1674  *                                  *
 1675  *          Package lists extraction            *
 1676  *                                  *
 1677  ************************************************************************/
 1678 int sqlDirListInitialized = 0;
 1679 rpmDirPtr sqlDirList[SQL_MAX_DISTRIBS];
 1680 int sqlVendorListInitialized = 0;
 1681 int sqlVendorListLen;
 1682 char **sqlVendorList;
 1683 int sqlDistributionListInitialized = 0;
 1684 int sqlDistributionListLen;
 1685 char **sqlDistributionList;
 1686 int sqlResourceListInitialized = 0;
 1687 int sqlResourceListLen = 0;
 1688 int sqlResourceListMax = 0;
 1689 char **sqlResourceList;
 1690 
 1691 char *
 1692 sqlRpmAnalyzeResourceRow(MYSQL_ROW row) {
 1693     const char *name;
 1694     char **tmp, *ret;
 1695 
 1696     if (row == NULL)
 1697     return(NULL);
 1698     name = row[0];
 1699     if (name == NULL)
 1700     return(NULL);
 1701 
 1702     if (sqlResourceListLen >= sqlResourceListMax) {
 1703     sqlResourceListMax *= 2;
 1704     tmp = (char **) xmlRealloc(sqlResourceList,
 1705                              (sqlResourceListMax) * sizeof(char *));
 1706     if (sqlResourceList == NULL) {
 1707         fprintf(stderr, "sqlInitSqlResourceList Error: out of memory\n");
 1708         return(NULL);
 1709     }
 1710     sqlResourceList = tmp;
 1711     }
 1712 
 1713     ret = sqlResourceList[sqlResourceListLen++] = xmlStrdup(name);
 1714     return(ret);
 1715 }
 1716 
 1717 void
 1718 sqlInitSqlResourceList(void) {
 1719     MYSQL_RES *result;
 1720     MYSQL_ROW row;
 1721     char *query;
 1722     int i;
 1723 
 1724     if (sqlResourceListInitialized != 0)
 1725     return;
 1726 
 1727 #ifdef SQL_DEBUG_MEM
 1728     printf("sqlInitSqlResourceList\n");
 1729 #endif
 1730     if (rpm2htmlVerbose)
 1731     printf("sqlInitSqlResourceList query\n");
 1732 
 1733     /*
 1734      * Allocate the array
 1735      */
 1736     sqlResourceListLen = 0;
 1737     sqlResourceListMax = 1000;
 1738     sqlResourceList = (char **) xmlMalloc((sqlResourceListMax) *
 1739                                         sizeof(char *));
 1740     if (sqlResourceList == NULL) {
 1741     fprintf(stderr, "sqlInitSqlResourceList Error: out of memory\n");
 1742     return;
 1743     }
 1744     
 1745     /*
 1746      * query the database for the values
 1747      */
 1748     query = "select Resource from Provides group by Resource";
 1749 
 1750     if (mysql_query(sql,query)) {
 1751     printf("sqlInitSqlResourceList: SELECT from Provides failed: %s\n",
 1752            mysql_error(sql));
 1753     return;
 1754     }
 1755 
 1756     i = 0;
 1757     result = mysql_use_result(sql);
 1758     if (result) {
 1759     while((row = mysql_fetch_row(result)))
 1760     {
 1761         if (sqlRpmAnalyzeResourceRow(row) != NULL)
 1762         i++;
 1763     }
 1764     mysql_free_result(result);
 1765     }
 1766     if(mysql_errno(sql)) {
 1767     fprintf(stderr, "sqlInitSqlResourceList Error: %s\n", mysql_error(sql));
 1768     return;
 1769     }
 1770 
 1771 
 1772     if (rpm2htmlVerbose)
 1773     printf("sqlInitSqlResourceList done: %d entries\n", i);
 1774     sqlResourceListInitialized++;
 1775     return;
 1776 }
 1777 
 1778 char *
 1779 sqlRpmAnalyzeDistributionRow(MYSQL_ROW row) {
 1780     const char *id;
 1781     const char *name;
 1782     char **tmp;
 1783     int distribution;
 1784 
 1785     if (row == NULL)
 1786     return(NULL);
 1787     id = row[0];
 1788     name = row[1];
 1789     if ((id == NULL) || (name == NULL))
 1790     return(NULL);
 1791 
 1792     if (sscanf(id, "%d", &distribution) != 1)
 1793     return(NULL);
 1794     if ((distribution <= 0) || (distribution > 100000)) {
 1795     fprintf(stderr, "Dist number out of range %d\n", distribution);
 1796     return(NULL);
 1797     }
 1798     if (distribution >= sqlDistributionListLen) {
 1799     int i = sqlDistributionListLen;
 1800     sqlDistributionListLen = distribution + 100;
 1801     tmp = (char **) xmlRealloc(sqlDistributionList,
 1802                              (sqlDistributionListLen) * sizeof(char *));
 1803     if (sqlDistributionList == NULL) {
 1804         fprintf(stderr, "sqlInitSqlDistributionList Error: out of memory\n");
 1805         return(NULL);
 1806     }
 1807     sqlDistributionList = tmp;
 1808     memset(&sqlDistributionList[i], 0, sizeof(char *) * (sqlDistributionListLen - i));
 1809     }
 1810 
 1811     sqlDistributionList[distribution] = xmlStrdup(name);
 1812     return(sqlDistributionList[distribution]);
 1813 }
 1814 
 1815 void
 1816 sqlInitSqlDistributionList(void) {
 1817     MYSQL_RES *result;
 1818     MYSQL_ROW row;
 1819     char *query;
 1820     int i;
 1821 
 1822     if (sqlDistributionListInitialized != 0)
 1823     return;
 1824 
 1825 #ifdef SQL_DEBUG_MEM
 1826     printf("sqlInitSqlDistributionList\n");
 1827 #endif
 1828     if (rpm2htmlVerbose)
 1829     printf("sqlInitSqlDistributionList query\n");
 1830 
 1831     /*
 1832      * Allocate the array
 1833      */
 1834     sqlDistributionListLen = 100;
 1835     sqlDistributionList = (char **) xmlMalloc((sqlDistributionListLen) * sizeof(char *));
 1836     if (sqlDistributionList == NULL) {
 1837     fprintf(stderr, "sqlInitSqlDistributionList Error: out of memory\n");
 1838     return;
 1839     }
 1840     memset(sqlDistributionList, 0, (sqlDistributionListLen) * sizeof(char *));
 1841 
 1842     /*
 1843      * query the database for the values
 1844      */
 1845     query = "select ID,Name from Distributions";
 1846 
 1847     if (mysql_query(sql,query)) {
 1848     printf("sqlInitSqlDistributionList: SELECT from Distributions failed: %s\n",
 1849            mysql_error(sql));
 1850     return;
 1851     }
 1852 
 1853     i = 0;
 1854     result = mysql_use_result(sql);
 1855     if (result) {
 1856     while((row = mysql_fetch_row(result)))
 1857     {
 1858         if (sqlRpmAnalyzeDistributionRow(row) != NULL)
 1859         i++;
 1860     }
 1861     mysql_free_result(result);
 1862     }
 1863     if(mysql_errno(sql)) {
 1864     fprintf(stderr, "sqlInitSqlDistributionList Error: %s\n", mysql_error(sql));
 1865     return;
 1866     }
 1867 
 1868 
 1869     if (rpm2htmlVerbose)
 1870     printf("sqlInitSqlDistributionList done: %d entries\n", i);
 1871     sqlDistributionListInitialized++;
 1872     return;
 1873 }
 1874 
 1875 
 1876 char *
 1877 sqlRpmAnalyzeVendorRow(MYSQL_ROW row) {
 1878     const char *id;
 1879     const char *name;
 1880     char **tmp;
 1881     int vendor;
 1882 
 1883     if (row == NULL)
 1884     return(NULL);
 1885     id = row[0];
 1886     name = row[1];
 1887     if ((id == NULL) || (name == NULL))
 1888     return(NULL);
 1889 
 1890     if (sscanf(id, "%d", &vendor) != 1)
 1891     return(NULL);
 1892     if ((vendor <= 0) || (vendor > 100000)) {
 1893     fprintf(stderr, "Dist number out of range %d\n", vendor);
 1894     return(NULL);
 1895     }
 1896     if (vendor >= sqlVendorListLen) {
 1897     int i = sqlVendorListLen;
 1898     sqlVendorListLen = vendor + 100;
 1899     tmp = (char **) xmlRealloc(sqlVendorList,
 1900                              (sqlVendorListLen) * sizeof(char *));
 1901     if (sqlVendorList == NULL) {
 1902         fprintf(stderr, "sqlInitSqlVendorList Error: out of memory\n");
 1903         return(NULL);
 1904     }
 1905     sqlVendorList = tmp;
 1906     memset(&sqlVendorList[i], 0, sizeof(char *) * (sqlVendorListLen - i));
 1907     }
 1908 
 1909     sqlVendorList[vendor] = xmlStrdup(name);
 1910     return(sqlVendorList[vendor]);
 1911 }
 1912 
 1913 void
 1914 sqlInitSqlVendorList(void) {
 1915     MYSQL_RES *result;
 1916     MYSQL_ROW row;
 1917     char *query;
 1918     int i;
 1919 
 1920     if (sqlVendorListInitialized != 0)
 1921     return;
 1922 
 1923 #ifdef SQL_DEBUG_MEM
 1924     printf("sqlInitSqlVendorList\n");
 1925 #endif
 1926     if (rpm2htmlVerbose)
 1927     printf("sqlInitSqlVendorList query\n");
 1928 
 1929     /*
 1930      * Allocate the array
 1931      */
 1932     sqlVendorListLen = 100;
 1933     sqlVendorList = (char **) xmlMalloc((sqlVendorListLen) * sizeof(char *));
 1934     if (sqlVendorList == NULL) {
 1935     fprintf(stderr, "sqlInitSqlVendorList Error: out of memory\n");
 1936     return;
 1937     }
 1938     memset(&sqlVendorList[0], 0, sizeof(char *) * sqlVendorListLen);
 1939     
 1940     /*
 1941      * query the database for the values
 1942      */
 1943     query = "select ID,Name from Vendors";
 1944 
 1945     if (mysql_query(sql,query)) {
 1946     printf("sqlInitSqlVendorList: SELECT from Vendors failed: %s\n",
 1947            mysql_error(sql));
 1948     return;
 1949     }
 1950 
 1951     i = 0;
 1952     result = mysql_use_result(sql);
 1953     if (result) {
 1954     while((row = mysql_fetch_row(result)))
 1955     {
 1956         if (sqlRpmAnalyzeVendorRow(row) != NULL)
 1957         i++;
 1958     }
 1959     mysql_free_result(result);
 1960     }
 1961     if(mysql_errno(sql)) {
 1962     fprintf(stderr, "sqlInitSqlVendorList Error: %s\n", mysql_error(sql));
 1963     return;
 1964     }
 1965 
 1966 
 1967     if (rpm2htmlVerbose)
 1968     printf("sqlInitSqlVendorList done: %d entries\n", i);
 1969     sqlVendorListInitialized++;
 1970     return;
 1971 }
 1972 
 1973 rpmDirPtr
 1974 sqlRpmAnalyzeDirRow(MYSQL_ROW row) {
 1975     rpmDirPtr ret;
 1976     const char *id;
 1977     const char *name;
 1978     const char *vendor;
 1979     const char *directory;
 1980     const char *path;
 1981     const char *url;
 1982     const char *urlSrc;
 1983     const char *html;
 1984     const char *color;
 1985     int dist;
 1986 
 1987     if (row == NULL)
 1988     return(NULL);
 1989     id = row[0];
 1990     name = row[1];
 1991     vendor = row[2];
 1992     directory = row[3];
 1993     path = row[4];
 1994     url = row[5];
 1995     urlSrc = row[6];
 1996     html = row[7];
 1997     color = row[8];
 1998     if ((id == NULL) || (name == NULL) ||
 1999         (directory == NULL) || (path == NULL) || (url == NULL))
 2000     return(NULL);
 2001 
 2002     if (sscanf(id, "%d", &dist) != 1)
 2003     return(NULL);
 2004     if ((dist <= 0) || (dist > SQL_MAX_DISTRIBS)) {
 2005     fprintf(stderr, "Dist number out of range %d\n", dist);
 2006     return(NULL);
 2007     }
 2008 
 2009 
 2010     ret = (rpmDirPtr) xmlMalloc(sizeof(rpmDir));
 2011     if (ret == NULL)
 2012     return(NULL);
 2013     memset(ret, 0, sizeof(rpmDir));
 2014     /* Generic stuff */
 2015     if (rpm2html_dir != NULL)
 2016     ret->dir = xmlStrdup(rpm2html_dir);
 2017     if (rpm2html_host != NULL)
 2018     ret->host = xmlStrdup(rpm2html_host);
 2019     if (rpm2html_maint != NULL)
 2020     ret->maint = xmlStrdup(rpm2html_maint);
 2021     if (rpm2html_mail != NULL)
 2022     ret->mail = xmlStrdup(rpm2html_mail);
 2023     if (rpm2html_url != NULL)
 2024     ret->url = xmlStrdup(rpm2html_url);
 2025     /* specific stuff */
 2026     ret->no = dist;
 2027     ret->rpmdir = xmlStrdup(directory);
 2028     ret->subdir = xmlStrdup(path);
 2029     ret->name = xmlStrdup(name);
 2030     ret->ftp = xmlStrdup(url);
 2031     if (color != NULL)
 2032     ret->color = xmlStrdup(color);
 2033     else
 2034     ret->color = xmlStrdup("#ffffff");
 2035     if (urlSrc != NULL)
 2036     ret->ftpsrc = xmlStrdup(urlSrc);
 2037     return(ret);
 2038 }
 2039 
 2040 void
 2041 sqlInitSqlDirList(void) {
 2042     char host[200];
 2043     rpmDirPtr cur;
 2044     MYSQL_RES *result;
 2045     MYSQL_ROW row;
 2046     char *query;
 2047     int i;
 2048 
 2049     if (sqlDirListInitialized != 0)
 2050     return;
 2051 
 2052 #ifdef SQL_DEBUG_MEM
 2053     printf("sqlInitSqlDirList\n");
 2054 #endif
 2055     gethostname(host, sizeof(host));
 2056     currentTime = time(NULL);
 2057     rpm2html_rpm2html_thishost = &host[0];
 2058     readConfigSql();
 2059     if (rpm2html_host == NULL) {
 2060     rpm2html_host = strdup(rpm2html_rpm2html_thishost);
 2061     }
 2062     for (i = 0; i < SQL_MAX_DISTRIBS;i++)
 2063      sqlDirList[i] = NULL;
 2064 
 2065     if (rpm2htmlVerbose)
 2066     printf("sqlInitSqlDirList query\n");
 2067 
 2068     query = "select ID,Name,Vendor,Directory,Path,URL,URLSrc,Html,Color from Distribs";
 2069 
 2070     if (mysql_query(sql,query)) {
 2071     printf("sqlInitSqlDirList: SELECT from Packages failed: %s\n",
 2072            mysql_error(sql));
 2073     return;
 2074     }
 2075 
 2076     i = 0;
 2077     result = mysql_use_result(sql);
 2078     if (result) {
 2079     while((row = mysql_fetch_row(result)))
 2080     {
 2081         cur = sqlRpmAnalyzeDirRow(row);
 2082         if (cur != NULL) {
 2083         sqlDirList[cur->no] = cur;
 2084         i++;
 2085         }
 2086     }
 2087     mysql_free_result(result);
 2088     }
 2089     if(mysql_errno(sql)) {
 2090     fprintf(stderr, "sqlInitSqlDirList Error: %s\n", mysql_error(sql));
 2091     return;
 2092     }
 2093 
 2094     if (rpm2htmlVerbose)
 2095     printf("sqlInitSqlDirList done: %d entries\n", i);
 2096     sqlDirListInitialized++;
 2097     return;
 2098 }
 2099 
 2100 rpmDataPtr
 2101 sqlRpmAnalyzeRow(MYSQL_ROW row) {
 2102     rpmDataPtr ret;
 2103     const char *id;
 2104     const char *name;
 2105     const char *version;
 2106     const char *release;
 2107     const char *arch;
 2108     const char *date;
 2109     const char *summary;
 2110     const char *filename;
 2111     const char *distrib;
 2112     const char *group;
 2113     const char *os;
 2114     const char *packager;
 2115     const char *vendor;
 2116     int dist;
 2117 
 2118     if (row == NULL)
 2119     return(NULL);
 2120     id = row[0];
 2121     name = row[1];
 2122     version = row[2];
 2123     release = row[3];
 2124     arch = row[4];
 2125     date = row[5];
 2126     summary = row[6];
 2127     filename = row[7];
 2128     distrib = row[8];
 2129     group = row[9];
 2130     os = row[10];
 2131     packager = row[11];
 2132     vendor = row[12];
 2133     if ((id == NULL) || (name == NULL) || (version == NULL) ||
 2134         (release == NULL) || (arch == NULL) || (date == NULL) ||
 2135     (summary == NULL) || (filename == NULL) || (distrib == NULL))
 2136     return(NULL);
 2137 
 2138     ret = (rpmDataPtr) xmlMalloc(sizeof(rpmData));
 2139     if (ret == NULL)
 2140     return(NULL);
 2141     memset(ret, 0, sizeof(rpmData));
 2142     ret->name = xmlStrdup(name);
 2143     ret->version = xmlStrdup(version);
 2144     ret->release = xmlStrdup(release);
 2145     ret->arch = xmlStrdup(arch);
 2146     ret->summary = xmlStrdup(summary);
 2147     ret->name = xmlStrdup(name);
 2148     ret->filename = xmlStrdup(filename);
 2149     if (os == NULL)
 2150     ret->os = xmlStrdup("Linux");
 2151     else
 2152     ret->os = xmlStrdup(os);
 2153     if (group != NULL)
 2154     ret->group = xmlStrdup(group);
 2155     sscanf(date, "%d", (int *) &(ret->date));
 2156     if (packager != NULL) {
 2157     int tmp = 0;
 2158     sscanf(packager, "%d", (int *) &tmp);
 2159     if ((tmp > 0) && (tmp < sqlVendorListLen)) {
 2160         if (sqlVendorList[tmp] != NULL)
 2161         ret->vendor = xmlStrdup(sqlVendorList[tmp]);
 2162     }
 2163     }
 2164     if (vendor != NULL) {
 2165     int tmp = 0;
 2166     sscanf(vendor, "%d", (int *) &tmp);
 2167     if ((tmp > 0) && (tmp < sqlDistributionListLen)) {
 2168         if (sqlVendorList[tmp] != NULL)
 2169         ret->distribution = xmlStrdup(sqlDistributionList[tmp]);
 2170     }
 2171     }
 2172     if (ret->vendor == NULL)
 2173     ret->vendor =  xmlStrdup("Unknown");
 2174     if (ret->distribution == NULL)
 2175     ret->distribution =  xmlStrdup("Unknown");
 2176     sscanf(distrib, "%d", &dist);
 2177     ret->dir = sqlDirList[dist];
 2178     if ((ret->dir != NULL) && (ret->dir->rpmdir != NULL)) {
 2179     int len = strlen(ret->dir->rpmdir);
 2180     if (!strncmp(ret->filename, ret->dir->rpmdir, len)) {
 2181         char *start, *end;
 2182         start = &(ret->filename[len]);
 2183         while (*start == '/') start++;
 2184         end = &start[strlen(start) - 1];
 2185         while ((end >= start) && (*end != '/')) end--;
 2186             if (end > start) {
 2187         char *tmp;
 2188         tmp = xmlMalloc((end - start) + 1);
 2189         if (tmp != NULL) {
 2190             strncpy(tmp, start, end - start);
 2191             tmp[end - start] = 0;
 2192             ret->subdir = tmp;
 2193         }
 2194         }
 2195     }
 2196     }
 2197     return(ret);
 2198 }
 2199 
 2200 rpmDataPtr
 2201 sqlRpmByDate(void) {
 2202     rpmDataPtr list = NULL;
 2203     rpmDataPtr cur, last = NULL;
 2204     MYSQL_RES *result;
 2205     MYSQL_ROW row;
 2206     char *query;
 2207 
 2208     sqlInitSqlDirList();
 2209     sqlInitSqlVendorList();
 2210     sqlInitSqlDistributionList();
 2211     if (rpm2htmlVerbose)
 2212     printf("sqlRpmByDate query\n");
 2213 
 2214     query = "select ID,Name,Version,Release,Arch,Date,Summary,filename,Dist,Category,Os,Packager,Vendor from Packages where Date IS NOT NULL  and Date < UNIX_TIMESTAMP() + 300000 order by Date desc limit 1000";
 2215 
 2216     /*
 2217      * Search first for the ID if it already exists
 2218      */
 2219     if (mysql_query(sql,query)) {
 2220     printf("sqlRpmByDate: SELECT from Packages failed: %s\n",
 2221            mysql_error(sql));
 2222     return(NULL);
 2223     }
 2224 
 2225     result = mysql_use_result(sql);
 2226     if (result) {
 2227     while((row = mysql_fetch_row(result)))
 2228     {
 2229         cur = sqlRpmAnalyzeRow(row);
 2230         if (cur != NULL) {
 2231         if (last == NULL)
 2232             list = cur;
 2233         else
 2234             last->next = cur;
 2235         last = cur;
 2236         }
 2237     }
 2238     mysql_free_result(result);
 2239     }
 2240     if(mysql_errno(sql)) {
 2241     fprintf(stderr, "sqlRpmByDate Error: %s\n", mysql_error(sql));
 2242     return(NULL);
 2243     }
 2244 
 2245     if (rpm2htmlVerbose)
 2246     printf("sqlRpmByDate done\n");
 2247     return(list);
 2248 }
 2249 
 2250 void 
 2251 sql_top_index(void) {
 2252     sqlInitSqlDirList();
 2253     dumpTopIndex(sqlDirList);
 2254 }
 2255 
 2256 rpmDataPtr
 2257 sqlRpmAll(void) {
 2258     rpmDataPtr list = NULL;
 2259     rpmDataPtr cur, last = NULL;
 2260     MYSQL_RES *result;
 2261     MYSQL_ROW row;
 2262     char *query;
 2263 
 2264     sqlInitSqlDirList();
 2265     dumpTopIndex(sqlDirList);
 2266     sqlInitSqlVendorList();
 2267     sqlInitSqlDistributionList();
 2268     if (rpm2htmlVerbose)
 2269     printf("sqlRpmAll query\n");
 2270 
 2271     query = "select ID,Name,Version,Release,Arch,Date,Summary,filename,Dist,Category,Os,Packager,Vendor from Packages";
 2272 
 2273     /*
 2274      * Search first for the ID if it already exists
 2275      */
 2276     if (mysql_query(sql,query)) {
 2277     printf("sqlRpmByDate: SELECT from Packages failed: %s\n",
 2278            mysql_error(sql));
 2279     return(NULL);
 2280     }
 2281 
 2282     result = mysql_use_result(sql);
 2283     if (result) {
 2284     while((row = mysql_fetch_row(result)))
 2285     {
 2286         cur = sqlRpmAnalyzeRow(row);
 2287         if (cur != NULL) {
 2288         if (last == NULL)
 2289             list = cur;
 2290         else
 2291             last->next = cur;
 2292         last = cur;
 2293 
 2294         rpmAddSoftware(cur);
 2295         }
 2296     }
 2297     mysql_free_result(result);
 2298     }
 2299     if(mysql_errno(sql)) {
 2300     fprintf(stderr, "sqlRpmByDate Error: %s\n", mysql_error(sql));
 2301     return(NULL);
 2302     }
 2303 
 2304     if (rpm2htmlVerbose)
 2305     printf("sqlRpmAll done\n");
 2306     return(list);
 2307 }
 2308 
 2309 void sqlListsCleanup(void) {
 2310     int i;
 2311 
 2312 #ifdef SQL_DEBUG_MEM
 2313     printf("sqlListsCleanup\n");
 2314 #endif
 2315     if (sqlDirListInitialized) {
 2316 #ifdef SQL_DEBUG_MEM
 2317     printf("sqlListsCleanup: freeing sqlDirList\n");
 2318 #endif
 2319     for (i = 0 ; i < SQL_MAX_DISTRIBS; i++) {
 2320         if (sqlDirList[i] != NULL)
 2321         rpmDirFree(sqlDirList[i]);
 2322         sqlDirList[i] = NULL;
 2323     }
 2324     sqlDirListInitialized = 0;
 2325     }
 2326     if (sqlVendorListInitialized) {
 2327 #ifdef SQL_DEBUG_MEM
 2328     printf("sqlListsCleanup: freeing sqlVendorList\n");
 2329 #endif
 2330     for (i = 1 ; i < sqlVendorListLen;i++) {
 2331         if (sqlVendorList[i] != NULL)
 2332         xmlFree(sqlVendorList[i]);
 2333         sqlVendorList[i] = NULL;
 2334     }
 2335     xmlFree(sqlVendorList);
 2336     sqlVendorListInitialized = 0;
 2337     }
 2338     if (sqlDistributionListInitialized) {
 2339 #ifdef SQL_DEBUG_MEM
 2340     printf("sqlListsCleanup: freeing sqlDistributionList\n");
 2341 #endif
 2342     for (i = 0 ; i < sqlDistributionListLen;i++) {
 2343         if (sqlDistributionList[i] != NULL)
 2344         xmlFree(sqlDistributionList[i]);
 2345         sqlDistributionList[i] = NULL;
 2346     }
 2347     xmlFree(sqlDistributionList);
 2348     sqlDistributionListInitialized = 0;
 2349     }
 2350     if (sqlResourceListInitialized) {
 2351 #ifdef SQL_DEBUG_MEM
 2352     printf("sqlListsCleanup: freeing sqlResourceList\n");
 2353 #endif
 2354     for (i = 0 ; i < sqlResourceListLen;i++) {
 2355         if (sqlResourceList[i] != NULL)
 2356         xmlFree(sqlResourceList[i]);
 2357         sqlResourceList[i] = NULL;
 2358     }
 2359     xmlFree(sqlResourceList);
 2360     sqlResourceListInitialized = 0;
 2361     }
 2362 }
 2363 
 2364 /************************************************************************
 2365  *                                  *
 2366  *          Export functions                *
 2367  *                                  *
 2368  ************************************************************************/
 2369 
 2370 void sql_dump_rdf_full_index(void) {
 2371     MYSQL_RES *result;
 2372     MYSQL_ROW row;
 2373     char query[SMALL_QUERY];
 2374     gzFile rdf;
 2375     char *filename;
 2376     char *dist;
 2377     char *name;
 2378     char *version;
 2379     char *release;
 2380     char *arch;
 2381     char *summary;
 2382     char *subdir;
 2383     char *psubdir;
 2384     char *rpmdir;
 2385     char *start, *end;
 2386     int distrib;
 2387     int len;
 2388     char path[500];
 2389 
 2390     if (!rpm2html_dump_rdf_resources)
 2391     return;
 2392     if (!rpm2html_rdf_resources_dir)
 2393     return;
 2394     if ((rpm2html_host == NULL) || (rpm2html_url == NULL))
 2395     return;
 2396 
 2397     /*
 2398      * Open the RDF resource descritpion.
 2399      */
 2400     mkdir(rpm2html_rdf_resources_dir, 0755);
 2401     snprintf(path, 500, "%s/fullIndex.rdf.gz",
 2402          rpm2html_rdf_resources_dir);
 2403     path[499] = 0;
 2404     rdf = gzopen(path, "w9");
 2405     if (rdf == NULL) {
 2406     fprintf(stderr, "unable to write to %s\n", path);
 2407     return;
 2408     }
 2409     gzprintf(rdf, "<?xml version=\"1.0\"?>\n");
 2410     gzprintf(rdf, "<rdf:RDF xmlns:RDF=\"http://www.w3.org/TR/WD-rdf-syntax#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:RPM=\"http://www.rpm.org/\">\n");
 2411 
 2412     /*
 2413      * Query the database for the information providing the resource.
 2414      */
 2415     snprintf(query, SMALL_QUERY,
 2416        "select filename,Dist,Name,Version,Release,Arch,Summary from Packages");
 2417     query[SMALL_QUERY - 1] = 0;
 2418     if (mysql_query(sql, query)) {
 2419     printf("sql_dump_rdf_full_index: SELECT from Packages failed: %s\n",
 2420            mysql_error(sql));
 2421         gzprintf(rdf, "</rdf:RDF>\n");
 2422     fclose(rdf);
 2423     return;
 2424     }
 2425     result = mysql_use_result(sql);
 2426     if (result == NULL) {
 2427     printf("sql_dump_rdf_full_index: SELECT from Packages failed: %s\n",
 2428            mysql_error(sql));
 2429         gzprintf(rdf, "</rdf:RDF>\n");
 2430     fclose(rdf);
 2431     return;
 2432     }
 2433     while((row = mysql_fetch_row(result))) {
 2434     filename = row[0];
 2435     dist = row[1];
 2436     name = row[2];
 2437     version = row[3];
 2438     release = row[4];
 2439     arch = row[5];
 2440     summary = row[6];
 2441     if ((filename == NULL) || (dist == NULL) || (name == NULL) ||
 2442         (summary == NULL)) {
 2443         continue;
 2444     }
 2445     if (!strncmp(filename, "localbase", 9))
 2446         continue;
 2447     if (sscanf(dist, "%d", &distrib) != 1)
 2448         continue;
 2449     if ((distrib < 1) || (distrib >= SQL_MAX_DISTRIBS) ||
 2450         (sqlDirList[distrib] == NULL))
 2451         continue;
 2452 
 2453     subdir = sqlDirList[distrib]->subdir;
 2454     rpmdir = sqlDirList[distrib]->rpmdir;
 2455     len = strlen(rpmdir);
 2456     if (strncmp(filename, rpmdir, len)) {
 2457         fprintf(stderr, "%s out of subdir %s\n", filename, subdir);
 2458         continue;
 2459     }
 2460     start = &(filename[len]);
 2461     while (*start == '/') start++;
 2462     end = &start[strlen(start) - 1];
 2463     while ((end >= start) && (*end != '/')) end--;
 2464     if (end <= start) {
 2465         psubdir = xmlStrdup("");
 2466     } else {
 2467         psubdir = xmlMalloc((end - start) + 1);
 2468         if (psubdir == NULL) {
 2469         fprintf(stderr, "Out of memory\n");
 2470         continue;
 2471         }
 2472         strncpy(psubdir, start, end - start);
 2473         psubdir[end - start] = 0;
 2474     }
 2475         gzprintf(rdf, "  <rdf:Description about=\"%s/%s/%s-%s-%s.%s.rpm\">\n",
 2476         sqlDirList[distrib]->ftp,psubdir,name,version,release,arch);
 2477     gzprintf(rdf, "    <RPM:Name>%s</RPM:Name>\n", name);
 2478     gzprintf(rdf, "    <RPM:Summary>%s</RPM:Summary>\n", 
 2479          convertXML(summary));
 2480     gzprintf(rdf, "  </rdf:Description>\n");
 2481     xmlFree(psubdir);
 2482     }
 2483     gzprintf(rdf, "</rdf:RDF>\n");
 2484     gzclose(rdf);
 2485     mysql_free_result(result);
 2486     if(mysql_errno(sql)) {
 2487     fprintf(stderr, "Error: %s\n", mysql_error(sql));
 2488     }
 2489 }
 2490 
 2491 
 2492 void sql_dump_rdf(const char *resource) {
 2493     MYSQL_RES *result;
 2494     MYSQL_ROW row;
 2495     char query[MAX_QUERY];
 2496     FILE *rdf;
 2497     char *filename;
 2498     char *dist;
 2499     char *name;
 2500     char *version;
 2501     char *release;
 2502     char *arch;
 2503     char *os;
 2504     char *packager;
 2505     char *date;
 2506     char *size;
 2507     char *vendorstr;
 2508     char *subdir;
 2509     char *psubdir;
 2510     char *rpmdir;
 2511     char *start, *end;
 2512     int distrib;
 2513     int vendor;
 2514     int person;
 2515     int len;
 2516     char path[500];
 2517 
 2518     if (resource == NULL)
 2519     return;
 2520     if (resource[0] == '/')
 2521     return;
 2522     if (!rpm2html_dump_rdf_resources)
 2523     return;
 2524     if (!rpm2html_rdf_resources_dir)
 2525     return;
 2526     if ((rpm2html_host == NULL) || (rpm2html_url == NULL))
 2527     return;
 2528 
 2529     /*
 2530      * Open the RDF resource descritpion.
 2531      */
 2532     mkdir(rpm2html_rdf_resources_dir, 0755);
 2533     snprintf(path, 500, "%s/%s.rdf",
 2534          rpm2html_rdf_resources_dir, resource);
 2535     path[499] = 0;
 2536     rdf = fopen(path, "w");
 2537     if (rdf == NULL) {
 2538     fprintf(stderr, "unable to write to %s\n", path);
 2539     return;
 2540     }
 2541     fprintf(rdf, "<?xml version=\"1.0\"?>\n");
 2542     fprintf(rdf, "<rdf:RDF xmlns:RDF=\"http://www.w3.org/TR/WD-rdf-syntax#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:RPM=\"http://www.rpm.org/\">\n");
 2543 
 2544     /*
 2545      * Query the database for the information providing the resource.
 2546      */
 2547     snprintf(query, MAX_QUERY,
 2548 "select Packages.filename,Packages.Dist,Packages.Name,Packages.Version,Packages.Release,Packages.Arch,Packages.Os,Packages.Packager,Packages.Date,Packages.Size,Packages.Vendor from Packages,Provides where Provides.Resource=\"%s\" and Provides.ID = Packages.ID",
 2549              resource);
 2550     query[MAX_QUERY - 1] = 0;
 2551     if (mysql_query(sql, query)) {
 2552     printf("sql_dump_rdf: SELECT from Packages failed: %s\n",
 2553            mysql_error(sql));
 2554         fprintf(rdf, "</rdf:RDF>\n");
 2555     fclose(rdf);
 2556     return;
 2557     }
 2558     result = mysql_use_result(sql);
 2559     if (result == NULL) {
 2560     printf("sql_dump_rdf: SELECT from Packages failed: %s\n",
 2561            mysql_error(sql));
 2562         fprintf(rdf, "</rdf:RDF>\n");
 2563     fclose(rdf);
 2564     return;
 2565     }
 2566     while((row = mysql_fetch_row(result))) {
 2567     filename = row[0];
 2568     dist = row[1];
 2569     name = row[2];
 2570     version = row[3];
 2571     release = row[4];
 2572     arch = row[5];
 2573     os = row[6];
 2574     packager = row[7];
 2575     date = row[8];
 2576     size = row[9];
 2577     vendorstr = row[10];
 2578     if ((filename == NULL) || (dist == NULL) || (name == NULL) ||
 2579         (version == NULL) || (release == NULL) || (arch == NULL) ||
 2580         (os == NULL) || (date == NULL) || (vendorstr == NULL) ||
 2581         (size == NULL)) {
 2582         continue;
 2583     }
 2584     if (!strncmp(filename, "localbase", 9))
 2585         continue;
 2586     if (sscanf(dist, "%d", &distrib) != 1)
 2587         continue;
 2588     if ((distrib < 1) || (distrib >= SQL_MAX_DISTRIBS) ||
 2589         (sqlDirList[distrib] == NULL))
 2590         continue;
 2591     if (vendorstr != NULL) {
 2592         if (sscanf(vendorstr, "%d", &vendor) != 1)
 2593         continue;
 2594     }
 2595     if ((vendor < 1) || (vendor >= sqlDistributionListLen) ||
 2596         (sqlDistributionList[vendor] == NULL))
 2597         continue;
 2598     if (packager != NULL) {
 2599         if (sscanf(packager, "%d", &person) != 1)
 2600         continue;
 2601     }
 2602     if ((person < 1) || (person >= sqlVendorListLen) ||
 2603         (sqlVendorList[person] == NULL))
 2604         continue;
 2605 
 2606     subdir = sqlDirList[distrib]->subdir;
 2607     rpmdir = sqlDirList[distrib]->rpmdir;
 2608     len = strlen(rpmdir);
 2609     if (strncmp(filename, rpmdir, len)) {
 2610         fprintf(stderr, "%s out of subdir %s\n", filename, subdir);
 2611         continue;
 2612     }
 2613     start = &(filename[len]);
 2614     while (*start == '/') start++;
 2615     end = &start[strlen(start) - 1];
 2616     while ((end >= start) && (*end != '/')) end--;
 2617     if (end <= start) {
 2618         psubdir = xmlStrdup("");
 2619     } else {
 2620         psubdir = xmlMalloc((end - start) + 1);
 2621         if (psubdir == NULL) {
 2622         fprintf(stderr, "Out of memory\n");
 2623         continue;
 2624         }
 2625         strncpy(psubdir, start, end - start);
 2626         psubdir[end - start] = 0;
 2627     }
 2628         fprintf(rdf, "  <rdf:Description about=\"%s/%s/%s-%s-%s.%s.rpm\" ",
 2629         sqlDirList[distrib]->ftp,psubdir,name,version,release,arch);
 2630     fprintf(rdf, "href=\"../%s/%s/%s-%s-%s.%s.rdf\">\n",
 2631             subdir,psubdir,name,version,release,arch);
 2632     fprintf(rdf, "    <RPM:Name>%s</RPM:Name>\n", name);
 2633     fprintf(rdf, "    <RPM:Version>%s</RPM:Version>\n", version);
 2634     fprintf(rdf, "    <RPM:Release>%s</RPM:Release>\n", release);
 2635     fprintf(rdf, "    <RPM:Arch>%s</RPM:Arch>\n", arch);
 2636     fprintf(rdf, "    <RPM:Os>%s</RPM:Os>\n", os);
 2637     if (packager == NULL)
 2638         fprintf(rdf, "    <RPM:Distribution>Unknown</RPM:Distribution>\n");
 2639     else
 2640         fprintf(rdf, "    <RPM:Distribution>%s</RPM:Distribution>\n",
 2641             convertXML(sqlDistributionList[vendor]));
 2642     fprintf(rdf, "    <RPM:Vendor>%s</RPM:Vendor>\n",
 2643         convertXML(sqlVendorList[person]));
 2644     fprintf(rdf, "    <RPM:Date>%s</RPM:Date>\n", date);
 2645     fprintf(rdf, "    <RPM:Size>%s</RPM:Size>\n", size);
 2646     fprintf(rdf, "    <RPM:Subdir>%s</RPM:Subdir>\n", subdir);
 2647     fprintf(rdf, "  </rdf:Description>\n");
 2648     xmlFree(psubdir);
 2649     }
 2650     fprintf(rdf, "</rdf:RDF>\n");
 2651     fclose(rdf);
 2652     mysql_free_result(result);
 2653     if(mysql_errno(sql)) {
 2654     fprintf(stderr, "Error: %s\n", mysql_error(sql));
 2655     }
 2656 }
 2657 
 2658 void sql_show_config(void) {
 2659     MYSQL_RES *result;
 2660     MYSQL_ROW row;
 2661     int id, i;
 2662     int index = 0;
 2663     int ids[500];
 2664     char query[SMALL_QUERY];
 2665 
 2666 
 2667     printf(";\n; Configuration file for rpm2html\n");
 2668     printf("; http://rpmfind.net/linux/rpm2html/\n;\n\n");
 2669     mysql_query(sql,"SELECT Name,Value FROM Config");
 2670     result = mysql_use_result(sql);
 2671 
 2672     while((row = mysql_fetch_row(result)))
 2673     {
 2674     if (row[0] == NULL) {
 2675         printf("\n");
 2676     } else {
 2677         if (!strcmp(row[0], "maint"))
 2678         printf("; maintainer of the local rpm mirror\n");
 2679         else if (!strcmp(row[0], "mail"))
 2680         printf("; mail for the maintainer\n");
 2681         else if (!strcmp(row[0], "dir"))
 2682                 printf("; Directory to store the HTML pages produced\n");
 2683         else if (!strcmp(row[0], "url"))
 2684                 printf("; The relative URL for front pages\n");
 2685         else if (!strcmp(row[0], "header"))
 2686                 printf("; Extra link in the navigation bar\n");
 2687         else if (!strcmp(row[0], "html"))
 2688                 printf("; Export the local packages in HTML format\n");
 2689         else if (!strcmp(row[0], "tree"))
 2690                 printf("; Build the tree for the distributions\n");
 2691         else if (!strcmp(row[0], "rdf"))
 2692                 printf("; Export the local packages in RDF format\n");
 2693         else if (!strcmp(row[0], "rdf_dir"))
 2694                 printf("; Directory to store the RDf tree\n");
 2695         else if (!strcmp(row[0], "rdf_resources"))
 2696                 printf("; Compile a list of resources in RDF format\n");
 2697         else if (!strcmp(row[0], "rdf_resources_dir"))
 2698                 printf("; Directory to store the RDf resources tree\n");
 2699 
 2700         if (row[1] == NULL)
 2701         printf("%s\n\n", row[0]);
 2702         else
 2703         printf("%s=%s\n\n", row[0], row[1]);
 2704     }
 2705     }
 2706     mysql_free_result(result);
 2707     if(mysql_errno(sql)) {
 2708     fprintf(stderr, "Error: %s\n", mysql_error(sql));
 2709     }
 2710 
 2711     /*
 2712      * Dump the Metadata
 2713      */
 2714     printf(";\n; The metadata mirrors list\n;\n\n[metadata]\n");
 2715     mysql_query(sql,"SELECT URL FROM Metadata");
 2716     result = mysql_use_result(sql);
 2717 
 2718     while((row = mysql_fetch_row(result)))
 2719     {
 2720     if (row[0] != NULL) {
 2721         printf("mirror=%s\n", row[0]);
 2722     }
 2723     }
 2724     mysql_free_result(result);
 2725     if(mysql_errno(sql)) {
 2726     fprintf(stderr, "Error: %s\n", mysql_error(sql));
 2727     }
 2728 
 2729     /*
 2730      * Dump the distributions information
 2731      * 1/ collect the list of IDs for the distribs
 2732      */
 2733     printf("\n\n;\n; The distribution list\n;\n\n");
 2734     mysql_query(sql,"SELECT ID FROM Distribs");
 2735     result = mysql_use_result(sql);
 2736 
 2737     while((row = mysql_fetch_row(result)))
 2738     {
 2739     if (row[0] != NULL) {
 2740         if (sscanf(row[0], "%d", &id) == 1) {
 2741         ids[index++] = id;
 2742         }
 2743     }
 2744     }
 2745     mysql_free_result(result);
 2746 
 2747     /*
 2748      * Dump each distribution separately.
 2749      */
 2750     for (i = 0;i < index;i++) {
 2751 
 2752     snprintf(query, SMALL_QUERY - 1,
 2753  "SELECT Directory,Name,Vendor,Path,URL,URLSrc,Description,Html,Color \
 2754           FROM Distribs WHERE ID=%d", ids[i]);
 2755     query[SMALL_QUERY - 1] = 0;
 2756     if (mysql_query(sql,query)) {
 2757         printf("sql_show_config: SELECT Distrib %d failed: %s\n",
 2758            ids[i], mysql_error(sql));
 2759         continue;
 2760     }
 2761 
 2762     result = mysql_use_result(sql);
 2763     if (result) {
 2764         while((row = mysql_fetch_row(result)))
 2765         {
 2766         if (row[0] == NULL)
 2767             break;
 2768         printf("[%s]\n", row[0]);
 2769         if (row[1] != NULL)
 2770             printf("name=%s\n", row[1]);
 2771         if (row[3] != NULL)
 2772             printf("subdir=%s\n", row[3]);
 2773         if (row[4] != NULL)
 2774             printf("ftp=%s\n", row[4]);
 2775         if (row[5] != NULL)
 2776             printf("ftpsrc=%s\n", row[5]);
 2777         if (row[7] != NULL)
 2778             printf("html=%s\n", row[7]);
 2779         if (row[8] != NULL)
 2780             printf("color=%s\n", row[8]);
 2781         }
 2782     }
 2783     mysql_free_result(result);
 2784 
 2785     /*
 2786      * Extract the mirrors for this distribution.
 2787      */
 2788     snprintf(query, SMALL_QUERY - 1,
 2789          "SELECT URL FROM Mirrors WHERE ID=%d", ids[i]);
 2790     query[SMALL_QUERY - 1] = 0;
 2791     if (mysql_query(sql,query)) {
 2792         printf("sql_show_config: SELECT Mirrors %d failed: %s\n",
 2793            ids[i], mysql_error(sql));
 2794         printf("\n\n");
 2795         continue;
 2796     }
 2797     result = mysql_use_result(sql);
 2798     if (result) {
 2799         while((row = mysql_fetch_row(result)))
 2800         {
 2801         if (row[0] == NULL)
 2802             continue;
 2803         printf("mirror=%s\n", row[0]);
 2804         }
 2805     }
 2806     mysql_free_result(result);
 2807 
 2808     printf("\n\n");
 2809     }
 2810 
 2811     printf(";\n; End of the configuration file for rpm2html\n;\n");
 2812 }
 2813 
 2814 void sql_show_metadata(void) {
 2815     MYSQL_RES *result;
 2816     MYSQL_ROW row;
 2817 
 2818 
 2819     mysql_query(sql,"SELECT URL FROM Metadata");
 2820     result = mysql_use_result(sql);
 2821 
 2822     while((row = mysql_fetch_row(result)))
 2823     {
 2824     if (row[0] == NULL)
 2825         printf("NULL !\n");
 2826     else {
 2827         printf("%s\n", row[0]);
 2828     }
 2829     }
 2830     if(mysql_errno(sql)) {
 2831     fprintf(stderr, "Error: %s\n", mysql_error(sql));
 2832     }
 2833 
 2834 }
 2835 
 2836 void sql_show_mirrors(void) {
 2837     MYSQL_RES *result;
 2838     MYSQL_ROW row;
 2839 
 2840 
 2841     mysql_query(sql,"SELECT URL FROM Mirrors");
 2842     result = mysql_use_result(sql);
 2843 
 2844     while((row = mysql_fetch_row(result)))
 2845     {
 2846     if (row[0] == NULL)
 2847         printf("NULL !\n");
 2848     else {
 2849         printf("%s\n", row[0]);
 2850     }
 2851     }
 2852     if(mysql_errno(sql)) {
 2853     fprintf(stderr, "Error: %s\n", mysql_error(sql));
 2854     }
 2855 }
 2856 
 2857 void sql_show_vendors(void) {
 2858     MYSQL_RES *result;
 2859     MYSQL_ROW row;
 2860 
 2861 
 2862     mysql_query(sql,"SELECT Name, URL FROM Vendors");
 2863     result = mysql_use_result(sql);
 2864 
 2865     while((row = mysql_fetch_row(result)))
 2866     {
 2867     if (row[0] == NULL)
 2868         printf("NULL !\n");
 2869     else {
 2870         if (row[1] == NULL)
 2871         printf("%s : no url\n", row[0]);
 2872         else
 2873         printf("%s : %s\n", row[0], row[1]);
 2874     }
 2875     }
 2876     if(mysql_errno(sql)) {
 2877     fprintf(stderr, "Error: %s\n", mysql_error(sql));
 2878     }
 2879 
 2880 }
 2881 
 2882 void sql_show_distribs(void) {
 2883     MYSQL_RES *result;
 2884     MYSQL_ROW row;
 2885 
 2886 
 2887     mysql_query(sql,"SELECT Name, Path, URL FROM Distribs");
 2888     result = mysql_use_result(sql);
 2889 
 2890     while((row = mysql_fetch_row(result)))
 2891     {
 2892     if (row[0] == NULL)
 2893         printf("NULL !\n");
 2894     else {
 2895         if (row[1] == NULL)
 2896         printf("%s : no Path\n", row[0]);
 2897         else {
 2898         if (row[2] == NULL)
 2899             printf("%s : %s : no url\n", row[0], row[1]);
 2900         else
 2901             printf("%s : %s : %s\n", row[0], row[1], row[2]);
 2902         }
 2903     }
 2904     }
 2905     if(mysql_errno(sql)) {
 2906     fprintf(stderr, "Error: %s\n", mysql_error(sql));
 2907     }
 2908 
 2909 }
 2910 
 2911 int sql_show_table_stats(const char *table, const char *key) {
 2912     char query[MAX_QUERY];
 2913     MYSQL_RES *result;
 2914     MYSQL_ROW row;
 2915     int res;
 2916 
 2917     /*
 2918      * Search first for the ID if it already exists
 2919      */
 2920     snprintf(query, MAX_QUERY - 1, "SELECT COUNT(%s) FROM %s", key, table);
 2921     query[MAX_QUERY - 1] = 0;
 2922     if (mysql_query(sql,query)) {
 2923     printf("sql_show_table_stats: SELECT COUNT failed: %s\n",
 2924            mysql_error(sql));
 2925     return(-1);
 2926     }
 2927 
 2928     result = mysql_use_result(sql);
 2929     if (result) {
 2930     while((row = mysql_fetch_row(result)))
 2931     {
 2932         /*
 2933          * Lookup the value and return it
 2934          */
 2935         if (row[0] == NULL) {
 2936         mysql_free_result(result);
 2937         printf("sql_show_table_stats: select count returns NULL !\n");
 2938         return(-1);
 2939         }
 2940         if (sscanf(row[0], "%d", &res) != 1) {
 2941         mysql_free_result(result);
 2942         printf("sql_show_table_stats: value non numeric %s\n", row[0]);
 2943         return(-1);
 2944         }
 2945         mysql_free_result(result);
 2946         if (res <= 0)
 2947         printf("   %s is empty\n", table);
 2948         else
 2949         printf("   %s contains %d records\n", table, res);
 2950         return(res);
 2951     }
 2952     mysql_free_result(result);
 2953     }
 2954     if(mysql_errno(sql)) {
 2955     fprintf(stderr, "sql_show_stats error: %s\n", mysql_error(sql));
 2956     }
 2957     return(-1);
 2958 }
 2959 
 2960 int sql_show_stats(void) {
 2961     const char *query = "SHOW TABLES";
 2962     MYSQL_RES *result;
 2963     MYSQL_ROW row;
 2964 
 2965     int tables = 0;
 2966     int records = 0;
 2967 
 2968     if (mysql_query(sql,query)) {
 2969     printf("sql_check_tables: SHOW TABLES failed %s\n",
 2970            mysql_error(sql));
 2971     return(-1);
 2972     }
 2973 
 2974     result = mysql_use_result(sql);
 2975     if (result) {
 2976     while((row = mysql_fetch_row(result)))
 2977     {
 2978         if (row[0] == NULL) {
 2979         mysql_free_result(result);
 2980         printf("sql_check_tables: SHOW TABLES returns NULL !\n");
 2981         return(-1);
 2982         }
 2983         tables++;
 2984     }
 2985     mysql_free_result(result);
 2986     }
 2987     if(mysql_errno(sql)) {
 2988     fprintf(stderr, "sql_show_stats error: %s\n", mysql_error(sql));
 2989     }
 2990     printf("%d tables in use\n", tables);
 2991     records += sql_show_table_stats("Config", "Name");
 2992     records += sql_show_table_stats("Distribs", "Name");
 2993     records += sql_show_table_stats("Vendors", "Name");
 2994     records += sql_show_table_stats("Mirrors", "URL");
 2995     records += sql_show_table_stats("Metadata", "URL");
 2996     records += sql_show_table_stats("Packages", "Name");
 2997     records += sql_show_table_stats("Requires", "Resource");
 2998     records += sql_show_table_stats("Provides", "Resource");
 2999     records += sql_show_table_stats("Files", "Path");
 3000     printf("Total: %d records\n", records);
 3001     return(records);
 3002 }
 3003 
 3004 void sql_show_latests(void) {
 3005     rpmDataPtr list;
 3006 
 3007     list = sqlRpmByDate();
 3008     if (list == NULL) {
 3009     fprintf(stderr, "no package in database\n");
 3010     return;
 3011     }
 3012     dumpRpmByDate(list, 0);
 3013 }
 3014 
 3015 
 3016 void sql_show_resources(void) {
 3017     int i;
 3018 
 3019     sqlInitSqlDirList();
 3020     sqlInitSqlResourceList();
 3021     sqlInitSqlDistributionList();
 3022     sqlInitSqlVendorList();
 3023     for (i = 0;i < sqlResourceListLen;i++) {
 3024     if (sqlResourceList[i] == NULL)
 3025         break;
 3026     dumpRessRedirHtml(sqlResourceList[i]);
 3027     if (rpm2html_dump_rdf_resources)
 3028         sql_dump_rdf(sqlResourceList[i]);
 3029     }
 3030     if (rpm2htmlVerbose)
 3031     fprintf(stderr, "Dumped %d redirrect resource pages\n", i);
 3032     if (rpm2html_dump_rdf_resources)
 3033     sql_dump_rdf_full_index();
 3034     sqlListsCleanup();
 3035 }
 3036 
 3037 void sql_reindex(void) {
 3038     int i;
 3039     int pid, status;
 3040 
 3041     sqlInitSqlDirList();
 3042 
 3043 #ifdef SQL_DEBUG
 3044     printf("sql_reindex\n");
 3045 #endif
 3046     if (sqlDirListInitialized) {
 3047     for (i = 0 ; i < SQL_MAX_DISTRIBS;i++) {
 3048         if (sqlDirList[i] == NULL)
 3049             continue;
 3050         if (sqlDirList[i]->rpmdir != NULL) {
 3051         printf("sqltools indexing: %s\n", sqlDirList[i]->name);
 3052         pid = fork();
 3053         if (pid < 0) {
 3054             fprintf(stderr, "fork failed \n");
 3055             continue;
 3056         }
 3057         if (pid == 0) {
 3058             printf("%s %s %s %s\n",
 3059                "./rpm2html", "-dir", sqlDirList[i]->rpmdir,
 3060                "./rpm2html-local.config");
 3061             execlp("./rpm2html",
 3062               "./rpm2html", "-dir", sqlDirList[i]->rpmdir,
 3063               "./rpm2html-local.config", NULL);
 3064             fprintf(stderr, "Unable to launch rpm2html\n");
 3065             exit(127);
 3066         }
 3067         do {
 3068             if (waitpid(pid, &status, 0) == -1) {
 3069             if (errno != EINTR)
 3070                 goto done;
 3071             } else {
 3072             if (status != 0)
 3073                 printf("indexing: %s exit %d\n",
 3074                    sqlDirList[i]->name, status);
 3075             break;
 3076             }
 3077         } while(1);
 3078 
 3079         }
 3080     }
 3081     }
 3082 done:
 3083     sqlListsCleanup();
 3084 }
 3085 
 3086 void sql_show_index(void) {
 3087     sqlInitSqlDirList();
 3088     sqlInitSqlDistributionList();
 3089     if (rpm2html_dump_rdf_resources)
 3090     sql_dump_rdf_full_index();
 3091     sqlListsCleanup();
 3092 }
 3093 
 3094 void sql_show_all(void) {
 3095     rpmDataPtr list;
 3096 #ifdef SQL_DEBUG_TIMING
 3097     time_t cur, last;
 3098 
 3099     last = time(NULL);
 3100 #endif
 3101     list = sqlRpmAll();
 3102     if (list == NULL) {
 3103     fprintf(stderr, "no package in database\n");
 3104     return;
 3105     }
 3106 #ifdef SQL_DEBUG_TIMING
 3107     cur = time(NULL);
 3108     fprintf(stderr, "sqlRpmAll took %d seconds\n", (int)(cur - last));
 3109     last = cur;
 3110 #endif
 3111 
 3112     rpmNameSort(&list, 0);
 3113     dumpRpmByName(rpmSoftwareList, 0);
 3114 
 3115 #ifdef SQL_DEBUG_TIMING
 3116     cur = time(NULL);
 3117     fprintf(stderr, "dumpRpmByName took %d seconds\n", (int)(cur - last));
 3118     last = cur;
 3119 #endif
 3120 
 3121     rpmGroupSort(&list, 0);
 3122     dumpRpmByGroups(rpmSoftwareList, 0);
 3123 
 3124 #ifdef SQL_DEBUG_TIMING
 3125     cur = time(NULL);
 3126     fprintf(stderr, "dumpRpmByGroups took %d seconds\n", (int)(cur - last));
 3127     last = cur;
 3128 #endif
 3129 
 3130     rpmVendorSort(&list, 0);
 3131     dumpRpmByVendors(list, 0);
 3132 
 3133 #ifdef SQL_DEBUG_TIMING
 3134     cur = time(NULL);
 3135     fprintf(stderr, "dumpRpmByVendors took %d seconds\n", (int)(cur - last));
 3136     last = cur;
 3137 #endif
 3138 
 3139     rpmDistribSort(&list, 0);
 3140     dumpRpmByDistribs(list, 0);
 3141 
 3142 #ifdef SQL_DEBUG_TIMING
 3143     cur = time(NULL);
 3144     fprintf(stderr, "dumpRpmByDistribs took %d seconds\n", (int)(cur - last));
 3145     last = cur;
 3146 #endif
 3147 
 3148     rpmDateSort(&list, 0);
 3149     dumpRpmByDate(list, 0);
 3150 
 3151 #ifdef SQL_DEBUG_TIMING
 3152     cur = time(NULL);
 3153     fprintf(stderr, "dumpRpmByDate took %d seconds\n", (int)(cur - last));
 3154     last = cur;
 3155 #endif
 3156 
 3157     rpmdataCleanup();
 3158     sleep(30);
 3159 }
 3160 
 3161 /************************************************************************
 3162  *                                  *
 3163  *          rpm2html configuration functions        *
 3164  *                                  *
 3165  ************************************************************************/
 3166 
 3167 int readConfigSql(void) {
 3168     char query[MAX_QUERY];
 3169     MYSQL_RES *result;
 3170     MYSQL_ROW row;
 3171     int dir = 0;
 3172 
 3173     /*
 3174      * General configuration information
 3175      */
 3176     snprintf(query, MAX_QUERY - 1, "SELECT Name,Value FROM Config");
 3177     query[MAX_QUERY - 1] = 0;
 3178     if (mysql_query(sql,query)) {
 3179     printf("sql_check_tables: SELECT Config failed %s\n",
 3180            mysql_error(sql));
 3181     return(-1);
 3182     }
 3183 
 3184     result = mysql_use_result(sql);
 3185     if (result) {
 3186     while((row = mysql_fetch_row(result)))
 3187     {
 3188         if ((row[0] == NULL) || (row[1] == NULL)) {
 3189         fprintf(stderr, "readConfigSql : found NULL value\n");
 3190         continue;
 3191         }
 3192         if (!strcmp(row[0], "dir"))
 3193         dir = 1;
 3194         addConfigEntry(RPM2HTML_NAME, row[0], row[1]);
 3195     }
 3196     mysql_free_result(result);
 3197     }
 3198     if(mysql_errno(sql)) {
 3199     fprintf(stderr, "sql_show_stats error: %s\n", mysql_error(sql));
 3200     }
 3201     if (dir == 0) {
 3202     fprintf(stderr, "readConfigSql : no directory\n");
 3203     return(-1);
 3204     }
 3205 
 3206     /*
 3207      * The metadata mirror list.
 3208      */
 3209     snprintf(query, MAX_QUERY - 1, "SELECT URL FROM Metadata");
 3210     query[MAX_QUERY - 1] = 0;
 3211     if (mysql_query(sql,query)) {
 3212     printf("sql_check_tables: SELECT Metadata failed %s\n",
 3213            mysql_error(sql));
 3214     return(-1);
 3215     }
 3216 
 3217     result = mysql_use_result(sql);
 3218     if (result) {
 3219     while((row = mysql_fetch_row(result)))
 3220     {
 3221         if (row[0] == NULL) {
 3222         fprintf(stderr, "readConfigSql : found NULL metadata\n");
 3223         continue;
 3224         }
 3225         addConfigEntry("metadata", "mirror", row[0]);
 3226     }
 3227     mysql_free_result(result);
 3228     }
 3229     if(mysql_errno(sql)) {
 3230     fprintf(stderr, "sql_show_stats error: %s\n", mysql_error(sql));
 3231     }
 3232 
 3233     /*
 3234      * The distribution lists
 3235      */
 3236     snprintf(query, MAX_QUERY - 1, "SELECT Directory,Name,URL,URLSrc,Path FROM Distribs");
 3237     query[MAX_QUERY - 1] = 0;
 3238     if (mysql_query(sql,query)) {
 3239     printf("sql_check_tables: SELECT Distribs failed %s\n",
 3240            mysql_error(sql));
 3241     return(-1);
 3242     }
 3243 
 3244     result = mysql_use_result(sql);
 3245     if (result) {
 3246     while((row = mysql_fetch_row(result)))
 3247     {
 3248         if (row[0] == NULL) {
 3249         fprintf(stderr, "readConfigSql : found NULL distro\n");
 3250         continue;
 3251         }
 3252 
 3253         if (row[1] != NULL)
 3254         addConfigEntry(row[0], "name", row[1]);
 3255         if (row[2] != NULL)
 3256         addConfigEntry(row[0], "ftp", row[2]);
 3257         if (row[3] != NULL)
 3258         addConfigEntry(row[0], "ftpsrc", row[3]);
 3259         if (row[4] != NULL)
 3260         addConfigEntry(row[0], "subdir", row[4]);
 3261     }
 3262     mysql_free_result(result);
 3263     }
 3264     if(mysql_errno(sql)) {
 3265     fprintf(stderr, "sql_show_stats error: %s\n", mysql_error(sql));
 3266     }
 3267 
 3268     /*
 3269      * The Mirrors
 3270      */
 3271     snprintf(query, MAX_QUERY - 1, "SELECT Distribs.Directory,Mirrors.URL FROM Distribs,Mirrors WHERE Distribs.ID = Mirrors.ID");
 3272     query[MAX_QUERY - 1] = 0;
 3273     if (mysql_query(sql,query)) {
 3274     printf("sql_check_tables: SELECT Distribs failed %s\n",
 3275            mysql_error(sql));
 3276     return(-1);
 3277     }
 3278 
 3279     result = mysql_use_result(sql);
 3280     if (result) {
 3281     while((row = mysql_fetch_row(result)))
 3282     {
 3283         if ((row[0] == NULL) || (row[1] == NULL)) {
 3284         fprintf(stderr, "readConfigSql : found NULL mirror\n");
 3285         continue;
 3286         }
 3287 
 3288         addConfigEntry(row[0], "mirror", row[1]);
 3289     }
 3290     mysql_free_result(result);
 3291     }
 3292     if(mysql_errno(sql)) {
 3293     fprintf(stderr, "sql_show_stats error: %s\n", mysql_error(sql));
 3294     }
 3295 
 3296     /*
 3297      * TODO: add the language(s) stuff
 3298      */
 3299     return(0);
 3300 }
 3301 
 3302 void sqlConfigEntry(const char *rpmdir, const char *name, const char *value) {
 3303     int distrib;
 3304 
 3305     if (rpm2htmlVerbose > 1)
 3306     printf("sqlConfigEntry(\"%s\", \"%s\", \"%s\")\n", rpmdir, name, value);
 3307 
 3308     /*
 3309      * case of global option for rpm2html.
 3310      */
 3311     if (!strcasecmp(rpmdir, RPM2HTML_NAME)) {
 3312     sql_add_config_info(name, value);
 3313     return;
 3314     }
 3315 
 3316     /*
 3317      * Options for the metadata mirrors.
 3318      */
 3319     if (!strcasecmp(rpmdir, "metadata")) {
 3320     if (!strcasecmp(name, "mirror")) {
 3321         sql_add_metadata_base(value);
 3322     } else {
 3323         printf("Config file : %s entry for [metadata] ignored\n", name);
 3324     }
 3325     return;
 3326     }
 3327 
 3328     /*
 3329      * option for a directory.
 3330      */
 3331     if (!strcasecmp(name, "name")) {
 3332     sql_add_distrib(value, NULL, rpmdir, NULL, NULL, NULL, NULL,
 3333                 NULL, NULL);
 3334     } else if (!strcasecmp(name, "subdir")) {
 3335     distrib = sql_read_info_key("Distribs", "Directory", rpmdir);
 3336     if (distrib > 0)
 3337             sql_update_id("Distribs", distrib, "Path", value);
 3338     else
 3339         fprintf(stderr, "database has no distrib: %s",
 3340             rpmdir);
 3341     } else if (!strcasecmp(name, "url")) {
 3342     distrib = sql_read_info_key("Distribs", "Directory", rpmdir);
 3343     if (distrib > 0)
 3344             sql_update_id("Distribs", distrib, "URL", value);
 3345     else
 3346         fprintf(stderr, "database has no distrib: %s",
 3347             rpmdir);
 3348     } else if (!strcasecmp(name, "ftp")) {
 3349     distrib = sql_read_info_key("Distribs", "Directory", rpmdir);
 3350     if (distrib > 0)
 3351             sql_update_id("Distribs", distrib, "URL", value);
 3352     else
 3353         fprintf(stderr, "database has no distrib: %s",
 3354             rpmdir);
 3355     } else if (!strcasecmp(name, "ftpsrc")) {
 3356     distrib = sql_read_info_key("Distribs", "Directory", rpmdir);
 3357     if (distrib > 0)
 3358             sql_update_id("Distribs", distrib, "URLSrc", value);
 3359     else
 3360         fprintf(stderr, "database has no distrib: %s",
 3361             rpmdir);
 3362     } else if (!strcasecmp(name, "html")) {
 3363     distrib = sql_read_info_key("Distribs", "Directory", rpmdir);
 3364     if (distrib > 0)
 3365             sql_update_id("Distribs", distrib, "Html", value);
 3366     else
 3367         fprintf(stderr, "database has no distrib: %s",
 3368             rpmdir);
 3369     } else if (!strcasecmp(name, "color")) {
 3370     distrib = sql_read_info_key("Distribs", "Directory", rpmdir);
 3371     if (distrib > 0)
 3372             sql_update_id("Distribs", distrib, "Color", value);
 3373     else
 3374         fprintf(stderr, "database has no distrib: %s",
 3375             rpmdir);
 3376     } else if (!strcasecmp(name, "mirror")) {
 3377     distrib = sql_read_info_key("Distribs", "Directory", rpmdir);
 3378     if (distrib > 0)
 3379         sql_add_dist_mirror(distrib, value, 0);
 3380     else
 3381         fprintf(stderr, "database has no distrib: %s",
 3382             rpmdir);
 3383     } else {
 3384     printf("Config file : %s entry for [%s] ignored\n", name, rpmdir);
 3385     }
 3386 }
 3387