diff --git a/src/index/recollindex.cpp b/src/index/recollindex.cpp index 26a6abda..e45f053c 100644 --- a/src/index/recollindex.cpp +++ b/src/index/recollindex.cpp @@ -585,6 +585,10 @@ static vector fileToArgs(const string& fn) } +// Working directory before we change: it's simpler to change early +// but some options need the original for computing absolute paths. +static std::string orig_cwd; + // A bit of history: it's difficult to pass non-ASCII parameters // (e.g. path names) on the command line under Windows without using // Unicode. It was first thought possible to use a temporary file to @@ -725,6 +729,7 @@ int main(int argc, char *argv[]) exit(0); } + orig_cwd = path_cwd(); string rundir; config->getConfParam("idxrundir", rundir); if (!rundir.empty()) { @@ -775,6 +780,7 @@ int main(int argc, char *argv[]) if (aremain != 1) Usage(); string top = args[argidx++]; aremain--; + top = path_canon(top, &orig_cwd); bool status = recursive_index(config, top, selpatterns); if (confindexer && !confindexer->getReason().empty()) { addIdxReason("indexer", confindexer->getReason()); diff --git a/src/utils/pathut.cpp b/src/utils/pathut.cpp index 2ddb39a1..0270fcfa 100644 --- a/src/utils/pathut.cpp +++ b/src/utils/pathut.cpp @@ -756,11 +756,7 @@ string path_absolute(const string& is) path_slashize(s); #endif if (!path_isabsolute(s)) { - char buf[MAXPATHLEN]; - if (!getcwd(buf, MAXPATHLEN)) { - return string(); - } - s = path_cat(string(buf), s); + s = path_cat(path_cwd(), s); #ifdef _WIN32 path_slashize(s); #endif @@ -783,16 +779,11 @@ string path_canon(const string& is, const string* cwd) #endif if (!path_isabsolute(s)) { - char buf[MAXPATHLEN]; - const char *cwdp = buf; if (cwd) { - cwdp = cwd->c_str(); + s = path_cat(*cwd, s); } else { - if (!getcwd(buf, MAXPATHLEN)) { - return string(); - } + s = path_cat(path_cwd(), s); } - s = path_cat(string(cwdp), s); } vector elems; stringToTokens(s, elems, "/"); @@ -871,6 +862,27 @@ bool path_chdir(const std::string& path) return CHDIR(syspath) == 0; } +std::string path_cwd() +{ +#ifdef _WIN32 + wchar_t *wd = _wgetcwd(nullptr, 0); + if (nullptr == wd) { + return std::string(); + } + string sdname; + wchartoutf8(wd, sdname); + free(wd); + path_slashize(sdname); + return sdname; +#else + char wd[MAXPATHLEN+1]; + if (nullptr == getcwd(wd, MAXPATHLEN+1)) { + return string(); + } + return wd; +#endif +} + bool path_unlink(const std::string& path) { SYSPATH(path, syspath); diff --git a/src/utils/pathut.h b/src/utils/pathut.h index cb390473..f86f62dc 100644 --- a/src/utils/pathut.h +++ b/src/utils/pathut.h @@ -135,6 +135,7 @@ extern bool path_makepath(const std::string& path, int mode); /// extern bool path_chdir(const std::string& path); +extern std::string path_cwd(); extern bool path_unlink(const std::string& path); /* Open file, trying to do the right thing with non-ASCII paths on