From e526026407c60c1481d7c17c09ba7982ba574cfc Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 28 Sep 2016 17:21:20 +0200 Subject: [PATCH] Properly fail when creating XapianSearcher on a zim without embedded index. The XapianSearcher creation must fail with a exception if we cannot open the xapian database. So, if we try to open a zim and there is no embedded index, we must fail. We raise the custom exception NoXapianIndexInZim in this case. --- src/common/kiwix/xapianSearcher.cpp | 14 ++++++-------- src/common/kiwix/xapianSearcher.h | 8 ++++++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/common/kiwix/xapianSearcher.cpp b/src/common/kiwix/xapianSearcher.cpp index 80dfedf21..923bc19d6 100644 --- a/src/common/kiwix/xapianSearcher.cpp +++ b/src/common/kiwix/xapianSearcher.cpp @@ -36,18 +36,16 @@ namespace kiwix { /* Open Xapian readable database */ void XapianSearcher::openIndex(const string &directoryPath) { - bool indexInZim = false; try { zim::File zimFile = zim::File(directoryPath); zim::Article xapianArticle = zimFile.getArticle('Z', "/Z/fulltextIndex/xapian"); - if (xapianArticle.good()) - { - zim::offset_type dbOffset = xapianArticle.getOffset(); - int databasefd = open(directoryPath.c_str(), O_RDONLY); - lseek(databasefd, dbOffset, SEEK_SET); - this->readableDatabase = Xapian::Database(databasefd); - } + if ( ! xapianArticle.good()) + throw NoXapianIndexInZim(); + zim::offset_type dbOffset = xapianArticle.getOffset(); + int databasefd = open(directoryPath.c_str(), O_RDONLY); + lseek(databasefd, dbOffset, SEEK_SET); + this->readableDatabase = Xapian::Database(databasefd); } catch (zim::ZimFileFormatError) { this->readableDatabase = Xapian::Database(directoryPath); diff --git a/src/common/kiwix/xapianSearcher.h b/src/common/kiwix/xapianSearcher.h index e01283be7..3fb24b29e 100644 --- a/src/common/kiwix/xapianSearcher.h +++ b/src/common/kiwix/xapianSearcher.h @@ -27,6 +27,14 @@ using namespace std; namespace kiwix { + class NoXapianIndexInZim: public exception + { + virtual const char* what() const throw() + { + return "There is no fulltext index in the zim file"; + } + }; + class XapianSearcher : public Searcher { public: