Merged the changes from the current windows port

This commit is contained in:
Jean-Francois Dockes
2015-09-25 16:14:27 +02:00
149 changed files with 10645 additions and 1575 deletions
+2 -2
View File
@@ -109,8 +109,8 @@ trfileudi.o : fileudi.cpp fileudi.h
EXECMD_OBJS= trexecmd.o
trexecmd : $(EXECMD_OBJS)
$(CXX) $(ALL_CXXFLAGS) -o trexecmd $(EXECMD_OBJS) $(LIBRECOLL)
trexecmd.o : execmd.cpp execmd.h
$(CXX) -o trexecmd.o -c $(ALL_CXXFLAGS) -DTEST_EXECMD execmd.cpp
trexecmd.o : trexecmd.cpp execmd.h
$(CXX) -o trexecmd.o -c $(ALL_CXXFLAGS) -I../xaposix trexecmd.cpp
TRANSCODE_OBJS= trtranscode.o
transcode : $(TRANSCODE_OBJS)
+2 -2
View File
@@ -217,7 +217,7 @@ void base64_encode(const string &in, string &out)
out.clear();
int srclength = in.length();
string::size_type srclength = in.length();
int sidx = 0;
while (2 < srclength) {
input[0] = in[sidx++];
@@ -244,7 +244,7 @@ void base64_encode(const string &in, string &out)
if (0 != srclength) {
/* Get what's left. */
input[0] = input[1] = input[2] = '\0';
for (int i = 0; i < srclength; i++)
for (string::size_type i = 0; i < srclength; i++)
input[i] = in[sidx++];
output[0] = input[0] >> 2;
+12 -12
View File
@@ -184,7 +184,7 @@ public:
// Offset of last write (newest header)
off_t m_nheadoffs;
// Pad size for newest entry.
int m_npadsize;
off_t m_npadsize;
// Keep history or only last entry
bool m_uniquentries;
///////////////////// End header entries
@@ -956,10 +956,10 @@ bool CirCache::erase(const string& udi)
// entry.
class CCScanHookSpacer : public CCScanHook {
public:
UINT sizewanted;
UINT sizeseen;
off_t sizewanted;
off_t sizeseen;
vector<pair<string, off_t> > squashed_udis;
CCScanHookSpacer(int sz)
CCScanHookSpacer(off_t sz)
: sizewanted(sz), sizeseen(0) {assert(sz > 0);}
virtual status takeone(off_t offs, const string& udi,
@@ -1009,14 +1009,14 @@ bool CirCache::put(const string& udi, const ConfSimple *iconf,
// Data compression ?
const char *datap = data.c_str();
unsigned int datalen = data.size();
size_t datalen = data.size();
unsigned short flags = 0;
TempBuf compbuf;
if (!(iflags & NoCompHint)) {
ULONG len = compressBound(data.size());
uLong len = compressBound(static_cast<uLong>(data.size()));
char *bf = compbuf.setsize(len);
if (bf != 0 &&
compress((Bytef*)bf, &len, (Bytef*)data.c_str(), data.size())
compress((Bytef*)bf, &len, (Bytef*)data.c_str(), static_cast<uLong>(data.size()))
== Z_OK) {
if (float(len) < 0.9 * float(data.size())) {
// bf is local but it's our static buffer address
@@ -1034,16 +1034,16 @@ bool CirCache::put(const string& udi, const ConfSimple *iconf,
}
// Characteristics for the new entry.
int nsize = CIRCACHE_HEADER_SIZE + dic.size() + datalen;
int nwriteoffs = m_d->m_oheadoffs;
int npadsize = 0;
off_t nsize = CIRCACHE_HEADER_SIZE + dic.size() + datalen;
off_t nwriteoffs = m_d->m_oheadoffs;
off_t npadsize = 0;
bool extending = false;
LOGDEB(("CirCache::put: nsz %d oheadoffs %d\n", nsize, m_d->m_oheadoffs));
// Check if we can recover some pad space from the (physically) previous
// entry.
int recovpadsize = m_d->m_oheadoffs == CIRCACHE_FIRSTBLOCK_SIZE ?
off_t recovpadsize = m_d->m_oheadoffs == CIRCACHE_FIRSTBLOCK_SIZE ?
0 : m_d->m_npadsize;
if (recovpadsize != 0) {
// Need to read the latest entry's header, to rewrite it with a
@@ -1082,7 +1082,7 @@ bool CirCache::put(const string& udi, const ConfSimple *iconf,
} else {
// Scan the file until we have enough space for the new entry,
// and determine the pad size up to the 1st preserved entry
int scansize = nsize - recovpadsize;
off_t scansize = nsize - recovpadsize;
LOGDEB(("CirCache::put: scanning for size %d from offs %u\n",
scansize, (UINT)m_d->m_oheadoffs));
CCScanHookSpacer spacer(scansize);
+13 -9
View File
@@ -14,9 +14,7 @@
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "autoconfig.h"
#ifndef TEST_CONFTREE
@@ -70,7 +68,7 @@ void ConfSimple::parseinput(istream &input)
}
{
int ll = strlen(cline);
size_t ll = strlen(cline);
while (ll > 0 && (cline[ll-1] == '\n' || cline[ll-1] == '\r')) {
cline[ll-1] = 0;
ll--;
@@ -576,8 +574,8 @@ bool ConfSimple::hasNameAnywhere(const string& nm) const
int ConfTree::get(const std::string &name, string &value, const string &sk)
const
{
if (sk.empty() || sk[0] != '/') {
// LOGDEB((stderr, "ConfTree::get: looking in global space\n"));
if (sk.empty() || !path_isabsolute(sk) ) {
// LOGDEB((stderr, "ConfTree::get: looking in global space for sk [%s]\n", sk.c_str()));
return ConfSimple::get(name, value, sk);
}
@@ -590,15 +588,21 @@ int ConfTree::get(const std::string &name, string &value, const string &sk)
// Look in subkey and up its parents until root ('')
for (;;) {
// LOGDEB((stderr,"ConfTree::get: looking for '%s' in '%s'\n",
// name.c_str(), msk.c_str()));
// LOGDEB((stderr,"ConfTree::get: looking for '%s' in '%s'\n",
// name.c_str(), msk.c_str()));
if (ConfSimple::get(name, value, msk))
return 1;
string::size_type pos = msk.rfind("/");
if (pos != string::npos) {
msk.replace(pos, string::npos, string());
} else
} else {
#ifdef _WIN32
if (msk.size() == 2 && isalpha(msk[0]) && msk[1] == ':')
msk.clear();
else
#endif
break;
}
}
return 0;
}
+7 -2
View File
@@ -15,14 +15,18 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef TEST_COPYFILE
#include "autoconfig.h"
#include <stdio.h>
#include <errno.h>
#include "safefcntl.h"
#include <sys/types.h>
#include "safesysstat.h"
#include "safeunistd.h"
#ifndef _WIN32
#include <sys/time.h>
#include <utime.h>
#endif
#include <cstring>
@@ -43,7 +47,7 @@ bool copyfile(const char *src, const char *dst, string &reason, int flags)
LOGDEB(("copyfile: %s to %s\n", src, dst));
if ((sfd = ::open(src, O_RDONLY)) < 0) {
if ((sfd = ::open(src, O_RDONLY, 0)) < 0) {
reason += string("open ") + src + ": " + strerror(errno);
goto out;
}
@@ -149,6 +153,7 @@ bool renameormove(const char *src, const char *dst, string &reason)
return false;
}
#ifndef _WIN32
// Try to preserve modes, owner, times. This may fail for a number
// of reasons
if ((st1.st_mode & 0777) != (st.st_mode & 0777)) {
@@ -167,7 +172,7 @@ bool renameormove(const char *src, const char *dst, string &reason)
times[1].tv_sec = st.st_mtime;
times[1].tv_usec = 0;
utimes(dst, times);
#endif
// All ok, get rid of origin
if (unlink(src) < 0) {
reason += string("Can't unlink ") + src + "Error : " + strerror(errno);
-1
View File
@@ -62,7 +62,6 @@ bool getCpuConf(CpuConf& cpus)
}
#endif
#else // TEST_CPUCONF
#include <stdlib.h>
+8 -7
View File
@@ -21,10 +21,8 @@
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
#include <fcntl.h>
#endif
#include <errno.h>
#include <safefcntl.h> // in case O_APPEND is in there
#ifdef INCLUDE_NEW_H
#include <new.h>
#endif
@@ -75,15 +73,18 @@ class DLFWImpl {
} else {
fp = fopen(filename, (truncate) ? "w" : "a");
if (fp) {
setvbuf(fp, 0, _IOLBF, 0);
#ifdef O_APPEND
setvbuf(fp, 0, _IOLBF, BUFSIZ);
#if defined(O_APPEND) && !defined(_WIN32)
{
int flgs = 0;
fcntl(fileno(fp), F_GETFL, &flgs);
fcntl(fileno(fp), F_SETFL, flgs|O_APPEND);
}
#endif
}
} else {
fprintf(stderr, "Debuglog: could not open [%s] errno %d\n",
filename, errno);
}
}
return;
}
+391 -238
View File
@@ -15,7 +15,7 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef TEST_EXECMD
#ifdef RECOLL_DATADIR
#ifdef BUILDING_RECOLL
#include "autoconfig.h"
#else
#include "config.h"
@@ -57,9 +57,7 @@ using namespace std;
extern char **environ;
bool ExecCmd::o_useVfork = false;
#ifdef RECOLL_DATADIR
#ifdef BUILDING_RECOLL
#include "debuglog.h"
#include "smallut.h"
@@ -112,7 +110,86 @@ static void stringToTokens(const string& str, vector<string>& tokens,
}
}
}
#endif // RECOLL_DATADIR
#endif // BUILDING_RECOLL
class ExecCmd::Internal {
public:
Internal()
: m_advise(0), m_provide(0), m_timeoutMs(1000),
m_rlimit_as_mbytes(0) {
}
static bool o_useVfork;
std::vector<std::string> m_env;
ExecCmdAdvise *m_advise;
ExecCmdProvide *m_provide;
bool m_killRequest;
int m_timeoutMs;
int m_rlimit_as_mbytes;
string m_stderrFile;
// Pipe for data going to the command
int m_pipein[2];
STD_SHARED_PTR<NetconCli> m_tocmd;
// Pipe for data coming out
int m_pipeout[2];
STD_SHARED_PTR<NetconCli> m_fromcmd;
// Subprocess id
pid_t m_pid;
// Saved sigmask
sigset_t m_blkcld;
// Reset internal state indicators. Any resources should have been
// previously freed
void reset() {
m_killRequest = false;
m_pipein[0] = m_pipein[1] = m_pipeout[0] = m_pipeout[1] = -1;
m_pid = -1;
sigemptyset(&m_blkcld);
}
// Child process code
inline void dochild(const std::string &cmd, const char **argv,
const char **envv, bool has_input, bool has_output);
};
bool ExecCmd::Internal::o_useVfork = false;
ExecCmd::ExecCmd()
{
m = new Internal();
if (m)
m->reset();
}
void ExecCmd::setAdvise(ExecCmdAdvise *adv)
{
m->m_advise = adv;
}
void ExecCmd::setProvide(ExecCmdProvide *p)
{
m->m_provide = p;
}
void ExecCmd::setTimeout(int mS)
{
if (mS > 30)
m->m_timeoutMs = mS;
}
void ExecCmd::setStderr(const std::string &stderrFile)
{
m->m_stderrFile = stderrFile;
}
pid_t ExecCmd::getChildPid()
{
return m->m_pid;
}
void ExecCmd::setKill()
{
m->m_killRequest = true;
}
void ExecCmd::zapChild()
{
setKill();
(void)wait();
}
/* From FreeBSD's which command */
static bool exec_is_there(const char *candidate)
@@ -174,12 +251,12 @@ void ExecCmd::useVfork(bool on)
// an executable file, we have a problem.
const char *argv[] = {"/", 0};
execve("/", (char *const *)argv, environ);
o_useVfork = on;
Internal::o_useVfork = on;
}
void ExecCmd::putenv(const string &ea)
{
m_env.push_back(ea);
m->m_env.push_back(ea);
}
void ExecCmd::putenv(const string &name, const string& value)
@@ -201,8 +278,12 @@ static void msleep(int millis)
* during method executions */
class ExecCmdRsrc {
public:
ExecCmdRsrc(ExecCmd *parent) : m_parent(parent), m_active(true) {}
void inactivate() {m_active = false;}
ExecCmdRsrc(ExecCmd::Internal *parent)
: m_parent(parent), m_active(true) {
}
void inactivate() {
m_active = false;
}
~ExecCmdRsrc() {
if (!m_active || !m_parent)
return;
@@ -250,13 +331,15 @@ public:
m_parent->reset();
}
private:
ExecCmd *m_parent;
ExecCmd::Internal *m_parent;
bool m_active;
};
ExecCmd::~ExecCmd()
{
ExecCmdRsrc(this);
ExecCmdRsrc(this->m);
if (m)
delete m;
}
// In child process. Set up pipes and exec command.
@@ -272,9 +355,9 @@ ExecCmd::~ExecCmd()
// If one of the calls block, the problem manifests itself by 20mn
// (filter timeout) of looping on "ExecCmd::doexec: selectloop
// returned 1', because the father is waiting on the read descriptor
inline void ExecCmd::dochild(const string &cmd, const char **argv,
const char **envv,
bool has_input, bool has_output)
inline void ExecCmd::Internal::dochild(const string &cmd, const char **argv,
const char **envv,
bool has_input, bool has_output)
{
// Start our own process group
if (setpgid(0, getpid())) {
@@ -384,7 +467,7 @@ inline void ExecCmd::dochild(const string &cmd, const char **argv,
void ExecCmd::setrlimit_as(int mbytes)
{
m_rlimit_as_mbytes = mbytes;
m->m_rlimit_as_mbytes = mbytes;
}
int ExecCmd::startExec(const string &cmd, const vector<string>& args,
@@ -401,13 +484,13 @@ int ExecCmd::startExec(const string &cmd, const vector<string>& args,
}
// The resource manager ensures resources are freed if we return early
ExecCmdRsrc e(this);
ExecCmdRsrc e(this->m);
if (has_input && pipe(m_pipein) < 0) {
if (has_input && pipe(m->m_pipein) < 0) {
LOGERR(("ExecCmd::startExec: pipe(2) failed. errno %d\n", errno));
return -1;
}
if (has_output && pipe(m_pipeout) < 0) {
if (has_output && pipe(m->m_pipeout) < 0) {
LOGERR(("ExecCmd::startExec: pipe(2) failed. errno %d\n", errno));
return -1;
}
@@ -441,7 +524,7 @@ int ExecCmd::startExec(const string &cmd, const vector<string>& args,
for (envsize = 0; ; envsize++)
if (environ[envsize] == 0)
break;
envv = (Ccharp *)malloc((envsize + m_env.size() + 2) * sizeof(char *));
envv = (Ccharp *)malloc((envsize + m->m_env.size() + 2) * sizeof(char *));
if (envv == 0) {
LOGERR(("ExecCmd::doexec: malloc() failed. errno %d\n", errno));
free(argv);
@@ -450,8 +533,8 @@ int ExecCmd::startExec(const string &cmd, const vector<string>& args,
int eidx;
for (eidx = 0; eidx < envsize; eidx++)
envv[eidx] = environ[eidx];
for (vector<string>::const_iterator it = m_env.begin();
it != m_env.end(); it++) {
for (vector<string>::const_iterator it = m->m_env.begin();
it != m->m_env.end(); it++) {
envv[eidx++] = it->c_str();
}
envv[eidx] = 0;
@@ -495,27 +578,27 @@ int ExecCmd::startExec(const string &cmd, const vector<string>& args,
posix_spawn_file_actions_init(&facts);
if (has_input) {
posix_spawn_file_actions_addclose(&facts, m_pipein[1]);
if (m_pipein[0] != 0) {
posix_spawn_file_actions_adddup2(&facts, m_pipein[0], 0);
posix_spawn_file_actions_addclose(&facts, m_pipein[0]);
posix_spawn_file_actions_addclose(&facts, m->m_pipein[1]);
if (m->m_pipein[0] != 0) {
posix_spawn_file_actions_adddup2(&facts, m->m_pipein[0], 0);
posix_spawn_file_actions_addclose(&facts, m->m_pipein[0]);
}
}
if (has_output) {
posix_spawn_file_actions_addclose(&facts, m_pipeout[0]);
if (m_pipeout[1] != 1) {
posix_spawn_file_actions_adddup2(&facts, m_pipeout[1], 1);
posix_spawn_file_actions_addclose(&facts, m_pipeout[1]);
posix_spawn_file_actions_addclose(&facts, m->m_pipeout[0]);
if (m->m_pipeout[1] != 1) {
posix_spawn_file_actions_adddup2(&facts, m->m_pipeout[1], 1);
posix_spawn_file_actions_addclose(&facts, m->m_pipeout[1]);
}
}
// Do we need to redirect stderr ?
if (!m_stderrFile.empty()) {
if (!m->m_stderrFile.empty()) {
int oflags = O_WRONLY|O_CREAT;
#ifdef O_APPEND
oflags |= O_APPEND;
#endif
posix_spawn_file_actions_addopen(&facts, 2, m_stderrFile.c_str(),
posix_spawn_file_actions_addopen(&facts, 2, m->m_stderrFile.c_str(),
oflags, 0600);
}
LOGDEB1(("using SPAWN\n"));
@@ -527,7 +610,7 @@ int ExecCmd::startExec(const string &cmd, const vector<string>& args,
posix_spawn_file_actions_addclose(&facts, i);
}
int ret = posix_spawn(&m_pid, exe.c_str(), &facts, &attrs,
int ret = posix_spawn(&m->m_pid, exe.c_str(), &facts, &attrs,
(char *const *)argv, (char *const *)envv);
posix_spawnattr_destroy(&attrs);
posix_spawn_file_actions_destroy(&facts);
@@ -539,22 +622,22 @@ int ExecCmd::startExec(const string &cmd, const vector<string>& args,
}
#else
if (o_useVfork) {
if (Internal::o_useVfork) {
LOGDEB1(("using VFORK\n"));
m_pid = vfork();
m->m_pid = vfork();
} else {
LOGDEB1(("using FORK\n"));
m_pid = fork();
m->m_pid = fork();
}
if (m_pid < 0) {
if (m->m_pid < 0) {
LOGERR(("ExecCmd::startExec: fork(2) failed. errno %d\n", errno));
return -1;
}
if (m_pid == 0) {
if (m->m_pid == 0) {
// e.inactivate() is not needed. As we do not return, the call
// stack won't be unwound and destructors of local objects
// won't be called.
dochild(exe, argv, envv, has_input, has_output);
m->dochild(exe, argv, envv, has_input, has_output);
// dochild does not return. Just in case...
_exit(1);
}
@@ -570,30 +653,30 @@ int ExecCmd::startExec(const string &cmd, const vector<string>& args,
// Set the process group for the child. This is also done in the
// child process see wikipedia(Process_group)
if (setpgid(m_pid, m_pid)) {
if (setpgid(m->m_pid, m->m_pid)) {
// This can fail with EACCES if the son has already done execve
// (linux at least)
LOGDEB2(("ExecCmd: father setpgid(son)(%d,%d) errno %d (ok)\n",
m_pid, m_pid, errno));
m->m_pid, m->m_pid, errno));
}
sigemptyset(&m_blkcld);
sigaddset(&m_blkcld, SIGCHLD);
pthread_sigmask(SIG_BLOCK, &m_blkcld, 0);
sigemptyset(&m->m_blkcld);
sigaddset(&m->m_blkcld, SIGCHLD);
pthread_sigmask(SIG_BLOCK, &m->m_blkcld, 0);
if (has_input) {
close(m_pipein[0]);
m_pipein[0] = -1;
close(m->m_pipein[0]);
m->m_pipein[0] = -1;
NetconCli *iclicon = new NetconCli();
iclicon->setconn(m_pipein[1]);
m_tocmd = NetconP(iclicon);
iclicon->setconn(m->m_pipein[1]);
m->m_tocmd = STD_SHARED_PTR<NetconCli>(iclicon);
}
if (has_output) {
close(m_pipeout[1]);
m_pipeout[1] = -1;
close(m->m_pipeout[1]);
m->m_pipeout[1] = -1;
NetconCli *oclicon = new NetconCli();
oclicon->setconn(m_pipeout[0]);
m_fromcmd = NetconP(oclicon);
oclicon->setconn(m->m_pipeout[0]);
m->m_fromcmd = STD_SHARED_PTR<NetconCli>(oclicon);
}
/* Don't want to undo what we just did ! */
@@ -605,28 +688,38 @@ int ExecCmd::startExec(const string &cmd, const vector<string>& args,
// Netcon callback. Send data to the command's input
class ExecWriter : public NetconWorker {
public:
ExecWriter(const string *input, ExecCmdProvide *provide)
: m_input(input), m_cnt(0), m_provide(provide)
{}
ExecWriter(const string *input, ExecCmdProvide *provide,
ExecCmd::Internal *parent)
: m_cmd(parent), m_input(input), m_cnt(0), m_provide(provide) {
}
void shutdown() {
close(m_cmd->m_pipein[1]);
m_cmd->m_pipein[1] = -1;
m_cmd->m_tocmd.reset();
}
virtual int data(NetconData *con, Netcon::Event reason)
{
if (!m_input) return -1;
if (!m_input)
return -1;
LOGDEB1(("ExecWriter: input m_cnt %d input length %d\n", m_cnt,
m_input->length()));
if (m_cnt >= m_input->length()) {
// Fd ready for more but we got none.
if (m_provide) {
m_provide->newData();
if (m_input->empty()) {
return 0;
} else {
m_cnt = 0;
}
LOGDEB2(("ExecWriter: provide m_cnt %d input length %d\n",
m_cnt, m_input->length()));
} else {
// Fd ready for more but we got none. Try to get data, else
// shutdown;
if (!m_provide) {
shutdown();
return 0;
}
}
m_provide->newData();
if (m_input->empty()) {
shutdown();
return 0;
} else {
// Ready with new buffer, reset use count
m_cnt = 0;
}
LOGDEB2(("ExecWriter: provide m_cnt %d input length %d\n",
m_cnt, m_input->length()));
}
int ret = con->send(m_input->c_str() + m_cnt,
m_input->length() - m_cnt);
@@ -639,6 +732,7 @@ public:
return ret;
}
private:
ExecCmd::Internal *m_cmd;
const string *m_input;
unsigned int m_cnt; // Current offset inside m_input
ExecCmdProvide *m_provide;
@@ -679,63 +773,63 @@ int ExecCmd::doexec(const string &cmd, const vector<string>& args,
}
// Cleanup in case we return early
ExecCmdRsrc e(this);
ExecCmdRsrc e(this->m);
SelectLoop myloop;
int ret = 0;
if (input || output) {
// Setup output
if (output) {
NetconCli *oclicon = dynamic_cast<NetconCli *>(m_fromcmd.get());
NetconCli *oclicon = m->m_fromcmd.get();
if (!oclicon) {
LOGERR(("ExecCmd::doexec: no connection from command\n"));
return -1;
}
oclicon->setcallback(STD_SHARED_PTR<NetconWorker>
(new ExecReader(output, m_advise)));
myloop.addselcon(m_fromcmd, Netcon::NETCONPOLL_READ);
(new ExecReader(output, m->m_advise)));
myloop.addselcon(m->m_fromcmd, Netcon::NETCONPOLL_READ);
// Give up ownership
m_fromcmd.reset();
m->m_fromcmd.reset();
}
// Setup input
if (input) {
NetconCli *iclicon = dynamic_cast<NetconCli *>(m_tocmd.get());
NetconCli *iclicon = m->m_tocmd.get();
if (!iclicon) {
LOGERR(("ExecCmd::doexec: no connection from command\n"));
return -1;
}
iclicon->setcallback(STD_SHARED_PTR<NetconWorker>
(new ExecWriter(input, m_provide)));
myloop.addselcon(m_tocmd, Netcon::NETCONPOLL_WRITE);
(new ExecWriter(input, m->m_provide, m)));
myloop.addselcon(m->m_tocmd, Netcon::NETCONPOLL_WRITE);
// Give up ownership
m_tocmd.reset();
m->m_tocmd.reset();
}
// Do the actual reading/writing/waiting
myloop.setperiodichandler(0, 0, m_timeoutMs);
myloop.setperiodichandler(0, 0, m->m_timeoutMs);
while ((ret = myloop.doLoop()) > 0) {
LOGDEB(("ExecCmd::doexec: selectloop returned %d\n", ret));
if (m_advise)
m_advise->newData(0);
if (m_killRequest) {
if (m->m_advise)
m->m_advise->newData(0);
if (m->m_killRequest) {
LOGINFO(("ExecCmd::doexec: cancel request\n"));
break;
}
}
LOGDEB0(("ExecCmd::doexec: selectloop returned %d\n", ret));
// Check for interrupt request: we won't want to waitpid()
if (m_advise)
m_advise->newData(0);
if (m->m_advise)
m->m_advise->newData(0);
// The netcons don't take ownership of the fds: we have to close them
// (have to do it before wait, this may be the signal the child is
// waiting for exiting).
if (input) {
close(m_pipein[1]);
m_pipein[1] = -1;
close(m->m_pipein[1]);
m->m_pipein[1] = -1;
}
if (output) {
close(m_pipeout[0]);
m_pipeout[0] = -1;
close(m->m_pipeout[0]);
m->m_pipeout[0] = -1;
}
}
@@ -750,14 +844,14 @@ int ExecCmd::doexec(const string &cmd, const vector<string>& args,
int ExecCmd::send(const string& data)
{
NetconCli *con = dynamic_cast<NetconCli *>(m_tocmd.get());
NetconCli *con = m->m_tocmd.get();
if (con == 0) {
LOGERR(("ExecCmd::send: outpipe is closed\n"));
return -1;
}
unsigned int nwritten = 0;
while (nwritten < data.length()) {
if (m_killRequest)
if (m->m_killRequest)
break;
int n = con->send(data.c_str() + nwritten, data.length() - nwritten);
if (n < 0) {
@@ -771,7 +865,7 @@ int ExecCmd::send(const string& data)
int ExecCmd::receive(string& data, int cnt)
{
NetconCli *con = dynamic_cast<NetconCli *>(m_fromcmd.get());
NetconCli *con = m->m_fromcmd.get();
if (con == 0) {
LOGERR(("ExecCmd::receive: inpipe is closed\n"));
return -1;
@@ -798,7 +892,7 @@ int ExecCmd::receive(string& data, int cnt)
int ExecCmd::getline(string& data)
{
NetconCli *con = dynamic_cast<NetconCli *>(m_fromcmd.get());
NetconCli *con = m->m_fromcmd.get();
if (con == 0) {
LOGERR(("ExecCmd::receive: inpipe is closed\n"));
return -1;
@@ -817,36 +911,54 @@ int ExecCmd::getline(string& data)
}
// 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:
// - Use signals (alarm), waitpid() is interruptible. but signals and
// threads... This would need a specialized thread, inter-thread comms etc.
// - Use an intermediary process when starting the command. The
// process forks a timer process, and the real command, then calls
// a blocking waitpid on all at the end, and is guaranteed to get
// at least the timer process status, thus yielding a select()
// equivalent. This is bad too, because the timeout is on the whole
// exec, not just the wait
// Just calling waitpid() with WNOHANG with a sleep() between tries
// does not work: the first waitpid() usually comes too early and
// reaps nothing, resulting in almost always one sleep() or more.
//
// So no timeout here. This has not been a problem in practise inside recoll.
// In case of need, using a semi-busy loop with short sleeps
// increasing from a few mS might work without creating too much
// overhead.
int ExecCmd::wait()
{
ExecCmdRsrc e(this);
ExecCmdRsrc e(this->m);
int status = -1;
if (!m_killRequest && m_pid > 0) {
if (waitpid(m_pid, &status, 0) < 0) {
if (!m->m_killRequest && m->m_pid > 0) {
if (waitpid(m->m_pid, &status, 0) < 0) {
LOGERR(("ExecCmd::waitpid: returned -1 errno %d\n", errno));
status = -1;
}
LOGDEB(("ExecCmd::wait: got status 0x%x\n", status));
m_pid = -1;
m->m_pid = -1;
}
// Let the ExecCmdRsrc cleanup
// Let the ExecCmdRsrc cleanup, it will do the killing/waiting if needed
return status;
}
bool ExecCmd::maybereap(int *status)
{
ExecCmdRsrc e(this);
ExecCmdRsrc e(this->m);
*status = -1;
if (m_pid <= 0) {
if (m->m_pid <= 0) {
// Already waited for ??
return true;
}
pid_t pid = waitpid(m_pid, status, WNOHANG);
pid_t pid = waitpid(m->m_pid, status, WNOHANG);
if (pid < 0) {
LOGERR(("ExecCmd::maybereap: returned -1 errno %d\n", errno));
m_pid = -1;
m->m_pid = -1;
return true;
} else if (pid == 0) {
LOGDEB1(("ExecCmd::maybereap: not exited yet\n"));
@@ -854,7 +966,7 @@ bool ExecCmd::maybereap(int *status)
return false;
} else {
LOGDEB(("ExecCmd::maybereap: got status 0x%x\n", status));
m_pid = -1;
m->m_pid = -1;
return true;
}
}
@@ -992,133 +1104,164 @@ void ReExec::reexec()
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
#include "debuglog.h"
#include "cancelcheck.h"
#include "execmd.h"
#include "smallut.h"
// Testing with rclaudio: use an mp3 as parameter
static const string tstcmd("/usr/share/recoll/filters/rclaudio");
static const string mimetype("audio/mpeg");
bool exercise_mhexecm(const string& filename)
using namespace std;
// Testing the rclexecm protocol outside of recoll. Here we use the
// rcldoc.py filter, you can try with rclaudio too, adjust the file arg
// accordingly
bool exercise_mhexecm(const string& cmdstr, const string& mimetype,
vector<string>& files)
{
ExecCmd cmd;
vector<string>myparams;
vector<string> myparams;
if (cmd.startExec(tstcmd, myparams, 1, 1) < 0) {
cerr << "startExec " << tstcmd << " failed. Missing command?\n";
if (cmd.startExec(cmdstr, myparams, 1, 1) < 0) {
cerr << "startExec " << cmdstr << " failed. Missing command?\n";
return false;
}
// Build request message
ostringstream obuf;
obuf << "FileName: " << filename.length() << "\n" << filename;
obuf << "Mimetype: " << mimetype.length() << "\n" << mimetype;
// Bogus parameter should be skipped by filter
obuf << "BogusParam: " << string("bogus").length() << "\n" << "bogus";
obuf << "\n";
cerr << "SENDING: [" << obuf.str() << "]\n";
// Send it
if (cmd.send(obuf.str()) < 0) {
// The real code calls zapchild here, but we don't need it as
// this will be handled by ~ExecCmd
//cmd.zapChild();
cerr << "send error\n";
return false;
for (vector<string>::const_iterator it = files.begin();
it != files.end(); it++) {
// Build request message
ostringstream obuf;
obuf << "Filename: " << (*it).length() << "\n" << (*it);
obuf << "Mimetype: " << mimetype.length() << "\n" << mimetype;
// Bogus parameter should be skipped by filter
obuf << "BogusParam: " << string("bogus").length() << "\n" << "bogus";
obuf << "\n";
cerr << "SENDING: [" << obuf.str() << "]\n";
// Send it
if (cmd.send(obuf.str()) < 0) {
// The real code calls zapchild here, but we don't need it as
// this will be handled by ~ExecCmd
//cmd.zapChild();
cerr << "send error\n";
return false;
}
// Read answer
for (int loop=0;;loop++) {
string name, data;
// Code from mh_execm.cpp: readDataElement
string ibuf;
// Read name and length
if (cmd.getline(ibuf) <= 0) {
cerr << "getline error\n";
return false;
}
// Empty line (end of message)
if (!ibuf.compare("\n")) {
cerr << "Got empty line\n";
name.clear();
break;
}
// Filters will sometimes abort before entering the real
// protocol, ie if a module can't be loaded. Check the
// special filter error first word:
if (ibuf.find("RECFILTERROR ") == 0) {
cerr << "Got RECFILTERROR\n";
return false;
}
// We're expecting something like Name: len\n
vector<string> tokens;
stringToTokens(ibuf, tokens);
if (tokens.size() != 2) {
cerr << "bad line in filter output: [" << ibuf << "]\n";
return false;
}
vector<string>::iterator it = tokens.begin();
name = *it++;
string& slen = *it;
int len;
if (sscanf(slen.c_str(), "%d", &len) != 1) {
cerr << "bad line in filter output (no len): [" <<
ibuf << "]\n";
return false;
}
// Read element data
data.erase();
if (len > 0 && cmd.receive(data, len) != len) {
cerr << "MHExecMultiple: expected " << len <<
" bytes of data, got " << data.length() << endl;
return false;
}
// Empty element: end of message
if (name.empty())
break;
cerr << "Got name: [" << name << "] data [" << data << "]\n";
}
}
// Read answer
for (int loop=0;;loop++) {
string name, data;
// Code from mh_execm.cpp: readDataElement
string ibuf;
// Read name and length
if (cmd.getline(ibuf) <= 0) {
cerr << "getline error\n";
return false;
}
// Empty line (end of message)
if (!ibuf.compare("\n")) {
cerr << "Got empty line\n";
name.clear();
return true;
}
// Filters will sometimes abort before entering the real protocol, ie if
// a module can't be loaded. Check the special filter error first word:
if (ibuf.find("RECFILTERROR ") == 0) {
cerr << "Got RECFILTERROR\n";
return false;
}
// We're expecting something like Name: len\n
vector<string> tokens;
stringToTokens(ibuf, tokens);
if (tokens.size() != 2) {
cerr << "bad line in filter output: [" << ibuf << "]\n";
return false;
}
vector<string>::iterator it = tokens.begin();
name = *it++;
string& slen = *it;
int len;
if (sscanf(slen.c_str(), "%d", &len) != 1) {
cerr << "bad line in filter output (no len): [" << ibuf << "]\n";
return false;
}
// Read element data
data.erase();
if (len > 0 && cmd.receive(data, len) != len) {
cerr << "MHExecMultiple: expected " << len <<
" bytes of data, got " << data.length() << endl;
return false;
}
// Empty element: end of message
if (name.empty())
break;
cerr << "Got name: [" << name << "] data [" << data << "]\n";
}
return true;
}
static char *thisprog;
static char usage [] =
"trexecmd [-c -r -i -o] cmd [arg1 arg2 ...]\n"
" -c : test cancellation (ie: trexecmd -c sleep 1000)\n"
" -r : run reexec. Must be separate option.\n"
" -i : command takes input\n"
" -o : command produces output\n"
" If -i is set, we send /etc/group contents to whatever command is run\n"
" If -o is set, we print whatever comes out\n"
"trexecmd -m <filter> <mimetype> <file> [file ...]: test execm:\n"
" <filter> should be the path to an execm filter\n"
" <mimetype> the type of the file parameters\n"
"trexecmd -w cmd : do the 'which' thing\n"
;
static void Usage(void)
{
fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
exit(1);
}
static int op_flags;
#define OPT_MOINS 0x1
#define OPT_b 0x4
#define OPT_i 0x4
#define OPT_w 0x8
#define OPT_c 0x10
#define OPT_r 0x20
#define OPT_m 0x40
#define OPT_o 0x80
const char *data = "Une ligne de donnees\n";
// Data sink for data coming out of the command. We also use it to set
// a cancellation after a moment.
class MEAdv : public ExecCmdAdvise {
public:
ExecCmd *cmd;
void newData(int cnt) {
if (op_flags & OPT_c) {
static int callcnt;
if (callcnt++ == 3) {
throw CancelExcept();
if (callcnt++ == 10) {
// Just sets the cancellation flag
CancelCheck::instance().setCancel();
// Would be called from somewhere else and throws an
// exception. We call it here for simplicity
CancelCheck::instance().checkCancel();
}
}
cerr << "newData(" << cnt << ")" << endl;
// CancelCheck::instance().setCancel();
// CancelCheck::instance().checkCancel();
// cmd->setCancel();
}
};
// Data provider, used if the -i flag is set
class MEPv : public ExecCmdProvide {
public:
FILE *m_fp;
@@ -1143,22 +1286,8 @@ public:
};
static char *thisprog;
static char usage [] =
"trexecmd [-c|-r] cmd [arg1 arg2 ...]\n"
" -c : test cancellation (ie: trexecmd -c sleep 1000)\n"
" -r : test reexec\n"
" -m <path to mp3 file>: test execm: needs installed and working rclaudio/mutagen\n"
"trexecmd -w cmd : do the which thing\n"
;
static void Usage(void)
{
fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
exit(1);
}
ReExec reexec;
int main(int argc, char *argv[])
{
reexec.init(argc, argv);
@@ -1188,6 +1317,8 @@ int main(int argc, char *argv[])
case 'r': op_flags |= OPT_r; break;
case 'w': op_flags |= OPT_w; break;
case 'm': op_flags |= OPT_m; break;
case 'i': op_flags |= OPT_i; break;
case 'o': op_flags |= OPT_o; break;
default: Usage(); break;
}
b1: argc--; argv++;
@@ -1201,15 +1332,22 @@ int main(int argc, char *argv[])
while (argc > 0) {
l.push_back(*argv++); argc--;
}
DebugLog::getdbl()->setloglevel(DEBDEB1);
DebugLog::setfilename("stderr");
signal(SIGPIPE, SIG_IGN);
if (op_flags & OPT_r) {
// Test reexec
// Test reexec. Normally only once, next time we fall through
// because we remove the -r option (only works if it was isolated, not like -rc
chdir("/");
argv[0] = strdup("");
sleep(1);
cerr << "Calling reexec\n";
// We remove the -r arg from list, otherwise we are going to
// loop (which you can try by commenting out the following
// line)
reexec.removeArg("-r");
reexec.reexec();
}
@@ -1218,44 +1356,59 @@ int main(int argc, char *argv[])
string path;
if (ExecCmd::which(arg1, path)) {
cout << path << endl;
exit(0);
return 0;
}
exit(1);
return 1;
} else if (op_flags & OPT_m) {
if (l.size() < 2)
Usage();
string mimetype = l[0];
l.erase(l.begin());
return exercise_mhexecm(arg1, mimetype, l) ? 0 : 1;
} else {
// Default: execute command line arguments
ExecCmd mexec;
// Set callback to be called whenever there is new data
// available and at a periodic interval, to check for
// cancellation
MEAdv adv;
mexec.setAdvise(&adv);
mexec.setTimeout(5);
// Stderr output goes there
mexec.setStderr("/tmp/trexecStderr");
// A few environment variables. Check with trexecmd env
mexec.putenv("TESTVARIABLE1=TESTVALUE1");
mexec.putenv("TESTVARIABLE2=TESTVALUE2");
mexec.putenv("TESTVARIABLE3=TESTVALUE3");
string input, output;
MEPv pv(&input);
string *ip = 0;
if (op_flags & OPT_i) {
ip = &input;
mexec.setProvide(&pv);
}
string *op = 0;
if (op_flags & OPT_o) {
op = &output;
}
int status = -1;
try {
status = mexec.doexec(arg1, l, ip, op);
} catch (CancelExcept) {
cerr << "CANCELLED" << endl;
}
fprintf(stderr, "Status: 0x%x\n", status);
if (op_flags & OPT_o) {
cout << output;
}
exit (status >> 8);
}
if (op_flags & OPT_m) {
return exercise_mhexecm(arg1) ? 0 : 1;
}
//////////////
// Default: execute command line arguments
ExecCmd mexec;
MEAdv adv;
adv.cmd = &mexec;
mexec.setAdvise(&adv);
mexec.setTimeout(5);
mexec.setStderr("/tmp/trexecStderr");
mexec.putenv("TESTVARIABLE1=TESTVALUE1");
mexec.putenv("TESTVARIABLE2=TESTVALUE2");
mexec.putenv("TESTVARIABLE3=TESTVALUE3");
string input, output;
// input = data;
string *ip = 0;
ip = &input;
MEPv pv(&input);
mexec.setProvide(&pv);
int status = -1;
try {
status = mexec.doexec(arg1, l, ip, &output);
} catch (CancelExcept) {
cerr << "CANCELLED" << endl;
}
fprintf(stderr, "Status: 0x%x\n", status);
cout << output;
exit (status >> 8);
}
#endif // TEST
+22 -54
View File
@@ -16,15 +16,12 @@
*/
#ifndef _EXECMD_H_INCLUDED_
#define _EXECMD_H_INCLUDED_
#include <signal.h>
#include "autoconfig.h"
#include <string>
#include <vector>
#include <stack>
#include "netcon.h"
/**
* Callback function object to advise of new data arrival, or just periodic
* heartbeat if cnt is 0.
@@ -42,7 +39,7 @@ class ExecCmdAdvise {
/**
* Callback function object to get more input data. Data has to be provided
* in the initial input string, set it to empty to signify eof.
* into the initial input string, set it to empty to signify eof.
*/
class ExecCmdProvide {
public:
@@ -93,18 +90,23 @@ class ExecCmd {
/**
* Set function objects to call whenever new data is available or on
* select timeout / whenever new data is needed to send. Must be called
* before doexec()
* select timeout. The data itself is stored in the output string.
* Must be set before calling doexec.
*/
void setAdvise(ExecCmdAdvise *adv) {m_advise = adv;}
void setProvide(ExecCmdProvide *p) {m_provide = p;}
void setAdvise(ExecCmdAdvise *adv);
/*
* Set function object to call whenever new data is needed. The
* data should be stored in the input string. Must be set before
* calling doexec()
*/
void setProvide(ExecCmdProvide *p);
/**
* Set select timeout in milliseconds. The default is 1 S.
* This is NOT a time after which an error will occur, but the period of
* the calls to the cancellation check routine.
* the calls to the advise routine (which normally checks for cancellation).
*/
void setTimeout(int mS) {if (mS > 30) m_timeoutMs = mS;}
void setTimeout(int mS);
/**
* Set destination for stderr data. The default is to let it alone (will
@@ -113,7 +115,7 @@ class ExecCmd {
* If the parameter can't be opened for writing, the command's
* stderr will be closed.
*/
void setStderr(const std::string &stderrFile) {m_stderrFile = stderrFile;}
void setStderr(const std::string &stderrFile);
/**
* Execute command.
@@ -130,7 +132,7 @@ class ExecCmd {
* @param args the argument vector (NOT including argv[0]).
* @param input Input to send TO the command.
* @param output Output FROM the command.
* @return the exec ouput status (0 if ok), or -1
* @return the exec output status (0 if ok), or -1
*/
int doexec(const std::string &cmd, const std::vector<std::string>& args,
const std::string *input = 0,
@@ -151,25 +153,21 @@ class ExecCmd {
@param O: status, the wait(2) call's status value */
bool maybereap(int *status);
pid_t getChildPid() {return m_pid;}
pid_t getChildPid();
/**
* Cancel/kill command. This can be called from another thread or
* from the advise callback, which could also raise an exception to
* accomplish the same thing
*/
void setKill() {m_killRequest = true;}
void setKill();
/**
* Get rid of current process (become ready for start).
*/
void zapChild() {setKill(); (void)wait();}
void zapChild();
ExecCmd()
: m_advise(0), m_provide(0), m_timeoutMs(1000), m_rlimit_as_mbytes(0)
{
reset();
}
ExecCmd();
~ExecCmd();
/**
@@ -190,40 +188,10 @@ class ExecCmd {
*/
static bool backtick(const std::vector<std::string> cmd, std::string& out);
friend class ExecCmdRsrc;
class Internal;
private:
static bool o_useVfork;
std::vector<std::string> m_env;
ExecCmdAdvise *m_advise;
ExecCmdProvide *m_provide;
bool m_killRequest;
int m_timeoutMs;
int m_rlimit_as_mbytes;
std::string m_stderrFile;
// Pipe for data going to the command
int m_pipein[2];
NetconP m_tocmd;
// Pipe for data coming out
int m_pipeout[2];
NetconP m_fromcmd;
// Subprocess id
pid_t m_pid;
// Saved sigmask
sigset_t m_blkcld;
// Reset internal state indicators. Any resources should have been
// previously freed
void reset() {
m_killRequest = false;
m_pipein[0] = m_pipein[1] = m_pipeout[0] = m_pipeout[1] = -1;
m_pid = -1;
sigemptyset(&m_blkcld);
}
// Child process code
inline void dochild(const std::string &cmd, const char **argv,
const char **envv, bool has_input, bool has_output);
/* Copyconst and assignment private and forbidden */
Internal *m;
/* Copyconst and assignment are private and forbidden */
ExecCmd(const ExecCmd &) {}
ExecCmd& operator=(const ExecCmd &) {return *this;};
};
+1
View File
@@ -15,6 +15,7 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef TEST_FILEUDI
#include "autoconfig.h"
#include <stdio.h>
#include <cstdlib>
+2 -2
View File
@@ -14,9 +14,8 @@
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "autoconfig.h"
#endif
#ifndef TEST_FSTREEWALK
@@ -25,6 +24,7 @@
#include <sys/stat.h>
#include <errno.h>
#include <fnmatch.h>
#include "safesysstat.h"
#include <cstring>
#include <algorithm>
+1 -1
View File
@@ -46,7 +46,7 @@ struct HighlightData {
* user term or group may generate many processed/expanded terms
* or groups, this is how we relate an expansion to its source.
*/
std::vector<unsigned int> grpsugidx;
std::vector<size_t> grpsugidx;
void clear()
{
+7 -3
View File
@@ -15,6 +15,8 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef TEST_IDFILE
#include "autoconfig.h"
#include <stdlib.h>
#include <ctype.h>
#include <cstring>
@@ -82,11 +84,11 @@ static string idFileInternal(istream& input, const char *fn)
}
// gcount includes the \n
int ll = input.gcount() - 1;
std::streamsize ll = input.gcount() - 1;
if (ll > 0)
gotnonempty = true;
LOGDEB2(("idfile: lnum %d ll %d: [%s]\n", lnum, ll, cline));
LOGDEB2(("idfile: lnum %d ll %u: [%s]\n", lnum, (unsigned int)ll, cline));
// Check for a few things that can't be found in a mail file,
// (optimization to get a quick negative)
@@ -123,7 +125,9 @@ static string idFileInternal(istream& input, const char *fn)
// Except for a possible first line with 'From ', lines must
// begin with whitespace or have a colon
// (hope no one comes up with a longer header name !
if (!isspace(cline[0])) {
// Take care to convert to unsigned char because ms ctype does
// like negative values
if (!isspace((unsigned char)cline[0])) {
char *cp = strchr(cline, ':');
if (cp == 0 || (cp - cline) > 70) {
LOGDEB2(("idfile: can't be mail header line: [%s]\n", cline));
+2
View File
@@ -16,6 +16,8 @@
*/
#ifndef TEST_MD5
#include "autoconfig.h"
#include <stdio.h>
#include <string.h>
+1 -3
View File
@@ -16,6 +16,7 @@
*/
#ifndef TEST_MIMEPARSE
#include "autoconfig.h"
#include <string>
#include <vector>
@@ -32,10 +33,7 @@
#include "transcode.h"
#include "smallut.h"
#ifndef NO_NAMESPACES
using namespace std;
#endif /* NO_NAMESPACES */
//#define DEBUG_MIMEPARSE
#ifdef DEBUG_MIMEPARSE
+353 -41
View File
@@ -19,17 +19,22 @@
#include "autoconfig.h"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include "safefcntl.h"
#include "safeunistd.h"
#include "dirent.h"
#include "cstr.h"
#ifdef _WIN32
#include "safewindows.h"
#else
#include <sys/param.h>
#include <pwd.h>
#include <sys/file.h>
#endif
#include <math.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/file.h>
#include "safesysstat.h"
#include <glob.h>
#include "ptmutex.h"
// Let's include all files where statfs can be defined and hope for no
// conflict...
@@ -52,15 +57,104 @@
#include <sstream>
#include <stack>
#include <set>
using namespace std;
#include <vector>
#include "pathut.h"
#include "transcode.h"
#include "wipedir.h"
#include "md5ut.h"
using namespace std;
#ifdef _WIN32
/// Convert \ separators to /
void path_slashize(string& s)
{
for (string::size_type i = 0; i < s.size(); i++) {
if (s[i] == '\\')
s[i] = '/';
}
}
static bool path_strlookslikedrive(const string& s)
{
return s.size() == 2 && isalpha(s[0]) && s[1] == ':';
}
static bool path_hasdrive(const string& s)
{
if (s.size() >= 2 && isalpha(s[0]) && s[1] == ':')
return true;
return false;
}
static bool path_isdriveabs(const string& s)
{
if (s.size() >= 3 && isalpha(s[0]) && s[1] == ':' && s[2] == '/')
return true;
return false;
}
#include <Shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
string path_tchartoutf8(TCHAR *text)
{
#ifdef UNICODE
// Simple C
// const size_t size = ( wcslen(text) + 1 ) * sizeof(wchar_t);
// wcstombs(&buffer[0], text, size);
// std::vector<char> buffer(size);
// Or:
// Windows API
std::vector<char> buffer;
int size = WideCharToMultiByte(CP_UTF8, 0, text, -1, NULL, 0, NULL, NULL);
if (size > 0) {
buffer.resize(size);
WideCharToMultiByte(CP_UTF8, 0, text, -1,
&buffer[0], int(buffer.size()), NULL, NULL);
} else {
return string();
}
return string(&buffer[0]);
#else
return text;
#endif
}
string path_thisexecpath()
{
TCHAR text[MAX_PATH];
DWORD length = GetModuleFileName(NULL, text, MAX_PATH);
#ifdef NTDDI_WIN8_future
PathCchRemoveFileSpec(text, MAX_PATH);
#else
PathRemoveFileSpec(text);
#endif
string path = path_tchartoutf8(text);
if (path.empty())
path = "c:/";
return path;
}
string path_wingettempfilename(TCHAR *pref)
{
TCHAR buf[(MAX_PATH +1)*sizeof(TCHAR)];
TCHAR dbuf[(MAX_PATH +1)*sizeof(TCHAR)];
GetTempPath(MAX_PATH+1, dbuf);
GetTempFileName(dbuf, pref, 0, buf);
// Windows will have created a temp file, we delete it.
string filename = path_tchartoutf8(buf);
unlink(filename.c_str());
return filename;
}
#endif
bool fsocc(const string &path, int *pc, long long *blocks)
{
#ifdef _WIN32
return false;
#else
#ifdef sun
struct statvfs buf;
if (statvfs(path.c_str(), &buf) != 0) {
@@ -94,6 +188,7 @@ bool fsocc(const string &path, int *pc, long long *blocks)
}
}
return true;
#endif
}
const string& tmplocation()
@@ -103,15 +198,61 @@ const string& tmplocation()
const char *tmpdir = getenv("RECOLL_TMPDIR");
if (tmpdir == 0)
tmpdir = getenv("TMPDIR");
if (tmpdir == 0)
tmpdir = "/tmp";
stmpdir = string(tmpdir);
if (tmpdir == 0)
tmpdir = getenv("TMP");
if (tmpdir == 0)
tmpdir = getenv("TEMP");
if (tmpdir == 0) {
#ifdef _WIN32
TCHAR bufw[(MAX_PATH+1)*sizeof(TCHAR)];
GetTempPath(MAX_PATH+1, bufw);
stmpdir = path_tchartoutf8(bufw);
#else
stmpdir = "/tmp";
#endif
} else {
stmpdir = tmpdir;
}
stmpdir = path_canon(stmpdir);
}
return stmpdir;
}
// Location for sample config, filters, etc. (e.g. /usr/share/recoll/)
const string& path_sharedatadir()
{
static string datadir;
if (datadir.empty()) {
#ifdef _WIN32
datadir = path_cat(path_thisexecpath(), "Share");
#else
const char *cdatadir = getenv("RECOLL_DATADIR");
if (cdatadir == 0) {
// If not in environment, use the compiled-in constant.
datadir = RECOLL_DATADIR;
} else {
datadir = cdatadir;
}
#endif
}
return datadir;
}
string path_PATHsep()
{
static const string w(";");
static const string u(":");
#ifdef _WIN32
return w;
#else
return u;
#endif
}
bool maketmpdir(string& tdir, string& reason)
{
#ifndef _WIN32
tdir = path_cat(tmplocation(), "rcltmpXXXXXX");
char *cp = strdup(tdir.c_str());
@@ -121,6 +262,14 @@ bool maketmpdir(string& tdir, string& reason)
return false;
}
// There is a race condition between name computation and
// mkdir. try to make sure that we at least don't shoot ourselves
// in the foot
#if !defined(HAVE_MKDTEMP) || defined(_WIN32)
static PTMutexInit mlock;
PTMutexLocker lock(mlock);
#endif
if (!
#ifdef HAVE_MKDTEMP
mkdtemp(cp)
@@ -136,8 +285,19 @@ bool maketmpdir(string& tdir, string& reason)
}
tdir = cp;
free(cp);
#else // _WIN32
// There is a race condition between name computation and
// mkdir. try to make sure that we at least don't shoot ourselves
// in the foot
static PTMutexInit mlock;
PTMutexLocker lock(mlock);
tdir = path_wingettempfilename(TEXT("rcltmp"));
#endif
#ifndef HAVE_MKDTEMP
// At this point the directory does not exist yet except if we used
// mkdtemp
#if !defined(HAVE_MKDTEMP) || defined(_WIN32)
if (mkdir(tdir.c_str(), 0700) < 0) {
reason = string("maketmpdir: mkdir ") + tdir + " failed";
tdir.erase();
@@ -151,6 +311,14 @@ bool maketmpdir(string& tdir, string& reason)
TempFileInternal::TempFileInternal(const string& suffix)
: m_noremove(false)
{
// Because we need a specific suffix, can't use mkstemp
// well. There is a race condition between name computation and
// file creation. try to make sure that we at least don't shoot
// our own selves in the foot. maybe we'll use mkstemps one day.
static PTMutexInit mlock;
PTMutexLocker lock(mlock);
#ifndef _WIN32
string filename = path_cat(tmplocation(), "rcltmpfXXXXXX");
char *cp = strdup(filename.c_str());
if (!cp) {
@@ -158,8 +326,8 @@ TempFileInternal::TempFileInternal(const string& suffix)
return;
}
// Yes using mkstemp this way is awful (bot the suffix adding and
// using mkstemp() just to avoid the warnings)
// Using mkstemp this way is awful (bot the suffix adding and
// using mkstemp() instead of mktemp just to avoid the warnings)
int fd;
if ((fd = mkstemp(cp)) < 0) {
free(cp);
@@ -168,9 +336,11 @@ TempFileInternal::TempFileInternal(const string& suffix)
}
close(fd);
unlink(cp);
filename = cp;
free(cp);
#else
string filename = path_wingettempfilename(TEXT("recoll"));
#endif
m_filename = filename + suffix;
if (close(open(m_filename.c_str(), O_CREAT|O_EXCL, 0600)) != 0) {
@@ -214,29 +384,39 @@ bool TempDir::wipe()
return true;
}
void path_catslash(string &s) {
void path_catslash(string &s)
{
#ifdef WIN32
path_slashize(s);
#endif
if (s.empty() || s[s.length() - 1] != '/')
s += '/';
}
string path_cat(const string &s1, const string &s2) {
string path_cat(const string &s1, const string &s2)
{
string res = s1;
path_catslash(res);
res += s2;
return res;
}
string path_getfather(const string &s) {
string path_getfather(const string &s)
{
string father = s;
#ifdef WIN32
path_slashize(father);
#endif
// ??
if (father.empty())
return "./";
if (path_isroot(father))
return father;
if (father[father.length() - 1] == '/') {
// Input ends with /. Strip it, handle special case for root
if (father.length() == 1)
return father;
// Input ends with /. Strip it, root special case was tested above
father.erase(father.length()-1);
}
@@ -249,8 +429,12 @@ string path_getfather(const string &s) {
return father;
}
string path_getsimple(const string &s) {
string path_getsimple(const string &s)
{
string simple = s;
#ifdef WIN32
path_slashize(simple);
#endif
if (simple.empty())
return simple;
@@ -285,6 +469,28 @@ string path_suffix(const string& s)
string path_home()
{
#ifdef _WIN32
string dir;
const char *cp = getenv("USERPROFILE");
if (cp != 0) {
dir = cp;
}
if (dir.empty()) {
cp = getenv("HOMEDRIVE");
if (cp != 0) {
const char *cp1 = getenv("HOMEPATH");
if (cp1 != 0) {
dir = string(cp) + string(cp1);
}
}
}
if (dir.empty()) {
dir = "C:\\";
}
dir = path_canon(dir);
path_catslash(dir);
return dir;
#else
uid_t uid = getuid();
struct passwd *entry = getpwuid(uid);
@@ -299,6 +505,26 @@ string path_home()
string homedir = entry->pw_dir;
path_catslash(homedir);
return homedir;
#endif
}
// The default place to store the default config and other stuff (e.g webqueue)
string path_homedata()
{
#ifdef _WIN32
const char *cp = getenv("LOCALAPPDATA");
string dir;
if (cp != 0) {
dir = path_canon(cp);
}
if (dir.empty()) {
dir = path_cat(path_home(), "AppData/Local/");
}
return dir;
#else
// We should use an xdg-conforming location, but, history...
return path_home();
#endif
}
string path_tildexpand(const string &s)
@@ -306,31 +532,67 @@ string path_tildexpand(const string &s)
if (s.empty() || s[0] != '~')
return s;
string o = s;
#ifdef WIN32
path_slashize(o);
#endif
if (s.length() == 1) {
o.replace(0, 1, path_home());
} else if (s[1] == '/') {
o.replace(0, 2, path_home());
} else {
string::size_type pos = s.find('/');
int l = (pos == string::npos) ? s.length() - 1 : pos - 1;
string::size_type l = (pos == string::npos) ? s.length() - 1 : pos - 1;
#ifdef _WIN32
// Dont know what this means. Just replace with HOME
o.replace(0, l+1, path_home());
#else
struct passwd *entry = getpwnam(s.substr(1, l).c_str());
if (entry)
o.replace(0, l+1, entry->pw_dir);
#endif
}
return o;
}
bool path_isroot(const string& path)
{
if (path.size() == 1 && path[0] == '/')
return true;
#ifdef _WIN32
if (path.size() == 3 && isalpha(path[0]) && path[1] == ':' &&
(path[2] == '/' || path[2] == '\\'))
return true;
#endif
return false;
}
bool path_isabsolute(const string &path)
{
if (!path.empty() && (path[0] == '/'
#ifdef _WIN32
|| path_isdriveabs(path)
#endif
)) {
return true;
}
return false;
}
string path_absolute(const string &is)
{
if (is.length() == 0)
return is;
string s = is;
if (s[0] != '/') {
if (!path_isabsolute(s)) {
char buf[MAXPATHLEN];
if (!getcwd(buf, MAXPATHLEN)) {
return string();
}
s = path_cat(string(buf), s);
s = path_cat(string(buf), s);
#ifdef _WIN32
path_slashize(s);
#endif
}
return s;
}
@@ -341,7 +603,15 @@ string path_canon(const string &is, const string* cwd)
if (is.length() == 0)
return is;
string s = is;
if (s[0] != '/') {
#ifdef _WIN32
path_slashize(s);
// fix possible path from file: absolute url
if (s.size() && s[0] == '/' && path_hasdrive(s.substr(1))) {
s = s.substr(1);
}
#endif
if (!path_isabsolute(s)) {
char buf[MAXPATHLEN];
const char *cwdp = buf;
if (cwd) {
@@ -370,7 +640,13 @@ string path_canon(const string &is, const string* cwd)
if (!cleaned.empty()) {
for (vector<string>::const_iterator it = cleaned.begin();
it != cleaned.end(); it++) {
ret += "/";
ret += "/";
#ifdef _WIN32
if (it == cleaned.begin() && path_strlookslikedrive(*it)) {
// Get rid of just added initial "/"
ret.clear();
}
#endif
ret += *it;
}
} else {
@@ -387,6 +663,10 @@ bool makepath(const string& ipath)
path = "/";
for (vector<string>::const_iterator it = elems.begin();
it != elems.end(); it++){
#ifdef _WIN32
if (it == elems.begin() && path_strlookslikedrive(*it))
path = "";
#endif
path += *it;
// Not using path_isdir() here, because this cant grok symlinks
// If we hit an existing file, no worry, mkdir will just fail.
@@ -400,21 +680,6 @@ bool makepath(const string& ipath)
return true;
}
vector<string> path_dirglob(const string &dir, const string pattern)
{
vector<string> res;
glob_t mglob;
string mypat=path_cat(dir, pattern);
if (glob(mypat.c_str(), 0, 0, &mglob)) {
return res;
}
for (int i = 0; i < int(mglob.gl_pathc); i++) {
res.push_back(mglob.gl_pathv[i]);
}
globfree(&mglob);
return res;
}
bool path_isdir(const string& path)
{
struct stat st;
@@ -553,6 +818,16 @@ string url_parentfolder(const string& url)
string("http://") + parenturl;
}
string path_defaultrecollconfsubdir()
{
#ifdef _WIN32
return "Recoll";
#else
return ".recoll";
#endif
}
// Convert to file path if url is like file:
// Note: this only works with our internal pseudo-urls which are not
// encoded/escaped
@@ -563,6 +838,14 @@ string fileurltolocalpath(string url)
else
return string();
#ifdef _WIN32
// Absolute file urls are like: file:///c:/mydir/...
// Get rid of the initial '/'
if (url.size() >= 3 && url[0] == '/' && isalpha(url[1]) && url[2] == ':') {
url = url.substr(1);
}
#endif
// Removing the fragment part. This is exclusively used when
// executing a viewer for the recoll manual, and we only strip the
// part after # if it is preceded by .html
@@ -576,6 +859,17 @@ string fileurltolocalpath(string url)
return url;
}
string path_pathtofileurl(const string& path)
{
// We're supposed to receive a canonic absolute path, but on windows we
// may need to add a '/' in front of the drive spec
string url(cstr_fileu);
if (path.empty() || path[0] != '/')
url.push_back('/');
url += path;
return url;
}
bool urlisfileurl(const string& url)
{
return url.find("file://") == 0;
@@ -686,6 +980,9 @@ int Pidfile::flopen()
m_reason = "fcntl lock failed";
return -1;
}
#else
#ifdef _WIN32
return 0;
#else
int operation = LOCK_EX | LOCK_NB;
if (flock(m_fd, operation) == -1) {
@@ -695,6 +992,7 @@ int Pidfile::flopen()
m_reason = "flock failed";
return -1;
}
#endif // ! win32
#endif // ! sun
if (ftruncate(m_fd, 0) != 0) {
@@ -815,6 +1113,7 @@ void pathut_init_mt()
path_home();
tmplocation();
thumbnailsdir();
path_sharedatadir();
}
@@ -933,7 +1232,7 @@ int main(int argc, const char **argv)
pidfile.remove();
#endif
#if 1
#if 0
if (argc > 1) {
cerr << "Usage: thumbpath <filepath>" << endl;
exit(1);
@@ -965,6 +1264,19 @@ int main(int argc, const char **argv)
cout << "Suffix: [" << ext << "]" << endl;
return 0;
#endif
#if 1
if (argc != 1) {
cerr << "Usage: trpathut url" << endl;
exit(1);
}
string url = *argv++;argc--;
cout << "File: [" << fileurltolocalpath(url) << "]\n";
return 0;
#endif
}
#endif // TEST_PATHUT
+18
View File
@@ -78,6 +78,9 @@ extern long long path_filesize(const std::string& path);
/// the file/dir does not exist or that an error occurred.
extern bool path_exists(const std::string& path);
/// Return separator for PATH environment variable
extern std::string path_PATHsep();
/// Dump directory
extern bool readdir(const std::string& dir, std::string& reason,
std::set<std::string>& entries);
@@ -97,6 +100,21 @@ extern bool maketmpdir(std::string& tdir, std::string& reason);
/// mkdir -p
extern bool makepath(const std::string& path);
/// Sub-directory for default recoll config (e.g: .recoll)
extern std::string path_defaultrecollconfsubdir();
/// Where we create the user data subdirs
extern std::string path_homedata();
/// e.g. /usr/share/recoll. Depends on OS and config
extern const std::string& path_sharedatadir();
/// Test if path is absolute
extern bool path_isabsolute(const std::string& s);
/// Test if path is root (x:/). root is defined by root/.. == root
extern bool path_isroot(const std::string& p);
/// Turn absolute path into file:// url
extern std::string path_pathtofileurl(const std::string& path);
/// Temporary file class
class TempFileInternal {
public:
+10 -5
View File
@@ -25,11 +25,11 @@
#include <string>
using std::string;
#include "readfile.h"
#include "smallut.h"
using std::string;
#ifndef MIN
#define MIN(A,B) ((A) < (B) ? (A) : (B))
#endif
@@ -78,6 +78,11 @@ const int RDBUFSZ = 8192;
bool file_scan(const string &fn, FileScanDo* doer, off_t startoffs,
size_t cnttoread, string *reason)
{
if (startoffs < 0) {
*reason += " file_scan: negative startoffs not allowed";
return false;
}
bool ret = false;
bool noclosing = true;
int fd = 0;
@@ -104,7 +109,7 @@ bool file_scan(const string &fn, FileScanDo* doer, off_t startoffs,
if (cnttoread != (size_t)-1 && cnttoread) {
doer->init(cnttoread+1, reason);
} else if (st.st_size > 0) {
doer->init(st.st_size+1, reason);
doer->init(size_t(st.st_size+1), reason);
} else {
doer->init(0, reason);
}
@@ -123,13 +128,13 @@ bool file_scan(const string &fn, FileScanDo* doer, off_t startoffs,
for (;;) {
size_t toread = RDBUFSZ;
if (startoffs > 0 && curoffs < startoffs) {
toread = MIN(RDBUFSZ, startoffs - curoffs);
toread = size_t(MIN(RDBUFSZ, startoffs - curoffs));
}
if (cnttoread != size_t(-1)) {
toread = MIN(toread, cnttoread - totread);
}
int n = read(fd, buf, toread);
ssize_t n = static_cast<ssize_t>(read(fd, buf, toread));
if (n < 0) {
catstrerror(reason, "read", errno);
goto out;
+8 -9
View File
@@ -20,7 +20,6 @@
#include <sys/types.h>
#include <string>
using std::string;
/**
* Read file in chunks, calling an accumulator for each chunk. Can be used
@@ -29,23 +28,23 @@ using std::string;
class FileScanDo {
public:
virtual ~FileScanDo() {}
virtual bool init(size_t size, string *reason) = 0;
virtual bool data(const char *buf, int cnt, string* reason) = 0;
virtual bool init(size_t size, std::string *reason) = 0;
virtual bool data(const char *buf, int cnt, std::string* reason) = 0;
};
bool file_scan(const string &filename, FileScanDo* doer, string *reason = 0);
bool file_scan(const std::string &filename, FileScanDo* doer, std::string *reason = 0);
/* Same but only process count cnt from offset offs. Set cnt to size_t(-1)
* for no limit */
bool file_scan(const string &fn, FileScanDo* doer, off_t offs, size_t cnt,
string *reason = 0);
bool file_scan(const std::string &fn, FileScanDo* doer, off_t offs, size_t cnt,
std::string *reason = 0);
/**
* Read file into string.
* @return true for ok, false else
*/
bool file_to_string(const string &filename, string &data, string *reason = 0);
bool file_to_string(const std::string &filename, std::string &data, std::string *reason = 0);
/** Read file chunk into string. Set cnt to size_t(-1) for whole file */
bool file_to_string(const string &filename, string &data,
off_t offs, size_t cnt, string *reason = 0);
bool file_to_string(const std::string &filename, std::string &data,
off_t offs, size_t cnt, std::string *reason = 0);
#endif /* _READFILE_H_INCLUDED_ */
+127 -28
View File
@@ -54,10 +54,10 @@ int stringicmp(const string & s1, const string& s2)
{
string::const_iterator it1 = s1.begin();
string::const_iterator it2 = s2.begin();
int size1 = s1.length(), size2 = s2.length();
string::size_type size1 = s1.length(), size2 = s2.length();
char c1, c2;
if (size1 > size2) {
if (size1 < size2) {
while (it1 != s1.end()) {
c1 = ::toupper(*it1);
c2 = ::toupper(*it2);
@@ -66,7 +66,7 @@ int stringicmp(const string & s1, const string& s2)
}
++it1; ++it2;
}
return size1 == size2 ? 0 : 1;
return size1 == size2 ? 0 : -1;
} else {
while (it2 != s2.end()) {
c1 = ::toupper(*it1);
@@ -76,7 +76,7 @@ int stringicmp(const string & s1, const string& s2)
}
++it1; ++it2;
}
return size1 == size2 ? 0 : -1;
return size1 == size2 ? 0 : 1;
}
}
void stringtolower(string& io)
@@ -114,10 +114,10 @@ int stringlowercmp(const string & s1, const string& s2)
{
string::const_iterator it1 = s1.begin();
string::const_iterator it2 = s2.begin();
int size1 = s1.length(), size2 = s2.length();
string::size_type size1 = s1.length(), size2 = s2.length();
char c2;
if (size1 > size2) {
if (size1 < size2) {
while (it1 != s1.end()) {
c2 = ::tolower(*it2);
if (*it1 != c2) {
@@ -125,7 +125,7 @@ int stringlowercmp(const string & s1, const string& s2)
}
++it1; ++it2;
}
return size1 == size2 ? 0 : 1;
return size1 == size2 ? 0 : -1;
} else {
while (it2 != s2.end()) {
c2 = ::tolower(*it2);
@@ -134,7 +134,7 @@ int stringlowercmp(const string & s1, const string& s2)
}
++it1; ++it2;
}
return size1 == size2 ? 0 : -1;
return size1 == size2 ? 0 : 1;
}
}
@@ -143,10 +143,10 @@ int stringuppercmp(const string & s1, const string& s2)
{
string::const_iterator it1 = s1.begin();
string::const_iterator it2 = s2.begin();
int size1 = s1.length(), size2 = s2.length();
string::size_type size1 = s1.length(), size2 = s2.length();
char c2;
if (size1 > size2) {
if (size1 < size2) {
while (it1 != s1.end()) {
c2 = ::toupper(*it2);
if (*it1 != c2) {
@@ -154,7 +154,7 @@ int stringuppercmp(const string & s1, const string& s2)
}
++it1; ++it2;
}
return size1 == size2 ? 0 : 1;
return size1 == size2 ? 0 : -1;
} else {
while (it2 != s2.end()) {
c2 = ::toupper(*it2);
@@ -163,7 +163,7 @@ int stringuppercmp(const string & s1, const string& s2)
}
++it1; ++it2;
}
return size1 == size2 ? 0 : -1;
return size1 == size2 ? 0 : 1;
}
}
@@ -486,7 +486,7 @@ void utf8truncate(string &s, int maxlen)
if (s.size() <= string::size_type(maxlen))
return;
Utf8Iter iter(s);
int pos = 0;
string::size_type pos = 0;
while (iter++ != string::npos)
if (iter.getBpos() < string::size_type(maxlen))
pos = iter.getBpos();
@@ -637,7 +637,7 @@ string displayableBytes(off_t size)
unit = " GB ";
roundable = double(size) / 1E9;
}
size = round(roundable);
size = off_t(round(roundable));
sprintf(sizebuf, "%lld" "%s", (long long)size, unit);
return string(sizebuf);
}
@@ -708,10 +708,80 @@ typedef int clockid_t;
#undef USE_CLOCK_GETTIME
#endif
#ifdef WIN32
#include "safewindows.h"
// Note: struct timespec is defined by pthread.h (from pthreads-w32)
#ifndef CLOCK_REALTIME
#define CLOCK_REALTIME 0
#endif
LARGE_INTEGER getFILETIMEoffset()
{
SYSTEMTIME s;
FILETIME f;
LARGE_INTEGER t;
s.wYear = 1970;
s.wMonth = 1;
s.wDay = 1;
s.wHour = 0;
s.wMinute = 0;
s.wSecond = 0;
s.wMilliseconds = 0;
SystemTimeToFileTime(&s, &f);
t.QuadPart = f.dwHighDateTime;
t.QuadPart <<= 32;
t.QuadPart |= f.dwLowDateTime;
return (t);
}
int clock_gettime(int X, struct timespec *tv)
{
LARGE_INTEGER t;
FILETIME f;
double microseconds;
static LARGE_INTEGER offset;
static double frequencyToMicroseconds;
static int initialized = 0;
static BOOL usePerformanceCounter = 0;
if (!initialized) {
LARGE_INTEGER performanceFrequency;
initialized = 1;
usePerformanceCounter = QueryPerformanceFrequency(&performanceFrequency);
if (usePerformanceCounter) {
QueryPerformanceCounter(&offset);
frequencyToMicroseconds = (double)performanceFrequency.QuadPart / 1000000.;
}
else {
offset = getFILETIMEoffset();
frequencyToMicroseconds = 10.;
}
}
if (usePerformanceCounter) QueryPerformanceCounter(&t);
else {
GetSystemTimeAsFileTime(&f);
t.QuadPart = f.dwHighDateTime;
t.QuadPart <<= 32;
t.QuadPart |= f.dwLowDateTime;
}
t.QuadPart -= offset.QuadPart;
microseconds = (double)t.QuadPart / frequencyToMicroseconds;
t.QuadPart = (long long)microseconds;
tv->tv_sec = t.QuadPart / 1000000;
tv->tv_nsec = (t.QuadPart % 1000000) * 1000;
return (0);
}
#define USE_CLOCK_GETTIME
#else /* -> !_WIN32 */
#ifndef USE_CLOCK_GETTIME
#include <sys/time.h>
#endif
#endif
static void gettime(clockid_t clk_id, struct timespec *ts)
{
#ifndef USE_CLOCK_GETTIME
@@ -740,29 +810,29 @@ Chrono::Chrono()
}
// Reset and return value before rest in milliseconds
long Chrono::restart()
time_t Chrono::restart()
{
struct timespec tv;
gettime(CLOCK_REALTIME, &tv);
long ret = MILLIS(tv);
time_t ret = MILLIS(tv);
m_secs = tv.tv_sec;
m_nsecs = tv.tv_nsec;
return ret;
}
// Get current timer value, milliseconds
long Chrono::millis(int frozen)
time_t Chrono::millis(int frozen)
{
return nanos() / 1000000;
}
//
long Chrono::micros(int frozen)
time_t Chrono::micros(int frozen)
{
return nanos() / 1000;
}
long long Chrono::nanos(int frozen)
time_t Chrono::nanos(int frozen)
{
if (frozen) {
return NANOS(frozen_tv);
@@ -773,12 +843,12 @@ long long Chrono::nanos(int frozen)
}
}
float Chrono::secs(int frozen)
double Chrono::secs(int frozen)
{
struct timespec tv;
gettime(CLOCK_REALTIME, &tv);
float secs = (float)(frozen?frozen_tv.tv_sec:tv.tv_sec - m_secs);
float nsecs = (float)(frozen?frozen_tv.tv_nsec:tv.tv_nsec - m_nsecs);
double secs = (double)(frozen?frozen_tv.tv_sec:tv.tv_sec - m_secs);
double nsecs = (double)(frozen?frozen_tv.tv_nsec:tv.tv_nsec - m_nsecs);
return secs + nsecs * 1e-9;
}
@@ -818,7 +888,7 @@ static bool parsedate(vector<string>::const_iterator& it,
return false;
}
if (it == end || sscanf(it++->c_str(), "%d", &dip->d1) != 1) {
return -1;
return false;
}
return true;
@@ -1062,7 +1132,7 @@ void catstrerror(string *reason, const char *what, int _errno)
reason->append(" : ");
#ifdef sun
#if defined(sun) || defined(_WIN32)
// Note: sun strerror is noted mt-safe ??
reason->append(strerror(_errno));
#else
@@ -1106,7 +1176,7 @@ void HighlightData::toString(std::string& out)
int(groups.size()), int(grpsugidx.size()), int(ugroups.size()));
out.append(cbuf);
unsigned int ugidx = (unsigned int)-1;
size_t ugidx = (size_t)-1;
for (unsigned int i = 0; i < groups.size(); i++) {
if (ugidx != grpsugidx[i]) {
ugidx = grpsugidx[i];
@@ -1135,7 +1205,7 @@ void HighlightData::append(const HighlightData& hl)
groups.insert(groups.end(), hl.groups.begin(), hl.groups.end());
slacks.insert(slacks.end(), hl.slacks.begin(), hl.slacks.end());
for (std::vector<unsigned int>::const_iterator it = hl.grpsugidx.begin();
for (std::vector<size_t>::const_iterator it = hl.grpsugidx.begin();
it != hl.grpsugidx.end(); it++) {
grpsugidx.push_back(*it + ugsz0);
}
@@ -1271,7 +1341,7 @@ int main(int argc, char **argv)
{
thisprog = *argv++;argc--;
#if 1
#if 0
if (argc <=0 ) {
cerr << "Usage: smallut <stringtosplit>" << endl;
exit(1);
@@ -1376,8 +1446,37 @@ int main(int argc, char **argv)
string out;
stringsToCSV(tokens, out);
cout << "CSV line: [" << out << "]" << endl;
#endif
#elif 1
string sshort("ABC");
string slong("ABCD");
string sshortsmaller("ABB");
vector<pair<string,string> > cmps;
cmps.push_back(pair<string,string>(sshort,sshort));
cmps.push_back(pair<string,string>(sshort,slong));
cmps.push_back(pair<string,string>(slong,sshort));
cmps.push_back(pair<string,string>(sshortsmaller,sshort));
cmps.push_back(pair<string,string>(sshort, sshortsmaller));
for (vector<pair<string,string> >::const_iterator it = cmps.begin();
it != cmps.end(); it++) {
cout << it->first << " " << it->second << " " <<
stringicmp(it->first, it->second) << endl;
}
cout << endl;
for (vector<pair<string,string> >::const_iterator it = cmps.begin();
it != cmps.end(); it++) {
cout << it->first << " " << it->second << " " <<
stringlowercmp(stringtolower(it->first), it->second) << endl;
}
cout << endl;
for (vector<pair<string,string> >::const_iterator it = cmps.begin();
it != cmps.end(); it++) {
cout << it->first << " " << it->second << " " <<
stringuppercmp(it->first, it->second) << endl;
}
#endif
}
#endif
+8 -8
View File
@@ -153,7 +153,7 @@ class Chrono {
public:
Chrono();
/** Reset origin */
long restart();
time_t restart();
/** Snapshot current time */
static void refnow();
/** Get current elapsed since creation or restart
@@ -162,14 +162,14 @@ class Chrono {
* allow for using one actual system call to get values from many
* chrono objects, like when examining timeouts in a queue)
*/
long millis(int frozen = 0);
long ms() {return millis();}
long micros(int frozen = 0);
long long nanos(int frozen = 0);
float secs(int frozen = 0);
time_t millis(int frozen = 0);
time_t ms() {return millis();}
time_t micros(int frozen = 0);
time_t nanos(int frozen = 0);
double secs(int frozen = 0);
private:
long m_secs;
long m_nsecs;
time_t m_secs;
time_t m_nsecs;
};
/** Temp buffer with automatic deallocation */
+32 -2
View File
@@ -19,16 +19,21 @@
#include <stdio.h>
#include <sys/types.h>
#ifdef _WIN32
#include <regex>
#else
#include <regex.h>
#endif
#include <fnmatch.h>
#include <string>
using std::string;
#include "cstr.h"
#include "debuglog.h"
#include "strmatcher.h"
#include "pathut.h"
#include "strmatcher.h"
using namespace std;
bool StrWildMatcher::match(const string& val) const
{
@@ -60,9 +65,25 @@ StrRegexpMatcher::StrRegexpMatcher(const string& exp)
bool StrRegexpMatcher::setExp(const string& exp)
{
if (m_compiled) {
#ifdef _WIN32
delete (regex*)m_compiled;
#else
regfree((regex_t*)m_compiled);
delete (regex_t*)m_compiled;
#endif
}
m_compiled = 0;
#ifdef _WIN32
try {
m_compiled = new regex(exp, std::regex_constants::nosubs |
std::regex_constants::extended);
} catch (...) {
m_reason = string("StrRegexpMatcher:regcomp failed for ")
+ exp + string("syntax error ?");
return false;
}
#else
m_compiled = new regex_t;
if ((m_errcode =
regcomp((regex_t*)m_compiled, exp.c_str(), REG_EXTENDED|REG_NOSUB))) {
@@ -72,6 +93,7 @@ bool StrRegexpMatcher::setExp(const string& exp)
+ exp + string(errbuf);
return false;
}
#endif
m_sexp = exp;
return true;
}
@@ -79,8 +101,12 @@ bool StrRegexpMatcher::setExp(const string& exp)
StrRegexpMatcher::~StrRegexpMatcher()
{
if (m_compiled) {
#ifdef _WIN32
delete (regex *)m_compiled;
#else
regfree((regex_t*)m_compiled);
delete (regex_t*)m_compiled;
#endif
}
}
@@ -88,7 +114,11 @@ bool StrRegexpMatcher::match(const string& val) const
{
if (m_errcode)
return false;
#ifdef _WIN32
return regex_match(val, *((regex *)m_compiled));
#else
return regexec((regex_t*)m_compiled, val.c_str(), 0, 0, 0) != REG_NOMATCH;
#endif
}
string::size_type StrRegexpMatcher::baseprefixlen() const
+383
View File
@@ -0,0 +1,383 @@
#include "autoconfig.h"
#include "execmd.h"
#include <stdio.h>
#include <stdlib.h>
#include "safeunistd.h"
#include <string.h>
#ifndef _WIN32
#include <signal.h>
#endif
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include "debuglog.h"
#include "cancelcheck.h"
#include "execmd.h"
#include "smallut.h"
using namespace std;
// Testing the rclexecm protocol outside of recoll. Here we use the
// rcldoc.py filter, you can try with rclaudio too, adjust the file
// arg accordingly. This simplified driver only really works with
// single-doc files (else it extracts only the first doc, usually the
// empty self-doc).
bool exercise_mhexecm(const string& cmdstr, const string& mimetype,
vector<string>& files)
{
if (files.empty())
return false;
ExecCmd cmd;
vector<string> myparams;
#ifdef _WIN32
// Hack for windows: the command is always "Python somescript"
myparams.push_back(files[0]);
files.erase(files.begin());
#endif
if (cmd.startExec(cmdstr, myparams, 1, 1) < 0) {
cerr << "startExec " << cmdstr << " failed. Missing command?\n";
return false;
}
for (vector<string>::const_iterator it = files.begin();
it != files.end(); it++) {
// Build request message
ostringstream obuf;
obuf << "Filename: " << (*it).length() << "\n" << (*it);
obuf << "Mimetype: " << mimetype.length() << "\n" << mimetype;
// Bogus parameter should be skipped by filter
obuf << "BogusParam: " << string("bogus").length() << "\n" << "bogus";
obuf << "\n";
cerr << "SENDING: [" << obuf.str() << "]\n";
// Send it
if (cmd.send(obuf.str()) < 0) {
// The real code calls zapchild here, but we don't need it as
// this will be handled by ~ExecCmd
//cmd.zapChild();
cerr << "send error\n";
return false;
}
// Read answer
for (int loop=0;;loop++) {
string name, data;
// Code from mh_execm.cpp: readDataElement
string ibuf;
// Read name and length
if (cmd.getline(ibuf) <= 0) {
cerr << "getline error\n";
return false;
}
// Empty line (end of message)
if (!ibuf.compare("\n")) {
cerr << "Got empty line\n";
name.clear();
break;
}
// Filters will sometimes abort before entering the real
// protocol, ie if a module can't be loaded. Check the
// special filter error first word:
if (ibuf.find("RECFILTERROR ") == 0) {
cerr << "Got RECFILTERROR\n";
return false;
}
// We're expecting something like Name: len\n
vector<string> tokens;
stringToTokens(ibuf, tokens);
if (tokens.size() != 2) {
cerr << "bad line in filter output: [" << ibuf << "]\n";
return false;
}
vector<string>::iterator it = tokens.begin();
name = *it++;
string& slen = *it;
int len;
if (sscanf(slen.c_str(), "%d", &len) != 1) {
cerr << "bad line in filter output (no len): [" <<
ibuf << "]\n";
return false;
}
// Read element data
data.erase();
if (len > 0 && cmd.receive(data, len) != len) {
cerr << "MHExecMultiple: expected " << len <<
" bytes of data, got " << data.length() << endl;
return false;
}
// Empty element: end of message
if (name.empty())
break;
cerr << "Got name: [" << name << "] data [" << data << "]\n";
}
}
return true;
}
static char *thisprog;
static char usage [] =
"trexecmd [-c -r -i -o] [-e <fn>] cmd [arg1 arg2 ...]\n"
" -c : test cancellation (ie: trexecmd -c sleep 1000)\n"
" -r : run reexec. Must be separate option.\n"
" -i : command takes input\n"
" -o : command produces output\n"
" -e <fn> : send stderr to file named fn (will truncate it)\n"
" If -i is set, we send /etc/group contents to whatever command is run\n"
" If -o is set, we print whatever comes out\n"
"trexecmd -f bogus filter for testing. Uses same options\n"
"trexecmd -m <filter> <mimetype> <file> [file ...]: test execm:\n"
" <filter> should be the path to an execm filter\n"
" <mimetype> the type of the file parameters\n"
"trexecmd -w cmd : do the 'which' thing\n"
;
static void Usage(FILE *fp = stderr)
{
fprintf(fp, "%s: usage:\n%s", thisprog, usage);
exit(1);
}
static int op_flags;
#define OPT_MOINS 0x1
#define OPT_i 0x4
#define OPT_w 0x8
#define OPT_c 0x10
#define OPT_r 0x20
#define OPT_m 0x40
#define OPT_o 0x80
#define OPT_e 0x100
#define OPT_f 0x200
void childfilter()
{
const int bs = 1024;
char buf[bs];
if (op_flags & OPT_c)
sleep(2000);
if (op_flags& OPT_i) {
while (read(0, buf, bs) > 0);
}
if (op_flags& OPT_o) {
for (int i = 0; i < 10; i++) {
printf("This is DATA 1 2 3\n");
}
}
exit(0);
}
// Data sink for data coming out of the command. We also use it to set
// a cancellation after a moment.
class MEAdv : public ExecCmdAdvise {
public:
void newData(int cnt) {
cerr << "newData(" << cnt << ")" << endl;
if (op_flags & OPT_c) {
static int callcnt;
if (callcnt++ == 5) {
// Just sets the cancellation flag
CancelCheck::instance().setCancel();
// Would be called from somewhere else and throws an
// exception. We call it here for simplicity
cerr << "newData: should throw !\n";
CancelCheck::instance().checkCancel();
}
}
}
};
// Data provider, used if the -i flag is set
class MEPv : public ExecCmdProvide {
public:
string *m_input;
int m_cnt;
MEPv(string *i)
: m_input(i), m_cnt(0) {
}
~MEPv() {
}
void newData() {
if (m_cnt++ < 10) {
char num[30];
sprintf(num, "%d", m_cnt);
*m_input = string("This is an input chunk ") + string(num) +
string("\n");
} else {
m_input->erase();
}
}
void reset() {
m_cnt = 0;
}
};
ReExec reexec;
int main(int argc, char *argv[])
{
#ifndef _WIN32
reexec.init(argc, argv);
if (0) {
// Disabled: For testing reexec arg handling
vector<string> newargs;
newargs.push_back("newarg");
newargs.push_back("newarg1");
newargs.push_back("newarg2");
newargs.push_back("newarg3");
newargs.push_back("newarg4");
reexec.insertArgs(newargs, 2);
}
#endif
string stderrFile;
thisprog = argv[0];
argc--; argv++;
while (argc > 0 && **argv == '-') {
(*argv)++;
if (!(**argv))
/* Cas du "adb - core" */
Usage();
while (**argv)
switch (*(*argv)++) {
case 'c': op_flags |= OPT_c; break;
case 'e':
op_flags |= OPT_e;
if (argc < 2) {
Usage();
}
stderrFile = *(++argv); argc--;
goto b1;
case 'f': op_flags |= OPT_f; break;
case 'h':
for (int i = 0; i < 10; i++) {
cout << "MESSAGE " << i << " FROM TREXECMD\n";
cout.flush();
//sleep(1);
}
return 0;
case 'i': op_flags |= OPT_i; break;
case 'o': op_flags |= OPT_o; break;
case 'm': op_flags |= OPT_m; break;
case 'r': op_flags |= OPT_r; break;
case 'w': op_flags |= OPT_w; break;
default: Usage(); break;
}
b1: argc--; argv++;
}
if (op_flags & OPT_f) {
childfilter();
}
if (argc < 1)
Usage();
string arg1 = *argv++; argc--;
vector<string> l;
while (argc > 0) {
l.push_back(*argv++); argc--;
}
DebugLog::getdbl()->setloglevel(DEBDEB1);
DebugLog::setfilename("stderr");
#ifndef _WIN32
signal(SIGPIPE, SIG_IGN);
if (op_flags & OPT_r) {
// Test reexec. Normally only once, next time we fall through
// because we remove the -r option (only works if it was
// isolated, not like -rc
chdir("/");
argv[0] = strdup("");
sleep(1);
cerr << "Calling reexec\n";
// We remove the -r arg from list, otherwise we are going to
// loop (which you can try by commenting out the following
// line)
reexec.removeArg("-r");
reexec.reexec();
}
#endif
if (op_flags & OPT_w) {
// Test "which" method
string path;
if (ExecCmd::which(arg1, path)) {
cout << path << endl;
return 0;
}
return 1;
} else if (op_flags & OPT_m) {
if (l.size() < 2)
Usage();
string mimetype = l[0];
l.erase(l.begin());
return exercise_mhexecm(arg1, mimetype, l) ? 0 : 1;
} else {
// Default: execute command line arguments
ExecCmd mexec;
// Set callback to be called whenever there is new data
// available and at a periodic interval, to check for
// cancellation
MEAdv adv;
mexec.setAdvise(&adv);
//mexec.setTimeout(5);
// Stderr output goes there
if (!stderrFile.empty())
mexec.setStderr(stderrFile);
// A few environment variables. Check with trexecmd env
mexec.putenv("TESTVARIABLE1=TESTVALUE1");
mexec.putenv("TESTVARIABLE2=TESTVALUE2");
mexec.putenv("TESTVARIABLE3=TESTVALUE3");
string input, output;
MEPv pv(&input);
string *ip = 0;
if (op_flags & OPT_i) {
ip = &input;
mexec.setProvide(&pv);
}
string *op = 0;
if (op_flags & OPT_o) {
op = &output;
}
int status = -1;
for (int i = 0; i < 10; i++) {
output.clear();
pv.reset();
try {
status = mexec.doexec(arg1, l, ip, op);
} catch (CancelExcept) {
cerr << "CANCELLED" << endl;
}
//fprintf(stderr, "Status: 0x%x\n", status);
if (op_flags & OPT_o) {
cout << "data received: [" << output << "]\n";
cerr << "iter " << i << " status " <<
status << " bytes received " << output.size() << endl;
}
if (status)
break;
}
return status >> 8;
}
}
+1 -1
View File
@@ -50,7 +50,7 @@ public:
/** "Direct" access. Awfully inefficient as we skip from start or current
* position at best. This can only be useful for a lookahead from the
* current position */
unsigned int operator[](unsigned int charpos) const
unsigned int operator[](std::string::size_type charpos) const
{
std::string::size_type mypos = 0;
unsigned int mycp = 0;
+1 -6
View File
@@ -21,12 +21,7 @@
#include <errno.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
static inline unsigned int sleep(unsigned int s) {Sleep(s * 1000); return 0;}
#else
#include <unistd.h>
#endif
#include "safeunistd.h"
#include "workqueue.h"