Debian Codegraph @

src/bnetd/attrlayer.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2004 Dizzy
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
19 #include "common/setup_before.h"
20 #define ATTRGROUP_INTERNAL_ACCESS
21 #include "attrlayer.h"
22 #include "common/eventlog.h"
23 #include "common/flags.h"
24 #include "attr.h"
25 #include "attrgroup.h"
26 #include "storage.h"
27 #include "prefs.h"
28 #include "common/setup_after.h"
29
30 namespace pvpgn
31 {
32
33 namespace bnetd
34 {
35
36 static t_attrgroup *defattrs = NULL;
37
38 static DECLARE_ELIST_INIT(loadedlist);
39 static DECLARE_ELIST_INIT(dirtylist);
40 std::vector<const char*> loadedtabs;
41
42 static int attrlayer_unload_default(void);
43
44 extern int attrlayer_init(void)
45 {
46 elist_init(&loadedlist);
47 elist_init(&dirtylist);
48 attrlayer_load_default();
49
50 return 0;
51 }
52
53 extern int attrlayer_cleanup(void)
54 {
55 attrlayer_flush(FS_FORCE | FS_ALL);
56 attrlayer_unload_default();
57
58 return 0;
59 }
60
61 extern int attrlayer_load_default(void)
62 {
63 if (defattrs) attrlayer_unload_default();
64
65 defattrs = attrgroup_create_storage(storage->get_defacct());
66 if (!defattrs) {
67 eventlog(eventlog_level_error, __FUNCTION__, "could not create attrgroup");
68 return -1;
69 }
70
71 return attrgroup_load(defattrs, "BNET");
72 }
73
74 static int attrlayer_unload_default(void)
75 {
76 attrgroup_destroy(defattrs);
77 defattrs = NULL;
78
79 return 0;
80 }
81
82 /* FIXME: dont copy most of the functionality, a good place for a C++ template ;) */
83
84 extern int attrlayer_flush(int flags)
85 {
86 static t_elist *curr = &loadedlist;
87 static t_elist *next = NULL;
88 t_attrgroup *attrgroup;
89 unsigned int fcount;
90 unsigned int tcount;
91
92 fcount = tcount = 0;
93 if (curr == &loadedlist || FLAG_ISSET(flags, FS_ALL)) {
94 curr = elist_next(&loadedlist);
95 next = elist_next(curr);
96 }
97
98 /* elist_for_each_safe splitted into separate startup for userstep function */
99 for (; curr != &loadedlist; curr = next, next = elist_next(curr)) {
100 if (!FLAG_ISSET(flags, FS_ALL) && tcount >= prefs_get_user_step()) break;
101
102 attrgroup = elist_entry(curr, t_attrgroup, loadedlist);
103 switch (attrgroup_flush(attrgroup, flags)) {
104 case 0:
105 /* stop on the first account not flushed (ie accessed too early) */
106 goto loopout;
107 case 1:
108 fcount++;
109 break;
110 case -1:
111 eventlog(eventlog_level_error, __FUNCTION__, "could not flush account");
112 break;
113 default:
114 break;
115 }
116 tcount++;
117 }
118
119 loopout:
120 if (fcount > 0)
121 eventlog(eventlog_level_debug, __FUNCTION__, "flushed {} user accounts", fcount);
122
123 if (!FLAG_ISSET(flags, FS_ALL) && curr != &loadedlist) return 1;
124
125 return 0;
126 }
127
128 extern int attrlayer_save(int flags)
129 {
130 static t_elist *curr = &dirtylist;
131 static t_elist *next = NULL;
132 t_attrgroup *attrgroup;
133 unsigned int scount;
134 unsigned int tcount;
135
136 scount = tcount = 0;
137 if (curr == &dirtylist || FLAG_ISSET(flags, FS_ALL)) {
138 curr = elist_next(&dirtylist);
139 next = elist_next(curr);
140 }
141
142 /* elist_for_each_safe splitted into separate startup for userstep function */
143 for (; curr != &dirtylist; curr = next, next = elist_next(curr)) {
144 if (!FLAG_ISSET(flags, FS_ALL) && tcount >= prefs_get_user_step()) break;
145
146 attrgroup = elist_entry(curr, t_attrgroup, dirtylist);
147 switch (attrgroup_save(attrgroup, flags)) {
148 case 0:
149 /* stop on the first account not saved (ie dirty too early) */
150 goto loopout;
151 case 1:
152 scount++;
153 break;
154 case -1:
155 eventlog(eventlog_level_error, __FUNCTION__, "could not save account");
156 break;
157 default:
158 break;
159 }
160 tcount++;
161 }
162
163 loopout:
164 if (scount > 0)
165 eventlog(eventlog_level_debug, __FUNCTION__, "saved {} user accounts", scount);
166
167 if (!FLAG_ISSET(flags, FS_ALL) && curr != &dirtylist) return 1;
168
169 return 0;
170 }
171
172 extern void attrlayer_add_loadedlist(t_elist *what)
173 {
174 elist_add_tail(&loadedlist, what);
175 }
176
177 extern void attrlayer_del_loadedlist(t_elist *what)
178 {
179 elist_del(what);
180 }
181
182 extern void attrlayer_add_dirtylist(t_elist *what)
183 {
184 elist_add_tail(&dirtylist, what);
185 }
186
187 extern void attrlayer_del_dirtylist(t_elist *what)
188 {
189 elist_del(what);
190 }
191
192 extern t_attrgroup * attrlayer_get_defattrgroup(void)
193 {
194 return defattrs;
195 }
196
197 extern void attrlayer_accessed(t_attrgroup* attrgroup)
198 {
199 /* move the attrgroup at the end of loaded list for the "flush" loop */
200 elist_del(&attrgroup->loadedlist);
201 elist_add_tail(&loadedlist, &attrgroup->loadedlist);
202 }
203
204 }
205
206 }