diff options
author | Aaron Griffin <aaronmgriffin@gmail.com> | 2008-04-15 13:20:32 -0400 |
---|---|---|
committer | Aaron Griffin <aaronmgriffin@gmail.com> | 2008-04-15 13:22:06 -0400 |
commit | 2e0364b61c96598191e75fb9556fe454d6a1fb5f (patch) | |
tree | 7d41b67dd30693ffb823860e565bbc0eefb740ee | |
parent | c3c08ee41c01b0beb2f53d7e75cf089dfbae9f99 (diff) |
Remove SQL database related tasks from these scripts
Updating a SQL database should NOT be a task of these
scripts. They should deal ONLY with the act of maintaining
a pacman repo
Signed-off-by: Aaron Griffin <aaronmgriffin@gmail.com>
-rw-r--r-- | Makefile | 20 | ||||
-rwxr-xr-x[-rw-r--r--] | db-update | 21 | ||||
-rwxr-xr-x | pkgdb | 158 | ||||
-rwxr-xr-x | pkgdb1 | 70 | ||||
-rw-r--r-- | pkgdb2-add.c | 276 | ||||
-rw-r--r-- | pkgdb2-del.c | 135 | ||||
-rw-r--r-- | pkgdb2.c | 298 |
7 files changed, 1 insertions, 977 deletions
diff --git a/Makefile b/Makefile deleted file mode 100644 index 8c24840..0000000 --- a/Makefile +++ /dev/null @@ -1,20 +0,0 @@ -# mysql db info. -DB_NAME:="archlinux" -DB_USER:="archlinux" -DB_PASS:="passwords-are-NOT-cool" - -MYSQL_DEFS:=-DDB_NAME=\"$(DB_NAME)\" -DDB_USER=\"$(DB_USER)\" -DDB_PASS=\"$(DB_PASS)\" -MYSQL_FLAGS:=-I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient - -all: pkgdb2-add pkgdb2-del - -pkgdb2-add: pkgdb2-add.c - gcc $(MYSQL_DEFS) $(MYSQL_FLAGS) -o pkgdb2-add pkgdb2-add.c - -pkgdb2-del: pkgdb2-del.c - gcc $(MYSQL_DEFS) $(MYSQL_FLAGS) -o pkgdb2-del pkgdb2-del.c - -clean: - rm -f pkgdb2-add pkgdb2-del - -.PHONY: all clean diff --git a/db-update b/db-update index 24ca489..d17fab4 100644..100755 --- a/db-update +++ b/db-update @@ -27,7 +27,7 @@ fi WORKDIR="~/.dbscripts" CHECKOUTDIR="$WORKDIR/checkout" -LOCKFILE="/tmp/.repolck.$arch.$repoid" +LOCKFILE="/tmp/.repolck.$arch.$reponame" DBFILE="$WORKDIR/$reponame.db.tar.gz" if [ ! `type -p fakeroot` ]; then @@ -129,13 +129,6 @@ if [ -d $stagedir/add -a "`ls $stagedir/add`" ]; then cp $DBFILE $ftppath - # only for i686 (for now) - if [ "$arch" = "i686" ]; then - echo "==> Scanning for New/Updated packages..." >&2 - cd $stagedir/add - $ARCHDIR/pkgdb1 $CHECKOUTDIR $svnrepo | $ARCHDIR/pkgdb2-add $repoid $stagedir/add - fi - # move the package files into the ftp directory mv -f $stagedir/add/*.pkg.tar.gz $ftppath fi @@ -157,18 +150,6 @@ if [ -d $stagedir/del -a "`ls $stagedir/del`" ]; then cp $DBFILE $ftppath - # only for i686 (for now) - if [ "$arch" = "i686" ]; then - echo "==> Scanning for Deleted packages..." >&2 - cd $stagedir/del - ( - for i in *.pkg.tar.gz; do - pkgname=$(getpkgname $i) - echo $pkgname - done - ) | $ARCHDIR/pkgdb2-del $repoid $stagedir/del - fi - # remove the package files rm -f $stagedir/del/*.pkg.tar.gz fi @@ -1,158 +0,0 @@ -#!/bin/bash - -tl=`pwd` - -repoid=$1 -# we use this instead of mktemp cuz the RH server's mktemp is broken :( -uid=`id -u` -TMPFILE="/tmp/.mysqltmp.$uid.$repoid.1" -TMPFILE2="/tmp/.mysqltmp.$uid.$repoid.2" - -dbuser='archweb' -dbname='archweb' -dbpass='YWkrOjqm' - -if [ "$repoid" = "" ]; then - me=`basename $0` - echo "usage: $me <repoid>" - exit 1 -fi - -deletepkg() -{ - echo "Deleting $pkgname" - mysql -u $dbuser -p$dbpass $dbname <<_EOF -DELETE FROM packages WHERE id='$id'; -_EOF - mysql -u $dbuser -p$dbpass $dbname <<_EOF -DELETE FROM todolist_pkgs WHERE pkgid='$id'; -_EOF -} - -updatepkg() -{ - echo "Updating $pkgname" - mysql -u $dbuser -p$dbpass $dbname <<_EOF -UPDATE packages SET categoryid='$categoryid',pkgname='$pkgname',pkgver='$pkgver', -pkgrel='$pkgrel',pkgdesc='$pkgdesc',url='$url',sources='$sources', -depends='$deplist',needupdate=0,lastupdate=NOW() WHERE id='$id'; -_EOF - mysql -u $dbuser -p$dbpass $dbname <<_EOF -UPDATE todolist_pkgs SET complete=1 WHERE pkgid='$id'; -_EOF -} - -insertpkg() -{ - echo "Inserting $pkgname" - mysql -u $dbuser -p$dbpass $dbname <<_EOF -INSERT INTO packages (id,repoid,categoryid,pkgname,pkgver,pkgrel, -pkgdesc,url,sources,depends,lastupdate) VALUES (NULL,$repoid, -'$categoryid','$pkgname','$pkgver','$pkgrel','$pkgdesc','$url', -'$sources','$deplist',NOW()); -_EOF -} - -process() { - pkg=$1 - - id=`echo $pkg | awk '{print $1}'` - pkgname=`echo $pkg | awk '{print $2}'` - oldver=`echo $pkg | awk '{print $3}'` - oldrel=`echo $pkg | awk '{print $4}'` - [ "$pkgname" = "" ] && return; - cd $tl - pkgdir=`find . -maxdepth 2 -mindepth 1 -type d -name "$pkgname" | head -1` - if [ "$pkgdir" = "" ]; then - deletepkg - else - cd $pkgdir - if [ ! -f PKGBUILD ]; then - echo "WARNING: missing PKGBUILD in `pwd`" >&2 - else - unset depends source deplist sources - unset pkgname pkgver pkgrel pkgdesc url - . PKGBUILD - # tag it - touch .tag - category=`pwd | sed "s|$tl/||" | sed 's|/.*$||'` - mysql --skip-column-names -u $dbuser -p$dbpass $dbname >$TMPFILE2 <<_EOF -SELECT id FROM categories WHERE category='$category'; -_EOF - categoryid=`cat $TMPFILE2 | awk '{print $1}'` - if [ "$categoryid" = "" ]; then - echo "WARNING: no categoryid found for '$category'" >&2 - fi - cd - &>/dev/null - deplist=${depends[@]} - deplist=`php -r "echo addslashes(\"$deplist\");"` - sources=${source[@]} - sources=`php -r "echo addslashes(\"$sources\");"` - pkgname=`php -r "echo addslashes(\"$pkgname\");"` - pkgver=`php -r "echo addslashes(\"$pkgver\");"` - pkgrel=`php -r "echo addslashes(\"$pkgrel\");"` - pkgdesc=`php -r "echo addslashes(\"$pkgdesc\");"` - url=`php -r "echo addslashes(\"$url\");"` - - if [ "$oldver" != "$pkgver" -o "$oldrel" != "$pkgrel" ]; then - updatepkg - fi - fi - fi -} - -echo "==> Updating Package Entries in Database..." -mysql --skip-column-names -u $dbuser -p$dbpass $dbname >$TMPFILE <<_EOF -SELECT id,pkgname,pkgver,pkgrel FROM packages WHERE repoid='$repoid'; -_EOF - -echo " -> Scanning for Updated/Deleted packages..." -cat $TMPFILE | ( \ - ln='a' - while [ "$ln" != "" ]; do - read ln - process "$ln" - done -) - -echo " -> Scanning for New packages..." -cd $tl -for category in `find . -maxdepth 1 -type d | grep -v CVS | grep -v "^extra" | grep -v "^unstable"`; do - category=`echo $category | sed 's|./||'` - if [ "$category" = "." ]; then - continue - fi - mysql --skip-column-names -u $dbuser -p$dbpass $dbname >$TMPFILE <<_EOF -SELECT id FROM categories WHERE category='$category'; -_EOF - categoryid=`cat $TMPFILE | awk '{print $1}'` - if [ "$categoryid" = "" ]; then - echo "WARNING: no categoryid found for '$category'" >&2 - fi - for pkg in `ls $category | sort`; do - cd $tl/$category/$pkg - if [ -f .tag ]; then - # already processed - rm -f .tag - else - if [ -f PKGBUILD ]; then - . PKGBUILD - deplist=${depends[@]} - deplist=`php -r "echo addslashes(\"$deplist\");"` - sources=${source[@]} - sources=`php -r "echo addslashes(\"$sources\");"` - pkgname=`php -r "echo addslashes(\"$pkgname\");"` - pkgver=`php -r "echo addslashes(\"$pkgver\");"` - pkgrel=`php -r "echo addslashes(\"$pkgrel\");"` - pkgdesc=`php -r "echo addslashes(\"$pkgdesc\");"` - url=`php -r "echo addslashes(\"$url\");"` - insertpkg - fi - fi - cd $tl - done -done - -rm -f $TMPFILE -exit 0 - @@ -1,70 +0,0 @@ -#!/bin/bash - -# Get the package name from the filename -# hackish, but should work for now -getpkgname() { - local tmp - - tmp=${1##*/} - tmp=${tmp%.pkg.tar.gz} - tmp=${tmp%-i686} - tmp=${tmp%-x86_64} - echo ${tmp%-*-*} -} - -STAGEDIR=$(pwd) - -#This is our "unknown" category. We will have to do something about this later. -# It is a stop-gap. We will insert new packages in this way -CATEGORY="unknown" - -SVNCO=$1 -REPOTAG=$2 -if [ "$SVNCO" = "" ]; then - me=$(basename $0) - echo "usage: $me <abs_dir> <repo_tag>" >&2 - exit 1 -fi - -if [ ! "$(ls $STAGEDIR/*.pkg.tar.gz 2>/dev/null)" ]; then - exit -fi - -cd $STAGEDIR -for pkgfile in $STAGEDIR/*.pkg.tar.gz; do - pkgname=$(getpkgname $pkgfile); - fullname=$(basename $pkgfile) - pkgpath="$SVNCO/$pkgname/repos/$REPOTAG" - - # find the matching PKGBUILD - if [ ! -d "$pkgpath" ]; then - msg "WARNING: could not find PKGBUILD for $pkgname, cannot update this entry" - return - fi - pkgbuild="${pkgpath}/PKGBUILD" - if [ ! -f $pkgbuild ]; then - msg "WARNING: could not find PKGBUILD for $fullname, cannot update this entry" - return - fi - - # pick out the category from the pathname - unset pkgname pkgver pkgrel pkgdesc license groups provides md5sums force - unset replaces depends conflicts backup source install build makedepends - unset options - source $pkgbuild || continue - - deplist=${depends[@]} - sources=${source[@]} - echo $fullname - echo $pkgname - echo $pkgver - echo $pkgrel - echo $pkgdesc - echo $CATEGORY - echo $url - echo $sources - echo $deplist -done - -exit 0 - diff --git a/pkgdb2-add.c b/pkgdb2-add.c deleted file mode 100644 index 92d1339..0000000 --- a/pkgdb2-add.c +++ /dev/null @@ -1,276 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <mysql/mysql.h> -#include <string.h> -#include <limits.h> - -#ifndef DB_PASS - #error "MySQL connection info undefined" -#endif - -typedef struct pkg { - unsigned int id; - char *name; - char *ver; - char *rel; - struct pkg *next; -} pkg_t; - -MYSQL_RES *doquery(MYSQL *m, const char* q) -{ - MYSQL_RES *res; - if(mysql_query(m, q)) { - fprintf(stderr, "mysql_query: %s\n", mysql_error(m)); - return(NULL); - } - res = mysql_store_result(m); - return(res); -} - -/* this function is ugly -- it malloc's for each string it - * returns, and they probably won't be freed by the caller. - */ -char* addslashes(const char *s) { - char slashed[8192]; - char *p; - - slashed[0] = '\0'; - p = slashed; - while(*s) { - if(*s == '\'' || *s == '"' || *s == '\\') { - *p++ = '\\'; - } - *p++ = *s++; - } - *p = '\0'; - return(strdup(slashed)); -} - -char* trim(char *str) -{ - char *pch = str; - while(isspace(*pch)) { - pch++; - } - if(pch != str) { - memmove(str, pch, (strlen(pch) + 1)); - } - - pch = (char*)(str + (strlen(str) - 1)); - while(isspace(*pch)) { - pch--; - } - *++pch = '\0'; - - return str; -} - -/* scan a .pkg.tar.gz file and put all files listed into the database. - * - * this function is hacky and should be done properly, but this route is - * easier than reading the file with libtar. - */ -void updatefilelist(MYSQL *db, unsigned long id, char *fn) -{ - FILE *fp; - char *tmp; - char cmd[PATH_MAX]; - char line[PATH_MAX]; - char query[PATH_MAX]; - - tmp = tempnam("/tmp", "pkgdb"); - snprintf(cmd, PATH_MAX-1, "/bin/tar tzvf %s | awk '{print $6}' >%s", fn, tmp); - system(cmd); - fp = fopen(tmp, "r"); - if(fp == NULL) { - fprintf(stderr, "pkgdb2-add: could not open tempfile: %s\n", tmp); - return; - } - snprintf(query, sizeof(query), "DELETE FROM packages_files WHERE pkg_id='%lu'", id); - doquery(db, query); - while(fgets(line, sizeof(line)-1, fp)) { - char *fixedfn = addslashes(trim(line)); - if(!strcmp(fixedfn, ".FILELIST") || !strcmp(fixedfn, ".PKGINFO") || !strcmp(fixedfn, ".INSTALL")) { - free(fixedfn); - continue; - } - /* varchars aren't case-sensitive but filesystems are, so we use REPLACE INTO */ - snprintf(query, sizeof(query), "REPLACE INTO packages_files (pkg_id,path) VALUES " - "('%lu', '%s')", id, fixedfn); - free(fixedfn); - doquery(db, query); - } - fclose(fp); - unlink(tmp); -} - -int main(int argc, char **argv) -{ - MYSQL db; - MYSQL_RES *result; - MYSQL_ROW row; - char query[4096]; - char fn[PATH_MAX]; - char ftppath[PATH_MAX]; - int repoid; - pkg_t *dblist = NULL; - pkg_t *pkglist = NULL; - pkg_t *pkgptr, *ptr; - - if(argc < 3) { - printf("usage: pkgdb2-add <repoid> <ftp_repo_root>\n"); - printf("\nWARNING: Do not run this manually! It is intended to be run from\n" - "the Arch db-generation scripts.\n\n"); - return(1); - } - repoid = atoi(argv[1]); - strncpy(ftppath, argv[2], PATH_MAX-1); - - if(mysql_init(&db) == NULL) { - fprintf(stderr, "could not initialize\n"); - return(1); - } - if(mysql_real_connect(&db, "localhost", DB_USER, DB_PASS, DB_NAME, - 0, NULL, 0) == NULL) { - fprintf(stderr, "failed to connect to database: %s\n", mysql_error(&db)); - return(1); - } - snprintf(query, sizeof(query), "SELECT id,pkgname,pkgver,pkgrel FROM packages " - "WHERE repo_id='%d'", repoid); - result = doquery(&db, query); - while(row = mysql_fetch_row(result)) { - int i; - /*unsigned long *lengths; - lengths = mysql_fetch_lengths(result);*/ - /* add the node to the list */ - if(dblist == NULL) { - dblist = (pkg_t*)malloc(sizeof(pkg_t)); - if(dblist == NULL) { - fprintf(stderr, "error: out of memory!\n"); - return(1); - } - ptr = dblist; - } else { - ptr->next = (pkg_t*)malloc(sizeof(pkg_t)); - if(ptr->next == NULL) { - fprintf(stderr, "error: out of memory!\n"); - return(1); - } - ptr = ptr->next; - } - ptr->next = NULL; - /* pick out the fields */ - ptr->id = atoi(row[0]); - ptr->name = strdup(row[1]); - ptr->ver = strdup(row[2]); - ptr->rel = strdup(row[3]); - } - mysql_free_result(result); - - while(!feof(stdin)) { - int found = 0; - unsigned int catid = 0; - char name[256], ver[256], rel[256], desc[4096]; - char cat[256], url[256], sources[4096], deplist[4096]; - char pkgfile[4096]; - /* get package data from stdin */ - fgets(pkgfile, 4096, stdin); trim(pkgfile); if(feof(stdin)) continue; - fgets(name, 256, stdin); trim(name); if(feof(stdin)) continue; - fgets(ver, 256, stdin); trim(ver); if(feof(stdin)) continue; - fgets(rel, 256, stdin); trim(rel); if(feof(stdin)) continue; - fgets(desc, 4096, stdin); trim(desc); if(feof(stdin)) continue; - fgets(cat, 256, stdin); trim(cat); if(feof(stdin)) continue; - fgets(url, 256, stdin); trim(url); if(feof(stdin)) continue; - fgets(sources, 4096, stdin); trim(sources); if(feof(stdin)) continue; - fgets(deplist, 4096, stdin); trim(deplist); if(feof(stdin)) continue; - /* check for overruns */ - if(strlen(name) > 254 || strlen(ver) >= 254 || strlen(rel) > 254 || - strlen(desc) > 4094 || strlen(cat) >= 254 || strlen(url) > 254 || - strlen(sources) > 4094 || strlen(deplist) > 4094 || strlen(pkgfile) > 4094) { - fprintf(stderr, "pkgdb2-add: one or more fields are too long in package '%s'\n", name); - fprintf(stderr, "pkgdb2-add: check the lengths of your strings, most are limited " - "to 255 chars, some are 4095\n"); - return(1); - } - /* add the node to the list */ - if(pkglist == NULL) { - pkglist = (pkg_t*)malloc(sizeof(pkg_t)); - if(pkglist == NULL) { - fprintf(stderr, "error: out of memory!\n"); - return(1); - } - pkgptr = pkglist; - } else { - pkgptr->next = (pkg_t*)malloc(sizeof(pkg_t)); - if(pkgptr->next == NULL) { - fprintf(stderr, "error: out of memory!\n"); - return(1); - } - pkgptr = pkgptr->next; - } - pkgptr->next = NULL; - pkgptr->name = strdup(name); - /* look it up in our cache */ - for(ptr = dblist; ptr; ptr = ptr->next) { - if(!strcmp(name, ptr->name)) { - found = 1; - break; - } - } - /* get the category */ - snprintf(query, sizeof(query), - "SELECT id FROM categories WHERE category='%s'", cat); - result = doquery(&db, query); - if(mysql_num_rows(result) == 0) { - catid = 25; - /* - snprintf(query, sizeof(query), "INSERT INTO categories (id,category) " - " VALUES (NULL,'%s')", addslashes(cat)); - doquery(&db, query); - catid = (unsigned int)mysql_insert_id(&db); - */ - } else { - row = mysql_fetch_row(result); - catid = (unsigned int)atoi(row[0]); - } - if(!found) { - /* Insert... */ - unsigned long id; - fprintf(stderr, "pkgdb2-add: inserting %s\n", name); - snprintf(query, sizeof(query), "INSERT INTO packages (id,repo_id," - "category_id,pkgname,pkgver,pkgrel,pkgdesc,url,sources,depends," - "last_update) VALUES (NULL,'%d','%d','%s','%s','%s','%s'," - "'%s','%s','%s',NOW())", - repoid, catid, addslashes(name), addslashes(ver), addslashes(rel), - addslashes(desc), addslashes(url), addslashes(sources), - addslashes(deplist)); - doquery(&db, query); - id = mysql_insert_id(&db); - snprintf(fn, PATH_MAX-1, "%s/%s", ftppath, pkgfile); - updatefilelist(&db, id, fn); - continue; - } else if(strcmp(ptr->ver, ver) || strcmp(ptr->rel, rel)) { - /* ...or Update */ - fprintf(stderr, "pkgdb2-add: updating %s (%s-%s ==> %s-%s)\n", - ptr->name, ptr->ver, ptr->rel, ver, rel); - snprintf(query, sizeof(query), "UPDATE packages SET " - "pkgname='%s',pkgver='%s',pkgrel='%s',pkgdesc='%s',url='%s'," - "sources='%s',depends='%s',needupdate=0,last_update=NOW() " - "WHERE id='%d'", - addslashes(name), addslashes(ver), addslashes(rel), - addslashes(desc), addslashes(url), addslashes(sources), - addslashes(deplist), ptr->id); - doquery(&db, query); - snprintf(fn, PATH_MAX-1, "%s/%s", ftppath, pkgfile); - updatefilelist(&db, ptr->id, fn); - /* - snprintf(query, sizeof(query), "UPDATE todolist_pkgs SET complete=1 " - "WHERE pkgid='%d'", ptr->id); - doquery(&db, query); - */ - } - } - - mysql_close(&db); - return(0); -} diff --git a/pkgdb2-del.c b/pkgdb2-del.c deleted file mode 100644 index 3ceb580..0000000 --- a/pkgdb2-del.c +++ /dev/null @@ -1,135 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <mysql.h> -#include <string.h> -#include <limits.h> - -#ifndef DB_PASS - #error "MySQL connection info undefined" -#endif - -typedef struct pkg { - unsigned int id; - char *name; - char *ver; - char *rel; - struct pkg *next; -} pkg_t; - -MYSQL_RES *doquery(MYSQL *m, const char* q) -{ - MYSQL_RES *res; - if(mysql_query(m, q)) { - fprintf(stderr, "mysql_query: %s\n", mysql_error(m)); - return(NULL); - } - res = mysql_store_result(m); - return(res); -} - -/* this function is ugly -- it malloc's for each string it - * returns, and they probably won't be freed by the caller. - */ -char* addslashes(const char *s) { - char slashed[8192]; - char *p; - - slashed[0] = '\0'; - p = slashed; - while(*s) { - if(*s == '\'' || *s == '"' || *s == '\\') { - *p++ = '\\'; - } - *p++ = *s++; - } - *p = '\0'; - return(strdup(slashed)); -} - -char* trim(char *str) -{ - char *pch = str; - while(isspace(*pch)) { - pch++; - } - if(pch != str) { - memmove(str, pch, (strlen(pch) + 1)); - } - - pch = (char*)(str + (strlen(str) - 1)); - while(isspace(*pch)) { - pch--; - } - *++pch = '\0'; - - return str; -} - -int main(int argc, char **argv) -{ - MYSQL db; - MYSQL_RES *result; - MYSQL_ROW row; - char query[4096]; - char fn[PATH_MAX]; - char ftppath[PATH_MAX]; - int repoid; - pkg_t *pkglist = NULL; - pkg_t *pkgptr, *ptr; - - if(argc < 3) { - printf("usage: pkgdb2-del <repoid> <ftp_repo_root>\n"); - printf("\nWARNING: Do not run this manually! It is intended to be run from\n" - "the Arch db-generation scripts.\n\n"); - return(1); - } - repoid = atoi(argv[1]); - strncpy(ftppath, argv[2], PATH_MAX-1); - - if(mysql_init(&db) == NULL) { - fprintf(stderr, "could not initialize\n"); - return(1); - } - if(mysql_real_connect(&db, "localhost", DB_USER, DB_PASS, DB_NAME, - 0, NULL, 0) == NULL) { - fprintf(stderr, "failed to connect to database: %s\n", mysql_error(&db)); - return(1); - } - - while(!feof(stdin)) { - int found = 0; - unsigned int catid = 0; - unsigned int pkgid = 0; - char name[256]; - /* get package data from stdin */ - fgets(name, 256, stdin); - trim(name); - if(feof(stdin)) continue; - /* check for overruns */ - if(strlen(name) > 254) { - fprintf(stderr, "pkgdb2-del: one or more fields are too long in package '%s'\n", name); - return(1); - } - /* get the package id */ - snprintf(query, sizeof(query), "SELECT id FROM packages WHERE " - "repo_id='%d' AND pkgname='%s'", repoid, addslashes(name)); - result = doquery(&db, query); - if(mysql_num_rows(result) == 0) { - fprintf(stderr, "pkgdb2-del: %s was not found in repo %d\n", name, repoid); - continue; - } - row = mysql_fetch_row(result); - pkgid = (unsigned int)atoi(row[0]); - /* delete from db */ - fprintf(stderr, "pkgdb2-del: deleting %s (id %d)\n", name, pkgid); - snprintf(query, sizeof(query), "DELETE FROM packages WHERE id='%d'", pkgid); - doquery(&db, query); - snprintf(query, sizeof(query), "DELETE FROM packages_files WHERE pkg_id='%d'", pkgid); - doquery(&db, query); - snprintf(query, sizeof(query), "DELETE FROM todolists_pkgs WHERE pkg_id='%d'", pkgid); - doquery(&db, query); - } - - mysql_close(&db); - return(0); -} diff --git a/pkgdb2.c b/pkgdb2.c deleted file mode 100644 index 0fc0cc9..0000000 --- a/pkgdb2.c +++ /dev/null @@ -1,298 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <mysql.h> -#include <string.h> -#include <limits.h> - -#define DB_USER "archweb" -#define DB_NAME "archweb" -#define DB_PASS "passwords-are-cool" - -typedef struct pkg { - unsigned int id; - char *name; - char *ver; - char *rel; - struct pkg *next; -} pkg_t; - -MYSQL_RES *doquery(MYSQL *m, const char* q) -{ - MYSQL_RES *res; - if(mysql_query(m, q)) { - fprintf(stderr, "mysql_query: %s\n", mysql_error(m)); - return(NULL); - } - res = mysql_store_result(m); - return(res); -} - -/* this function is ugly -- it malloc's for each string it - * returns, and they probably won't be freed by the caller. - */ -char* addslashes(const char *s) { - char slashed[8192]; - char *p; - - slashed[0] = '\0'; - p = slashed; - while(*s) { - if(*s == '\'' || *s == '"' || *s == '\\') { - *p++ = '\\'; - } - *p++ = *s++; - } - *p = '\0'; - return(strdup(slashed)); -} - -char* trim(char *str) -{ - char *pch = str; - while(isspace(*pch)) { - pch++; - } - if(pch != str) { - memmove(str, pch, (strlen(pch) + 1)); - } - - pch = (char*)(str + (strlen(str) - 1)); - while(isspace(*pch)) { - pch--; - } - *++pch = '\0'; - - return str; -} - -/* scan a .pkg.tar.gz file and put all files listed into the database. - * - * this function is hacky and should be done properly, but this route is - * easier than reading the file with libtar. - */ -void updatefilelist(MYSQL *db, unsigned long id, char *fn) -{ - FILE *fp; - char *tmp; - char cmd[PATH_MAX]; - char line[PATH_MAX]; - char query[PATH_MAX]; - - tmp = tempnam("/tmp", "pkgdb"); - snprintf(cmd, PATH_MAX-1, "/bin/tar tzvf %s | awk '{print $6}' >%s", fn, tmp); - system(cmd); - fp = fopen(tmp, "r"); - if(fp == NULL) { - fprintf(stderr, "pkgdb2: could not open tempfile: %s\n", tmp); - return; - } - snprintf(query, sizeof(query), "DELETE FROM packages_files WHERE id='%d'", id); - doquery(db, query); - while(fgets(line, sizeof(line)-1, fp)) { - char *fixedfn = addslashes(trim(line)); - if(!strcmp(fixedfn, ".FILELIST") || !strcmp(fixedfn, ".PKGINFO") || !strcmp(fixedfn, ".INSTALL")) { - free(fixedfn); - continue; - } - /* varchars aren't case-sensitive but filesystems are, so we use REPLACE INTO */ - snprintf(query, sizeof(query), "REPLACE INTO packages_files (id,path) VALUES " - "('%d', '%s')", id, fixedfn); - free(fixedfn); - doquery(db, query); - } - fclose(fp); - unlink(tmp); -} - -int main(int argc, char **argv) -{ - MYSQL db; - MYSQL_RES *result; - MYSQL_ROW row; - char query[4096]; - char fn[PATH_MAX]; - char ftppath[PATH_MAX]; - int repoid; - pkg_t *dblist = NULL; - pkg_t *pkglist = NULL; - pkg_t *pkgptr, *ptr; - - if(argc < 3) { - printf("usage: pkgdb2 <repoid> <ftp_repo_root>\n"); - printf("\nWARNING: Do not run this manually! It is intended to be" - " run with pkgdb1 only.\n"); - return(1); - } - repoid = atoi(argv[1]); - strncpy(ftppath, argv[2], PATH_MAX-1); - - if(mysql_init(&db) == NULL) { - fprintf(stderr, "could not initialize\n"); - return(1); - } - if(mysql_real_connect(&db, "localhost", DB_USER, DB_PASS, DB_NAME, - 0, NULL, 0) == NULL) { - fprintf(stderr, "failed to connect to database: %s\n", mysql_error(&db)); - return(1); - } - snprintf(query, sizeof(query), "SELECT id,pkgname,pkgver,pkgrel FROM packages " - "WHERE repoid='%d'", repoid); - result = doquery(&db, query); - while(row = mysql_fetch_row(result)) { - int i; - /*unsigned long *lengths; - lengths = mysql_fetch_lengths(result);*/ - /* add the node to the list */ - if(dblist == NULL) { - dblist = (pkg_t*)malloc(sizeof(pkg_t)); - if(dblist == NULL) { - fprintf(stderr, "error: out of memory!\n"); - return(1); - } - ptr = dblist; - } else { - ptr->next = (pkg_t*)malloc(sizeof(pkg_t)); - if(ptr->next == NULL) { - fprintf(stderr, "error: out of memory!\n"); - return(1); - } - ptr = ptr->next; - } - ptr->next = NULL; - /* pick out the fields */ - ptr->id = atoi(row[0]); - ptr->name = strdup(row[1]); - ptr->ver = strdup(row[2]); - ptr->rel = strdup(row[3]); - } - mysql_free_result(result); - - while(!feof(stdin)) { - int found = 0; - unsigned int catid = 0; - char name[256], ver[256], rel[256], desc[4096]; - char cat[256], url[256], sources[4096], deplist[4096]; - /* get package data from stdin */ - fgets(name, 256, stdin); trim(name); if(feof(stdin)) continue; - fgets(ver, 256, stdin); trim(ver); if(feof(stdin)) continue; - fgets(rel, 256, stdin); trim(rel); if(feof(stdin)) continue; - fgets(desc, 4096, stdin); trim(desc); if(feof(stdin)) continue; - fgets(cat, 256, stdin); trim(cat); if(feof(stdin)) continue; - fgets(url, 256, stdin); trim(url); if(feof(stdin)) continue; - fgets(sources, 4096, stdin); trim(sources); if(feof(stdin)) continue; - fgets(deplist, 4096, stdin); trim(deplist); if(feof(stdin)) continue; - /* check for overruns */ - if(strlen(name) > 254 || strlen(ver) >= 254 || strlen(rel) > 254 || - strlen(desc) > 4094 || strlen(cat) >= 254 || strlen(url) > 254 || - strlen(sources) > 4094 || strlen(deplist) > 4094) { - fprintf(stderr, "pkgdb2: one or more fields are too long in package '%s'\n", name); - fprintf(stderr, "pkgdb2: check the lengths of your strings, most are limited " - "to 255 chars, some are 4095\n"); - return(1); - } - /* add the node to the list */ - if(pkglist == NULL) { - pkglist = (pkg_t*)malloc(sizeof(pkg_t)); - if(pkglist == NULL) { - fprintf(stderr, "error: out of memory!\n"); - return(1); - } - pkgptr = pkglist; - } else { - pkgptr->next = (pkg_t*)malloc(sizeof(pkg_t)); - if(pkgptr->next == NULL) { - fprintf(stderr, "error: out of memory!\n"); - return(1); - } - pkgptr = pkgptr->next; - } - pkgptr->next = NULL; - pkgptr->name = strdup(name); - /* look it up in our cache */ - for(ptr = dblist; ptr; ptr = ptr->next) { - if(!strcmp(name, ptr->name)) { - found = 1; - break; - } - } - /* get the category */ - snprintf(query, sizeof(query), - "SELECT id FROM categories WHERE category='%s'", cat); - result = doquery(&db, query); - if(mysql_num_rows(result) == 0) { - fprintf(stderr, "pkgdb2: no db category found for '%s'\n", cat); - /* - snprintf(query, sizeof(query), "INSERT INTO categories (id,category) " - " VALUES (NULL,'%s')", addslashes(cat)); - doquery(&db, query); - catid = (unsigned int)mysql_insert_id(&db); - */ - } else { - row = mysql_fetch_row(result); - catid = (unsigned int)atoi(row[0]); - } - if(!found) { - /* Insert... */ - unsigned long id; - fprintf(stderr, "pkgdb2: inserting %s\n", name); - snprintf(query, sizeof(query), "INSERT INTO packages (id,repoid," - "categoryid,pkgname,pkgver,pkgrel,pkgdesc,url,sources,depends," - "lastupdate) VALUES (NULL,'%d','%d','%s','%s','%s','%s'," - "'%s','%s','%s',NOW())", - repoid, catid, addslashes(name), addslashes(ver), addslashes(rel), - addslashes(desc), addslashes(url), addslashes(sources), - addslashes(deplist)); - doquery(&db, query); - id = mysql_insert_id(&db); - snprintf(fn, PATH_MAX-1, "%s/%s-%s-%s.pkg.tar.gz", ftppath, name, ver, rel); - updatefilelist(&db, id, fn); - continue; - } else if(strcmp(ptr->ver, ver) || strcmp(ptr->rel, rel)) { - /* ...or Update */ - fprintf(stderr, "pkgdb2: updating %s (%s-%s ==> %s-%s)\n", - ptr->name, ptr->ver, ptr->rel, ver, rel); - snprintf(query, sizeof(query), "UPDATE packages SET categoryid='%d'," - "pkgname='%s',pkgver='%s',pkgrel='%s',pkgdesc='%s',url='%s'," - "sources='%s',depends='%s',needupdate=0,lastupdate=NOW() " - "WHERE id='%d'", - catid, addslashes(name), addslashes(ver), addslashes(rel), - addslashes(desc), addslashes(url), addslashes(sources), - addslashes(deplist), ptr->id); - doquery(&db, query); - snprintf(fn, PATH_MAX-1, "%s/%s-%s-%s.pkg.tar.gz", ftppath, name, ver, rel); - updatefilelist(&db, ptr->id, fn); - /* - snprintf(query, sizeof(query), "UPDATE todolist_pkgs SET complete=1 " - "WHERE pkgid='%d'", ptr->id); - doquery(&db, query); - */ - } - } - - /* look for deleted packages */ - for(ptr = dblist; ptr; ptr = ptr->next) { - int found = 0; - for(pkgptr = pkglist; pkgptr; pkgptr = pkgptr->next) { - if(!strcmp(ptr->name, pkgptr->name)) { - found = 1; - break; - } - } - if(!found) { - /* delete from db */ - fprintf(stderr, "pkgdb2: deleting %s\n", ptr->name); - snprintf(query, sizeof(query), "DELETE FROM packages WHERE id='%d'", - ptr->id); - doquery(&db, query); - snprintf(query, sizeof(query), "DELETE FROM packages_files WHERE id='%d'", - ptr->id); - doquery(&db, query); - snprintf(query, sizeof(query), "DELETE FROM todolist_pkgs WHERE pkgid='%d'", - ptr->id); - doquery(&db, query); - } - } - - mysql_close(&db); - return(0); -} |