Debian Codegraph @

src/ListManager.cpp

raw
1 // ListManager - contains the ListContainers for all item and phrase lists, and can create new ones
2
3 // For all support, instructions and copyright go to:
4 // http://e2guardian.org/
5 // Released under the GPL v2, with the OpenSSL exception described in the README file.
6
7 // INCLUDES
8 #ifdef HAVE_CONFIG_H
9 #include "e2config.h"
10 #endif
11 #include "ListManager.hpp"
12 #include "Logger.hpp"
13
14 #include <ctime>
15 #include <cstring>
16 #include <sys/stat.h>
17
18 // GLOBALS
19
20 extern thread_local std::string thread_id;
21
22
23 // IMPLEMENTATION
24
25 ListManager::~ListManager()
26 {
27 for (unsigned int i = 0; i < l.size(); i++) {
28 if (l[i] != NULL) {
29 delete l[i];
30 l[i] = NULL;
31 }
32 }
33 }
34
35 void ListManager::clear()
36 {
37 for (unsigned int i = 0; i < l.size(); i++) {
38 if (l[i] != NULL) {
39 delete l[i];
40 l[i] = NULL;
41 }
42 }
43 }
44
45 // find an unused list in our collection of lists
46 int ListManager::findNULL()
47 {
48 for (unsigned int i = 0; i < l.size(); i++) {
49 if (l[i] == NULL) {
50 DEBUG_debug("found free list:", std::to_string(i));
51 // std::cerr << thread_id << "found free list:" << i << std::endl;
52 return (signed)i;
53 }
54 }
55 return -1;
56 }
57
58 // delete all lists with zero reference count
59 void ListManager::garbageCollect()
60 {
61 for (unsigned int i = 0; i < l.size(); i++) {
62 if (l[i] != NULL) {
63 if ((*l[i]).refcount < 1) {
64 DEBUG_debug("deleting zero ref list: ", String(i), " ", String(l[i]->refcount) );
65 delete l[i];
66 l[i] = NULL;
67 }
68 }
69 }
70 }
71
72 void ListManager::deRefList(size_t i)
73 {
74 if (l[i] == NULL) return;
75 l[i]->refcount--;
76 for (size_t j = 0; j < l[i]->morelists.size(); ++j)
77 deRefList(l[i]->morelists[j]);
78 }
79
80 void ListManager::refList(size_t i)
81 {
82 l[i]->refcount++;
83 DEBUG_debug("referencing list ref: ", String(i),
84 ", refcount: ", String(l[i]->refcount),
85 " (", l[i]->sourcefile, ")" );
86
87 for (size_t j = 0; j < l[i]->morelists.size(); ++j)
88 refList(l[i]->morelists[j]);
89 }
90
91 // load the given list, or increase refcount on list if it's already been loaded.
92 int ListManager::newItemList(const char *filename, const char *pwd, bool startswith, int filters, bool parent, bool isip, bool istime, bool ismap)
93 {
94 for (size_t i = 0; i < l.size(); i++) {
95 if (l[i] == NULL) {
96 continue;
97 }
98 if ((*l[i]).previousUseItem(filename, startswith, filters)) {
99 // this upToDate check also checks all .Included files
100 if ((*l[i]).upToDate()) {
101 DEBUG_debug("Using previous item: ", String(i), " ", filename);
102 refList(i);
103 return i;
104 }
105 }
106 }
107 // find an empty list slot, create a new listcontainer, and load the list
108 int free = findNULL();
109 if (free > -1) {
110 l[free] = new ListContainer;
111 } else {
112 l.push_back(new ListContainer);
113 free = l.size() - 1;
114 }
115 (l[free])->parent = parent;
116 if (!(l[free]->readItemList(filename, pwd, startswith, filters, isip, istime, ismap))) {
117 delete l[free];
118 l[free] = NULL;
119 return -1;
120 }
121 return free;
122 }
123 #ifdef NOTDEF
124 // load list from stdin
125 int ListManager::newStdinItemList(bool startswith, int filters, bool parent)
126 {
127 char *filename;
128 // find an empty list slot, create a new listcontainer, and load the list
129 int free = findNULL();
130 if (free > -1) {
131 l[free] = new ListContainer;
132 } else {
133 l.push_back(new ListContainer);
134 free = l.size() - 1;
135 }
136 (*l[free]).parent = parent;
137 if (!(*l[free]).readStdinItemList(startswith, filters, filename)) {
138 delete l[free];
139 l[free] = NULL;
140 return -1;
141 }
142 return free;
143 }
144 #endif
145
146 // create a new phrase list. check dates on top-level list files to see if a reload is necessary.
147 // note: unlike above, doesn't automatically call readPhraseList.
148 // pass in exception, banned, and weighted phrase lists all at once.
149 int ListManager::newPhraseList(const char *exception, const char *banned, const char *weighted, int nlimit)
150 {
151 if ( !strlen(exception) )
152 { E2LOGGER_error("missing exception phrase file "); return -1; }
153 if ( !strlen(banned) )
154 { E2LOGGER_error("missing banned phrase file "); return -1; }
155 if ( !strlen(weighted) )
156 { E2LOGGER_error("missing weighted phrase file "); return -1; }
157
158 time_t bannedpfiledate = getFileDate(banned);
159 time_t exceptionpfiledate = getFileDate(exception);
160 time_t weightedpfiledate = getFileDate(weighted);
161 for (size_t i = 0; i < l.size(); i++) {
162 if (l[i] == NULL) {
163 continue;
164 }
165 if ((*l[i]).exceptionpfile == String(exception) && (*l[i]).bannedpfile == String(banned) && (*l[i]).weightedpfile == String(weighted)
166 && (*l[i]).naughtynesslimit == nlimit) {
167 if (bannedpfiledate <= (*l[i]).bannedpfiledate && exceptionpfiledate <= (*l[i]).exceptionpfiledate && weightedpfiledate <= (*l[i]).weightedpfiledate) {
168 // Known limitation - only weighted, exception, banned phrase
169 // list checked for changes - not the included files.
170 //
171 //need to check all files that were included for phrase list
172 //so when phrases read in in list container it needs to store
173 //all the file names and if a single one has changed needs a
174 //complete regenerate
175 DEBUG_debug("Using previous phrase: ", exception, " - ", banned, " - ", weighted);
176 refList(i);
177 return i;
178 }
179 }
180 }
181 int free = findNULL();
182 if (free > -1) {
183 l[(unsigned)free] = new ListContainer;
184 } else {
185 l.push_back(new ListContainer);
186 free = l.size() - 1;
187 }
188 (*l[(unsigned)free]).parent = true; // all phrase lists are parent as
189 // there are no sub lists
190 (*l[(unsigned)free]).bannedpfiledate = bannedpfiledate;
191 (*l[(unsigned)free]).exceptionpfiledate = exceptionpfiledate;
192 (*l[(unsigned)free]).weightedpfiledate = weightedpfiledate;
193 (*l[(unsigned)free]).exceptionpfile = exception;
194 (*l[(unsigned)free]).bannedpfile = banned;
195 (*l[(unsigned)free]).weightedpfile = weighted;
196 return (unsigned)free;
197 }
198
199 bool ListManager::readbplfile(const char *banned, const char *exception, const char *weighted, unsigned int &list, bool force_quick_search,
200 int nlimit)
201 {
202 bool return_error = false;
203 int res = newPhraseList(exception, banned, weighted, nlimit);
204 if (res < 0) {
205 E2LOGGER_error("Error opening phraselists");
206 return_error = true;
207 // return false;
208 }
209 if (!(*l[res]).used) {
210 DEBUG_debug("Reading new phraselists");
211
212 bool result = (*l[res]).readPhraseList(exception, true);
213 if (!result) {
214 E2LOGGER_error("Error opening exceptionphraselist");
215 return_error = true;
216 // return false;
217 }
218
219 result = (*l[res]).readPhraseList(banned, false, -1, -1, false,nlimit);
220 if (!result) {
221 E2LOGGER_error("Error opening bannedphraselist");
222 return_error = true;
223 // return false;
224 }
225 result = (*l[res]).readPhraseList(weighted, false, -1, -1, false,nlimit);
226 if (!result) {
227 E2LOGGER_error("Error opening weightedphraselist");
228 return_error = true;
229 //return false;
230 }
231
232 if (return_error) return false;
233
234 if (!(*l[res]).makeGraph(force_quick_search))
235 return false;
236
237 (*l[res]).used = true;
238 }
239 list = res;
240 return true;
241 }