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 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
|
/* This file is part of Strigi Desktop Search
*
* Copyright (C) 2006,2009 Jos van den Oever <jos@vandenoever.info>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <strigi/archivereader.h>
#include <strigi/sdfinputstream.h>
#include <strigi/tarinputstream.h>
#include <strigi/mailinputstream.h>
#include <strigi/rpminputstream.h>
#include <strigi/arinputstream.h>
#include <strigi/zipinputstream.h>
#include "archiveentrycache.h"
#include "listinginprogress.h"
#include <iostream>
#include <set>
using namespace std;
using namespace Strigi;
class ArchiveReader::DirLister::Private {
private:
int pos;
vector<EntryInfo> entries;
ListingInProgress* listing;
const ArchiveEntryCache::SubEntry* entry;
set<std::string> done;
public:
const std::string url;
explicit Private(const vector<EntryInfo>& v, int p = 0)
:pos(p), entries(v), listing(NULL), entry(NULL) {
}
explicit Private(ListingInProgress* l, const std::string& u)
:pos(0), listing(l), entry(NULL), url(u) {
listing->ref();
}
~Private() {
if (listing && listing->unref()) delete listing;
}
void operator=(const Private& a) {
pos = a.pos;
entries = a.entries;
if (listing && listing->unref()) delete listing;
listing = a.listing;
if (listing) listing->ref();
entry = a.entry;
done = a.done;
}
bool
nextEntry(EntryInfo& e) {
if (pos < 0 || (listing == NULL && pos >= (int)entries.size())) {
// there are no (more) valid entries
pos = -1;
} else if (listing == NULL) {
// get the next entry from the static list
e = entries[pos++];
} else if (entry == NULL) {
// acquire a dynamic list and get the first entry
entry = listing->nextEntry(url);
if (entry && entry->entries.size()) {
e = nextEntry();
pos = 1;
} else {
pos = -1;
}
} else if (pos < (int)entry->entries.size()
|| listing->nextEntry(entry)) {
// e is now the next entry from the dynamic list
e = nextEntry();
} else {
// there are no more entries
pos = -1;
}
return pos != -1;
}
private:
/**
* Get the first entry from entry->entries() that is not in the entries
* vector.
**/
EntryInfo
nextEntry() {
// we can make this more efficient by sorting a set with the entries
// that have already been returned. A set is faster than a vector for
// lookups by value.
ArchiveEntryCache::SubEntryMap::const_iterator i
= entry->entries.begin();
do {
if (done.find(i->first) == done.end()) {
done.insert(i->first);
pos++;
return i->second->entry;
}
} while (++i != entry->entries.end());
// this should never happen!
return i->second->entry;
}
};
ArchiveReader::DirLister::DirLister(Private* d) :p(d) {
assert(d);
}
ArchiveReader::DirLister::DirLister(const DirLister& dl)
:p(new Private(*dl.p)) {
}
ArchiveReader::DirLister::~DirLister() {
delete p;
}
bool
ArchiveReader::DirLister::nextEntry(EntryInfo& e) {
return p->nextEntry(e);
}
const ArchiveReader::DirLister&
ArchiveReader::DirLister::operator=(const DirLister& a) {
*p = *a.p;
return a;
}
/** Private data members and functions for ArchiveReader */
class ArchiveReader::ArchiveReaderPrivate {
public:
/** List of streams that have been opened for openStream() calls. **/
typedef std::map<InputStream*, std::list<StreamPtr> > OpenstreamsType;
OpenstreamsType openstreams;
/** List of objects that can open streams. **/
std::list<StreamOpener*> openers;
/** Cache of EntryInfo object to files that have been read. **/
ArchiveEntryCache cache;
/** Map with pointers to a headerCheck function as key and a factory
* function as value. */
std::map<bool (*)(const char*, int32_t),
SubStreamProvider* (*)(InputStream*)> subs;
/**
* A list of dir listings in progress. Each ListingInProgress has a physical
* file in common. The url of the physical file is the key.
**/
std::map<std::string, ListingInProgress*> listingsInProgress;
std::vector<size_t> cullName(const std::string& url,
InputStream*& stream) const;
SubStreamProvider* positionedProvider(const std::string& url);
InputStream* open(const std::string& url) const;
/**
* @brief Query known StreamOpeners about an URL.
*
* Adds the info to the cache if it was found
*
* @param url the URL to query
* @param e the EntryInfo structure to populate with the information
* @return 0 on success, -1 if the url was not found
*/
int localStat(const std::string& url, EntryInfo& e);
ArchiveReaderPrivate();
~ArchiveReaderPrivate();
ListingInProgress* findListingInProgress(const string& url) const;
};
ArchiveReader::ArchiveReaderPrivate::ArchiveReaderPrivate() {
typedef std::pair<bool (*)(const char*, int32_t),
SubStreamProvider* (*)(InputStream*)> SubsPair;
subs.insert(SubsPair(MailInputStream::checkHeader,
MailInputStream::factory));
subs.insert(SubsPair(RpmInputStream::checkHeader,
RpmInputStream::factory));
subs.insert(SubsPair(ArInputStream::checkHeader,
ArInputStream::factory));
subs.insert(SubsPair(ZipInputStream::checkHeader,
ZipInputStream::factory));
subs.insert(SubsPair(TarInputStream::checkHeader,
TarInputStream::factory));
subs.insert(SubsPair(SdfInputStream::checkHeader,
SdfInputStream::factory));
}
ArchiveReader::ArchiveReaderPrivate::~ArchiveReaderPrivate() {
if (openstreams.size() > 0) {
cerr << openstreams.size() << " streams were not closed." << endl;
OpenstreamsType::iterator i;
for (i = openstreams.begin(); i != openstreams.end(); ++i) {
free(i->second);
}
}
map<string, ListingInProgress*>::const_iterator end
= listingsInProgress.end();
for (map<string, ListingInProgress*>::const_iterator i
= listingsInProgress.begin(); i != end; ++i) {
if (i->second->unref()) delete i->second;
}
}
/**
* Try to open stream corresponding to a url.
* If this fails, remove the last part of the url and try again.
* This continues until a stream can be opened or the remaining '/' does not
* contain '/'.
**/
vector<size_t>
ArchiveReader::ArchiveReaderPrivate::cullName(const string& url,
InputStream*& stream) const {
vector<size_t> partpos;
size_t p = url.rfind('/');
stream = open(url);
while (p != string::npos && p != 0 && !stream) {
stream = open(url.substr(0, p));
partpos.push_back(p+1);
p = url.rfind('/', p-1);
}
return partpos;
}
/**
* Obtain a substream provider for which the current stream (currentEntry())
* points to the stream specified by @p url.
* On failure, 0 is returned.
**/
SubStreamProvider*
ArchiveReader::ArchiveReaderPrivate::positionedProvider(const string& url) {
InputStream* stream = 0;
// cull the url until a stream can be opened
vector<size_t> partpos = cullName(url, stream);
if (!stream) {
return 0;
}
// open the substreams until the complete path has been opened
SubStreamProvider* provider;
InputStream* substream = stream;
vector<size_t>::reverse_iterator i;
list<StreamPtr> streams;
streams.push_back(stream);
for (i = partpos.rbegin(); i != partpos.rend(); ++i) {
// try to open the stream as a SubStreamProvider
provider = subStreamProvider(subs, substream, streams);
if (provider == 0) {
free(streams);
return 0;
}
// let sn point to the trailing part of the url
const char* sn = url.c_str() + *i;
size_t len = url.length();
bool nextstream = false;
// try to open the first substream of the current SubStreamProvider
substream = provider->currentEntry();
do {
const EntryInfo& e = provider->entryInfo();
// check that the filename matches at least one entry
if (e.type == EntryInfo::File
&& e.filename.length() < len
&& strncmp(e.filename.c_str(), sn,
e.filename.length()) == 0) {
nextstream = true;
// skip the number of entries that are matched
size_t end = *i + e.filename.length();
do {
++i;
} while (i != partpos.rend() && *i < end);
if (i == partpos.rend()) {
// success!
openstreams[substream] = streams;
return provider;
}
// no match: rewind
--i;
} else {
substream = provider->nextEntry();
}
} while(substream && !nextstream);
}
if (substream) {
openstreams[substream] = streams;
} else {
free(streams);
}
return 0;
}
/**
* Try with each of the streamopeners to open a stream.
**/
InputStream*
ArchiveReader::ArchiveReaderPrivate::open(const string& url) const {
InputStream* stream = 0;
list<StreamOpener*>::const_iterator i;
for (i = openers.begin(); i != openers.end() && stream == 0; ++i) {
stream = (*i)->openStream(url);
}
return stream;
}
int
ArchiveReader::ArchiveReaderPrivate::localStat(const std::string& url,
EntryInfo& e) {
// try with the supplied streamOpeners
list<StreamOpener*>::const_iterator i;
for (i = openers.begin(); i != openers.end(); ++i) {
if ((*i)->stat(url, e) == 0) {
if (!(e.type & EntryInfo::File)) {
return 0;
}
// check if a ListingInProgress points to this url
map<string, ListingInProgress*>::const_iterator li =
listingsInProgress.find(url);
if (li != listingsInProgress.end()) {
// use the information in this entry
e = li->second->root->entry;
return 0;
}
// check if this file is in the cache
map<string, ArchiveEntryCache::RootSubEntry*>::const_iterator se
= cache.cache.find(url);
if (se != cache.cache.end()) {
if (se->second->entry.mtime == e.mtime) {
e.type = se->second->entry.type;
return 0;
}
// the file has changed: it is removed from the cache
ArchiveEntryCache::RootSubEntry* rse = se->second;
cache.cache.erase(se->second->entry.filename);
delete rse;
}
// The file exists, but is it an archive?
InputStream* s = (*i)->openStream(url);
list<StreamPtr> streams;
SubStreamProvider* provider = subStreamProvider(subs, s, streams);
if (provider) {
// this file contains substreams
e.type = (EntryInfo::Type)(EntryInfo::Dir|EntryInfo::File);
free(streams);
/*
// create an empty entry in the cache
ArchiveEntryCache::RootSubEntry* rse = cache.cache[url];
if (rse == NULL) {
rse = new ArchiveEntryCache::RootSubEntry();
cache.cache[url] = rse;
}
rse->indexed = false;
rse->entry = e;*/
}
delete s;
return 0;
}
}
return -1;
}
ArchiveReader::ArchiveReader() :p(new ArchiveReaderPrivate()) {
}
ArchiveReader::~ArchiveReader() {
delete p;
}
int
ArchiveReader::stat(const std::string& url, EntryInfo& e) {
// try to stat the url as a physical file
if (p->localStat(url, e) == 0) return 0;
// check the cache (this assumes dirEntries was already called)
const ArchiveEntryCache::SubEntry* subentry = p->cache.findEntry(url);
if (subentry) {
e = subentry->entry;
return 0;
}
// try reading the entries from the collection to which this file belongs
size_t pos = url.rfind('/');
if (pos == string::npos) return -1;
std::string parenturl(url, 0, pos);
ArchiveReader::DirLister dirlister(dirEntries(parenturl));
while (dirlister.nextEntry(e)) {
if (e.filename == url.c_str()+pos+1) {
return 0;
}
}
return -1;
}
InputStream*
ArchiveReader::openStream(const string& url) {
InputStream* stream = p->open(url);
if (stream) return stream;
// open the substreams until the complete path has been opened
SubStreamProvider* provider = p->positionedProvider(url);
if (provider) {
stream = provider->currentEntry();
}
return stream;
}
void
ArchiveReader::addStreamOpener(StreamOpener* opener) {
p->openers.push_back(opener);
}
void
ArchiveReader::closeStream(InputStream* s) {
ArchiveReaderPrivate::OpenstreamsType::iterator i(
p->openstreams.find(s));
if (i == p->openstreams.end()) {
delete s;
return;
}
free(i->second);
p->openstreams.erase(i);
}
bool
ArchiveReader::isArchive(const std::string& url) {
EntryInfo e;
if (p->localStat(url, e) != 0) {
return false;
}
return ((e.type & (EntryInfo::File | EntryInfo::Dir)) != 0);
}
std::vector<EntryInfo>
convert(const ArchiveEntryCache::SubEntry* entry) {
std::vector<EntryInfo> v;
if (entry == NULL) return v;
ArchiveEntryCache::SubEntryMap::const_iterator i;
for (i = entry->entries.begin(); i != entry->entries.end(); ++i) {
v.push_back(i->second->entry);
}
return v;
}
ArchiveReader::DirLister
ArchiveReader::dirEntries(const std::string& url) {
// find the entry in the cache
const ArchiveEntryCache::SubEntry* subentry = p->cache.findEntry(url);
// look for a ListingInProgress
ListingInProgress* lip = NULL;
if (subentry == NULL) {
lip = p->findListingInProgress(url);
}
std::vector<EntryInfo> v;
if (subentry == NULL && lip == NULL) {
// this entry is not in the cache, we try to open it
InputStream* s = 0;
vector<size_t> l = p->cullName(url, s);
// no entries were found: we return an empty dirlister
// we have no other way of signaling failure
// the caller should have checked with stat if the entry is valid
if (!s) return DirLister(new DirLister::Private(v));
string name(url);
if (l.size()) {
// let name be the name of physical file
name.resize(l[l.size()-1]-1);
}
EntryInfo e;
// get the properties of the physical file
p->localStat(name, e);
lip = new ListingInProgress(p->subs, e, name, s);
lip->ref();
p->listingsInProgress[name] = lip;
}
if (lip) {
if (lip->isDone()) {
p->cache.cache[lip->url] = lip->root;
lip->root = 0;
p->listingsInProgress.erase(lip->url);
if (lip->unref()) delete lip;
} else {
return DirLister(new DirLister::Private(lip, url));
}
}
if (subentry == NULL) {
subentry = p->cache.findEntry(url);
}
if (subentry) {
v = convert(subentry);
}
return DirLister(new DirLister::Private(v));
}
bool
ArchiveReader::canHandle(const std::string& url) {
// remove parts from the back of url until url matches a physical file
// return true of the physical file is an archive, i.e. contains
// substreams
size_t pos = url.rfind('/');
EntryInfo e;
int r = p->localStat(url, e);
while (pos != string::npos && pos != 0 && r == -1) {
r = p->localStat(url.substr(0, pos), e);
pos = url.rfind('/', pos-1);
}
return r == 0 && e.type & EntryInfo::File && e.type & EntryInfo::Dir;
}
ListingInProgress*
ArchiveReader::ArchiveReaderPrivate::findListingInProgress(const string& url)
const {
string n(url);
size_t p = n.size();
do {
map<string, ListingInProgress*>::const_iterator i
= listingsInProgress.find(n);
if (i != listingsInProgress.end()) {
// the root entry is in the map - we are done
return i->second;
}
// remove the last element in the path, and look for that
p = n.rfind('/');
if (p != string::npos) {
n.resize(p);
}
} while (p != string::npos);
// couldn't find it
return 0;
}
|