Parent: [f6a999] (diff)

Child: [d69d2a] (diff)

Download this file

uncomp.cpp    137 lines (119 with data), 4.1 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
130
131
132
133
134
/* Copyright (C) 2013 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 <errno.h>
#include <string>
#include <vector>
#include <map>
#include "uncomp.h"
#include "log.h"
#include "smallut.h"
#include "execmd.h"
#include "pathut.h"
using std::map;
using std::string;
using std::vector;
Uncomp::UncompCache Uncomp::o_cache;
bool Uncomp::uncompressfile(const string& ifn,
const vector<string>& cmdv, string& tfile)
{
if (m_docache) {
std::unique_lock<std::mutex> lock(o_cache.m_lock);
if (!o_cache.m_srcpath.compare(ifn)) {
m_dir = o_cache.m_dir;
m_tfile = tfile = o_cache.m_tfile;
m_srcpath = ifn;
o_cache.m_dir = 0;
o_cache.m_srcpath.clear();
return true;
}
}
m_srcpath.clear();
m_tfile.clear();
if (m_dir == 0) {
m_dir = new TempDir;
}
// Make sure tmp dir is empty. we guarantee this to filters
if (!m_dir || !m_dir->ok() || !m_dir->wipe()) {
LOGERR("uncompressfile: can't clear temp dir " << (m_dir->dirname()) << "\n" );
return false;
}
// Check that we have enough available space to have some hope of
// decompressing the file.
int pc;
long long availmbs;
if (!fsocc(m_dir->dirname(), &pc, &availmbs)) {
LOGERR("uncompressfile: can't retrieve avail space for " << (m_dir->dirname()) << "\n" );
// Hope for the best
} else {
long long fsize = path_filesize(ifn);
if (fsize < 0) {
LOGERR("uncompressfile: stat input file " << (ifn) << " errno " << (errno) << "\n" );
return false;
}
// We need at least twice the file size for the uncompressed
// and compressed versions. Most compressors don't store the
// uncompressed size, so we have no way to be sure that we
// have enough space before trying. We take a little margin
// use same Mb def as fsocc()
long long filembs = fsize / (1024 * 1024);
if (availmbs < 2 * filembs + 1) {
LOGERR("uncompressfile. " << (lltodecstr(availmbs)) << " MBs available in " << (m_dir->dirname()) << " not enough to uncompress " << (ifn) << " of size " << (lltodecstr(filembs)) << " mbs\n" );
return false;
}
}
string cmd = cmdv.front();
// Substitute file name and temp dir in command elements
vector<string>::const_iterator it = cmdv.begin();
++it;
vector<string> args;
map<char, string> subs;
subs['f'] = ifn;
subs['t'] = m_dir->dirname();
for (; it != cmdv.end(); it++) {
string ns;
pcSubst(*it, ns, subs);
args.push_back(ns);
}
// Execute command and retrieve output file name, check that it exists
ExecCmd ex;
int status = ex.doexec(cmd, args, 0, &tfile);
if (status || tfile.empty()) {
LOGERR("uncompressfile: doexec: failed for [" << (ifn) << "] status 0x" << (status) << "\n" );
if (!m_dir->wipe()) {
LOGERR("uncompressfile: wipedir failed\n" );
}
return false;
}
if (tfile[tfile.length() - 1] == '\n')
tfile.erase(tfile.length() - 1, 1);
m_tfile = tfile;
m_srcpath = ifn;
return true;
}
Uncomp::~Uncomp()
{
if (m_docache) {
std::unique_lock<std::mutex> lock(o_cache.m_lock);
delete o_cache.m_dir;
o_cache.m_dir = m_dir;
o_cache.m_tfile = m_tfile;
o_cache.m_srcpath = m_srcpath;
} else {
delete m_dir;
}
}