|
a |
|
b/src/internfile/uncomp.cpp |
|
|
1 |
/* Copyright (C) 2013 J.F.Dockes
|
|
|
2 |
* This program is free software; you can redistribute it and/or modify
|
|
|
3 |
* it under the terms of the GNU General Public License as published by
|
|
|
4 |
* the Free Software Foundation; either version 2 of the License, or
|
|
|
5 |
* (at your option) any later version.
|
|
|
6 |
*
|
|
|
7 |
* This program is distributed in the hope that it will be useful,
|
|
|
8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
10 |
* GNU General Public License for more details.
|
|
|
11 |
*
|
|
|
12 |
* You should have received a copy of the GNU General Public License
|
|
|
13 |
* along with this program; if not, write to the
|
|
|
14 |
* Free Software Foundation, Inc.,
|
|
|
15 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
16 |
*/
|
|
|
17 |
|
|
|
18 |
#include "autoconfig.h"
|
|
|
19 |
|
|
|
20 |
#include <string>
|
|
|
21 |
#include <vector>
|
|
|
22 |
#include <map>
|
|
|
23 |
using std::map;
|
|
|
24 |
using std::string;
|
|
|
25 |
using std::vector;
|
|
|
26 |
|
|
|
27 |
#include "uncomp.h"
|
|
|
28 |
#include "debuglog.h"
|
|
|
29 |
#include "smallut.h"
|
|
|
30 |
#include "execmd.h"
|
|
|
31 |
|
|
|
32 |
Uncomp::UncompCache Uncomp::o_cache;
|
|
|
33 |
|
|
|
34 |
bool Uncomp::uncompressfile(const string& ifn,
|
|
|
35 |
const vector<string>& cmdv, string& tfile)
|
|
|
36 |
{
|
|
|
37 |
if (m_docache) {
|
|
|
38 |
PTMutexLocker lock(o_cache.m_lock);
|
|
|
39 |
if (!o_cache.m_srcpath.compare(ifn)) {
|
|
|
40 |
m_dir = o_cache.m_dir;
|
|
|
41 |
m_tfile = tfile = o_cache.m_tfile;
|
|
|
42 |
m_srcpath = ifn;
|
|
|
43 |
o_cache.m_dir = 0;
|
|
|
44 |
o_cache.m_srcpath.clear();
|
|
|
45 |
return true;
|
|
|
46 |
}
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
m_srcpath.clear();
|
|
|
50 |
m_tfile.clear();
|
|
|
51 |
if (m_dir == 0) {
|
|
|
52 |
m_dir = new TempDir;
|
|
|
53 |
}
|
|
|
54 |
// Make sure tmp dir is empty. we guarantee this to filters
|
|
|
55 |
if (!m_dir || !m_dir->ok() || !m_dir->wipe()) {
|
|
|
56 |
LOGERR(("uncompressfile: can't clear temp dir %s\n", m_dir->dirname()));
|
|
|
57 |
return false;
|
|
|
58 |
}
|
|
|
59 |
string cmd = cmdv.front();
|
|
|
60 |
|
|
|
61 |
// Substitute file name and temp dir in command elements
|
|
|
62 |
vector<string>::const_iterator it = cmdv.begin();
|
|
|
63 |
++it;
|
|
|
64 |
vector<string> args;
|
|
|
65 |
map<char, string> subs;
|
|
|
66 |
subs['f'] = ifn;
|
|
|
67 |
subs['t'] = m_dir->dirname();
|
|
|
68 |
for (; it != cmdv.end(); it++) {
|
|
|
69 |
string ns;
|
|
|
70 |
pcSubst(*it, ns, subs);
|
|
|
71 |
args.push_back(ns);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
// Execute command and retrieve output file name, check that it exists
|
|
|
75 |
ExecCmd ex;
|
|
|
76 |
int status = ex.doexec(cmd, args, 0, &tfile);
|
|
|
77 |
if (status || tfile.empty()) {
|
|
|
78 |
LOGERR(("uncompressfile: doexec: failed for [%s] status 0x%x\n",
|
|
|
79 |
ifn.c_str(), status));
|
|
|
80 |
if (!m_dir->wipe()) {
|
|
|
81 |
LOGERR(("uncompressfile: wipedir failed\n"));
|
|
|
82 |
}
|
|
|
83 |
return false;
|
|
|
84 |
}
|
|
|
85 |
if (tfile[tfile.length() - 1] == '\n')
|
|
|
86 |
tfile.erase(tfile.length() - 1, 1);
|
|
|
87 |
m_tfile = tfile;
|
|
|
88 |
m_srcpath = ifn;
|
|
|
89 |
return true;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
Uncomp::~Uncomp()
|
|
|
93 |
{
|
|
|
94 |
if (m_docache) {
|
|
|
95 |
PTMutexLocker lock(o_cache.m_lock);
|
|
|
96 |
delete o_cache.m_dir;
|
|
|
97 |
o_cache.m_dir = m_dir;
|
|
|
98 |
o_cache.m_tfile = m_tfile;
|
|
|
99 |
o_cache.m_srcpath = m_srcpath;
|
|
|
100 |
} else {
|
|
|
101 |
delete m_dir;
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
|