This repository has been archived by the owner on Aug 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.c
137 lines (132 loc) · 4.08 KB
/
database.c
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
136
137
/*
* database.c: The efserv database controller.
* This is part of efserv, a services.int implementation.
* efserv is Copyright(C) 2001 by Andrew Miller, and others.
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA.
* $Id: database.c,v 1.5 2001/12/10 07:47:19 a1kmm Exp $
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include "config.h"
#include "define.h"
#include "struct.h"
#include "utils.h"
#include "funcs.h"
#define EFCDB_MAJOR_VERSION 0
#define EFCDB_MINOR_VERSION 1
struct FileHeader
{
char check[5];
unsigned long v_maj, v_min;
time_t record_time;
};
struct ChannelHeader
{
char name[CHANLEN];
unsigned long entries;
};
void
save_channel_opdb(void)
{
struct List *node, *node2;
struct Channel *ch;
struct ChanopUser *cou;
FILE *efcdb_file;
struct FileHeader fh =
{ {'E', 'F', 'C', 'D', 'B'}, EFCDB_MAJOR_VERSION, EFCDB_MINOR_VERSION,
0 };
struct ChannelHeader chh;
int exopc;
if ((efcdb_file = fopen(CHANNEL_DB, "w")) == NULL)
{
log("[CHANDB] Could not open the channel database for write.\n");
return;
}
fh.record_time = channel_record_time;
fwrite(&fh, sizeof(fh), 1, efcdb_file);
FORLIST(node, Channels, struct Channel *, ch) if (ch->exops)
{
exopc = 0;
FORLIST(node2, ch->exops, struct ChanopUser *, cou) exopc++;
strncpy(chh.name, ch->name, CHANLEN - 1)[CHANLEN - 1] = 0;
chh.entries = exopc;
fwrite(&chh, sizeof(chh), 1, efcdb_file);
FORLIST(node2, ch->exops, struct ChanopUser *, cou)
fwrite(cou, sizeof(*cou), 1, efcdb_file);
}
fclose(efcdb_file);
}
void
load_channel_opdb(void)
{
FILE *efcdb_file;
struct FileHeader fh;
struct ChannelHeader chh;
struct Channel *ch;
struct ChanopUser *cou;
int i;
if ((efcdb_file = fopen(CHANNEL_DB, "r")) == NULL)
{
log("[CHANDB] Could not open the channel database for read.\n");
/* Set the channel record time, so we don't go doing stuff on too
* little data... */
channel_record_time = timenow;
return;
}
/* Check the header. Fatal errors are bad, but if we cannot be
* confident it is the right file, we do not want to risk overwriting
* it... */
if ((fread(&fh, sizeof(fh), 1, efcdb_file) <= 0) ||
memcmp(fh.check, "EFCDB", 5))
fatal_error
("[CHANDB] Channel record database is corrupt or wrong type.\n");
if (fh.v_maj != EFCDB_MAJOR_VERSION || fh.v_min != EFCDB_MINOR_VERSION)
fatal_error
("[CHANDB] The database found is for version %lu.%lu, need %d.%d\n",
fh.v_maj, fh.v_min, EFCDB_MAJOR_VERSION, EFCDB_MINOR_VERSION);
channel_record_time = fh.record_time;
/* Okay, header is okay, now read channel headers followed by
* records...*/
while (fread(&chh, sizeof(chh), 1, efcdb_file) > 0)
{
chh.entries = 1;
ch = malloc(sizeof(*ch));
strncpy(ch->name, chh.name, CHANLEN - 1)[CHANLEN - 1] = 0;
ch->flags = 0;
ch->ts = -1;
ch->ops = ch->nonops = ch->exops = NULL;
#ifdef USE_CYCLE
ch->cycops = NULL;
#endif
/* *sigh* hope services stays up for longer than 2 hour average... */
ch->last_notempty = timenow;
add_to_list(&Channels, ch);
add_to_hash(HASH_CHAN, ch->name, ch);
for (i = chh.entries; i > 0; i--)
{
cou = malloc(sizeof(*cou));
if (fread(cou, sizeof(*cou), 1, efcdb_file) <= 0)
{
log("[CHANDB] Warning: Channel database is corrupt.\n");
free(cou);
return;
}
add_to_list(&ch->exops, cou);
}
}
}