/[LeafOK_CVS]/pvpgn-1.7.4/src/bnetd/file_cdb.c
ViewVC logotype

Annotation of /pvpgn-1.7.4/src/bnetd/file_cdb.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Tue Jun 6 03:41:37 2006 UTC (19 years, 9 months ago) by sysadm
Branch point for: GNU, MAIN
Content type: text/x-csrc
Initial revision

1 sysadm 1.1 /*
2     * Copyright (C) 2003,2004 Mihai RUSU (dizzy@rdsnet.ro)
3     *
4     * This program is free software; you can redistribute it and/or
5     * modify it under the terms of the GNU General Public License
6     * as published by the Free Software Foundation; either version 2
7     * of the License, or (at your option) any later version.
8     *
9     * This program is distributed in the hope that it will be useful,
10     * but WITHOUT ANY WARRANTY; without even the implied warranty of
11     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     * GNU General Public License for more details.
13     *
14     * You should have received a copy of the GNU General Public License
15     * along with this program; if not, write to the Free Software
16     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17     */
18     #include "common/setup_before.h"
19     #include <stdio.h>
20     #ifdef HAVE_STDDEF_H
21     # include <stddef.h>
22     #else
23     # ifndef NULL
24     # define NULL ((void *)0)
25     # endif
26     #endif
27     #ifdef STDC_HEADERS
28     # include <stdlib.h>
29     #else
30     # ifdef HAVE_MALLOC_H
31     # include <malloc.h>
32     # endif
33     #endif
34     #ifdef HAVE_STRING_H
35     # include <string.h>
36     #else
37     # ifdef HAVE_STRINGS_H
38     # include <strings.h>
39     # endif
40     #endif
41     #include "compat/strchr.h"
42     #include "compat/strdup.h"
43     #include "compat/strcasecmp.h"
44     #include "compat/strncasecmp.h"
45     #include <ctype.h>
46     #ifdef HAVE_LIMITS_H
47     # include <limits.h>
48     #endif
49     #include "compat/char_bit.h"
50     #ifdef TIME_WITH_SYS_TIME
51     # include <sys/time.h>
52     # include <time.h>
53     #else
54     # ifdef HAVE_SYS_TIME_H
55     # include <sys/time.h>
56     # else
57     # include <time.h>
58     # endif
59     #endif
60     #include <errno.h>
61     #include "compat/strerror.h"
62     #ifdef HAVE_SYS_TYPES_H
63     # include <sys/types.h>
64     #endif
65     #ifdef HAVE_UNISTD_H
66     # include <unistd.h>
67     #else
68     # ifdef WIN32
69     # include <io.h>
70     # endif
71     #endif
72     #include "compat/pdir.h"
73     #include "common/eventlog.h"
74     #include "prefs.h"
75     #include "common/util.h"
76     #include "common/field_sizes.h"
77     #include "common/bnethash.h"
78     #include "common/introtate.h"
79     #define CLAN_INTERNAL_ACCESS
80     #define ACCOUNT_INTERNAL_ACCESS
81     #include "account.h"
82     #include "common/hashtable.h"
83     #include "storage.h"
84     #include "storage_file.h"
85     #include "common/list.h"
86     #include "common/xalloc.h"
87     #include "connection.h"
88     #include "watch.h"
89     #include "clan.h"
90     #undef ACCOUNT_INTERNAL_ACCESS
91     #undef CLAN_INTERNAL_ACCESS
92     #include "common/tag.h"
93     #ifdef HAVE_FCNTL_H
94     # include <fcntl.h>
95     #else
96     # ifdef HAVE_SYS_FILE_H
97     # include <sys/file.h>
98     # endif
99     #endif
100     #include "tinycdb/cdb.h"
101     #include "common/setup_after.h"
102    
103     /* cdb file storage API functions */
104    
105     static int cdb_read_attrs(const char *filename, t_read_attr_func cb, void *data);
106     static void * cdb_read_attr(const char *filename, const char *key);
107     static int cdb_write_attrs(const char *filename, void *attributes);
108    
109     /* file_engine struct populated with the functions above */
110    
111     t_file_engine file_cdb = {
112     cdb_read_attr,
113     cdb_read_attrs,
114     cdb_write_attrs
115     };
116    
117     /* start of actual cdb file storage code */
118    
119     //#define CDB_ON_DEMAND 1
120    
121     static int cdb_write_attrs(const char *filename, void *attributes)
122     {
123     FILE *cdbfile;
124     t_attribute *attr;
125     struct cdb_make cdbm;
126    
127     if ((cdbfile = fopen(filename, "w+b")) == NULL) {
128     eventlog(eventlog_level_error, __FUNCTION__, "unable to open file \"%s\" for writing ",filename);
129     return -1;
130     }
131    
132     cdb_make_start(&cdbm, cdbfile);
133     for (attr=(t_attribute *)attributes; attr; attr=attr->next) {
134     if (attr->key && attr->val) {
135     if (strncmp("BNET\\CharacterDefault\\", attr->key, 20) == 0) {
136     eventlog(eventlog_level_debug, __FUNCTION__, "skipping attribute key=\"%s\"",attr->key);
137     } else {
138     eventlog(eventlog_level_debug, __FUNCTION__, "saving attribute key=\"%s\" val=\"%s\"",attr->key,attr->val);
139     if (cdb_make_add(&cdbm, attr->key, strlen(attr->key), attr->val, strlen(attr->val))<0)
140     {
141     eventlog(eventlog_level_error, __FUNCTION__, "got error on cdb_make_add ('%s' = '%s')", attr->key, attr->val);
142     cdb_make_finish(&cdbm); /* try to bail out nicely */
143     fclose(cdbfile);
144     return -1;
145     }
146     }
147     } else eventlog(eventlog_level_error, __FUNCTION__,"could not save attribute key=\"%s\"",attr->key);
148    
149     }
150    
151     if (cdb_make_finish(&cdbm)<0) {
152     eventlog(eventlog_level_error, __FUNCTION__, "got error on cdb_make_finish");
153     fclose(cdbfile);
154     return -1;
155     }
156    
157     if (fclose(cdbfile)<0) {
158     eventlog(eventlog_level_error, __FUNCTION__, "got error on fclose()");
159     return -1;
160     }
161    
162     return 0;
163     }
164    
165     #ifndef CDB_ON_DEMAND
166     /* code adapted from tinycdb-0.73/cdb.c */
167    
168     static int fget(FILE * fd, unsigned char *b, cdbi_t len, cdbi_t *posp, cdbi_t limit)
169     {
170     if (posp && limit - *posp < len) {
171     eventlog(eventlog_level_error, __FUNCTION__, "invalid cdb database format");
172     return -1;
173     }
174    
175     if (fread(b, 1, len, fd) != len) {
176     if (ferror(fd)) {
177     eventlog(eventlog_level_error, __FUNCTION__, "got error reading from db file");
178     return -1;
179     }
180     eventlog(eventlog_level_error, __FUNCTION__, "unable to read from cdb file, incomplete file");
181     return -1;
182     }
183    
184     if (posp) *posp += len;
185     return 0;
186     }
187    
188     static const char * fcpy(FILE *fd, cdbi_t len, cdbi_t *posp, cdbi_t limit, unsigned char * buf)
189     {
190     static char *str;
191     static unsigned strl;
192     unsigned int res = 0, no = 0;
193    
194     if (strl < len + 1) {
195     char *tmp;
196    
197     tmp = xmalloc(len + 1);
198     if (str) xfree((void*)str);
199     str = tmp;
200     strl = len + 1;
201     }
202    
203     while(len - res > 0) {
204     if (len > 2048) no = 2048;
205     else no = len;
206    
207     if (fget(fd, buf, no, posp, limit)) return NULL;
208     memmove(str + res, buf, no);
209     res += no;
210     }
211    
212     if (res > strl - 1) {
213     eventlog(eventlog_level_error, __FUNCTION__, "BUG, this should not happen");
214     return NULL;
215     }
216    
217     str[res] = '\0';
218     return str;
219     }
220    
221     static int cdb_read_attrs(const char *filename, t_read_attr_func cb, void *data)
222     {
223     cdbi_t eod, klen, vlen;
224     cdbi_t pos = 0;
225     const char *key;
226     const char *val;
227     unsigned char buf[2048];
228     FILE *f;
229    
230     if ((f = fopen(filename, "rb")) == NULL) {
231     eventlog(eventlog_level_error, __FUNCTION__, "got error opening file '%s'", filename);
232     return -1;
233     }
234    
235     if (fget(f, buf, 2048, &pos, 2048)) goto err_fd;
236     eod = cdb_unpack(buf);
237     while(pos < eod) {
238     if (fget(f, buf, 8, &pos, eod)) goto err_fd;
239     klen = cdb_unpack(buf);
240     vlen = cdb_unpack(buf + 4);
241     if ((key = fcpy(f, klen, &pos, eod, buf)) == NULL) {
242     eventlog(eventlog_level_error, __FUNCTION__, "error reading attribute key");
243     goto err_fd;
244     }
245    
246     key = xstrdup(key);
247    
248     if ((val = fcpy(f, vlen, &pos, eod, buf)) == NULL) {
249     eventlog(eventlog_level_error, __FUNCTION__, "error reading attribute val");
250     goto err_key;
251     }
252    
253     // eventlog(eventlog_level_trace, __FUNCTION__, "read atribute : '%s' -> '%s'", key, val);
254     if (cb(key, val, data))
255     eventlog(eventlog_level_error, __FUNCTION__, "got error from callback on account file '%s'", filename);
256     xfree((void *)key);
257     }
258    
259     fclose(f);
260     return 0;
261    
262     err_key:
263     xfree((void *)key);
264    
265     err_fd:
266     fclose(f);
267     return -1;
268     }
269     #else /* CDB_ON_DEMAND */
270     static int cdb_read_attrs(const char *filename, t_read_attr_func cb, void *data)
271     {
272     return 0;
273     }
274     #endif
275    
276     static void * cdb_read_attr(const char *filename, const char *key)
277     {
278     #ifdef CDB_ON_DEMAND
279     FILE *cdbfile;
280     t_attribute *attr;
281     char *val;
282     unsigned vlen = 1;
283    
284     // eventlog(eventlog_level_trace, __FUNCTION__, "reading key '%s'", key);
285     if ((cdbfile = fopen(filename, "rb")) == NULL) {
286     // eventlog(eventlog_level_debug, __FUNCTION__, "unable to open file \"%s\" for reading ",filename);
287     return NULL;
288     }
289    
290     if (cdb_seek(cdbfile, key, strlen(key), &vlen) <= 0) {
291     // eventlog(eventlog_level_debug, __FUNCTION__, "could not find key '%s'", key);
292     fclose(cdbfile);
293     return NULL;
294     }
295    
296     attr = xmalloc(sizeof(t_attribute));
297     attr->key = xstrdup(key);
298     val = xmalloc(vlen + 1);
299    
300     cdb_bread(cdbfile, val, vlen);
301     fclose(cdbfile);
302    
303     val[vlen] = '\0';
304    
305     attr->val = val;
306     attr->dirty = 0;
307     // eventlog(eventlog_level_trace, __FUNCTION__, "read key '%s' value '%s'", attr->key, attr->val);
308     return attr;
309     #else
310     return NULL;
311     #endif
312     }
313    

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1