From 7aa4edf91b2e231f9c96b8055fe474f22e32c8d8 Mon Sep 17 00:00:00 2001 From: Jean-Francois Dockes Date: Tue, 22 Mar 2016 13:35:35 +0100 Subject: [PATCH] Merged utility file versions with other packages --- src/index/beaglequeue.cpp | 2 +- src/utils/conftree.cpp | 21 ++++++++-- src/utils/conftree.h | 1 + src/utils/execmd.cpp | 82 +++++++++++++++++++++------------------ src/utils/execmd.h | 6 +++ src/utils/pathut.cpp | 6 +-- src/utils/pathut.h | 2 +- src/utils/readfile.cpp | 8 ++-- 8 files changed, 79 insertions(+), 49 deletions(-) diff --git a/src/index/beaglequeue.cpp b/src/index/beaglequeue.cpp index cf78b717..194beccf 100644 --- a/src/index/beaglequeue.cpp +++ b/src/index/beaglequeue.cpp @@ -259,7 +259,7 @@ bool BeagleQueueIndexer::index() return false; LOGDEB(("BeagleQueueIndexer::processqueue: [%s]\n", m_queuedir.c_str())); 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", m_queuedir.c_str(), errno)); return false; diff --git a/src/utils/conftree.cpp b/src/utils/conftree.cpp index db75aa65..91b961f2 100644 --- a/src/utils/conftree.cpp +++ b/src/utils/conftree.cpp @@ -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 * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or @@ -14,15 +14,26 @@ * Free Software Foundation, Inc., * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "autoconfig.h" - #ifndef TEST_CONFTREE +#ifdef BUILDING_RECOLL +#include "autoconfig.h" +#else +#include "config.h" +#endif + #include "conftree.h" #include #include +#ifdef _WIN32 #include "safesysstat.h" +#else +#include +#include +#include +#include +#endif #include #include @@ -672,6 +683,10 @@ const #include #include +#include +#include +#include + #include "conftree.h" #include "smallut.h" #include "readfile.h" diff --git a/src/utils/conftree.h b/src/utils/conftree.h index a3910ae2..f06643ba 100644 --- a/src/utils/conftree.h +++ b/src/utils/conftree.h @@ -49,6 +49,7 @@ * (useful to have central/personal config files) */ +#include #include #include #include diff --git a/src/utils/execmd.cpp b/src/utils/execmd.cpp index f35db46a..1afe7524 100644 --- a/src/utils/execmd.cpp +++ b/src/utils/execmd.cpp @@ -37,6 +37,7 @@ #include #include +#include #ifdef HAVE_SPAWN_H #ifndef __USE_GNU #define __USE_GNU @@ -52,6 +53,7 @@ #include "netcon.h" #include "closefrom.h" +#include "smallut.h" using namespace std; @@ -59,7 +61,6 @@ extern char **environ; #ifdef BUILDING_RECOLL #include "debuglog.h" -#include "smallut.h" #else // If compiling outside of recoll, make the file as standalone as reasonable. @@ -74,42 +75,6 @@ extern char **environ; #define LOGDEB3(X) #define LOGDEB4(X) -#ifndef MIN -#define MIN(A,B) ((A) < (B) ? (A) : (B)) -#endif - -static void stringToTokens(const string &s, vector &tokens, - const string &delims = " \t", bool skipinit=true); - -static void stringToTokens(const string& str, vector& 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 class ExecCmd::Internal { @@ -932,6 +897,30 @@ again: 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. // We would like to avoid blocking here too, but there is no simple // way to do this. The 2 possible approaches would be to: @@ -1250,6 +1239,7 @@ static char usage [] = " should be the path to an execm filter\n" " the type of the file parameters\n" "trexecmd -w cmd : do the 'which' thing\n" +"trexecmd -l cmd test getline\n" ; static void Usage(void) @@ -1266,6 +1256,7 @@ static int op_flags; #define OPT_r 0x20 #define OPT_m 0x40 #define OPT_o 0x80 +#define OPT_l 0x100 // Data sink for data coming out of the command. We also use it to set // a cancellation after a moment. @@ -1347,6 +1338,7 @@ int main(int argc, char *argv[]) case 'm': op_flags |= OPT_m; break; #endif case 'i': op_flags |= OPT_i; break; + case 'l': op_flags |= OPT_l; break; case 'o': op_flags |= OPT_o; break; default: Usage(); break; } @@ -1362,8 +1354,10 @@ int main(int argc, char *argv[]) l.push_back(*argv++); argc--; } +#ifdef BUILDING_RECOLL DebugLog::getdbl()->setloglevel(DEBDEB1); DebugLog::setfilename("stderr"); +#endif signal(SIGPIPE, SIG_IGN); if (op_flags & OPT_r) { @@ -1396,6 +1390,20 @@ int main(int argc, char *argv[]) l.erase(l.begin()); return exercise_mhexecm(arg1, mimetype, l) ? 0 : 1; #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 { // Default: execute command line arguments ExecCmd mexec; diff --git a/src/utils/execmd.h b/src/utils/execmd.h index d593b51c..64b7aa0c 100644 --- a/src/utils/execmd.h +++ b/src/utils/execmd.h @@ -156,7 +156,13 @@ class ExecCmd { bool has_input, bool has_output); int send(const std::string& data); int receive(std::string& data, int cnt = -1); + + /** Read line. Will call back periodically to check for cancellation */ int getline(std::string& data); + + /** Read line. Timeout after timeosecs seconds */ + int getline(std::string& data, int timeosecs); + int wait(); /** Wait with WNOHANG set. @return true if process exited, false else. diff --git a/src/utils/pathut.cpp b/src/utils/pathut.cpp index 5dd7510c..69cd22d4 100644 --- a/src/utils/pathut.cpp +++ b/src/utils/pathut.cpp @@ -66,6 +66,7 @@ #include #include "pathut.h" +#include "smallut.h" using namespace std; @@ -396,7 +397,6 @@ string path_absolute(const string& is) return s; } -#include string path_canon(const string& is, const string* cwd) { if (is.length() == 0) { @@ -456,7 +456,7 @@ string path_canon(const string& is, const string* cwd) return ret; } -bool makepath(const string& ipath) +bool path_makepath(const string& ipath, int mode) { string path = path_canon(ipath); vector elems; @@ -473,7 +473,7 @@ bool makepath(const string& ipath) // Not using path_isdir() here, because this cant grok symlinks // If we hit an existing file, no worry, mkdir will just fail. if (access(path.c_str(), 0) != 0) { - if (mkdir(path.c_str(), 0700) != 0) { + if (mkdir(path.c_str(), mode) != 0) { return false; } } diff --git a/src/utils/pathut.h b/src/utils/pathut.h index cf5ecd8d..d8df5f5e 100644 --- a/src/utils/pathut.h +++ b/src/utils/pathut.h @@ -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); /// 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 extern std::string path_homedata(); diff --git a/src/utils/readfile.cpp b/src/utils/readfile.cpp index a6b16f96..bbb71cc9 100644 --- a/src/utils/readfile.cpp +++ b/src/utils/readfile.cpp @@ -15,7 +15,11 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef TEST_READFILE +#ifdef BUILDING_RECOLL #include "autoconfig.h" +#else +#include "config.h" +#endif #include #include "safefcntl.h" @@ -32,10 +36,6 @@ using std::string; -#ifndef MIN -#define MIN(A,B) ((A) < (B) ? (A) : (B)) -#endif - class FileToString : public FileScanDo { public: FileToString(string& data) : m_data(data) {}