more safexx posix stuff

This commit is contained in:
Jean-Francois Dockes 2015-08-22 08:37:58 +02:00
parent 34e7ad0936
commit f4ecd5c29e
18 changed files with 50 additions and 18 deletions

View file

@ -22,6 +22,7 @@
#include <langinfo.h>
#include <limits.h>
#include "safesysstat.h"
#include "safeunistd.h"
#include <sys/param.h>
#ifdef __FreeBSD__
#include <osreldate.h>
@ -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);

View file

@ -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<int>
// 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<int, vector<string> > 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<string>::const_iterator it = group.begin();
it != group.end(); it++) {
cout << *it << " ";
cout << "[" << *it << "] ";
}
cout << endl;
return 0;

View file

@ -19,6 +19,7 @@
#include <string.h>
#include <errno.h>
#include "safesysstat.h"
#include "safeunistd.h"
#include "cstr.h"
#include "pathut.h"

View file

@ -27,6 +27,7 @@
#include <pthread.h>
#include <errno.h>
#include <fnmatch.h>
#include "safeunistd.h"
#include <cstring>
#include <cstdio>
@ -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;
}

View file

@ -22,6 +22,7 @@
#include <cstdio>
#include <cstring>
#include "safesysstat.h"
#include "safeunistd.h"
#include "debuglog.h"
#include "rclmon.h"

View file

@ -24,6 +24,7 @@
#include <sys/time.h>
#include <sys/resource.h>
#include <fcntl.h>
#include "safeunistd.h"
#include <iostream>
#include <list>

View file

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

View file

@ -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");

View file

@ -123,7 +123,7 @@ void multiSave(QWidget *p, vector<Rcl::Doc>& 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."));

View file

@ -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 <list>
#include <utility>

View file

@ -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());

View file

@ -16,6 +16,8 @@
*/
#include "autoconfig.h"
#include "safeunistd.h"
#include <list>
#include <QMessageBox>

View file

@ -17,6 +17,7 @@
#include "autoconfig.h"
#include <fcntl.h>
#include "safeunistd.h"
#include <utility>
#include MEMORY_INCLUDE

View file

@ -18,6 +18,7 @@
#include <stdio.h>
#include "safesysstat.h"
#include "safeunistd.h"
#include <string>
@ -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());

View file

@ -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();

View file

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

View file

@ -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
// -_.!~*'():@&=+$,
/*

View file

@ -18,8 +18,6 @@
#define _PATHUT_H_INCLUDED_
#include "autoconfig.h"
#include <unistd.h>
#include <string>
#include <vector>
#include <set>
@ -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<std::string>& entries);