Merged utility file versions with other packages

This commit is contained in:
Jean-Francois Dockes 2016-03-22 13:35:35 +01:00
parent e6af8c6020
commit 90afc535a4
8 changed files with 79 additions and 49 deletions

View file

@ -259,7 +259,7 @@ bool BeagleQueueIndexer::index()
return false; return false;
LOGDEB(("BeagleQueueIndexer::processqueue: [%s]\n", m_queuedir.c_str())); LOGDEB(("BeagleQueueIndexer::processqueue: [%s]\n", m_queuedir.c_str()));
m_config->setKeyDir(m_queuedir); m_config->setKeyDir(m_queuedir);
if (!makepath(m_queuedir)) { if (!path_makepath(m_queuedir, 0700)) {
LOGERR(("BeagleQueueIndexer:: can't create queuedir [%s] errno %d\n", LOGERR(("BeagleQueueIndexer:: can't create queuedir [%s] errno %d\n",
m_queuedir.c_str(), errno)); m_queuedir.c_str(), errno));
return false; return false;

View file

@ -1,4 +1,4 @@
/* Copyright (C) 2003 J.F.Dockes /* Copyright (C) 2003-2016 J.F.Dockes
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
@ -14,15 +14,26 @@
* Free Software Foundation, Inc., * Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/ */
#include "autoconfig.h"
#ifndef TEST_CONFTREE #ifndef TEST_CONFTREE
#ifdef BUILDING_RECOLL
#include "autoconfig.h"
#else
#include "config.h"
#endif
#include "conftree.h" #include "conftree.h"
#include <ctype.h> #include <ctype.h>
#include <fnmatch.h> #include <fnmatch.h>
#ifdef _WIN32
#include "safesysstat.h" #include "safesysstat.h"
#else
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#endif
#include <algorithm> #include <algorithm>
#include <cstring> #include <cstring>
@ -672,6 +683,10 @@ const
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "conftree.h" #include "conftree.h"
#include "smallut.h" #include "smallut.h"
#include "readfile.h" #include "readfile.h"

View file

@ -49,6 +49,7 @@
* (useful to have central/personal config files) * (useful to have central/personal config files)
*/ */
#include <time.h>
#include <algorithm> #include <algorithm>
#include <map> #include <map>
#include <string> #include <string>

View file

@ -37,6 +37,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <stdexcept>
#ifdef HAVE_SPAWN_H #ifdef HAVE_SPAWN_H
#ifndef __USE_GNU #ifndef __USE_GNU
#define __USE_GNU #define __USE_GNU
@ -52,6 +53,7 @@
#include "netcon.h" #include "netcon.h"
#include "closefrom.h" #include "closefrom.h"
#include "smallut.h"
using namespace std; using namespace std;
@ -59,7 +61,6 @@ extern char **environ;
#ifdef BUILDING_RECOLL #ifdef BUILDING_RECOLL
#include "debuglog.h" #include "debuglog.h"
#include "smallut.h"
#else #else
// If compiling outside of recoll, make the file as standalone as reasonable. // If compiling outside of recoll, make the file as standalone as reasonable.
@ -74,42 +75,6 @@ extern char **environ;
#define LOGDEB3(X) #define LOGDEB3(X)
#define LOGDEB4(X) #define LOGDEB4(X)
#ifndef MIN
#define MIN(A,B) ((A) < (B) ? (A) : (B))
#endif
static void stringToTokens(const string &s, vector<string> &tokens,
const string &delims = " \t", bool skipinit=true);
static void stringToTokens(const string& str, vector<string>& tokens,
const string& delims, bool skipinit)
{
string::size_type startPos = 0, pos;
// Skip initial delims, return empty if this eats all.
if (skipinit &&
(startPos = str.find_first_not_of(delims, 0)) == string::npos) {
return;
}
while (startPos < str.size()) {
// Find next delimiter or end of string (end of token)
pos = str.find_first_of(delims, startPos);
// Add token to the vector and adjust start
if (pos == string::npos) {
tokens.push_back(str.substr(startPos));
break;
} else if (pos == startPos) {
// Dont' push empty tokens after first
if (tokens.empty())
tokens.push_back(string());
startPos = ++pos;
} else {
tokens.push_back(str.substr(startPos, pos - startPos));
startPos = ++pos;
}
}
}
#endif // BUILDING_RECOLL #endif // BUILDING_RECOLL
class ExecCmd::Internal { class ExecCmd::Internal {
@ -932,6 +897,30 @@ again:
return n; return n;
} }
class GetlineWatchdog : public ExecCmdAdvise {
public:
GetlineWatchdog(int secs) : m_secs(secs), tstart(time(0)) {}
void newData(int cnt) {
if (time(0) - tstart >= m_secs) {
throw std::runtime_error("getline timeout");
}
}
int m_secs;
time_t tstart;
};
int ExecCmd::getline(string& data, int timeosecs)
{
GetlineWatchdog gwd(timeosecs);
setAdvise(&gwd);
try {
return getline(data);
} catch (...) {
return -1;
}
}
// Wait for command status and clean up all resources. // Wait for command status and clean up all resources.
// We would like to avoid blocking here too, but there is no simple // We would like to avoid blocking here too, but there is no simple
// way to do this. The 2 possible approaches would be to: // way to do this. The 2 possible approaches would be to:
@ -1250,6 +1239,7 @@ static char usage [] =
" <filter> should be the path to an execm filter\n" " <filter> should be the path to an execm filter\n"
" <mimetype> the type of the file parameters\n" " <mimetype> the type of the file parameters\n"
"trexecmd -w cmd : do the 'which' thing\n" "trexecmd -w cmd : do the 'which' thing\n"
"trexecmd -l cmd test getline\n"
; ;
static void Usage(void) static void Usage(void)
@ -1266,6 +1256,7 @@ static int op_flags;
#define OPT_r 0x20 #define OPT_r 0x20
#define OPT_m 0x40 #define OPT_m 0x40
#define OPT_o 0x80 #define OPT_o 0x80
#define OPT_l 0x100
// Data sink for data coming out of the command. We also use it to set // Data sink for data coming out of the command. We also use it to set
// a cancellation after a moment. // a cancellation after a moment.
@ -1347,6 +1338,7 @@ int main(int argc, char *argv[])
case 'm': op_flags |= OPT_m; break; case 'm': op_flags |= OPT_m; break;
#endif #endif
case 'i': op_flags |= OPT_i; break; case 'i': op_flags |= OPT_i; break;
case 'l': op_flags |= OPT_l; break;
case 'o': op_flags |= OPT_o; break; case 'o': op_flags |= OPT_o; break;
default: Usage(); break; default: Usage(); break;
} }
@ -1362,8 +1354,10 @@ int main(int argc, char *argv[])
l.push_back(*argv++); argc--; l.push_back(*argv++); argc--;
} }
#ifdef BUILDING_RECOLL
DebugLog::getdbl()->setloglevel(DEBDEB1); DebugLog::getdbl()->setloglevel(DEBDEB1);
DebugLog::setfilename("stderr"); DebugLog::setfilename("stderr");
#endif
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
if (op_flags & OPT_r) { if (op_flags & OPT_r) {
@ -1396,6 +1390,20 @@ int main(int argc, char *argv[])
l.erase(l.begin()); l.erase(l.begin());
return exercise_mhexecm(arg1, mimetype, l) ? 0 : 1; return exercise_mhexecm(arg1, mimetype, l) ? 0 : 1;
#endif #endif
} else if (op_flags & OPT_l) {
ExecCmd mexec;
if (mexec.startExec(arg1, l, false, true) < 0) {
cerr << "Startexec failed\n";
exit(1);
}
string output;
int ret = mexec.getline(output, 2);
cerr << "Got ret " << ret << " output " << output << endl;
cerr << "Waiting\n";
int status = mexec.wait();
cerr << "Got status " << status << endl;
exit (status);
} else { } else {
// Default: execute command line arguments // Default: execute command line arguments
ExecCmd mexec; ExecCmd mexec;

View file

@ -156,7 +156,13 @@ class ExecCmd {
bool has_input, bool has_output); bool has_input, bool has_output);
int send(const std::string& data); int send(const std::string& data);
int receive(std::string& data, int cnt = -1); int receive(std::string& data, int cnt = -1);
/** Read line. Will call back periodically to check for cancellation */
int getline(std::string& data); int getline(std::string& data);
/** Read line. Timeout after timeosecs seconds */
int getline(std::string& data, int timeosecs);
int wait(); int wait();
/** Wait with WNOHANG set. /** Wait with WNOHANG set.
@return true if process exited, false else. @return true if process exited, false else.

View file

@ -66,6 +66,7 @@
#include <vector> #include <vector>
#include "pathut.h" #include "pathut.h"
#include "smallut.h"
using namespace std; using namespace std;
@ -396,7 +397,6 @@ string path_absolute(const string& is)
return s; return s;
} }
#include <smallut.h>
string path_canon(const string& is, const string* cwd) string path_canon(const string& is, const string* cwd)
{ {
if (is.length() == 0) { if (is.length() == 0) {
@ -456,7 +456,7 @@ string path_canon(const string& is, const string* cwd)
return ret; return ret;
} }
bool makepath(const string& ipath) bool path_makepath(const string& ipath, int mode)
{ {
string path = path_canon(ipath); string path = path_canon(ipath);
vector<string> elems; vector<string> elems;
@ -473,7 +473,7 @@ bool makepath(const string& ipath)
// Not using path_isdir() here, because this cant grok symlinks // Not using path_isdir() here, because this cant grok symlinks
// If we hit an existing file, no worry, mkdir will just fail. // If we hit an existing file, no worry, mkdir will just fail.
if (access(path.c_str(), 0) != 0) { if (access(path.c_str(), 0) != 0) {
if (mkdir(path.c_str(), 0700) != 0) { if (mkdir(path.c_str(), mode) != 0) {
return false; return false;
} }
} }

View file

@ -101,7 +101,7 @@ extern bool readdir(const std::string& dir, std::string& reason,
bool fsocc(const std::string& path, int *pc, long long *avmbs = 0); bool fsocc(const std::string& path, int *pc, long long *avmbs = 0);
/// mkdir -p /// mkdir -p
extern bool makepath(const std::string& path); extern bool path_makepath(const std::string& path, int mode);
/// Where we create the user data subdirs /// Where we create the user data subdirs
extern std::string path_homedata(); extern std::string path_homedata();

View file

@ -15,7 +15,11 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/ */
#ifndef TEST_READFILE #ifndef TEST_READFILE
#ifdef BUILDING_RECOLL
#include "autoconfig.h" #include "autoconfig.h"
#else
#include "config.h"
#endif
#include <errno.h> #include <errno.h>
#include "safefcntl.h" #include "safefcntl.h"
@ -32,10 +36,6 @@
using std::string; using std::string;
#ifndef MIN
#define MIN(A,B) ((A) < (B) ? (A) : (B))
#endif
class FileToString : public FileScanDo { class FileToString : public FileScanDo {
public: public:
FileToString(string& data) : m_data(data) {} FileToString(string& data) : m_data(data) {}