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
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.
32 #define STATSDIR "/var/cache/popcorn/"
33 #define STATSFILE "stats.tdb"
39 #define is_sane(c) ( ((c)>='A'&&(c)<='Z') || ((c)>='a'&&(c)<='z') || ((c)>='0'&&(c)<='9') || ((c)=='.') || ((c)=='-') || ((c)=='_') )
42 unsigned int n
; /* no-files */
43 unsigned int r
; /* recent */
44 unsigned int v
; /* voted */
45 unsigned int o
; /* old */
48 inline void sanitize(char *str
)
58 if (str
[0]=='\0') strcpy(str
,"unknown");
65 char buf
[BUFSIZE
], ver
[BUFSIZE
], arch
[BUFSIZE
], cat
, *pkg
, *c
;
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)) {
77 fprintf(stderr
, "Popcorn header not found\n");
81 /* read and sanitize version and architecture */
82 sscanf(buf
, "POPCORN %s %s", ver
, arch
);
87 db
= tdb_open(STATSDIR STATSFILE
, 0, 0, O_CREAT
| O_RDWR
, 0644);
90 fprintf(stderr
, "Can't open database: %s\n", STATSDIR STATSFILE
);
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
);
116 while (fgets(buf
, BUFSIZE
, stdin
)) {
120 c
= strpbrk(buf
+ 2, " \n\r\t");
124 key
.dsize
= strlen(pkg
);
125 key
.dptr
= (unsigned char *)pkg
;
126 val
= tdb_fetch(db
, key
);
133 tuple
= *((TUPLE
*)val
.dptr
);
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
);