Debian Codegraph @

src/bnetd/storage_file.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1998,1999,2000,2001 Ross Combs ([email protected])
3 * Copyright (C) 2000,2001 Marco Ziech ([email protected])
4 * Copyright (C) 2002,2003,2004 Dizzy
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20 #include "common/setup_before.h"
21 #include "storage_file.h"
22
23 #include <sstream>
24 #include <cstring>
25 #include <cstdio>
26 #include <cerrno>
27 #include <ctime>
28 #include <cstdlib>
29
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33
34 #include "compat/strcasecmp.h"
35 #include "compat/pdir.h"
36 #include "compat/rename.h"
37 #include "common/eventlog.h"
38 #include "common/list.h"
39 #include "common/tag.h"
40 #include "common/util.h"
41
42 #define CLAN_INTERNAL_ACCESS
43 #define TEAM_INTERNAL_ACCESS
44 #include "team.h"
45 #include "account.h"
46 #include "file_plain.h"
47 #include "prefs.h"
48 #include "clan.h"
49 #undef CLAN_INTERNAL_ACCESS
50 #undef TEAM_INTERNAL_ACCESS
51 #include "common/setup_after.h"
52
53 namespace pvpgn
54 {
55
56 namespace bnetd
57 {
58
59 /* file storage API functions */
60
61 static int file_init(const char *);
62 static int file_close(void);
63 static unsigned file_read_maxuserid(void);
64 static t_storage_info *file_create_account(char const *);
65 static t_storage_info *file_get_defacct(void);
66 static int file_free_info(t_storage_info *);
67 static int file_read_attrs(t_storage_info *, t_read_attr_func, void *, const char *);
68 static t_attr *file_read_attr(t_storage_info *, const char *);
69 static int file_write_attrs(t_storage_info *, const t_hlist *);
70 static int file_read_accounts(int, t_read_accounts_func, void *);
71 static t_storage_info *file_read_account(const char *, unsigned);
72 static int file_cmp_info(t_storage_info *, t_storage_info *);
73 static const char *file_escape_key(const char *);
74 static int file_load_clans(t_load_clans_func);
75 static int file_write_clan(void *);
76 static int file_remove_clan(int);
77 static int file_remove_clanmember(int);
78 static int file_load_teams(t_load_teams_func);
79 static int file_write_team(void *);
80 static int file_remove_team(unsigned int);
81
82 /* storage struct populated with the functions above */
83
84 t_storage storage_file = {
85 file_init,
86 file_close,
87 file_read_maxuserid,
88 file_create_account,
89 file_get_defacct,
90 file_free_info,
91 file_read_attrs,
92 file_write_attrs,
93 file_read_attr,
94 file_read_accounts,
95 file_read_account,
96 file_cmp_info,
97 file_escape_key,
98 file_load_clans,
99 file_write_clan,
100 file_remove_clan,
101 file_remove_clanmember,
102 file_load_teams,
103 file_write_team,
104 file_remove_team
105 };
106
107 /* start of actual file storage code */
108
109 static const char *accountsdir = NULL;
110 static const char *clansdir = NULL;
111 static const char *teamsdir = NULL;
112 static const char *defacct = NULL;
113 static t_file_engine *file = NULL;
114
115 static unsigned file_read_maxuserid(void)
116 {
117 return maxuserid;
118 }
119
120 static int file_init(const char *path)
121 {
122 char *tok, *copy, *tmp, *p;
123 const char *dir = NULL;
124 const char *clan = NULL;
125 const char *team = NULL;
126 const char *def = NULL;
127 const char *driver = NULL;
128
129 if (path == NULL || path[0] == '\0')
130 {
131 eventlog(eventlog_level_error, __FUNCTION__, "got NULL or empty path");
132 return -1;
133 }
134
135 copy = xstrdup(path);
136 tmp = copy;
137 while ((tok = std::strtok(tmp, ";")) != NULL)
138 {
139 tmp = NULL;
140 if ((p = std::strchr(tok, '=')) == NULL)
141 {
142 eventlog(eventlog_level_error, __FUNCTION__, "invalid storage_path, no '=' present in token");
143 xfree((void *)copy);
144 return -1;
145 }
146 *p = '\0';
147 if (strcasecmp(tok, "dir") == 0)
148 dir = p + 1;
149 else if (strcasecmp(tok, "clan") == 0)
150 clan = p + 1;
151 else if (strcasecmp(tok, "team") == 0)
152 team = p + 1;
153 else if (strcasecmp(tok, "default") == 0)
154 def = p + 1;
155 else if (strcasecmp(tok, "mode") == 0)
156 driver = p + 1;
157 else
158 eventlog(eventlog_level_warn, __FUNCTION__, "unknown token in storage_path : '{}'", tok);
159 }
160
161 if (def == NULL || clan == NULL || team == NULL || dir == NULL || driver == NULL)
162 {
163 eventlog(eventlog_level_error, __FUNCTION__, "invalid storage_path line for file module (doesnt have a 'dir', a 'clan', a 'team', a 'default' token and a 'mode' token)");
164 xfree((void *)copy);
165 return -1;
166 }
167
168 if (!strcasecmp(driver, "plain"))
169 file = &file_plain;
170 else
171 {
172 eventlog(eventlog_level_error, __FUNCTION__, "unknown mode '{}', mode must be plain", driver);
173 xfree((void *)copy);
174 return -1;
175 }
176
177 if (accountsdir)
178 file_close();
179
180 accountsdir = xstrdup(dir);
181 clansdir = xstrdup(clan);
182 teamsdir = xstrdup(team);
183 defacct = xstrdup(def);
184
185 xfree((void *)copy);
186
187 return 0;
188 }
189
190 static int file_close(void)
191 {
192 if (accountsdir)
193 xfree((void *)accountsdir);
194 accountsdir = NULL;
195
196 if (clansdir)
197 xfree((void *)clansdir);
198 clansdir = NULL;
199
200 if (teamsdir)
201 xfree((void *)teamsdir);
202 teamsdir = NULL;
203
204 if (defacct)
205 xfree((void *)defacct);
206 defacct = NULL;
207
208 file = NULL;
209
210 return 0;
211 }
212
213 static t_storage_info *file_create_account(const char *username)
214 {
215 char *temp;
216
217 if (accountsdir == NULL || file == NULL)
218 {
219 eventlog(eventlog_level_error, __FUNCTION__, "file storage not initilized");
220 return NULL;
221 }
222
223 if (prefs_get_savebyname())
224 {
225 char const *safename;
226
227 if (!std::strcmp(username, defacct))
228 {
229 eventlog(eventlog_level_error, __FUNCTION__, "username as defacct not allowed");
230 return NULL;
231 }
232
233 if (!(safename = escape_fs_chars(username, std::strlen(username))))
234 {
235 eventlog(eventlog_level_error, __FUNCTION__, "could not escape username");
236 return NULL;
237 }
238 temp = (char*)xmalloc(std::strlen(accountsdir) + 1 + std::strlen(safename) + 1); /* dir + / + name + NUL */
239 std::sprintf(temp, "%s/%s", accountsdir, safename);
240 xfree((void *)safename); /* avoid warning */
241 }
242 else
243 {
244 temp = (char*)xmalloc(std::strlen(accountsdir) + 1 + 8 + 1); /* dir + / + uid + NUL */
245 std::sprintf(temp, "%s/%06u", accountsdir, maxuserid + 1); /* FIXME: hmm, maybe up the %06 to %08... */
246 }
247
248 return temp;
249 }
250
251 static int file_write_attrs(t_storage_info * info, const t_hlist *attributes)
252 {
253 char *tempname;
254
255 if (accountsdir == NULL || file == NULL)
256 {
257 eventlog(eventlog_level_error, __FUNCTION__, "file storage not initilized");
258 return -1;
259 }
260
261 if (info == NULL)
262 {
263 eventlog(eventlog_level_error, __FUNCTION__, "got NULL info storage");
264 return -1;
265 }
266
267 if (attributes == NULL)
268 {
269 eventlog(eventlog_level_error, __FUNCTION__, "got NULL attributes");
270 return -1;
271 }
272
273 tempname = (char*)xmalloc(std::strlen(accountsdir) + 1 + std::strlen(BNETD_ACCOUNT_TMP) + 1);
274 std::sprintf(tempname, "%s/%s", accountsdir, BNETD_ACCOUNT_TMP);
275
276 if (file->write_attrs(tempname, attributes))
277 {
278 /* no eventlog here, it should be reported from the file layer */
279 xfree(tempname);
280 return -1;
281 }
282
283 if (p_rename(tempname, (const char *)info) < 0)
284 {
285 eventlog(eventlog_level_error, __FUNCTION__, "could not std::rename account file to \"{}\" (std::rename: {})", (char *)info, std::strerror(errno));
286 xfree(tempname);
287 return -1;
288 }
289
290 xfree(tempname);
291
292 return 0;
293 }
294
295 static int file_read_attrs(t_storage_info * info, t_read_attr_func cb, void *data, const char *ktab)
296 {
297 if (accountsdir == NULL || file == NULL)
298 {
299 eventlog(eventlog_level_error, __FUNCTION__, "file storage not initilized");
300 return -1;
301 }
302
303 if (info == NULL)
304 {
305 eventlog(eventlog_level_error, __FUNCTION__, "got NULL info storage");
306 return -1;
307 }
308
309 if (cb == NULL)
310 {
311 eventlog(eventlog_level_error, __FUNCTION__, "got NULL callback");
312 return -1;
313 }
314
315 eventlog(eventlog_level_debug, __FUNCTION__, "loading \"{}\"", reinterpret_cast<const char*>(info));
316
317 if (file->read_attrs((const char *)info, cb, data))
318 {
319 /* no eventlog, error reported earlier */
320 return -1;
321 }
322
323 return 0;
324 }
325
326 static t_attr *file_read_attr(t_storage_info * info, const char *key)
327 {
328 if (accountsdir == NULL || file == NULL)
329 {
330 eventlog(eventlog_level_error, __FUNCTION__, "file storage not initilized");
331 return NULL;
332 }
333
334 if (info == NULL)
335 {
336 eventlog(eventlog_level_error, __FUNCTION__, "got NULL info storage");
337 return NULL;
338 }
339
340 if (key == NULL)
341 {
342 eventlog(eventlog_level_error, __FUNCTION__, "got NULL key");
343 return NULL;
344 }
345
346 return file->read_attr((const char *)info, key);
347 }
348
349 static int file_free_info(t_storage_info * info)
350 {
351 if (info)
352 xfree((void *)info);
353 return 0;
354 }
355
356 static t_storage_info *file_get_defacct(void)
357 {
358 t_storage_info *info;
359
360 if (defacct == NULL)
361 {
362 eventlog(eventlog_level_error, __FUNCTION__, "file storage not initilized");
363 return NULL;
364 }
365
366 info = xstrdup(defacct);
367
368 return info;
369 }
370
371 static int file_read_accounts(int flag, t_read_accounts_func cb, void *data)
372 {
373 if (!accountsdir) {
374 ERROR0("file storage not initilized");
375 return -1;
376 }
377
378 if (!cb) {
379 ERROR0("got NULL callback");
380 return -1;
381 }
382
383 try {
384 Directory accdir(accountsdir);
385
386 char const *dentry;
387 while ((dentry = accdir.read())) {
388 std::ostringstream ostr;
389 ostr << accountsdir << '/' << dentry;
390
391 cb(xstrdup(ostr.str().c_str()), data);
392 }
393 }
394 catch (const Directory::OpenError& ex) {
395 ERROR2("unable to open user directory \"{}\" for reading (error: {})", accountsdir, ex.what());
396 return -1;
397 }
398
399 return 0;
400 }
401
402 static t_storage_info *file_read_account(const char *accname, unsigned uid)
403 {
404 char *pathname;
405
406 if (accountsdir == NULL)
407 {
408 eventlog(eventlog_level_error, __FUNCTION__, "file storage not initilized");
409 return NULL;
410 }
411
412 /* ONLY if requesting for a username and if savebyname() is true
413 * PS: yes its kind of a hack, we will make a proper index file
414 */
415 if (accname && prefs_get_savebyname()) {
416 pathname = (char*)xmalloc(std::strlen(accountsdir) + 1 + std::strlen(accname) + 1); /* dir + / + file + NUL */
417 std::sprintf(pathname, "%s/%s", accountsdir, accname);
418 if (access(pathname, F_OK)) /* if it doesn't exist */
419 {
420 xfree((void *)pathname);
421 return NULL;
422 }
423 return pathname;
424 }
425
426 return NULL;
427 }
428
429 static int file_cmp_info(t_storage_info * info1, t_storage_info * info2)
430 {
431 return std::strcmp((const char *)info1, (const char *)info2);
432 }
433
434 static const char *file_escape_key(const char *key)
435 {
436 return key;
437 }
438
439 static int file_load_clans(t_load_clans_func cb)
440 {
441 char const *dentry;
442 char *pathname;
443 int clantag;
444 t_clan *clan;
445 std::FILE *fp;
446 char *clanname, *motd, *p;
447 char line[1024];
448 int cid, creation_time;
449 int member_uid, member_join_time;
450 char member_status;
451 t_clanmember *member;
452
453 if (cb == NULL)
454 {
455 eventlog(eventlog_level_error, __FUNCTION__, "get NULL callback");
456 return -1;
457 }
458
459 try {
460 Directory clandir(clansdir);
461
462 eventlog(eventlog_level_trace, __FUNCTION__, "start reading clans");
463
464 pathname = (char*)xmalloc(std::strlen(clansdir) + 1 + 4 + 1);
465 while ((dentry = clandir.read()))
466 {
467 if (std::strlen(dentry) > 4)
468 {
469 eventlog(eventlog_level_error, __FUNCTION__, "found too long clan filename in clandir \"{}\"", dentry);
470 continue;
471 }
472
473 std::sprintf(pathname, "%s/%s", clansdir, dentry);
474
475 clantag = str_to_clantag(dentry);
476
477 if ((fp = std::fopen(pathname, "r")) == NULL)
478 {
479 eventlog(eventlog_level_error, __FUNCTION__, "can't open clanfile \"{}\"", pathname);
480 continue;
481 }
482
483 clan = (t_clan*)xmalloc(sizeof(t_clan));
484 clan->tag = clantag;
485
486 if (!std::fgets(line, 1024, fp))
487 {
488 eventlog(eventlog_level_error, __FUNCTION__, "invalid clan file: no first line");
489 xfree((void*)clan);
490 continue;
491 }
492
493 clanname = line;
494 if (*clanname != '"')
495 {
496 eventlog(eventlog_level_error, __FUNCTION__, "invalid clan file: invalid first line");
497 xfree((void*)clan);
498 continue;
499 }
500 clanname++;
501 p = std::strchr(clanname, '"');
502 if (!p)
503 {
504 eventlog(eventlog_level_error, __FUNCTION__, "invalid clan file: invalid first line");
505 xfree((void*)clan);
506 continue;
507 }
508 *p = '\0';
509 if (std::strlen(clanname) >= CLAN_NAME_MAX)
510 {
511 eventlog(eventlog_level_error, __FUNCTION__, "invalid clan file: invalid first line");
512 xfree((void*)clan);
513 continue;
514 }
515
516 p++;
517 if (*p != ',')
518 {
519 eventlog(eventlog_level_error, __FUNCTION__, "invalid clan file: invalid first line");
520 xfree((void*)clan);
521 continue;
522 }
523 p++;
524 if (*p != '"')
525 {
526 eventlog(eventlog_level_error, __FUNCTION__, "invalid clan file: invalid first line");
527 xfree((void*)clan);
528 continue;
529 }
530 motd = p + 1;
531 p = std::strchr(motd, '"');
532 if (!p)
533 {
534 eventlog(eventlog_level_error, __FUNCTION__, "invalid clan file: invalid first line");
535 xfree((void*)clan);
536 continue;
537 }
538 *p = '\0';
539
540 if (std::sscanf(p + 1, ",%d,%d\n", &cid, &creation_time) != 2)
541 {
542 eventlog(eventlog_level_error, __FUNCTION__, "invalid first line in clanfile");
543 xfree((void*)clan);
544 continue;
545 }
546 clan->clanname = xstrdup(clanname);
547 clan->clan_motd = xstrdup(motd);
548 clan->clanid = cid;
549 clan->creation_time = (std::time_t) creation_time;
550 clan->created = 1;
551 clan->modified = 0;
552 clan->channel_type = prefs_get_clan_channel_default_private();
553
554 eventlog(eventlog_level_trace, __FUNCTION__, "name: {} motd: {} clanid: {} time: {}", clanname, motd, cid, creation_time);
555
556 clan->members = list_create();
557
558 while (std::fscanf(fp, "%i,%c,%i\n", &member_uid, &member_status, &member_join_time) == 3)
559 {
560 member = (t_clanmember*)xmalloc(sizeof(t_clanmember));
561 if (!(member->memberacc = accountlist_find_account_by_uid(member_uid)))
562 {
563 eventlog(eventlog_level_error, __FUNCTION__, "cannot find uid {}", member_uid);
564 xfree((void *)member);
565 continue;
566 }
567 member->status = member_status - '0';
568 member->join_time = member_join_time;
569 member->clan = clan;
570 member->fullmember = 1; /* In files we have only fullmembers */
571
572 if ((member->status == CLAN_NEW) && (std::time(NULL) - member->join_time > prefs_get_clan_newer_time() * 3600))
573 {
574 member->status = CLAN_PEON;
575 clan->modified = 1;
576 }
577
578 list_append_data(clan->members, member);
579
580 account_set_clanmember((t_account*)member->memberacc, member);
581 eventlog(eventlog_level_trace, __FUNCTION__, "added member: uid: {} status: {} join_time: {}", member_uid, member_status + '0', member_join_time);
582 }
583
584 std::fclose(fp);
585
586 cb(clan);
587
588 }
589
590 xfree((void *)pathname);
591
592 }
593 catch (const Directory::OpenError& ex) {
594 ERROR2("unable to open clan directory \"{}\" for reading (error: {})", clansdir, ex.what());
595 return -1;
596 }
597
598 eventlog(eventlog_level_trace, __FUNCTION__, "finished reading clans");
599
600 return 0;
601 }
602
603 static int file_write_clan(void *data)
604 {
605 std::FILE *fp;
606 t_elem *curr;
607 t_clanmember *member;
608 char *clanfile;
609 t_clan *clan = (t_clan *)data;
610
611 clanfile = (char*)xmalloc(std::strlen(clansdir) + 1 + 4 + 1);
612 std::sprintf(clanfile, "%s/%s", clansdir, clantag_to_str(clan->tag));
613
614 if ((fp = std::fopen(clanfile, "w")) == NULL)
615 {
616 eventlog(eventlog_level_error, __FUNCTION__, "can't open clanfile \"{}\"", clanfile);
617 xfree((void *)clanfile);
618 return -1;
619 }
620
621 std::fprintf(fp, "\"%s\",\"%s\",%i,%i\n", clan->clanname, clan->clan_motd, clan->clanid, (int)clan->creation_time);
622
623 LIST_TRAVERSE(clan->members, curr)
624 {
625 if (!(member = (t_clanmember*)elem_get_data(curr)))
626 {
627 eventlog(eventlog_level_error, __FUNCTION__, "got NULL elem in list");
628 continue;
629 }
630 if ((member->status == CLAN_NEW) && (std::time(NULL) - member->join_time > prefs_get_clan_newer_time() * 3600))
631 member->status = CLAN_PEON;
632 if (member->fullmember == 1) /* only fullmembers are stored */
633 std::fprintf(fp, "%i,%c,%u\n", account_get_uid((t_account*)member->memberacc), member->status + '0', (unsigned)member->join_time);
634 }
635
636 std::fclose(fp);
637 xfree((void *)clanfile);
638 return 0;
639 }
640
641 static int file_remove_clan(int clantag)
642 {
643 char *tempname;
644
645 tempname = (char*)xmalloc(std::strlen(clansdir) + 1 + 4 + 1);
646 std::sprintf(tempname, "%s/%s", clansdir, clantag_to_str(clantag));
647 if (std::remove((const char *)tempname) < 0)
648 {
649 eventlog(eventlog_level_error, __FUNCTION__, "could not delete clan file \"{}\" (std::remove: {})", (char *)tempname, std::strerror(errno));
650 xfree(tempname);
651 return -1;
652 }
653 xfree(tempname);
654 return 0;
655 }
656
657 static int file_remove_clanmember(int uid)
658 {
659 return 0;
660 }
661
662 static int file_load_teams(t_load_teams_func cb)
663 {
664 char const *dentry;
665 char *pathname;
666 unsigned int teamid;
667 t_team *team;
668 std::FILE *fp;
669 char * line;
670 unsigned int fteamid, lastgame;
671 unsigned char size;
672 char clienttag[5];
673 int i;
674
675 if (cb == NULL)
676 {
677 eventlog(eventlog_level_error, __FUNCTION__, "get NULL callback");
678 return -1;
679 }
680
681 try {
682 Directory teamdir(teamsdir);
683
684 eventlog(eventlog_level_trace, __FUNCTION__, "start reading teams");
685
686 pathname = (char*)xmalloc(std::strlen(teamsdir) + 1 + 8 + 1);
687 while ((dentry = teamdir.read()))
688 {
689 if (std::strlen(dentry) != 8)
690 {
691 eventlog(eventlog_level_error, __FUNCTION__, "found invalid team filename in teamdir \"{}\"", dentry);
692 continue;
693 }
694
695 std::sprintf(pathname, "%s/%s", teamsdir, dentry);
696
697 teamid = (unsigned int)std::strtoul(dentry, NULL, 16); // we use hexadecimal teamid as filename
698
699 if ((fp = std::fopen(pathname, "r")) == NULL)
700 {
701 eventlog(eventlog_level_error, __FUNCTION__, "can't open teamfile \"{}\"", pathname);
702 continue;
703 }
704
705 team = (t_team*)xmalloc(sizeof(t_team));
706 team->teamid = teamid;
707
708 if (!(line = file_get_line(fp)))
709 {
710 eventlog(eventlog_level_error, __FUNCTION__, "invalid team file: file is empty");
711 goto load_team_failure;
712 }
713
714 if (std::sscanf(line, "%u,%c,%4s,%u", &fteamid, &size, clienttag, &lastgame) != 4)
715 {
716 eventlog(eventlog_level_error, __FUNCTION__, "invalid team file: invalid number of arguments on first line");
717 goto load_team_failure;
718 }
719
720 if (fteamid != teamid)
721 {
722 eventlog(eventlog_level_error, __FUNCTION__, "invalid team file: filename and stored teamid don't match");
723 goto load_team_failure;
724 }
725
726 size -= '0';
727 if ((size<2) || (size>4))
728 {
729 eventlog(eventlog_level_error, __FUNCTION__, "invalid team file: invalid team size");
730 goto load_team_failure;
731 }
732 team->size = size;
733
734 if (!(tag_check_client(team->clienttag = tag_str_to_uint(clienttag))))
735 {
736 eventlog(eventlog_level_error, __FUNCTION__, "invalid team file: invalid clienttag");
737 goto load_team_failure;
738 }
739 team->lastgame = (std::time_t)lastgame;
740
741 if (!(line = file_get_line(fp)))
742 {
743 eventlog(eventlog_level_error, __FUNCTION__, "invalid team file: missing 2nd line");
744 goto load_team_failure;
745 }
746
747 if (std::sscanf(line, "%u,%u,%u,%u", &team->teammembers[0], &team->teammembers[1], &team->teammembers[2], &team->teammembers[3]) != 4)
748 {
749 eventlog(eventlog_level_error, __FUNCTION__, "invalid team file: invalid number of arguments on 2nd line");
750 goto load_team_failure;
751 }
752
753 for (i = 0; i < MAX_TEAMSIZE; i++)
754 {
755 if (i < size)
756 {
757 if ((team->teammembers[i] == 0))
758 {
759 eventlog(eventlog_level_error, __FUNCTION__, "invalid team file: too few members");
760 goto load_team_failure;
761 }
762 }
763 else
764 {
765 if ((team->teammembers[i] != 0))
766 {
767 eventlog(eventlog_level_error, __FUNCTION__, "invalid team file: too many members");
768 goto load_team_failure;
769 }
770
771 }
772 team->members[i] = NULL;
773 }
774
775 if (!(line = file_get_line(fp)))
776 {
777 eventlog(eventlog_level_error, __FUNCTION__, "invalid team file: missing 3rd line");
778 goto load_team_failure;
779 }
780
781 if (std::sscanf(line, "%d,%d,%d,%d,%d", &team->wins, &team->losses, &team->xp, &team->level, &team->rank) != 5)
782 {
783 eventlog(eventlog_level_error, __FUNCTION__, "invalid team file: invalid number of arguments on 3rd line");
784 goto load_team_failure;
785 }
786
787 eventlog(eventlog_level_trace, __FUNCTION__, "succesfully loaded team {}", dentry);
788 cb(team);
789
790 goto load_team_success;
791 load_team_failure:
792 xfree((void*)team);
793 eventlog(eventlog_level_error, __FUNCTION__, "error while reading file \"{}\"", dentry);
794
795 load_team_success:
796
797 file_get_line(NULL); // clear file_get_line buffer
798 std::fclose(fp);
799
800
801 }
802
803 xfree((void *)pathname);
804
805 }
806 catch (const Directory::OpenError& ex) {
807 ERROR2("unable to open team directory \"{}\" for reading (error: {})", teamsdir, ex.what());
808 return -1;
809 }
810
811 eventlog(eventlog_level_trace, __FUNCTION__, "finished reading teams");
812
813 return 0;
814 }
815
816 static int file_write_team(void *data)
817 {
818 std::FILE *fp;
819 char *teamfile;
820 t_team *team = (t_team *)data;
821
822 teamfile = (char*)xmalloc(std::strlen(teamsdir) + 1 + 8 + 1);
823 std::sprintf(teamfile, "%s/%08x", teamsdir, team->teamid);
824
825 if ((fp = std::fopen(teamfile, "w")) == NULL)
826 {
827 eventlog(eventlog_level_error, __FUNCTION__, "can't open teamfile \"{}\"", teamfile);
828 xfree((void *)teamfile);
829 return -1;
830 }
831
832 std::fprintf(fp, "%u,%c,%s,%u\n", team->teamid, team->size + '0', clienttag_uint_to_str(team->clienttag), (unsigned int)team->lastgame);
833 std::fprintf(fp, "%u,%u,%u,%u\n", team->teammembers[0], team->teammembers[1], team->teammembers[2], team->teammembers[3]);
834 std::fprintf(fp, "%d,%d,%d,%d,%d\n", team->wins, team->losses, team->xp, team->level, team->rank);
835
836 std::fclose(fp);
837 xfree((void *)teamfile);
838
839 return 0;
840 }
841
842 static int file_remove_team(unsigned int teamid)
843 {
844
845 char *tempname;
846
847 tempname = (char*)xmalloc(std::strlen(clansdir) + 1 + 8 + 1);
848 std::sprintf(tempname, "%s/%08x", clansdir, teamid);
849 if (std::remove((const char *)tempname) < 0)
850 {
851 eventlog(eventlog_level_error, __FUNCTION__, "could not delete team file \"{}\" (std::remove: {})", (char *)tempname, std::strerror(errno));
852 xfree(tempname);
853 return -1;
854 }
855 xfree(tempname);
856
857 return 0;
858 }
859
860 }
861
862 }