Store original working directory before changing, for later turning user args into absolute paths

This commit is contained in:
Jean-Francois Dockes 2013-06-06 14:05:26 +02:00
parent 77cba9d0b3
commit e77c510dfe
5 changed files with 35 additions and 7 deletions

View File

@ -23,6 +23,7 @@
#include <errno.h>
#include <langinfo.h>
#include <limits.h>
#include <sys/param.h>
#include <algorithm>
#include <list>
@ -60,6 +61,7 @@ typedef pair<int,int> 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) {

View File

@ -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<string> 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;

View File

@ -157,9 +157,10 @@ bool ConfIndexer::index(bool resetbefore, ixType typestorun)
bool ConfIndexer::indexFiles(list<string>& ifiles, IxFlag flag)
{
list<string> myfiles;
string origcwd = m_config->getOrigCwd();
for (list<string>::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<Rcl::Doc> &docs, IxFlag flag)
bool ConfIndexer::purgeFiles(std::list<string> &files)
{
list<string> myfiles;
string origcwd = m_config->getOrigCwd();
for (list<string>::const_iterator it = files.begin();
it != files.end(); it++) {
myfiles.push_back(path_canon(*it));
myfiles.push_back(path_canon(*it, &origcwd));
}
myfiles.sort();

View File

@ -335,17 +335,22 @@ extern string path_absolute(const string &is)
}
#include <smallut.h>
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<string> elems;
stringToTokens(s, elems, "/");

View File

@ -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<std::string> path_dirglob(const std::string &dir,
const std::string pattern);