|
a/src/readfile.cpp |
|
b/src/readfile.cpp |
|
... |
|
... |
12 |
* You should have received a copy of the GNU Lesser General Public License
|
12 |
* You should have received a copy of the GNU Lesser General Public License
|
13 |
* along with this program; if not, write to the
|
13 |
* along with this program; if not, write to the
|
14 |
* Free Software Foundation, Inc.,
|
14 |
* Free Software Foundation, Inc.,
|
15 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
15 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
16 |
*/
|
16 |
*/
|
17 |
#ifndef TEST_READFILE
|
|
|
18 |
#ifdef BUILDING_RECOLL
|
17 |
#ifdef BUILDING_RECOLL
|
19 |
#include "autoconfig.h"
|
18 |
#include "autoconfig.h"
|
20 |
#else
|
19 |
#else
|
21 |
#include "config.h"
|
20 |
#include "config.h"
|
22 |
#endif
|
21 |
#endif
|
23 |
|
22 |
|
|
|
23 |
#include "readfile.h"
|
|
|
24 |
|
24 |
#include <errno.h>
|
25 |
#include <errno.h>
|
25 |
#include <sys/types.h>
|
26 |
#include <sys/types.h>
|
|
|
27 |
|
26 |
#ifdef _WIN32
|
28 |
#ifdef _WIN32
|
27 |
#include "safefcntl.h"
|
29 |
#include "safefcntl.h"
|
28 |
#include "safesysstat.h"
|
30 |
#include "safesysstat.h"
|
29 |
#include "safeunistd.h"
|
31 |
#include "safeunistd.h"
|
|
|
32 |
#include "transcode.h"
|
|
|
33 |
#define OPEN _wopen
|
|
|
34 |
|
30 |
#else
|
35 |
#else
|
31 |
#define O_BINARY 0
|
36 |
#define O_BINARY 0
|
32 |
#include <fcntl.h>
|
37 |
#include <fcntl.h>
|
33 |
#include <sys/stat.h>
|
38 |
#include <sys/stat.h>
|
34 |
#include <unistd.h>
|
39 |
#include <unistd.h>
|
|
|
40 |
#define OPEN open
|
|
|
41 |
|
35 |
#endif
|
42 |
#endif
|
|
|
43 |
|
36 |
#include <string>
|
44 |
#include <string>
|
37 |
|
45 |
|
38 |
#include "readfile.h"
|
|
|
39 |
#include "smallut.h"
|
46 |
#include "smallut.h"
|
|
|
47 |
#include "pathut.h"
|
40 |
|
48 |
|
41 |
using std::string;
|
49 |
#ifdef READFILE_ENABLE_MD5
|
|
|
50 |
#include "md5.h"
|
|
|
51 |
#endif
|
42 |
|
52 |
|
|
|
53 |
#ifdef MDU_INCLUDE_LOG
|
|
|
54 |
#include MDU_INCLUDE_LOG
|
|
|
55 |
#else
|
|
|
56 |
#include "log.h"
|
|
|
57 |
#endif
|
|
|
58 |
|
|
|
59 |
using namespace std;
|
|
|
60 |
|
|
|
61 |
///////////////
|
|
|
62 |
// Implementation of basic interface: read whole file to memory buffer
|
43 |
class FileToString : public FileScanDo {
|
63 |
class FileToString : public FileScanDo {
|
44 |
public:
|
64 |
public:
|
45 |
FileToString(string& data) : m_data(data) {}
|
65 |
FileToString(string& data) : m_data(data) {}
|
46 |
string& m_data;
|
66 |
|
|
|
67 |
// Note: the fstat() + reserve() (in init()) calls divide cpu
|
|
|
68 |
// usage almost by 2 on both linux i586 and macosx (compared to
|
|
|
69 |
// just append()) Also tried a version with mmap, but it's
|
|
|
70 |
// actually slower on the mac and not faster on linux.
|
47 |
bool init(size_t size, string *reason) {
|
71 |
virtual bool init(int64_t size, string *reason) {
|
48 |
if (size > 0) {
|
72 |
if (size > 0) {
|
49 |
m_data.reserve(size);
|
73 |
m_data.reserve(size);
|
50 |
}
|
74 |
}
|
51 |
return true;
|
75 |
return true;
|
52 |
}
|
76 |
}
|
53 |
bool data(const char *buf, int cnt, string *reason) {
|
77 |
virtual bool data(const char *buf, int cnt, string *reason) {
|
54 |
try {
|
78 |
try {
|
55 |
m_data.append(buf, cnt);
|
79 |
m_data.append(buf, cnt);
|
56 |
} catch (...) {
|
80 |
} catch (...) {
|
57 |
catstrerror(reason, "append", errno);
|
81 |
catstrerror(reason, "append", errno);
|
58 |
return false;
|
82 |
return false;
|
59 |
}
|
83 |
}
|
60 |
return true;
|
84 |
return true;
|
61 |
}
|
85 |
}
|
|
|
86 |
|
|
|
87 |
string& m_data;
|
|
|
88 |
};
|
|
|
89 |
|
|
|
90 |
bool file_to_string(const string& fn, string& data, int64_t offs, size_t cnt,
|
|
|
91 |
string *reason)
|
|
|
92 |
{
|
|
|
93 |
FileToString accum(data);
|
|
|
94 |
return file_scan(fn, &accum, offs, cnt, reason
|
|
|
95 |
#ifdef READFILE_ENABLE_MD5
|
|
|
96 |
, nullptr
|
|
|
97 |
#endif
|
|
|
98 |
);
|
62 |
};
|
99 |
}
|
63 |
|
100 |
|
64 |
bool file_to_string(const string& fn, string& data, string *reason)
|
101 |
bool file_to_string(const string& fn, string& data, string *reason)
|
65 |
{
|
102 |
{
|
66 |
return file_to_string(fn, data, 0, size_t(-1), reason);
|
103 |
return file_to_string(fn, data, 0, size_t(-1), reason);
|
67 |
}
|
104 |
}
|
68 |
bool file_to_string(const string& fn, string& data, int64_t offs, size_t cnt,
|
105 |
|
|
|
106 |
|
|
|
107 |
/////////////
|
|
|
108 |
// Callback/filtering interface
|
|
|
109 |
|
|
|
110 |
// Abstract class base for both source (origin) and filter
|
|
|
111 |
// (midstream). Both have a downstream
|
|
|
112 |
class FileScanUpstream {
|
|
|
113 |
public:
|
|
|
114 |
virtual void setDownstream(FileScanDo *down) {
|
|
|
115 |
m_down = down;
|
|
|
116 |
}
|
|
|
117 |
virtual FileScanDo *out() {
|
|
|
118 |
return m_down;
|
|
|
119 |
}
|
|
|
120 |
protected:
|
|
|
121 |
FileScanDo *m_down{nullptr};
|
|
|
122 |
};
|
|
|
123 |
|
|
|
124 |
// Source element.
|
|
|
125 |
class FileScanSource : public FileScanUpstream {
|
|
|
126 |
public:
|
|
|
127 |
FileScanSource(FileScanDo *down) {
|
|
|
128 |
setDownstream(down);
|
|
|
129 |
}
|
|
|
130 |
virtual bool scan() = 0;
|
|
|
131 |
};
|
|
|
132 |
|
|
|
133 |
// Inside element of a transformation pipe. The idea is that elements
|
|
|
134 |
// which don't recognize the data get themselves out of the pipe
|
|
|
135 |
// (pop()). Typically, only one of the decompression modules
|
|
|
136 |
// (e.g. gzip/bzip2/xz...) would remain. For now there is only gzip,
|
|
|
137 |
// it pops itself if the data does not have the right magic number
|
|
|
138 |
class FileScanFilter : public FileScanDo, public FileScanUpstream {
|
|
|
139 |
public:
|
|
|
140 |
virtual void insertAtSink(FileScanDo *sink, FileScanUpstream *upstream) {
|
|
|
141 |
setDownstream(sink);
|
|
|
142 |
if (m_down) {
|
|
|
143 |
m_down->setUpstream(this);
|
|
|
144 |
}
|
|
|
145 |
setUpstream(upstream);
|
|
|
146 |
if (m_up) {
|
|
|
147 |
m_up->setDownstream(this);
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
// Remove myself from the pipe.
|
|
|
152 |
virtual void pop() {
|
|
|
153 |
if (m_down) {
|
|
|
154 |
m_down->setUpstream(m_up);
|
|
|
155 |
}
|
|
|
156 |
if (m_up) {
|
|
|
157 |
m_up->setDownstream(m_down);
|
|
|
158 |
}
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
virtual void setUpstream(FileScanUpstream *up) override {
|
|
|
162 |
m_up = up;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
private:
|
|
|
166 |
FileScanUpstream *m_up{nullptr};
|
|
|
167 |
};
|
|
|
168 |
|
|
|
169 |
|
|
|
170 |
#if defined(READFILE_ENABLE_ZLIB)
|
|
|
171 |
#include <zlib.h>
|
|
|
172 |
|
|
|
173 |
class GzFilter : public FileScanFilter {
|
|
|
174 |
public:
|
|
|
175 |
virtual ~GzFilter() {
|
|
|
176 |
if (m_initdone) {
|
|
|
177 |
inflateEnd(&m_stream);
|
|
|
178 |
}
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
virtual bool init(int64_t size, string *reason) override {
|
|
|
182 |
LOGDEB1("GzFilter::init\n");
|
|
|
183 |
if (out()) {
|
|
|
184 |
return out()->init(size, reason);
|
|
|
185 |
}
|
|
|
186 |
return true;
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
virtual bool data(const char *buf, int cnt, string *reason) override {
|
|
|
190 |
LOGDEB1("GzFilter::data: cnt " << cnt << endl);
|
|
|
191 |
|
|
|
192 |
int error;
|
|
|
193 |
m_stream.next_in = (Bytef*)buf;
|
|
|
194 |
m_stream.avail_in = cnt;
|
|
|
195 |
|
|
|
196 |
if (m_initdone == false) {
|
|
|
197 |
// We do not support a first read cnt < 2. This quite
|
|
|
198 |
// probably can't happen with a compressed file (size>2)
|
|
|
199 |
// except if we're reading a tty which is improbable. So
|
|
|
200 |
// assume this is a regular file.
|
|
|
201 |
const unsigned char *ubuf = (const unsigned char *)buf;
|
|
|
202 |
if ((cnt < 2) || ubuf[0] != 0x1f || ubuf[1] != 0x8b) {
|
|
|
203 |
LOGDEB1("GzFilter::data: not gzip. out() is " << out() << "\n");
|
|
|
204 |
pop();
|
|
|
205 |
if (out()) {
|
|
|
206 |
return out()->data(buf, cnt, reason);
|
|
|
207 |
} else {
|
|
|
208 |
return false;
|
|
|
209 |
}
|
|
|
210 |
}
|
|
|
211 |
m_stream.opaque = nullptr;
|
|
|
212 |
m_stream.zalloc = alloc_func;
|
|
|
213 |
m_stream.zfree = free_func;
|
|
|
214 |
m_stream.next_out = (Bytef*)m_obuf;
|
|
|
215 |
m_stream.avail_out = m_obs;
|
|
|
216 |
if ((error = inflateInit2(&m_stream, 15+32)) != Z_OK) {
|
|
|
217 |
LOGERR("inflateInit2 error: " << error << endl);
|
|
|
218 |
if (reason) {
|
|
|
219 |
*reason += " Zlib inflateinit failed";
|
|
|
220 |
if (m_stream.msg && *m_stream.msg) {
|
|
|
221 |
*reason += string(": ") + m_stream.msg;
|
|
|
222 |
}
|
|
|
223 |
}
|
|
|
224 |
return false;
|
|
|
225 |
}
|
|
|
226 |
m_initdone = true;
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
while (m_stream.avail_in != 0) {
|
|
|
230 |
m_stream.next_out = (Bytef*)m_obuf;
|
|
|
231 |
m_stream.avail_out = m_obs;
|
|
|
232 |
if ((error = inflate(&m_stream, Z_SYNC_FLUSH)) < Z_OK) {
|
|
|
233 |
LOGERR("inflate error: " << error << endl);
|
|
|
234 |
if (reason) {
|
|
|
235 |
*reason += " Zlib inflate failed";
|
|
|
236 |
if (m_stream.msg && *m_stream.msg) {
|
|
|
237 |
*reason += string(": ") + m_stream.msg;
|
|
|
238 |
}
|
|
|
239 |
}
|
|
|
240 |
return false;
|
|
|
241 |
}
|
|
|
242 |
if (out() &&
|
|
|
243 |
!out()->data(m_obuf, m_obs - m_stream.avail_out, reason)) {
|
|
|
244 |
return false;
|
|
|
245 |
}
|
|
|
246 |
}
|
|
|
247 |
return true;
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
static voidpf alloc_func(voidpf opaque, uInt items, uInt size) {
|
|
|
251 |
return malloc(items * size);
|
|
|
252 |
}
|
|
|
253 |
static void free_func(voidpf opaque, voidpf address) {
|
|
|
254 |
free(address);
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
bool m_initdone{false};
|
|
|
258 |
z_stream m_stream;
|
|
|
259 |
char m_obuf[10000];
|
|
|
260 |
const int m_obs{10000};
|
|
|
261 |
};
|
|
|
262 |
#endif // GZ
|
|
|
263 |
|
|
|
264 |
#ifdef READFILE_ENABLE_MD5
|
|
|
265 |
|
|
|
266 |
class FileScanMd5 : public FileScanFilter {
|
|
|
267 |
public:
|
|
|
268 |
FileScanMd5(string& d) : digest(d) {}
|
|
|
269 |
virtual bool init(int64_t size, string *reason) override {
|
|
|
270 |
LOGDEB1("FileScanMd5: init\n");
|
|
|
271 |
MD5Init(&ctx);
|
|
|
272 |
if (out()) {
|
|
|
273 |
return out()->init(size, reason);
|
|
|
274 |
}
|
|
|
275 |
return true;
|
|
|
276 |
}
|
|
|
277 |
virtual bool data(const char *buf, int cnt, string *reason) override {
|
|
|
278 |
LOGDEB1("FileScanMd5: data. cnt " << cnt << endl);
|
|
|
279 |
MD5Update(&ctx, (const unsigned char*)buf, cnt);
|
|
|
280 |
if (out() && !out()->data(buf, cnt, reason)) {
|
|
|
281 |
return false;
|
|
|
282 |
}
|
|
|
283 |
return true;
|
|
|
284 |
}
|
|
|
285 |
bool finish() {
|
|
|
286 |
LOGDEB1("FileScanMd5: finish\n");
|
|
|
287 |
MD5Final(digest, &ctx);
|
|
|
288 |
return true;
|
|
|
289 |
}
|
|
|
290 |
string &digest;
|
|
|
291 |
MD5_CTX ctx;
|
|
|
292 |
};
|
|
|
293 |
#endif // MD5
|
|
|
294 |
|
|
|
295 |
// Source taking data from a regular file
|
|
|
296 |
class FileScanSourceFile : public FileScanSource {
|
|
|
297 |
public:
|
|
|
298 |
FileScanSourceFile(FileScanDo *next, const string& fn, int64_t startoffs,
|
69 |
string *reason)
|
299 |
int64_t cnttoread, string *reason)
|
|
|
300 |
: FileScanSource(next), m_fn(fn), m_startoffs(startoffs),
|
|
|
301 |
m_cnttoread(cnttoread), m_reason(reason) { }
|
|
|
302 |
|
|
|
303 |
virtual bool scan() {
|
|
|
304 |
LOGDEB1("FileScanSourceFile: reading " << m_fn << " offs " <<
|
|
|
305 |
m_startoffs<< " cnt " << m_cnttoread << " out " << out() << endl);
|
|
|
306 |
const int RDBUFSZ = 8192;
|
|
|
307 |
bool ret = false;
|
|
|
308 |
bool noclosing = true;
|
|
|
309 |
int fd = 0;
|
|
|
310 |
struct stat st;
|
|
|
311 |
// Initialize st_size: if fn.empty() , the fstat() call won't happen.
|
|
|
312 |
st.st_size = 0;
|
|
|
313 |
|
|
|
314 |
// If we have a file name, open it, else use stdin.
|
|
|
315 |
if (!m_fn.empty()) {
|
|
|
316 |
SYSPATH(m_fn, realpath);
|
|
|
317 |
fd = OPEN(realpath, O_RDONLY | O_BINARY);
|
|
|
318 |
if (fd < 0 || fstat(fd, &st) < 0) {
|
|
|
319 |
catstrerror(m_reason, "open/stat", errno);
|
|
|
320 |
return false;
|
|
|
321 |
}
|
|
|
322 |
noclosing = false;
|
|
|
323 |
}
|
|
|
324 |
|
|
|
325 |
#if defined O_NOATIME && O_NOATIME != 0
|
|
|
326 |
if (fcntl(fd, F_SETFL, O_NOATIME) < 0) {
|
|
|
327 |
// perror("fcntl");
|
|
|
328 |
}
|
|
|
329 |
#endif
|
|
|
330 |
if (out()) {
|
|
|
331 |
if (m_cnttoread != -1 && m_cnttoread) {
|
|
|
332 |
out()->init(m_cnttoread + 1, m_reason);
|
|
|
333 |
} else if (st.st_size > 0) {
|
|
|
334 |
out()->init(st.st_size + 1, m_reason);
|
|
|
335 |
} else {
|
|
|
336 |
out()->init(0, m_reason);
|
|
|
337 |
}
|
|
|
338 |
}
|
|
|
339 |
|
|
|
340 |
int64_t curoffs = 0;
|
|
|
341 |
if (m_startoffs > 0 && !m_fn.empty()) {
|
|
|
342 |
if (lseek(fd, m_startoffs, SEEK_SET) != m_startoffs) {
|
|
|
343 |
catstrerror(m_reason, "lseek", errno);
|
|
|
344 |
return false;
|
|
|
345 |
}
|
|
|
346 |
curoffs = m_startoffs;
|
|
|
347 |
}
|
|
|
348 |
|
|
|
349 |
char buf[RDBUFSZ];
|
|
|
350 |
int64_t totread = 0;
|
|
|
351 |
for (;;) {
|
|
|
352 |
size_t toread = RDBUFSZ;
|
|
|
353 |
if (m_startoffs > 0 && curoffs < m_startoffs) {
|
|
|
354 |
toread = size_t(MIN(RDBUFSZ, m_startoffs - curoffs));
|
|
|
355 |
}
|
|
|
356 |
|
|
|
357 |
if (m_cnttoread != -1) {
|
|
|
358 |
toread = MIN(toread, (uint64_t)(m_cnttoread - totread));
|
|
|
359 |
}
|
|
|
360 |
ssize_t n = static_cast<ssize_t>(read(fd, buf, toread));
|
|
|
361 |
if (n < 0) {
|
|
|
362 |
catstrerror(m_reason, "read", errno);
|
|
|
363 |
goto out;
|
|
|
364 |
}
|
|
|
365 |
if (n == 0) {
|
|
|
366 |
break;
|
|
|
367 |
}
|
|
|
368 |
curoffs += n;
|
|
|
369 |
if (curoffs - n < m_startoffs) {
|
|
|
370 |
continue;
|
|
|
371 |
}
|
|
|
372 |
if (!out()->data(buf, n, m_reason)) {
|
|
|
373 |
goto out;
|
|
|
374 |
}
|
|
|
375 |
totread += n;
|
|
|
376 |
if (m_cnttoread > 0 && totread >= m_cnttoread) {
|
|
|
377 |
break;
|
|
|
378 |
}
|
|
|
379 |
}
|
|
|
380 |
|
|
|
381 |
ret = true;
|
|
|
382 |
out:
|
|
|
383 |
if (fd >= 0 && !noclosing) {
|
|
|
384 |
close(fd);
|
|
|
385 |
}
|
|
|
386 |
return ret;
|
|
|
387 |
}
|
|
|
388 |
|
|
|
389 |
protected:
|
|
|
390 |
string m_fn;
|
|
|
391 |
int64_t m_startoffs;
|
|
|
392 |
int64_t m_cnttoread;
|
|
|
393 |
string *m_reason;
|
|
|
394 |
};
|
|
|
395 |
|
|
|
396 |
|
|
|
397 |
#if defined(READFILE_ENABLE_MINIZ)
|
|
|
398 |
#include "miniz.h"
|
|
|
399 |
|
|
|
400 |
// Source taking data from a ZIP archive member
|
|
|
401 |
class FileScanSourceZip : public FileScanSource {
|
|
|
402 |
public:
|
|
|
403 |
FileScanSourceZip(FileScanDo *next, const string& fn,
|
|
|
404 |
const string& member, string *reason)
|
|
|
405 |
: FileScanSource(next), m_fn(fn), m_member(member),
|
|
|
406 |
m_reason(reason) {}
|
|
|
407 |
|
|
|
408 |
FileScanSourceZip(const char *data, size_t cnt, FileScanDo *next,
|
|
|
409 |
const string& member, string *reason)
|
|
|
410 |
: FileScanSource(next), m_data(data), m_cnt(cnt), m_member(member),
|
|
|
411 |
m_reason(reason) {}
|
|
|
412 |
|
|
|
413 |
virtual bool scan() {
|
|
|
414 |
bool ret = false;
|
|
|
415 |
mz_zip_archive zip;
|
|
|
416 |
mz_zip_zero_struct(&zip);
|
|
|
417 |
void *opaque = this;
|
|
|
418 |
|
|
|
419 |
bool ret1;
|
|
|
420 |
if (m_fn.empty()) {
|
|
|
421 |
ret1 = mz_zip_reader_init_mem(&zip, m_data, m_cnt, 0);
|
|
|
422 |
} else {
|
|
|
423 |
SYSPATH(m_fn, realpath);
|
|
|
424 |
ret1 = mz_zip_reader_init_file(&zip, realpath, 0);
|
|
|
425 |
}
|
|
|
426 |
if (!ret1) {
|
|
|
427 |
if (m_reason) {
|
|
|
428 |
*m_reason += "mz_zip_reader_init_xx() failed: ";
|
|
|
429 |
*m_reason +=
|
|
|
430 |
string(mz_zip_get_error_string(zip.m_last_error));
|
|
|
431 |
}
|
|
|
432 |
return false;
|
|
|
433 |
}
|
|
|
434 |
|
|
|
435 |
mz_uint32 file_index;
|
|
|
436 |
if (mz_zip_reader_locate_file_v2(&zip, m_member.c_str(), NULL, 0,
|
|
|
437 |
&file_index) < 0) {
|
|
|
438 |
if (m_reason) {
|
|
|
439 |
*m_reason += "mz_zip_reader_locate_file() failed: ";
|
|
|
440 |
*m_reason += string(mz_zip_get_error_string(zip.m_last_error));
|
|
|
441 |
}
|
|
|
442 |
goto out;
|
|
|
443 |
}
|
|
|
444 |
|
|
|
445 |
mz_zip_archive_file_stat zstat;
|
|
|
446 |
if (!mz_zip_reader_file_stat(&zip, file_index, &zstat)) {
|
|
|
447 |
if (m_reason) {
|
|
|
448 |
*m_reason += "mz_zip_reader_file_stat() failed: ";
|
|
|
449 |
*m_reason += string(mz_zip_get_error_string(zip.m_last_error));
|
|
|
450 |
}
|
|
|
451 |
goto out;
|
|
|
452 |
}
|
|
|
453 |
if (out()) {
|
|
|
454 |
if (!out()->init(zstat.m_uncomp_size, m_reason)) {
|
|
|
455 |
goto out;
|
|
|
456 |
}
|
|
|
457 |
}
|
|
|
458 |
|
|
|
459 |
if (!mz_zip_reader_extract_to_callback(
|
|
|
460 |
&zip, file_index, write_cb, opaque, 0)) {
|
|
|
461 |
if (m_reason) {
|
|
|
462 |
*m_reason += "mz_zip_reader_extract_to_callback() failed: ";
|
|
|
463 |
*m_reason += string(mz_zip_get_error_string(zip.m_last_error));
|
|
|
464 |
}
|
|
|
465 |
goto out;
|
|
|
466 |
}
|
|
|
467 |
|
|
|
468 |
ret = true;
|
|
|
469 |
out:
|
|
|
470 |
mz_zip_reader_end(&zip);
|
|
|
471 |
return ret;
|
|
|
472 |
}
|
|
|
473 |
|
|
|
474 |
static size_t write_cb(void *pOpaque, mz_uint64 file_ofs,
|
|
|
475 |
const void *pBuf, size_t n) {
|
|
|
476 |
const char *cp = (const char*)pBuf;
|
|
|
477 |
LOGDEB1("write_cb: ofs " << file_ofs << " cnt " << n << " data: " <<
|
|
|
478 |
string(cp, n) << endl);
|
|
|
479 |
FileScanSourceZip *ths = (FileScanSourceZip *)pOpaque;
|
|
|
480 |
if (ths->out()) {
|
|
|
481 |
if (!ths->out()->data(cp, n, ths->m_reason)) {
|
|
|
482 |
return (size_t)-1;
|
|
|
483 |
}
|
|
|
484 |
}
|
|
|
485 |
return n;
|
|
|
486 |
}
|
|
|
487 |
|
|
|
488 |
protected:
|
|
|
489 |
const char *m_data;
|
|
|
490 |
size_t m_cnt;
|
|
|
491 |
string m_fn;
|
|
|
492 |
string m_member;
|
|
|
493 |
string *m_reason;
|
|
|
494 |
};
|
|
|
495 |
|
|
|
496 |
bool file_scan(const std::string& filename, const std::string& membername,
|
|
|
497 |
FileScanDo* doer, std::string *reason)
|
70 |
{
|
498 |
{
|
71 |
FileToString accum(data);
|
499 |
if (membername.empty()) {
|
72 |
return file_scan(fn, &accum, offs, cnt, reason);
|
500 |
return file_scan(filename, doer, 0, -1, reason
|
|
|
501 |
#ifdef READFILE_ENABLE_MD5
|
|
|
502 |
, nullptr
|
|
|
503 |
#endif
|
|
|
504 |
);
|
|
|
505 |
} else {
|
|
|
506 |
FileScanSourceZip source(doer, filename, membername, reason);
|
|
|
507 |
return source.scan();
|
|
|
508 |
}
|
|
|
509 |
}
|
|
|
510 |
|
|
|
511 |
bool string_scan(const char *data, size_t cnt, const std::string& membername,
|
|
|
512 |
FileScanDo* doer, std::string *reason)
|
|
|
513 |
{
|
|
|
514 |
if (membername.empty()) {
|
|
|
515 |
return string_scan(data, cnt, doer, reason
|
|
|
516 |
#ifdef READFILE_ENABLE_MD5
|
|
|
517 |
, nullptr
|
|
|
518 |
#endif
|
|
|
519 |
);
|
|
|
520 |
} else {
|
|
|
521 |
FileScanSourceZip source(data, cnt, doer, membername, reason);
|
|
|
522 |
return source.scan();
|
|
|
523 |
}
|
|
|
524 |
}
|
|
|
525 |
|
|
|
526 |
#endif // READFILE_ENABLE_ZIP
|
|
|
527 |
|
|
|
528 |
bool file_scan(const string& fn, FileScanDo* doer, int64_t startoffs,
|
|
|
529 |
int64_t cnttoread, string *reason
|
|
|
530 |
#ifdef READFILE_ENABLE_MD5
|
|
|
531 |
, string *md5p
|
|
|
532 |
#endif
|
|
|
533 |
)
|
|
|
534 |
{
|
|
|
535 |
LOGDEB1("file_scan: doer " << doer << endl);
|
|
|
536 |
#if defined(READFILE_ENABLE_ZLIB)
|
|
|
537 |
bool nodecomp = startoffs != 0;
|
|
|
538 |
#endif
|
|
|
539 |
if (startoffs < 0) {
|
|
|
540 |
startoffs = 0;
|
|
|
541 |
}
|
|
|
542 |
|
|
|
543 |
FileScanSourceFile source(doer, fn, startoffs, cnttoread, reason);
|
|
|
544 |
FileScanUpstream *up = &source;
|
|
|
545 |
up = up;
|
|
|
546 |
|
|
|
547 |
#if defined(READFILE_ENABLE_ZLIB)
|
|
|
548 |
GzFilter gzfilter;
|
|
|
549 |
if (!nodecomp) {
|
|
|
550 |
gzfilter.insertAtSink(doer, up);
|
|
|
551 |
up = &gzfilter;
|
|
|
552 |
}
|
|
|
553 |
#endif
|
|
|
554 |
|
|
|
555 |
#ifdef READFILE_ENABLE_MD5
|
|
|
556 |
// We compute the MD5 on the uncompressed data, so insert this
|
|
|
557 |
// right at the source (after the decompressor).
|
|
|
558 |
string digest;
|
|
|
559 |
FileScanMd5 md5filter(digest);
|
|
|
560 |
if (md5p) {
|
|
|
561 |
md5filter.insertAtSink(doer, up);
|
|
|
562 |
up = &md5filter;
|
|
|
563 |
}
|
|
|
564 |
#endif
|
|
|
565 |
|
|
|
566 |
bool ret = source.scan();
|
|
|
567 |
|
|
|
568 |
#ifdef READFILE_ENABLE_MD5
|
|
|
569 |
if (md5p) {
|
|
|
570 |
md5filter.finish();
|
|
|
571 |
MD5HexPrint(digest, *md5p);
|
|
|
572 |
}
|
|
|
573 |
#endif
|
|
|
574 |
return ret;
|
73 |
}
|
575 |
}
|
74 |
|
576 |
|
75 |
bool file_scan(const string& fn, FileScanDo* doer, string *reason)
|
577 |
bool file_scan(const string& fn, FileScanDo* doer, string *reason)
|
76 |
{
|
578 |
{
|
77 |
return file_scan(fn, doer, 0, size_t(-1), reason);
|
579 |
return file_scan(fn, doer, 0, -1, reason
|
|
|
580 |
#ifdef READFILE_ENABLE_MD5
|
|
|
581 |
, nullptr
|
|
|
582 |
#endif
|
|
|
583 |
);
|
78 |
}
|
584 |
}
|
79 |
|
585 |
|
80 |
const int RDBUFSZ = 8192;
|
586 |
|
81 |
// Note: the fstat() + reserve() (in init()) calls divide cpu usage almost by 2
|
587 |
class FileScanSourceBuffer : public FileScanSource {
|
82 |
// on both linux i586 and macosx (compared to just append())
|
588 |
public:
|
83 |
// Also tried a version with mmap, but it's actually slower on the mac and not
|
589 |
FileScanSourceBuffer(FileScanDo *next, const char *data, size_t cnt,
|
84 |
// faster on linux.
|
590 |
string *reason)
|
85 |
bool file_scan(const string& fn, FileScanDo* doer, int64_t startoffs,
|
591 |
: FileScanSource(next), m_data(data), m_cnt(cnt), m_reason(reason) {}
|
|
|
592 |
|
|
|
593 |
virtual bool scan() {
|
|
|
594 |
if (out()) {
|
|
|
595 |
if (!out()->init(m_cnt, m_reason)) {
|
|
|
596 |
return false;
|
|
|
597 |
}
|
|
|
598 |
return out()->data(m_data, m_cnt, m_reason);
|
|
|
599 |
} else {
|
|
|
600 |
return true;
|
|
|
601 |
}
|
|
|
602 |
}
|
|
|
603 |
|
|
|
604 |
protected:
|
|
|
605 |
const char *m_data{nullptr};
|
|
|
606 |
size_t m_cnt{0};
|
|
|
607 |
string *m_reason{nullptr};
|
|
|
608 |
};
|
|
|
609 |
|
|
|
610 |
bool string_scan(const char *data, size_t cnt, FileScanDo* doer,
|
86 |
size_t cnttoread, string *reason)
|
611 |
std::string *reason
|
|
|
612 |
#ifdef READFILE_ENABLE_MD5
|
|
|
613 |
, std::string *md5p
|
|
|
614 |
#endif
|
|
|
615 |
)
|
87 |
{
|
616 |
{
|
88 |
if (startoffs < 0) {
|
617 |
FileScanSourceBuffer source(doer, data, cnt, reason);
|
89 |
*reason += " file_scan: negative startoffs not allowed";
|
618 |
FileScanUpstream *up = &source;
|
90 |
return false;
|
619 |
up = up;
|
91 |
}
|
620 |
|
92 |
|
621 |
#ifdef READFILE_ENABLE_MD5
|
93 |
bool ret = false;
|
622 |
string digest;
|
94 |
bool noclosing = true;
|
623 |
FileScanMd5 md5filter(digest);
|
95 |
int fd = 0;
|
624 |
if (md5p) {
|
96 |
struct stat st;
|
625 |
md5filter.insertAtSink(doer, up);
|
97 |
// Initialize st_size: if fn.empty() , the fstat() call won't happen.
|
626 |
up = &md5filter;
|
98 |
st.st_size = 0;
|
|
|
99 |
|
|
|
100 |
// If we have a file name, open it, else use stdin.
|
|
|
101 |
if (!fn.empty()) {
|
|
|
102 |
fd = open(fn.c_str(), O_RDONLY | O_BINARY);
|
|
|
103 |
if (fd < 0 || fstat(fd, &st) < 0) {
|
|
|
104 |
catstrerror(reason, "open/stat", errno);
|
|
|
105 |
return false;
|
|
|
106 |
}
|
627 |
}
|
107 |
noclosing = false;
|
628 |
#endif
|
108 |
}
|
629 |
|
|
|
630 |
bool ret = source.scan();
|
109 |
|
631 |
|
110 |
#if defined O_NOATIME && O_NOATIME != 0
|
632 |
#ifdef READFILE_ENABLE_MD5
|
111 |
if (fcntl(fd, F_SETFL, O_NOATIME) < 0) {
|
633 |
if (md5p) {
|
112 |
// perror("fcntl");
|
634 |
md5filter.finish();
|
113 |
}
|
635 |
MD5HexPrint(digest, *md5p);
|
114 |
#endif
|
|
|
115 |
|
|
|
116 |
if (cnttoread != (size_t) - 1 && cnttoread) {
|
|
|
117 |
doer->init(cnttoread + 1, reason);
|
|
|
118 |
} else if (st.st_size > 0) {
|
|
|
119 |
doer->init(size_t(st.st_size + 1), reason);
|
|
|
120 |
} else {
|
|
|
121 |
doer->init(0, reason);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
int64_t curoffs = 0;
|
|
|
125 |
if (startoffs > 0 && !fn.empty()) {
|
|
|
126 |
if (lseek(fd, startoffs, SEEK_SET) != startoffs) {
|
|
|
127 |
catstrerror(reason, "lseek", errno);
|
|
|
128 |
return false;
|
|
|
129 |
}
|
636 |
}
|
130 |
curoffs = startoffs;
|
637 |
#endif
|
131 |
}
|
|
|
132 |
|
|
|
133 |
char buf[RDBUFSZ];
|
|
|
134 |
size_t totread = 0;
|
|
|
135 |
for (;;) {
|
|
|
136 |
size_t toread = RDBUFSZ;
|
|
|
137 |
if (startoffs > 0 && curoffs < startoffs) {
|
|
|
138 |
toread = size_t(MIN(RDBUFSZ, startoffs - curoffs));
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
if (cnttoread != size_t(-1)) {
|
|
|
142 |
toread = MIN(toread, cnttoread - totread);
|
|
|
143 |
}
|
|
|
144 |
ssize_t n = static_cast<ssize_t>(read(fd, buf, toread));
|
|
|
145 |
if (n < 0) {
|
|
|
146 |
catstrerror(reason, "read", errno);
|
|
|
147 |
goto out;
|
|
|
148 |
}
|
|
|
149 |
if (n == 0) {
|
|
|
150 |
break;
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
curoffs += n;
|
|
|
154 |
if (curoffs - n < startoffs) {
|
|
|
155 |
continue;
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
if (!doer->data(buf, n, reason)) {
|
|
|
159 |
goto out;
|
|
|
160 |
}
|
|
|
161 |
totread += n;
|
|
|
162 |
if (cnttoread > 0 && totread >= cnttoread) {
|
|
|
163 |
break;
|
|
|
164 |
}
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
ret = true;
|
|
|
168 |
out:
|
|
|
169 |
if (fd >= 0 && !noclosing) {
|
|
|
170 |
close(fd);
|
|
|
171 |
}
|
|
|
172 |
return ret;
|
638 |
return ret;
|
173 |
}
|
639 |
}
|
174 |
|
640 |
|
175 |
#else // Test
|
|
|
176 |
#include "autoconfig.h"
|
|
|
177 |
|
|
|
178 |
#include <stdio.h>
|
|
|
179 |
#include <sys/types.h>
|
|
|
180 |
#include "safesysstat.h"
|
|
|
181 |
#include <stdlib.h>
|
|
|
182 |
|
|
|
183 |
#include <string>
|
|
|
184 |
#include <iostream>
|
|
|
185 |
using namespace std;
|
|
|
186 |
|
|
|
187 |
#include "readfile.h"
|
|
|
188 |
#include "fstreewalk.h"
|
|
|
189 |
|
|
|
190 |
using namespace std;
|
|
|
191 |
|
|
|
192 |
class myCB : public FsTreeWalkerCB {
|
|
|
193 |
public:
|
|
|
194 |
FsTreeWalker::Status processone(const string& path,
|
|
|
195 |
const struct stat *st,
|
|
|
196 |
FsTreeWalker::CbFlag flg) {
|
|
|
197 |
if (flg == FsTreeWalker::FtwDirEnter) {
|
|
|
198 |
//cout << "[Entering " << path << "]" << endl;
|
|
|
199 |
} else if (flg == FsTreeWalker::FtwDirReturn) {
|
|
|
200 |
//cout << "[Returning to " << path << "]" << endl;
|
|
|
201 |
} else if (flg == FsTreeWalker::FtwRegular) {
|
|
|
202 |
//cout << path << endl;
|
|
|
203 |
string s, reason;
|
|
|
204 |
if (!file_to_string(path, s, &reason)) {
|
|
|
205 |
cerr << "Failed: " << reason << " : " << path << endl;
|
|
|
206 |
} else {
|
|
|
207 |
//cout <<
|
|
|
208 |
//"================================================" << endl;
|
|
|
209 |
cout << path << endl;
|
|
|
210 |
// cout << s;
|
|
|
211 |
}
|
|
|
212 |
reason.clear();
|
|
|
213 |
}
|
|
|
214 |
return FsTreeWalker::FtwOk;
|
|
|
215 |
}
|
|
|
216 |
};
|
|
|
217 |
|
|
|
218 |
static int op_flags;
|
|
|
219 |
#define OPT_MOINS 0x1
|
|
|
220 |
#define OPT_c 0x2
|
|
|
221 |
#define OPT_o 0x4
|
|
|
222 |
|
|
|
223 |
static const char *thisprog;
|
|
|
224 |
static char usage [] =
|
|
|
225 |
"trreadfile [-o offs] [-c cnt] topdirorfile\n\n"
|
|
|
226 |
;
|
|
|
227 |
static void
|
|
|
228 |
Usage(void)
|
|
|
229 |
{
|
|
|
230 |
fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
|
|
|
231 |
exit(1);
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
int main(int argc, const char **argv)
|
|
|
235 |
{
|
|
|
236 |
int64_t offs = 0;
|
|
|
237 |
size_t cnt = size_t(-1);
|
|
|
238 |
thisprog = argv[0];
|
|
|
239 |
argc--;
|
|
|
240 |
argv++;
|
|
|
241 |
|
|
|
242 |
while (argc > 0 && **argv == '-') {
|
|
|
243 |
(*argv)++;
|
|
|
244 |
if (!(**argv))
|
|
|
245 |
/* Cas du "adb - core" */
|
|
|
246 |
{
|
|
|
247 |
Usage();
|
|
|
248 |
}
|
|
|
249 |
while (**argv)
|
|
|
250 |
switch (*(*argv)++) {
|
|
|
251 |
case 'c':
|
|
|
252 |
op_flags |= OPT_c;
|
|
|
253 |
if (argc < 2) {
|
|
|
254 |
Usage();
|
|
|
255 |
}
|
|
|
256 |
cnt = atoll(*(++argv));
|
|
|
257 |
argc--;
|
|
|
258 |
goto b1;
|
|
|
259 |
case 'o':
|
|
|
260 |
op_flags |= OPT_o;
|
|
|
261 |
if (argc < 2) {
|
|
|
262 |
Usage();
|
|
|
263 |
}
|
|
|
264 |
offs = strtoull(*(++argv), 0, 0);
|
|
|
265 |
argc--;
|
|
|
266 |
goto b1;
|
|
|
267 |
default:
|
|
|
268 |
Usage();
|
|
|
269 |
break;
|
|
|
270 |
}
|
|
|
271 |
b1:
|
|
|
272 |
argc--;
|
|
|
273 |
argv++;
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
if (argc != 1) {
|
|
|
277 |
Usage();
|
|
|
278 |
}
|
|
|
279 |
string top = *argv++;
|
|
|
280 |
argc--;
|
|
|
281 |
cerr << "filename " << top << " offs " << offs << " cnt " << cnt << endl;
|
|
|
282 |
|
|
|
283 |
struct stat st;
|
|
|
284 |
if (!top.empty() && stat(top.c_str(), &st) < 0) {
|
|
|
285 |
perror("stat");
|
|
|
286 |
exit(1);
|
|
|
287 |
}
|
|
|
288 |
if (!top.empty() && S_ISDIR(st.st_mode)) {
|
|
|
289 |
FsTreeWalker walker;
|
|
|
290 |
myCB cb;
|
|
|
291 |
walker.walk(top, cb);
|
|
|
292 |
if (walker.getErrCnt() > 0) {
|
|
|
293 |
cout << walker.getReason();
|
|
|
294 |
}
|
|
|
295 |
} else {
|
|
|
296 |
string s, reason;
|
|
|
297 |
if (!file_to_string(top, s, offs, cnt, &reason)) {
|
|
|
298 |
cerr << reason << endl;
|
|
|
299 |
exit(1);
|
|
|
300 |
} else {
|
|
|
301 |
cout << s;
|
|
|
302 |
}
|
|
|
303 |
}
|
|
|
304 |
exit(0);
|
|
|
305 |
}
|
|
|
306 |
#endif //TEST_READFILE
|
|
|