diff --git a/src/common/rclconfig.cpp b/src/common/rclconfig.cpp index 1b8aeae7..046fd929 100644 --- a/src/common/rclconfig.cpp +++ b/src/common/rclconfig.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -60,6 +61,7 @@ typedef pair RclPII; bool o_index_stripchars = true; string RclConfig::o_localecharset; +string RclConfig::o_origcwd; bool ParamStale::needrecompute() { @@ -118,6 +120,16 @@ bool RclConfig::isDefaultConfig() const RclConfig::RclConfig(const string *argcnf) { zeroMe(); + + if (o_origcwd.empty()) { + char buf[MAXPATHLEN]; + if (getcwd(buf, MAXPATHLEN)) { + o_origcwd = string(buf); + } else { + fprintf(stderr, "recollxx: can't retrieve current working directory: relative path translations will fail\n"); + } + } + // Compute our data dir name, typically /usr/local/share/recoll const char *cdatadir = getenv("RECOLL_DATADIR"); if (cdatadir == 0) { diff --git a/src/common/rclconfig.h b/src/common/rclconfig.h index 3b5dbd8e..11abe361 100644 --- a/src/common/rclconfig.h +++ b/src/common/rclconfig.h @@ -257,6 +257,11 @@ class RclConfig { call it after primary init */ void initThrConf(); + const string& getOrigCwd() + { + return o_origcwd; + } + ~RclConfig() { freeAll(); } @@ -302,6 +307,10 @@ class RclConfig { ParamStale m_skpnstate; vector m_skpnlist; + // Original current working directory. Set once at init before we do any + // chdir'ing and used for converting user args to absolute paths. + static string o_origcwd; + // Parameters auto-fetched on setkeydir string m_defcharset; static string o_localecharset; diff --git a/src/index/indexer.cpp b/src/index/indexer.cpp index 7f15fab3..7e93fc3c 100644 --- a/src/index/indexer.cpp +++ b/src/index/indexer.cpp @@ -157,9 +157,10 @@ bool ConfIndexer::index(bool resetbefore, ixType typestorun) bool ConfIndexer::indexFiles(list& ifiles, IxFlag flag) { list myfiles; + string origcwd = m_config->getOrigCwd(); for (list::const_iterator it = ifiles.begin(); it != ifiles.end(); it++) { - myfiles.push_back(path_canon(*it)); + myfiles.push_back(path_canon(*it, &origcwd)); } myfiles.sort(); @@ -239,9 +240,10 @@ bool ConfIndexer::updateDocs(std::vector &docs, IxFlag flag) bool ConfIndexer::purgeFiles(std::list &files) { list myfiles; + string origcwd = m_config->getOrigCwd(); for (list::const_iterator it = files.begin(); it != files.end(); it++) { - myfiles.push_back(path_canon(*it)); + myfiles.push_back(path_canon(*it, &origcwd)); } myfiles.sort(); diff --git a/src/utils/pathut.cpp b/src/utils/pathut.cpp index 298279d8..f8f8bd22 100644 --- a/src/utils/pathut.cpp +++ b/src/utils/pathut.cpp @@ -335,17 +335,22 @@ extern string path_absolute(const string &is) } #include -extern string path_canon(const string &is) +extern string path_canon(const string &is, const string* cwd) { if (is.length() == 0) return is; string s = is; if (s[0] != '/') { char buf[MAXPATHLEN]; - if (!getcwd(buf, MAXPATHLEN)) { - return string(); + const char *cwdp = buf; + if (cwd) { + cwdp = cwd->c_str(); + } else { + if (!getcwd(buf, MAXPATHLEN)) { + return string(); + } } - s = path_cat(string(buf), s); + s = path_cat(string(cwdp), s); } vector elems; stringToTokens(s, elems, "/"); diff --git a/src/utils/pathut.h b/src/utils/pathut.h index 82f0d45f..873e8dcf 100644 --- a/src/utils/pathut.h +++ b/src/utils/pathut.h @@ -45,7 +45,7 @@ extern std::string path_tildexpand(const std::string &s); /// we return an empty path in this case. extern std::string path_absolute(const std::string &s); /// Clean up path by removing duplicated / and resolving ../ + make it absolute -extern std::string path_canon(const std::string &s); +extern std::string path_canon(const std::string &s, const std::string *cwd=0); /// Use glob(3) to return the file names matching pattern inside dir extern std::vector path_dirglob(const std::string &dir, const std::string pattern);