Parent: [be3ac2] (diff)

Download this file

webqueue.cpp    480 lines (429 with data), 15.9 kB

  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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/* Copyright (C) 2005 J.F.Dockes
* 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.
*/
#include "autoconfig.h"
#include "webqueue.h"
#include <string.h>
#include <errno.h>
#include "safesysstat.h"
#include "safeunistd.h"
#include "cstr.h"
#include "pathut.h"
#include "rclutil.h"
#include "log.h"
#include "fstreewalk.h"
#include "webstore.h"
#include "circache.h"
#include "smallut.h"
#include "fileudi.h"
#include "internfile.h"
#include "wipedir.h"
#include "indexer.h"
#include "readfile.h"
#include "conftree.h"
#include "transcode.h"
#include "cancelcheck.h"
#include <vector>
#include <fstream>
using namespace std;
// The browser plugin creates a file named .xxx (where xxx is the name
// for the main file in the queue), to hold external metadata (http or
// created by the plugin). This class reads the .xxx, dotfile, and turns
// it into an Rcl::Doc holder
class WebQueueDotFile {
public:
WebQueueDotFile(RclConfig *conf, const string& fn)
: m_conf(conf), m_fn(fn)
{}
// Read input line, strip it of eol and return as c++ string
bool readLine(string& line)
{
static const int LL = 2048;
char cline[LL];
cline[0] = 0;
m_input.getline(cline, LL-1);
if (!m_input.good()) {
if (m_input.bad()) {
LOGERR("WebQueueDotFileRead: input.bad()\n" );
}
return false;
}
int ll = strlen(cline);
while (ll > 0 && (cline[ll-1] == '\n' || cline[ll-1] == '\r')) {
cline[ll-1] = 0;
ll--;
}
line.assign(cline, ll);
LOGDEB2("WebQueueDotFile:readLine: [" << (line) << "]\n" );
return true;
}
// Process a Web queue dot file and set interesting stuff in the doc
bool toDoc(Rcl::Doc& doc)
{
string line;
m_input.open(m_fn.c_str(), ios::in);
if (!m_input.good()) {
LOGERR("WebQueueDotFile: open failed for [" << (m_fn) << "]\n" );
return false;
}
// Read the 3 first lines:
// - url
// - hit type: we only know about Bookmark and WebHistory for now
// - content-type.
if (!readLine(line))
return false;
doc.url = line;
if (!readLine(line))
return false;
doc.meta[Rcl::Doc::keybght] = line;
if (!readLine(line))
return false;
doc.mimetype = line;
// We set the bookmarks mtype as html (the text is empty
// anyway), so that the html viewer will be called on 'Open'
bool isbookmark = false;
if (!stringlowercmp("bookmark", doc.meta[Rcl::Doc::keybght])) {
isbookmark = true;
doc.mimetype = "text/html";
}
string confstr;
string ss(" ");
// Read the rest: fields and keywords. We do a little
// massaging of the input lines, then use a ConfSimple to
// parse, and finally insert the key/value pairs into the doc
// meta[] array
for (;;) {
if (!readLine(line)) {
// Eof hopefully
break;
}
if (line.find("t:") != 0)
continue;
line = line.substr(2);
confstr += line + "\n";
}
ConfSimple fields(confstr, 1);
vector<string> names = fields.getNames(cstr_null);
for (vector<string>::iterator it = names.begin();
it != names.end(); it++) {
string value;
fields.get(*it, value, cstr_null);
if (!value.compare("undefined") || !value.compare("null"))
continue;
string *valuep = &value;
string cvalue;
if (isbookmark) {
// It appears that bookmarks are stored in the users'
// locale charset (not too sure). No idea what to do
// for other types, would have to check the plugin.
string charset = m_conf->getDefCharset(true);
transcode(value, cvalue, charset, "UTF-8");
valuep = &cvalue;
}
string caname = m_conf->fieldCanon(*it);
doc.meta[caname].append(ss + *valuep);
}
// Finally build the confsimple that we will save to the
// cache, from the doc fields. This could also be done in
// parallel with the doc.meta build above, but simpler this
// way. We need it because not all interesting doc fields are
// in the meta array (ie: mimetype, url), and we want
// something homogenous and easy to save.
for (map<string,string>::const_iterator it = doc.meta.begin();
it != doc.meta.end(); it++) {
m_fields.set((*it).first, (*it).second, cstr_null);
}
m_fields.set(cstr_url, doc.url, cstr_null);
m_fields.set(cstr_bgc_mimetype, doc.mimetype, cstr_null);
return true;
}
RclConfig *m_conf;
ConfSimple m_fields;
string m_fn;
ifstream m_input;
};
// Initialize. Compute paths and create a temporary directory that will be
// used by internfile()
WebQueueIndexer::WebQueueIndexer(RclConfig *cnf, Rcl::Db *db,
DbIxStatusUpdater *updfunc)
: m_config(cnf), m_db(db), m_cache(0), m_updater(updfunc),
m_nocacheindex(false)
{
m_queuedir = m_config->getWebQueueDir();
path_catslash(m_queuedir);
m_cache = new WebStore(cnf);
}
WebQueueIndexer::~WebQueueIndexer()
{
LOGDEB("WebQueueIndexer::~\n" );
deleteZ(m_cache);
}
// Index document stored in the cache.
bool WebQueueIndexer::indexFromCache(const string& udi)
{
if (!m_db)
return false;
CancelCheck::instance().checkCancel();
Rcl::Doc dotdoc;
string data;
string hittype;
if (!m_cache || !m_cache->getFromCache(udi, dotdoc, data, &hittype)) {
LOGERR("WebQueueIndexer::indexFromCache: cache failed\n" );
return false;
}
if (hittype.empty()) {
LOGERR("WebQueueIndexer::index: cc entry has no hit type\n" );
return false;
}
if (!stringlowercmp("bookmark", hittype)) {
// Just index the dotdoc
dotdoc.meta[Rcl::Doc::keybcknd] = "BGL";
return m_db->addOrUpdate(udi, cstr_null, dotdoc);
} else {
Rcl::Doc doc;
FileInterner interner(data, m_config,
FileInterner::FIF_doUseInputMimetype,
dotdoc.mimetype);
FileInterner::Status fis;
try {
fis = interner.internfile(doc);
} catch (CancelExcept) {
LOGERR("WebQueueIndexer: interrupted\n" );
return false;
}
if (fis != FileInterner::FIDone) {
LOGERR("WebQueueIndexer: bad status from internfile\n" );
return false;
}
doc.mimetype = dotdoc.mimetype;
doc.fmtime = dotdoc.fmtime;
doc.url = dotdoc.url;
doc.pcbytes = dotdoc.pcbytes;
doc.sig.clear();
doc.meta[Rcl::Doc::keybcknd] = "BGL";
return m_db->addOrUpdate(udi, cstr_null, doc);
}
}
void WebQueueIndexer::updstatus(const string& udi)
{
if (m_updater) {
++(m_updater->status.docsdone);
if (m_updater->status.dbtotdocs < m_updater->status.docsdone)
m_updater->status.dbtotdocs = m_updater->status.docsdone;
m_updater->status.fn = udi;
m_updater->update();
}
}
bool WebQueueIndexer::index()
{
if (!m_db)
return false;
LOGDEB("WebQueueIndexer::processqueue: [" << (m_queuedir) << "]\n" );
m_config->setKeyDir(m_queuedir);
if (!path_makepath(m_queuedir, 0700)) {
LOGERR("WebQueueIndexer:: can't create queuedir [" << (m_queuedir) << "] errno " << (errno) << "\n" );
return false;
}
if (!m_cache || !m_cache->cc()) {
LOGERR("WebQueueIndexer: cache initialization failed\n" );
return false;
}
CirCache *cc = m_cache->cc();
// First check/index files found in the cache. If the index was reset,
// this actually does work, else it sets the existence flags (avoid
// purging). We don't do this when called from indexFiles
if (!m_nocacheindex) {
bool eof;
if (!cc->rewind(eof)) {
// rewind can return eof if the cache is empty
if (!eof)
return false;
}
int nentries = 0;
do {
string udi;
if (!cc->getCurrentUdi(udi)) {
LOGERR("WebQueueIndexer:: cache file damaged\n" );
break;
}
if (udi.empty())
continue;
if (m_db->needUpdate(udi, cstr_null)) {
try {
// indexFromCache does a CirCache::get(). We could
// arrange to use a getCurrent() instead, would be more
// efficient
indexFromCache(udi);
updstatus(udi);
} catch (CancelExcept) {
LOGERR("WebQueueIndexer: interrupted\n" );
return false;
}
}
nentries++;
} while (cc->next(eof));
}
// Finally index the queue
FsTreeWalker walker(FsTreeWalker::FtwNoRecurse);
walker.addSkippedName(".*");
FsTreeWalker::Status status = walker.walk(m_queuedir, *this);
LOGDEB("WebQueueIndexer::processqueue: done: status " << (status) << "\n" );
return true;
}
// Index a list of files (sent by the real time monitor)
bool WebQueueIndexer::indexFiles(list<string>& files)
{
LOGDEB("WebQueueIndexer::indexFiles\n" );
if (!m_db) {
LOGERR("WebQueueIndexer::indexfiles no db??\n" );
return false;
}
for (list<string>::iterator it = files.begin(); it != files.end();) {
if (it->empty()) {//??
it++; continue;
}
string father = path_getfather(*it);
if (father.compare(m_queuedir)) {
LOGDEB("WebQueueIndexer::indexfiles: skipping [" << *it << "] (nq)\n" );
it++; continue;
}
// Pb: we are often called with the dot file, before the
// normal file exists, and sometimes never called for the
// normal file afterwards (ie for bookmarks where the normal
// file is empty). So we perform a normal queue run at the end
// of the function to catch older stuff. Still this is not
// perfect, sometimes some files will not be indexed before
// the next run.
string fn = path_getsimple(*it);
if (fn.empty() || fn.at(0) == '.') {
it++; continue;
}
struct stat st;
if (path_fileprops(*it, &st) != 0) {
LOGERR("WebQueueIndexer::indexfiles: cant stat [" << *it << "]\n" );
it++; continue;
}
if (!S_ISREG(st.st_mode)) {
LOGDEB("WebQueueIndexer::indexfiles: skipping [" << *it << "] (nr)\n" );
it++; continue;
}
processone(*it, &st, FsTreeWalker::FtwRegular);
it = files.erase(it);
}
m_nocacheindex = true;
index();
// Note: no need to reset nocacheindex, we're in the monitor now
return true;
}
FsTreeWalker::Status
WebQueueIndexer::processone(const string &path,
const struct stat *stp,
FsTreeWalker::CbFlag flg)
{
if (!m_db) //??
return FsTreeWalker::FtwError;
bool dounlink = false;
if (flg != FsTreeWalker::FtwRegular)
return FsTreeWalker::FtwOk;
string dotpath = path_cat(path_getfather(path),
string(".") + path_getsimple(path));
LOGDEB("WebQueueIndexer: prc1: [" << (path) << "]\n" );
WebQueueDotFile dotfile(m_config, dotpath);
Rcl::Doc dotdoc;
string udi, udipath;
if (!dotfile.toDoc(dotdoc))
goto out;
//dotdoc.dump(1);
// Have to use the hit type for the udi, because the same url can exist
// as a bookmark or a page.
udipath = path_cat(dotdoc.meta[Rcl::Doc::keybght], url_gpath(dotdoc.url));
make_udi(udipath, cstr_null, udi);
LOGDEB("WebQueueIndexer: prc1: udi [" << (udi) << "]\n" );
char ascdate[30];
sprintf(ascdate, "%ld", long(stp->st_mtime));
if (!stringlowercmp("bookmark", dotdoc.meta[Rcl::Doc::keybght])) {
// For bookmarks, we just index the doc that was built from the
// metadata.
if (dotdoc.fmtime.empty())
dotdoc.fmtime = ascdate;
dotdoc.pcbytes = lltodecstr(stp->st_size);
// Document signature for up to date checks: none.
dotdoc.sig.clear();
dotdoc.meta[Rcl::Doc::keybcknd] = "BGL";
if (!m_db->addOrUpdate(udi, cstr_null, dotdoc))
return FsTreeWalker::FtwError;
} else {
Rcl::Doc doc;
// Store the dotdoc fields in the future doc. In case someone wants
// to use fields generated by the browser plugin like inurl
doc.meta = dotdoc.meta;
FileInterner interner(path, stp, m_config,
FileInterner::FIF_doUseInputMimetype,
&dotdoc.mimetype);
FileInterner::Status fis;
try {
fis = interner.internfile(doc);
} catch (CancelExcept) {
LOGERR("WebQueueIndexer: interrupted\n" );
goto out;
}
if (fis != FileInterner::FIDone && fis != FileInterner::FIAgain) {
LOGERR("WebQueueIndexer: bad status from internfile\n" );
// TOBEDONE: internfile can return FIAgain here if it is
// paging a big text file, we should loop. Means we're
// only indexing the first page for text/plain files
// bigger than the page size (dlft: 1MB) for now.
goto out;
}
if (doc.fmtime.empty())
doc.fmtime = ascdate;
dotdoc.fmtime = doc.fmtime;
doc.pcbytes = lltodecstr(stp->st_size);
// Document signature for up to date checks: none.
doc.sig.clear();
doc.url = dotdoc.url;
doc.meta[Rcl::Doc::keybcknd] = "BGL";
if (!m_db->addOrUpdate(udi, cstr_null, doc))
return FsTreeWalker::FtwError;
}
// Copy to cache
{
// doc fields not in meta, needing saving to the cache
dotfile.m_fields.set("fmtime", dotdoc.fmtime, cstr_null);
// fbytes is used for historical reasons, should be pcbytes, but makes
// no sense to change.
dotfile.m_fields.set(cstr_fbytes, dotdoc.pcbytes, cstr_null);
dotfile.m_fields.set("udi", udi, cstr_null);
string fdata;
file_to_string(path, fdata);
if (!m_cache || !m_cache->cc()) {
LOGERR("WebQueueIndexer: cache initialization failed\n" );
goto out;
}
if (!m_cache->cc()->put(udi, &dotfile.m_fields, fdata, 0)) {
LOGERR("WebQueueIndexer::prc1: cache_put failed; " << (m_cache->cc()->getReason()) << "\n" );
goto out;
}
}
updstatus(udi);
dounlink = true;
out:
if (dounlink) {
unlink(path.c_str());
unlink(dotpath.c_str());
}
return FsTreeWalker::FtwOk;
}