small changes in spec
[popcorn.git] / popcorn-server.c
blob664c9ec8017aba4ab0354c5e589e78d1caf43897
1 /*
2 Copyright (c) 2009 Pavol Rusnak <stick@gk2.sk>
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation
6 files (the "Software"), to deal in the Software without
7 restriction, including without limitation the rights to use,
8 copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following
11 conditions:
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
26 #include <stdio.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <signal.h>
30 #include <tdb.h>
32 #define STATSDIR "/var/cache/popcorn/"
33 #define STATSFILE "stats.tdb"
35 #define SILENT 0
37 #define BUFSIZE 1024
39 #define is_sane(c) ( ((c)>='A'&&(c)<='Z') || ((c)>='a'&&(c)<='z') || ((c)>='0'&&(c)<='9') || ((c)=='.') || ((c)=='-') || ((c)=='_') )
41 typedef struct {
42 unsigned int n; /* no-files */
43 unsigned int r; /* recent */
44 unsigned int v; /* voted */
45 unsigned int o; /* old */
46 } TUPLE;
48 inline void sanitize(char *str)
50 char *c = str;
51 while (*c) {
52 if (!is_sane(*c)) {
53 str[0]='\0';
54 break;
56 ++c;
58 if (str[0]=='\0') strcpy(str,"unknown");
61 int main()
63 int l;
64 unsigned int cnt;
65 char buf[BUFSIZE], ver[BUFSIZE], arch[BUFSIZE], cat, *pkg, *c;
66 TDB_CONTEXT *db;
67 TDB_DATA key, val;
68 TUPLE tuple;
70 /* read from stdin until line starting with "POPCORN " comes */
71 while (fgets(buf, BUFSIZE, stdin)) {
72 if (!strncmp(buf, "POPCORN ", 8)) break;
74 /* if not present then exit */
75 if (strncmp(buf, "POPCORN ", 8)) {
76 #if !SILENT
77 fprintf(stderr, "Popcorn header not found\n");
78 #endif
79 return 1;
81 /* read and sanitize version and architecture */
82 sscanf(buf, "POPCORN %s %s", ver, arch);
83 sanitize(ver);
84 sanitize(arch);
86 /* open database */
87 db = tdb_open(STATSDIR STATSFILE, 0, 0, O_CREAT | O_RDWR, 0644);
88 if ( !db ) {
89 #if !SILENT
90 fprintf(stderr, "Can't open database: %s\n", STATSDIR STATSFILE);
91 #endif
92 return 1;
95 // update architecture field
96 key.dsize = strlen(arch) + 5;
97 snprintf(buf, BUFSIZE, "arch/%s", arch);
98 key.dptr = (unsigned char *)buf;
99 val = tdb_fetch(db, key);
100 cnt = val.dptr ? *((unsigned int *)val.dptr) + 1 : 1;
101 val.dptr = (unsigned char *)&cnt;
102 val.dsize = sizeof(cnt);
103 tdb_store(db, key, val, TDB_REPLACE);
105 // update version field
106 key.dsize = strlen(ver) + 4;
107 snprintf(buf, BUFSIZE, "ver/%s", ver);
108 key.dptr = (unsigned char *)buf;
109 val = tdb_fetch(db, key);
110 cnt = val.dptr ? *((unsigned int *)val.dptr) + 1 : 1;
111 val.dptr = (unsigned char *)&cnt;
112 val.dsize = sizeof(cnt);
113 tdb_store(db, key, val, TDB_REPLACE);
115 // update packages
116 while (fgets(buf, BUFSIZE, stdin)) {
117 l = strlen(buf);
118 if (l<3) continue;
119 cat = buf[0];
120 c = strpbrk(buf + 2, " \n\r\t");
121 *c = '\0';
122 pkg = buf + 2;
124 key.dsize = strlen(pkg);
125 key.dptr = (unsigned char *)pkg;
126 val = tdb_fetch(db, key);
127 if (!val.dptr) {
128 tuple.n = 0;
129 tuple.r = 0;
130 tuple.v = 0;
131 tuple.o = 0;
132 } else {
133 tuple = *((TUPLE *)val.dptr);
135 switch (cat) {
136 case 'n': ++tuple.n; break;
137 case 'r': ++tuple.r; break;
138 case 'v': ++tuple.v; break;
139 case 'o': ++tuple.o; break;
141 val.dptr = (unsigned char *)&tuple;
142 val.dsize = sizeof(tuple);
143 tdb_store(db, key, val, TDB_REPLACE);
146 tdb_close(db);
147 return 0;