Child: [f6a999] (diff)

Download this file

exefetcher.cpp    130 lines (116 with data), 4.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* Copyright (C) 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
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "autoconfig.h"
#include <string>
#include <vector>
#include <iostream>
#include "exefetcher.h"
#include "debuglog.h"
#include "pathut.h"
#include "rclconfig.h"
#include "execmd.h"
#include "rcldoc.h"
using namespace std;
class EXEDocFetcher::Internal {
public:
string bckid;
vector<string> sfetch;
vector<string> smkid;
bool docmd(const vector<string>& cmd, const Rcl::Doc& idoc, string& out) {
ExecCmd ecmd;
// We're always called for preview (or Open)
ecmd.putenv("RECOLL_FILTER_FORPREVIEW=yes");
string udi;
idoc.getmeta(Rcl::Doc::keyudi, &udi);
vector<string> args(cmd);
args.push_back(udi);
args.push_back(idoc.url);
args.push_back(idoc.ipath);
int status = ecmd.doexec1(args, 0, &out);
if (status == 0) {
LOGDEB(("EXEDocFetcher::Internal: got [%s]\n", out.c_str()));
return true;
} else {
LOGERR(("EXEDOcFetcher::fetch: %s: %s failed for %s %s %s 0x%u\n",
bckid.c_str(), stringsToString(cmd).c_str(), udi.c_str(),
idoc.url.c_str(), idoc.ipath.c_str()));
return false;
}
}
};
EXEDocFetcher::EXEDocFetcher(const EXEDocFetcher::Internal& _m)
{
m = new Internal(_m);
LOGDEB(("EXEDocFetcher::EXEDocFetcher: fetch is %s\n",
stringsToString(m->sfetch).c_str()));
}
bool EXEDocFetcher::fetch(RclConfig* cnf, const Rcl::Doc& idoc, RawDoc& out)
{
out.kind = RawDoc::RDK_DATADIRECT;
return m->docmd(m->sfetch, idoc, out.data);
}
bool EXEDocFetcher::makesig(RclConfig* cnf, const Rcl::Doc& idoc, string& sig)
{
return m->docmd(m->smkid, idoc, sig);
}
// Lookup bckid in the config and create an appropriate fetcher.
EXEDocFetcher *exeDocFetcherMake(RclConfig *config, const string& bckid)
{
EXEDocFetcher *fetcher = 0;
// The config we only read once, not gonna change.
static ConfSimple *bconf;
if (!bconf) {
string bconfname = path_cat(config->getConfDir(), "backends");
LOGDEB(("exeDocFetcherMake: using config in %s\n", bconfname.c_str()));
bconf = new ConfSimple(bconfname.c_str(), true);
if (!bconf->ok()) {
delete bconf;
bconf = 0;
LOGDEB(("exeDocFetcherMake: bad/no config: %s\n",
bconfname.c_str()));
return 0;
}
}
EXEDocFetcher::Internal m;
m.bckid = bckid;
string sfetch;
if (!bconf->get("fetch", sfetch, bckid) || sfetch.empty()) {
LOGERR(("exeDocFetcherMake: no 'fetch' for [%s]\n", bckid.c_str()));
return 0;
}
stringToStrings(sfetch, m.sfetch);
// We look up the command as we do for filters for now
m.sfetch[0] = config->findFilter(m.sfetch[0]);
if (!path_isabsolute(m.sfetch[0])) {
LOGERR(("exeDocFetcherMake: %s not found in exec path or filters dir\n",
m.sfetch[0].c_str()));
return 0;
}
string smkid;
if (!bconf->get("makesig", smkid, bckid) || smkid.empty()) {
LOGDEB(("exeDocFetcherMake: no 'makesig' for [%s]\n", bckid.c_str()));
return 0;
}
stringToStrings(smkid, m.smkid);
m.smkid[0] = config->findFilter(m.smkid[0]);
if (!path_isabsolute(m.smkid[0])) {
LOGERR(("exeDocFetcherMake: %s not found in exec path or filters dir\n",
m.smkid[0].c_str()));
return 0;
}
return new EXEDocFetcher(m);
}