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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
|
/*
* Routines for reading and writing compressed files using zlib
*
* Copyright (C) 2013 Craig Barratt.
*
* 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 3 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, visit the http://fsf.org website.
*/
#include "backuppc.h"
/*
* A freelist of unused data buffers.
* We use the first sizeof(void*) bytes of the buffer as a single-linked
* list, with a NULL at the end.
*/
static void *DataBufferFreeList = (void*)NULL;
/*
* Open a regular or compressed file for reading or writing/create
*/
int bpc_fileZIO_open(bpc_fileZIO_fd *fd, char *fileName, int writeFile, int compressLevel)
{
fd->strm.next_out = NULL;
fd->strm.zalloc = NULL;
fd->strm.zfree = NULL;
fd->strm.opaque = NULL;
fd->compressLevel = compressLevel;
fd->first = 1;
fd->write = writeFile;
fd->eof = 0;
fd->error = 0;
fd->writeTeeStderr = 0;
fd->lineBuf = NULL;
fd->lineBufSize = 0;
fd->lineBufLen = 0;
fd->lineBufIdx = 0;
fd->lineBufEof = 0;
fd->bufSize = 1 << 20; /* 1MB */
if ( writeFile ) {
fd->fd = open(fileName, O_WRONLY | O_CREAT | O_TRUNC, 0660);
if ( fd->fd < 0 ) {
/*
* try removing first
*/
unlink(fileName);
fd->fd = open(fileName, O_WRONLY | O_CREAT | O_TRUNC, 0660);
}
if ( fd->fd < 0 ) return -1;
if ( fd->compressLevel ) {
if (deflateInit2(&fd->strm, compressLevel, Z_DEFLATED, MAX_WBITS, 8,
Z_DEFAULT_STRATEGY) != Z_OK) {
bpc_logErrf("bpc_fileZIO_open: compression init failed\n");
return -1;
}
fd->strm.next_out = (Bytef*)fd->buf;
fd->strm.avail_out = fd->bufSize;
}
} else {
fd->fd = open(fileName, O_RDONLY);
if ( fd->fd < 0 ) return -1;
if ( fd->compressLevel ) {
if ( inflateInit(&fd->strm) != Z_OK ) {
bpc_logErrf("bpc_fileZIO_open: compression init failed\n");
return -1;
}
fd->strm.avail_in = 0;
}
}
if ( DataBufferFreeList ) {
fd->buf = DataBufferFreeList;
DataBufferFreeList = *(void**)DataBufferFreeList;
} else {
fd->buf = malloc(fd->bufSize);
}
if ( !fd->buf ) {
bpc_logErrf("bpc_fileZIO_open: fatal error: can't allocate %u bytes\n", (unsigned)fd->bufSize);
return -1;
}
if ( BPC_LogLevel >= 8 ) bpc_logMsgf("bpc_fileZIO_open(%s, %d, %d) -> %d\n", fileName, writeFile, compressLevel, fd->fd);
return 0;
}
/*
* Open an existing FILE stream for reading/writing.
* Note: we used unbuffered integer fds here, and we simply grab the underlying integer fd. That will
* mess up the FILE stream buffering if anything has been read/written from/to the FILE.
*
* This function is only used to support the legacy BackupPC::FileZIO feature that allows you to
* pass STDIN in as an argument to open().
*/
int bpc_fileZIO_fdopen(bpc_fileZIO_fd *fd, FILE *stream, int writeFile, int compressLevel)
{
fd->strm.next_out = NULL;
fd->strm.zalloc = NULL;
fd->strm.zfree = NULL;
fd->strm.opaque = NULL;
fd->compressLevel = compressLevel;
fd->first = 1;
fd->write = writeFile;
fd->eof = 0;
fd->error = 0;
fd->writeTeeStderr = 0;
fd->lineBuf = NULL;
fd->lineBufSize = 0;
fd->lineBufLen = 0;
fd->lineBufIdx = 0;
fd->lineBufEof = 0;
fd->fd = fileno(stream);
if ( fd->fd < 0 ) return -1;
fd->bufSize = 1 << 20; /* 1MB */
if ( !(fd->buf = malloc(fd->bufSize)) ) {
bpc_logErrf("bpc_fileZIO_fdopen: can't allocate %u bytes\n", (unsigned)fd->bufSize);
return -1;
}
if ( fd->compressLevel ) {
if ( writeFile ) {
if (deflateInit2(&fd->strm, compressLevel, Z_DEFLATED, MAX_WBITS, 8,
Z_DEFAULT_STRATEGY) != Z_OK) {
bpc_logErrf("bpc_fileZIO_open: compression init failed\n");
return -1;
}
fd->strm.next_out = (Bytef*)fd->buf;
fd->strm.avail_out = fd->bufSize;
} else {
if ( inflateInit(&fd->strm) != Z_OK ) {
bpc_logErrf("bpc_fileZIO_open: compression init failed\n");
return -1;
}
fd->strm.avail_in = 0;
}
}
if ( BPC_LogLevel >= 8 ) bpc_logMsgf("bpc_fileZIO_fdopen(%d, %d) -> %d\n", writeFile, compressLevel, fd->fd);
return 0;
}
void bpc_fileZIO_writeTeeStderr(bpc_fileZIO_fd *fd, int tee)
{
fd->writeTeeStderr = tee;
}
/*
* Read from a compressed or regular file.
*/
ssize_t bpc_fileZIO_read(bpc_fileZIO_fd *fd, uchar *buf, size_t nRead)
{
size_t totalRead = 0;
if ( fd->write || fd->fd < 0 ) return -1;
if ( fd->compressLevel == 0 ) {
ssize_t thisRead;
while ( nRead > 0 ) {
do {
thisRead = read(fd->fd, buf, nRead);
} while ( thisRead < 0 && errno == EINTR );
if ( thisRead < 0 ) return thisRead;
if ( thisRead == 0 ) return totalRead;
buf += thisRead;
nRead -= thisRead;
totalRead += thisRead;
}
return totalRead;
}
if ( fd->error ) return fd->error;
while ( nRead > 0 ) {
/*
* Start by trying to read more of the compressed input file
*/
int maxRead, thisRead = -1;
if ( fd->strm.avail_in == 0 ) {
fd->strm.next_in = (Bytef*)fd->buf;
}
maxRead = fd->bufSize - ((fd->strm.next_in - (Bytef*)fd->buf) + fd->strm.avail_in);
if ( !fd->eof && maxRead > 0 ) {
do {
thisRead = read(fd->fd, fd->strm.next_in + fd->strm.avail_in, maxRead);
} while ( thisRead < 0 && errno == EINTR );
if ( thisRead < 0 ) {
fd->error = thisRead;
return fd->error;
}
fd->strm.avail_in += thisRead;
if ( thisRead == 0 ) {
fd->eof = 1;
}
}
while ( nRead > 0 ) {
int status, numOut;
fd->strm.next_out = (Bytef*)buf;
fd->strm.avail_out = nRead;
if ( fd->first && fd->strm.avail_in > 0 ) {
/*
* we are at the very start of a new zlib block (or it could be cached checksums)
*/
fd->first = 0;
if ( fd->strm.next_in[0] == 0xd6 || fd->strm.next_in[0] == 0xd7 ) {
/*
* Flag 0xd6 or 0xd7 means this is a compressed file with
* appended md4 block checksums for rsync. Change
* the first byte back to 0x78 and proceed.
*/
fd->strm.next_in[0] = 0x78;
} else if ( fd->strm.next_in[0] == 0xb3 ) {
/*
* Flag 0xb3 means this is the start of the rsync
* block checksums, so consider this as EOF for
* the compressed file. Also seek the file so
* it is positioned at the 0xb3.
*/
fd->eof = 1;
/* TODO: check return status */
lseek(fd->fd, -fd->strm.avail_in, SEEK_CUR);
fd->strm.avail_in = 0;
}
}
status = inflate(&fd->strm, fd->eof ? Z_SYNC_FLUSH : Z_NO_FLUSH);
numOut = fd->strm.next_out - (Bytef*)buf;
nRead -= numOut;
buf += numOut;
totalRead += numOut;
if ( BPC_LogLevel >= 10 ) bpc_logMsgf("inflate returns %d; thisRead = %d, avail_in = %d, numOut = %d\n", status, thisRead, fd->strm.avail_in, numOut);
if ( fd->eof && fd->strm.avail_in == 0 && numOut == 0 ) return totalRead;
if ( status == Z_OK && fd->strm.avail_in == 0 ) break;
if ( status == Z_BUF_ERROR && fd->strm.avail_in == 0 && numOut == 0 ) break;
if ( status == Z_STREAM_END ) {
inflateReset(&fd->strm);
fd->first = 1;
}
if ( status < 0 ) return status;
}
}
return totalRead;
}
/*
* Write to a compressed or regular file.
* Write flush and eof is indicated with nWrite == 0.
*/
ssize_t bpc_fileZIO_write(bpc_fileZIO_fd *fd, uchar *buf, size_t nWrite)
{
if ( !fd->write || fd->fd < 0 ) return -1;
if ( fd->eof ) return 0;
if ( fd->writeTeeStderr && nWrite > 0 ) (void)fwrite((char*)buf, nWrite, 1, stderr);
if ( fd->compressLevel == 0 ) {
int thisWrite, totalWrite = 0;
while ( nWrite > 0 ) {
do {
thisWrite = write(fd->fd, buf, nWrite);
} while ( thisWrite < 0 && errno == EINTR );
if ( thisWrite < 0 ) return thisWrite;
buf += thisWrite;
nWrite -= thisWrite;
totalWrite += thisWrite;
}
return totalWrite;
}
if ( fd->error ) return fd->error;
if ( nWrite == 0 || (fd->strm.total_in > (1 << 23) && fd->strm.total_out < (1 << 18)) ) {
/*
* final or intermediate flush (if the compression ratio is too high, since the
* perl Compress::Zlib implementation allocates the output buffer for inflate
* and it could grow to be very large).
*/
if ( BPC_LogLevel >= 10 ) bpc_logMsgf("Flushing (nWrite = %d)\n", nWrite);
while ( 1 ) {
int status, numOut, thisWrite;
char *writePtr = fd->buf;
fd->strm.next_in = NULL;
fd->strm.avail_in = 0;
fd->strm.next_out = (Bytef*)fd->buf;
fd->strm.avail_out = fd->bufSize;
status = deflate(&fd->strm, Z_FINISH);
numOut = fd->strm.next_out - (Bytef*)fd->buf;
while ( numOut > 0 ) {
do {
thisWrite = write(fd->fd, writePtr, numOut);
} while ( thisWrite < 0 && errno == EINTR );
if ( thisWrite < 0 ) return thisWrite;
numOut -= thisWrite;
writePtr += thisWrite;
}
if ( status != Z_OK ) break;
}
deflateReset(&fd->strm);
}
if ( nWrite == 0 ) {
fd->eof = 1;
return nWrite;
}
fd->strm.next_in = (Bytef*)buf;
fd->strm.avail_in = nWrite;
while ( fd->strm.avail_in > 0 ) {
int numOut, thisWrite;
char *writePtr = fd->buf;
fd->strm.next_out = (Bytef*)fd->buf;
fd->strm.avail_out = fd->bufSize;
deflate(&fd->strm, Z_NO_FLUSH);
numOut = fd->strm.next_out - (Bytef*)fd->buf;
while ( numOut > 0 ) {
do {
thisWrite = write(fd->fd, writePtr, numOut);
} while ( thisWrite < 0 && errno == EINTR );
if ( thisWrite < 0 ) return thisWrite;
numOut -= thisWrite;
writePtr += thisWrite;
}
}
return nWrite;
}
int bpc_fileZIO_close(bpc_fileZIO_fd *fd)
{
if ( fd->fd < 0 ) return -1;
if ( fd->compressLevel ) {
if ( fd->write ) {
/*
* Flush the output file
*/
bpc_fileZIO_write(fd, NULL, 0);
deflateEnd(&fd->strm);
} else {
inflateEnd(&fd->strm);
}
}
if ( BPC_LogLevel >= 8 ) bpc_logMsgf("bpc_fileZIO_close(%d)\n", fd->fd);
close(fd->fd);
if ( fd->lineBuf ) free(fd->lineBuf);
fd->lineBuf = NULL;
if ( fd->buf ) {
*(void**)fd->buf = DataBufferFreeList;
DataBufferFreeList = fd->buf;
fd->buf = NULL;
}
fd->fd = -1;
return 0;
}
int bpc_fileZIO_rewind(bpc_fileZIO_fd *fd)
{
if ( fd->write ) return -1;
if ( fd->compressLevel ) {
inflateReset(&fd->strm);
fd->first = 1;
fd->eof = 0;
fd->error = 0;
fd->strm.avail_in = 0;
}
return lseek(fd->fd, 0, SEEK_SET) == 0 ? 0 : -1;
}
/*
* Returns \n terminated lines, one at a time, from the opened read stream.
* The returned string is not '\0' terminated. At EOF sets *str = NULL;
*/
int bpc_fileZIO_readLine(bpc_fileZIO_fd *fd, char **str, size_t *strLen)
{
if ( !fd->lineBuf ) {
/*
* allocate initial read buffer
*/
fd->lineBufSize = 65536;
if ( !(fd->lineBuf = malloc(fd->lineBufSize)) ) {
bpc_logErrf("bpc_fileZIO_readLine: can't allocate %u bytes\n", (unsigned)fd->lineBufSize);
return -1;
}
fd->lineBufLen = 0;
fd->lineBufIdx = 0;
fd->lineBufEof = 0;
}
while ( 1 ) {
char *p;
if ( fd->lineBufIdx < fd->lineBufLen ) {
if ( (p = memchr(fd->lineBuf + fd->lineBufIdx, '\n', fd->lineBufLen - fd->lineBufIdx)) ) {
/*
* found next complete line
*/
p++;
*str = fd->lineBuf + fd->lineBufIdx;
*strLen = p - (fd->lineBuf + fd->lineBufIdx);
fd->lineBufIdx += p - (fd->lineBuf + fd->lineBufIdx);
return 0;
} else if ( fd->lineBufEof ) {
/*
* return last string - not \n terminated
*/
*str = fd->lineBuf + fd->lineBufIdx;
*strLen = fd->lineBufLen - fd->lineBufIdx;
fd->lineBufIdx += fd->lineBufLen - fd->lineBufIdx;
return 0;
} else if ( fd->lineBufLen >= fd->lineBufSize ) {
/*
* No complete lines left, and buffer is full. Either move the unused buffer down to make
* more room for reading, or make the buffer bigger.
*/
if ( fd->lineBufIdx > 0 ) {
memmove(fd->lineBuf, fd->lineBuf + fd->lineBufIdx, fd->lineBufLen - fd->lineBufIdx);
fd->lineBufLen -= fd->lineBufIdx;
fd->lineBufIdx = 0;
} else {
fd->lineBufSize *= 2;
if ( !(fd->lineBuf = realloc(fd->lineBuf, fd->lineBufSize)) ) {
bpc_logErrf("bpc_fileZIO_readLine: can't reallocate %u bytes\n", (unsigned)fd->lineBufSize);
return -1;
}
}
}
}
if ( fd->lineBufIdx >= fd->lineBufLen && fd->lineBufEof ) {
/*
* at EOF
*/
*str = NULL;
*strLen = 0;
return 0;
}
if ( fd->lineBufIdx >= fd->lineBufLen ) {
fd->lineBufLen = 0;
fd->lineBufIdx = 0;
}
if ( fd->lineBufLen < fd->lineBufSize && !fd->lineBufEof ) {
int nread = bpc_fileZIO_read(fd, (uchar*)fd->lineBuf + fd->lineBufLen, fd->lineBufSize - fd->lineBufLen);
if ( nread < 0 ) {
bpc_logErrf("bpc_fileZIO_readLine: reading %u returned %d\n", (unsigned)(fd->lineBufSize - fd->lineBufLen), nread);
return nread;
}
if ( nread == 0 ) fd->lineBufEof = 1;
fd->lineBufLen += nread;
}
}
}
|