summaryrefslogtreecommitdiff
path: root/pkgdb2-del.c
blob: 3ceb580e64e81c582e1505dad22580fb2a0239fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#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);
}