diff --git a/src/common/rclconfig.cpp b/src/common/rclconfig.cpp index bf897080..298a1702 100644 --- a/src/common/rclconfig.cpp +++ b/src/common/rclconfig.cpp @@ -22,6 +22,7 @@ #include #include #include "safesysstat.h" +#include "safeunistd.h" #include #ifdef __FreeBSD__ #include @@ -179,7 +180,7 @@ RclConfig::RclConfig(const string *argcnf) // want to avoid the imperfect test in isDefaultConfig() if we actually know // this is the default conf if (!autoconfdir && !isDefaultConfig()) { - if (access(m_confdir.c_str(), 0) < 0) { + if (!path_exists(m_confdir)) { m_reason = "Explicitly specified configuration " "directory must exist" " (won't be automatically created). Use mkdir first"; @@ -187,7 +188,7 @@ RclConfig::RclConfig(const string *argcnf) } } - if (access(m_confdir.c_str(), 0) < 0) { + if (!path_exists(m_confdir)) { if (!initUserConfig()) return; } @@ -1401,7 +1402,7 @@ bool RclConfig::initUserConfig() // Use protective 700 mode to create the top configuration // directory: documents can be reconstructed from index data. - if (access(m_confdir.c_str(), 0) < 0 && + if (!path_exists(m_confdir) && mkdir(m_confdir.c_str(), 0700) < 0) { m_reason += string("mkdir(") + m_confdir + ") failed: " + strerror(errno); @@ -1410,7 +1411,7 @@ bool RclConfig::initUserConfig() string lang = localelang(); for (int i = 0; i < ncffiles; i++) { string dst = path_cat(m_confdir, string(configfiles[i])); - if (access(dst.c_str(), 0) < 0) { + if (!path_exists(dst)) { FILE *fp = fopen(dst.c_str(), "w"); if (fp) { fprintf(fp, "%s\n", blurb); diff --git a/src/common/syngroups.cpp b/src/common/syngroups.cpp index c796502d..e72cf122 100644 --- a/src/common/syngroups.cpp +++ b/src/common/syngroups.cpp @@ -30,6 +30,16 @@ using namespace std; +// Note that we are storing each term twice. I don't think that the +// size could possibly be a serious issue, but if it was, we could +// reduce the storage a bit by storing (short hash)-> vector +// correspondances in the direct map, and then checking all the +// resulting groups for the input word. +// +// As it is, a word can only index one group (the last it is found +// in). It can be part of several groups though (appear in +// expansions). I really don't know what we should do with multiple +// groups anyway class SynGroups::Internal { public: Internal() : ok(false) { @@ -40,10 +50,12 @@ public: // Group num to group STD_UNORDERED_MAP > groups; }; + bool SynGroups::ok() { return m && m->ok; } + SynGroups::~SynGroups() { delete m; @@ -215,7 +227,7 @@ int main(int argc, char **argv) cout << group.size() << " terms in group\n"; for (vector::const_iterator it = group.begin(); it != group.end(); it++) { - cout << *it << " "; + cout << "[" << *it << "] "; } cout << endl; return 0; diff --git a/src/index/beaglequeue.cpp b/src/index/beaglequeue.cpp index 69747251..8a763b46 100644 --- a/src/index/beaglequeue.cpp +++ b/src/index/beaglequeue.cpp @@ -19,6 +19,7 @@ #include #include #include "safesysstat.h" +#include "safeunistd.h" #include "cstr.h" #include "pathut.h" diff --git a/src/index/rclmonprc.cpp b/src/index/rclmonprc.cpp index dde15f2c..c359b805 100644 --- a/src/index/rclmonprc.cpp +++ b/src/index/rclmonprc.cpp @@ -27,6 +27,7 @@ #include #include #include +#include "safeunistd.h" #include #include @@ -439,7 +440,7 @@ bool RclMonEventQueue::pushEvent(const RclMonEvent &ev) static bool checkfileanddelete(const string& fname) { bool ret; - ret = access(fname.c_str(), 0) == 0; + ret = path_exists(fname); unlink(fname.c_str()); return ret; } diff --git a/src/index/rclmonrcv.cpp b/src/index/rclmonrcv.cpp index 826956b3..1465fcac 100644 --- a/src/index/rclmonrcv.cpp +++ b/src/index/rclmonrcv.cpp @@ -22,6 +22,7 @@ #include #include #include "safesysstat.h" +#include "safeunistd.h" #include "debuglog.h" #include "rclmon.h" diff --git a/src/index/recollindex.cpp b/src/index/recollindex.cpp index f6e4eecf..f20a699d 100644 --- a/src/index/recollindex.cpp +++ b/src/index/recollindex.cpp @@ -24,6 +24,7 @@ #include #include #include +#include "safeunistd.h" #include #include diff --git a/src/internfile/mh_mbox.cpp b/src/internfile/mh_mbox.cpp index 142745c4..ee7e25a8 100644 --- a/src/internfile/mh_mbox.cpp +++ b/src/internfile/mh_mbox.cpp @@ -294,7 +294,7 @@ bool MimeHandlerMbox::set_document_file(const string& mt, const string &fn) // And double check for thunderbird string tbirdmsf = fn + ".msf"; - if ((m_quirks&MBOXQUIRK_TBIRD) == 0 && access(tbirdmsf.c_str(), 0) == 0) { + if ((m_quirks&MBOXQUIRK_TBIRD) == 0 && path_exists(tbirdmsf)) { LOGDEB(("MimeHandlerMbox: detected unconfigured tbird mbox in %s\n", fn.c_str())); m_quirks |= MBOXQUIRK_TBIRD; diff --git a/src/qtgui/fragbuts.cpp b/src/qtgui/fragbuts.cpp index b2ce3054..269320d4 100644 --- a/src/qtgui/fragbuts.cpp +++ b/src/qtgui/fragbuts.cpp @@ -148,7 +148,7 @@ FragButs::FragButs(QWidget* parent) m_fn = path_cat(theconfig->getConfDir(), "fragbuts.xml"); string data, reason; - if (access(m_fn.c_str(), 0) != 0) { + if (!path_exists(m_fn)) { // config does not exist: try to create it from sample string src = path_cat(theconfig->getDatadir(), "examples"); src = path_cat(src, "fragbuts.xml"); diff --git a/src/qtgui/multisave.cpp b/src/qtgui/multisave.cpp index 5f779165..b96d264a 100644 --- a/src/qtgui/multisave.cpp +++ b/src/qtgui/multisave.cpp @@ -123,7 +123,7 @@ void multiSave(QWidget *p, vector& docs) for (unsigned int i = 0; i != docs.size(); i++) { string fn = path_cat(dir, filenames[i]); - if (access(fn.c_str(), 0) == 0) { + if (path_exists(fn)) { QMessageBox::warning(0, "Recoll", QWidget::tr("Unexpected file name collision, " "cancelling.")); diff --git a/src/qtgui/preview_w.cpp b/src/qtgui/preview_w.cpp index 0f565ea6..f9399c47 100644 --- a/src/qtgui/preview_w.cpp +++ b/src/qtgui/preview_w.cpp @@ -14,6 +14,9 @@ * Free Software Foundation, Inc., * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#include "autoconfig.h" + +#include "safeunistd.h" #include #include diff --git a/src/qtgui/rclm_saveload.cpp b/src/qtgui/rclm_saveload.cpp index f4e0ce95..4d2120d8 100644 --- a/src/qtgui/rclm_saveload.cpp +++ b/src/qtgui/rclm_saveload.cpp @@ -41,7 +41,7 @@ static QString prevDir() settings.value("/Recoll/prefs/lastQuerySaveDir").toString(); string defpath = path_cat(theconfig->getConfDir(), "saved_queries"); if (prevdir.isEmpty()) { - if (access(defpath.c_str(), 0) != 0) { + if (!path_exists(defpath)) { mkdir(defpath.c_str(), 0700); } return QString::fromLocal8Bit(defpath.c_str()); diff --git a/src/qtgui/rclm_view.cpp b/src/qtgui/rclm_view.cpp index 3a926ef7..97ed2ed5 100644 --- a/src/qtgui/rclm_view.cpp +++ b/src/qtgui/rclm_view.cpp @@ -16,6 +16,8 @@ */ #include "autoconfig.h" +#include "safeunistd.h" + #include #include diff --git a/src/qtgui/rclmain_w.cpp b/src/qtgui/rclmain_w.cpp index 319e8865..1c13a671 100644 --- a/src/qtgui/rclmain_w.cpp +++ b/src/qtgui/rclmain_w.cpp @@ -17,6 +17,7 @@ #include "autoconfig.h" #include +#include "safeunistd.h" #include #include MEMORY_INCLUDE diff --git a/src/qtgui/rtitool.cpp b/src/qtgui/rtitool.cpp index 038f2bc4..d03ffa90 100644 --- a/src/qtgui/rtitool.cpp +++ b/src/qtgui/rtitool.cpp @@ -18,6 +18,7 @@ #include #include "safesysstat.h" +#include "safeunistd.h" #include @@ -60,7 +61,7 @@ void RTIToolW::init() connect(this->sesCB, SIGNAL(clicked(bool)), this, SLOT(sesclicked(bool))); string autostartfile = path_cat(path_home(), rautostartfile); - if (access(autostartfile.c_str(), 0) == 0) { + if (path_exists(autostartfile)) { sesCB->setChecked(true); } } @@ -80,7 +81,7 @@ void RTIToolW::accept() if (sesCB->isChecked()) { // Setting up daemon indexing autostart - if (::access(autostartfile.c_str(), 0) == 0) { + if (path_exists(autostartfile)) { QString msg = tr("Replacing: ") + QString::fromLocal8Bit(autostartfile.c_str()); @@ -96,7 +97,7 @@ void RTIToolW::accept() if (theconfig) { string sourcefile = path_cat(theconfig->getDatadir(), "examples"); sourcefile = path_cat(sourcefile, "recollindex.desktop"); - if (::access(sourcefile.c_str(), 0) == 0) { + if (path_exists(sourcefile)) { file_to_string(sourcefile, text); } } @@ -138,7 +139,7 @@ void RTIToolW::accept() exitdial = true; } else { // Turning autostart off - if (::access(autostartfile.c_str(), 0) == 0) { + if (path_exists(autostartfile)) { QString msg = tr("Deleting: ") + QString::fromLocal8Bit(autostartfile.c_str()); diff --git a/src/qtgui/uiprefs_w.cpp b/src/qtgui/uiprefs_w.cpp index 58150c5f..2027ae74 100644 --- a/src/qtgui/uiprefs_w.cpp +++ b/src/qtgui/uiprefs_w.cpp @@ -565,7 +565,7 @@ void UIPrefsDialog::addExtraDbPB_clicked() if (input.isEmpty()) return; string dbdir = (const char *)input.toLocal8Bit(); - if (access(path_cat(dbdir, "recoll.conf").c_str(), 0) == 0) { + if (path_exists(path_cat(dbdir, "recoll.conf"))) { // Chosen dir is config dir. RclConfig conf(&dbdir); dbdir = conf.getDbDir(); diff --git a/src/utils/conftree.cpp b/src/utils/conftree.cpp index 3671f1a8..97103da7 100644 --- a/src/utils/conftree.cpp +++ b/src/utils/conftree.cpp @@ -172,7 +172,7 @@ ConfSimple::ConfSimple(const char *fname, int readonly, bool tildexp) // It seems that there is no separate 'create if not exists' // open flag. Have to truncate to create, but dont want to do // this to an existing file ! - if (access(fname, 0) < 0) { + if (!path_exists(fname)) { mode |= ios::trunc; } input.open(fname, mode); diff --git a/src/utils/pathut.cpp b/src/utils/pathut.cpp index 60e1b3a7..4b6d1a58 100644 --- a/src/utils/pathut.cpp +++ b/src/utils/pathut.cpp @@ -433,6 +433,11 @@ long long path_filesize(const string& path) return (long long)st.st_size; } +bool path_exists(const string& path) +{ + return access(path.c_str(), 0) == 0; +} + // Allowed punctuation in the path part of an URI according to RFC2396 // -_.!~*'():@&=+$, /* diff --git a/src/utils/pathut.h b/src/utils/pathut.h index 8e8ca5b7..9be4f3dc 100644 --- a/src/utils/pathut.h +++ b/src/utils/pathut.h @@ -18,8 +18,6 @@ #define _PATHUT_H_INCLUDED_ #include "autoconfig.h" -#include - #include #include #include @@ -75,6 +73,11 @@ extern bool path_isdir(const std::string& path); /// Retrieve file size extern long long path_filesize(const std::string& path); +/// Check that path is traversable and last element exists +/// Returns true if last elt could be checked to exist. False may mean that +/// the file/dir does not exist or that an error occurred. +extern bool path_exists(const std::string& path); + /// Dump directory extern bool readdir(const std::string& dir, std::string& reason, std::set& entries);