Add a simple log facility.

Refactor all printfs to use log facility.
This commit is contained in:
Moinak Ghosh 2013-10-02 20:45:33 +05:30
parent fa78621cbf
commit 8c1f4ebe61
24 changed files with 286 additions and 211 deletions

View file

@ -342,6 +342,7 @@ $(LIB): $(OBJS)
$(PROG): $(LIB) $(PROGOBJS)
$(LINK.PROG) -o $@ $(PROGOBJS) $(LDLIBS) -L. -l$(LINKLIB)
mkdir -p buildtmp
cp $@ buildtmp
cat utils/pcompress.sh | sed "s#<PC_PATH>#`pwd`#" > pcompress
chmod +x pcompress

View file

@ -85,13 +85,13 @@ adapt_stats(int show)
{
if (show) {
if (bzip2_count > 0 || bsc_count > 0 || ppmd_count > 0 || lzma_count > 0) {
fprintf(stderr, "Adaptive mode stats:\n");
fprintf(stderr, " BZIP2 chunk count: %u\n", bzip2_count);
fprintf(stderr, " LIBBSC chunk count: %u\n", bsc_count);
fprintf(stderr, " PPMd chunk count: %u\n", ppmd_count);
fprintf(stderr, " LZMA chunk count: %u\n\n", lzma_count);
log_msg(LOG_INFO, 0, "Adaptive mode stats:\n");
log_msg(LOG_INFO, 0, " BZIP2 chunk count: %u\n", bzip2_count);
log_msg(LOG_INFO, 0, " LIBBSC chunk count: %u\n", bsc_count);
log_msg(LOG_INFO, 0, " PPMd chunk count: %u\n", ppmd_count);
log_msg(LOG_INFO, 0, " LZMA chunk count: %u\n\n", lzma_count);
} else {
fprintf(stderr, "\n");
log_msg(LOG_INFO, 0, "\n");
}
}
lzma_count = 0;
@ -276,12 +276,12 @@ adapt_decompress(void *src, uint64_t srclen, void *dst,
#ifdef ENABLE_PC_LIBBSC
return (libbsc_decompress(src, srclen, dst, dstlen, level, chdr, adat->bsc_data));
#else
fprintf(stderr, "Cannot decompress chunk. Libbsc support not present.\n");
log_msg(LOG_ERR, 0, "Cannot decompress chunk. Libbsc support not present.\n");
return (-1);
#endif
} else {
fprintf(stderr, "Unrecognized compression mode: %d, file corrupt.\n", cmp_flags);
log_msg(LOG_ERR, 0, "Unrecognized compression mode: %d, file corrupt.\n", cmp_flags);
}
return (-1);
}

View file

@ -186,10 +186,10 @@ slab_cleanup(int quiet)
if (bypass) return;
if (!quiet) {
fprintf(stderr, "Slab Allocation Stats\n");
fprintf(stderr, "==================================================================\n");
fprintf(stderr, " Slab Size | Allocations | Hits |\n");
fprintf(stderr, "==================================================================\n");
log_msg(LOG_INFO, 0, "Slab Allocation Stats\n");
log_msg(LOG_INFO, 0, "==================================================================\n");
log_msg(LOG_INFO, 0, " Slab Size | Allocations | Hits |\n");
log_msg(LOG_INFO, 0, "==================================================================\n");
}
for (i=0; i<NUM_SLABS; i++)
@ -200,7 +200,7 @@ slab_cleanup(int quiet)
while (slab) {
if (slab->avail) {
if (!quiet) {
fprintf(stderr, "%21" PRIu64 " %21" PRIu64 " %21" PRIu64 "\n",slab->sz,
log_msg(LOG_INFO, 0, "%21" PRIu64 " %21" PRIu64 " %21" PRIu64 "\n",slab->sz,
slab->allocs, slab->hits);
}
slab->allocs = 0;
@ -217,11 +217,11 @@ slab_cleanup(int quiet)
}
if (!quiet) {
fprintf(stderr, "==================================================================\n");
fprintf(stderr, "Oversize Allocations : %" PRIu64 "\n", oversize_allocs);
fprintf(stderr, "Total Requests : %" PRIu64 "\n", total_allocs);
fprintf(stderr, "Hash collisions : %" PRIu64 "\n", hash_collisions);
fprintf(stderr, "Leaked allocations : %" PRIu64 "\n", hash_entries);
log_msg(LOG_INFO, 0, "==================================================================\n");
log_msg(LOG_INFO, 0, "Oversize Allocations : %" PRIu64 "\n", oversize_allocs);
log_msg(LOG_INFO, 0, "Total Requests : %" PRIu64 "\n", total_allocs);
log_msg(LOG_INFO, 0, "Hash collisions : %" PRIu64 "\n", hash_collisions);
log_msg(LOG_INFO, 0, "Leaked allocations : %" PRIu64 "\n", hash_entries);
}
if (hash_entries > 0) {
@ -245,9 +245,9 @@ slab_cleanup(int quiet)
free(hbucket_locks);
if (!quiet) {
fprintf(stderr, "==================================================================\n");
fprintf(stderr, " Slab Size | Allocations: leaked |\n");
fprintf(stderr, "==================================================================\n");
log_msg(LOG_INFO, 0, "==================================================================\n");
log_msg(LOG_INFO, 0, " Slab Size | Allocations: leaked |\n");
log_msg(LOG_INFO, 0, "==================================================================\n");
for (i=0; i<NUM_SLABS; i++)
{
struct slabentry *slab;
@ -255,7 +255,7 @@ slab_cleanup(int quiet)
slab = &slabheads[i];
do {
if (slab->allocs > 0)
fprintf(stderr, "%21" PRIu64 " %21" PRIu64 "\n", \
log_msg(LOG_INFO, 0, "%21" PRIu64 " %21" PRIu64 "\n", \
slab->sz, slab->allocs);
slab = slab->next;
} while (slab);
@ -276,7 +276,7 @@ slab_cleanup(int quiet)
j++;
} while (slab);
}
if (!quiet) fprintf(stderr, "\n\n");
if (!quiet) log_msg(LOG_INFO, 0, "\n\n");
}
void *
@ -461,7 +461,7 @@ slab_free(void *p, void *address)
while (buf) {
if (buf->ptr == address) {
if (hash_entries <=0) {
fprintf(stderr, "Inconsistent allocation hash\n");
log_msg(LOG_ERR, 0, "Inconsistent allocation hash\n");
abort();
}
if (pbuf)
@ -490,7 +490,7 @@ slab_free(void *p, void *address)
}
if (!found) {
pthread_mutex_unlock(&hbucket_locks[hindx]);
fprintf(stderr, "Freed buf(%p) not in slab allocations!\n", address);
log_msg(LOG_ERR, 0, "Freed buf(%p) not in slab allocations!\n", address);
free(address);
abort();
fflush(stderr);

View file

@ -280,7 +280,7 @@ bsdiff(u_char *oldbuf, bsize_t oldsize, u_char *newbuf, bsize_t newsize,
if(((db=(u_char *)slab_alloc(NULL, newsize+1))==NULL) ||
((eb=(u_char *)slab_alloc(NULL, newsize+1))==NULL)) {
fprintf(stderr, "bsdiff: Memory allocation error.\n");
log_msg(LOG_ERR, 0, "bsdiff: Memory allocation error.\n");
slab_free(NULL, I);
slab_free(NULL, V);
return (0);
@ -310,7 +310,7 @@ bsdiff(u_char *oldbuf, bsize_t oldsize, u_char *newbuf, bsize_t newsize,
valouti32(0, header + 4*5);
valouti32(newsize, header + 4*6);
if (BUFWRITE(&pf, header, 4*7) != 4*7) {
fprintf(stderr, "bsdiff: Write to compressed buffer failed.\n");
log_msg(LOG_ERR, 0, "bsdiff: Write to compressed buffer failed.\n");
rv = 0;
goto out;
}

View file

@ -134,11 +134,11 @@ bspatch(u_char *pbuf, u_char *oldbuf, bsize_t oldsize, u_char *newbuf, bsize_t *
newsize = valini32(pbuf+4*6);
if((ctrllen<0) || (lzdatalen<0) || (newsize<0) || (lzextralen<0)) {
fprintf(stderr, "1: Corrupt patch\n");
log_msg(LOG_ERR, 0, "1: Corrupt patch\n");
return (0);
}
if (newsize > *_newsize) {
fprintf(stderr, "Output buffer too small.\n");
log_msg(LOG_ERR, 0, "Output buffer too small.\n");
return (0);
}
*_newsize = newsize;
@ -147,7 +147,7 @@ bspatch(u_char *pbuf, u_char *oldbuf, bsize_t oldsize, u_char *newbuf, bsize_t *
diffdata = (u_char *)slab_alloc(NULL, datalen);
extradata = (u_char *)slab_alloc(NULL, extralen);
if (diffdata == NULL || extradata == NULL) {
fprintf(stderr, "bspatch: Out of memory.\n");
log_msg(LOG_ERR, 0, "bspatch: Out of memory.\n");
if (diffdata) slab_free(NULL, diffdata);
if (extradata) slab_free(NULL, extradata);
return (0);
@ -158,7 +158,7 @@ bspatch(u_char *pbuf, u_char *oldbuf, bsize_t oldsize, u_char *newbuf, bsize_t *
/* Ctrl data will be RLE-d if RLE size is less. */
ctrldata = (u_char *)slab_alloc(NULL, ctrllen);
if (ctrldata == NULL) {
fprintf(stderr, "bspatch: Out of memory.\n");
log_msg(LOG_ERR, 0, "bspatch: Out of memory.\n");
slab_free(NULL, diffdata);
slab_free(NULL, extradata);
return (0);
@ -166,7 +166,7 @@ bspatch(u_char *pbuf, u_char *oldbuf, bsize_t oldsize, u_char *newbuf, bsize_t *
len = ctrllen;
if (zero_rle_decode(pbuf + hdrsz, lzctrllen, ctrldata, &len) == -1 ||
len != ctrllen) {
fprintf(stderr, "bspatch: Failed to decompress control data.\n");
log_msg(LOG_ERR, 0, "bspatch: Failed to decompress control data.\n");
rv = 0;
goto out;
}
@ -177,7 +177,7 @@ bspatch(u_char *pbuf, u_char *oldbuf, bsize_t oldsize, u_char *newbuf, bsize_t *
len = datalen;
if (zero_rle_decode(pbuf + hdrsz + lzctrllen, lzdatalen, diffdata, &len) == -1 ||
len != datalen) {
fprintf(stderr, "bspatch: Failed to decompress diff data.\n");
log_msg(LOG_ERR, 0, "bspatch: Failed to decompress diff data.\n");
rv = 0;
goto out;
}
@ -190,7 +190,7 @@ bspatch(u_char *pbuf, u_char *oldbuf, bsize_t oldsize, u_char *newbuf, bsize_t *
} else if (zero_rle_decode(pbuf + hdrsz + lzctrllen + lzdatalen, lzextralen, extradata, &len) == -1 ||
len != extralen) {
fprintf(stderr, "bspatch: Failed to decompress extra data.\n");
log_msg(LOG_ERR, 0, "bspatch: Failed to decompress extra data.\n");
rv = 0;
goto out;
}
@ -207,7 +207,7 @@ bspatch(u_char *pbuf, u_char *oldbuf, bsize_t oldsize, u_char *newbuf, bsize_t *
for(i=0;i<=2;i++) {
lenread = BUFREAD(&cpf, buf, 4);
if (lenread < 4) {
fprintf(stderr, "2: Corrupt diff data\n");
log_msg(LOG_ERR, 0, "2: Corrupt diff data\n");
rv = 0;
goto out;
}
@ -216,7 +216,7 @@ bspatch(u_char *pbuf, u_char *oldbuf, bsize_t oldsize, u_char *newbuf, bsize_t *
/* Sanity-check */
if(newpos+ctrl[0]>newsize) {
fprintf(stderr, "3: Corrupt diff data\n");
log_msg(LOG_ERR, 0, "3: Corrupt diff data\n");
rv = 0;
goto out;
}
@ -224,7 +224,7 @@ bspatch(u_char *pbuf, u_char *oldbuf, bsize_t oldsize, u_char *newbuf, bsize_t *
/* Read diff string */
lenread = BUFREAD(&dpf, newbuf + newpos, ctrl[0]);
if (lenread < ctrl[0]) {
fprintf(stderr, "4: Corrupt diff data\n");
log_msg(LOG_ERR, 0, "4: Corrupt diff data\n");
rv = 0;
goto out;
}
@ -240,7 +240,7 @@ bspatch(u_char *pbuf, u_char *oldbuf, bsize_t oldsize, u_char *newbuf, bsize_t *
/* Sanity-check */
if(newpos+ctrl[1]>newsize) {
fprintf(stderr, "5: Corrupt diff data\n");
log_msg(LOG_ERR, 0, "5: Corrupt diff data\n");
rv = 0;
goto out;
}
@ -248,7 +248,7 @@ bspatch(u_char *pbuf, u_char *oldbuf, bsize_t oldsize, u_char *newbuf, bsize_t *
/* Read extra string */
lenread = BUFREAD(&epf, newbuf + newpos, ctrl[1]);
if (lenread < ctrl[1]) {
fprintf(stderr, "6: Corrupt diff data\n");
log_msg(LOG_ERR, 0, "6: Corrupt diff data\n");
rv = 0;
goto out;
}

View file

@ -110,18 +110,18 @@ zero_rle_decode(const void* ibuf, unsigned int ilen,
if (count & ZERO_MASK) {
count &= DATA_MASK;
if (pos2 + count > *olen) {
fprintf(stderr, "Output buffer overflow in Zero RLE decode.\n");
log_msg(LOG_ERR, 0, "Output buffer overflow in Zero RLE decode.\n");
return (-1);
}
memset(ob+pos2, 0, count);
pos2 += count;
} else {
if (pos1 + count > ilen) {
fprintf(stderr, "Input underflow in Zero RLE decode.\n");
log_msg(LOG_ERR, 0, "Input underflow in Zero RLE decode.\n");
return (-1);
}
if (pos2 + count > *olen) {
fprintf(stderr, "Output buffer overflow in Zero RLE decode.\n");
log_msg(LOG_ERR, 0, "Output buffer overflow in Zero RLE decode.\n");
return (-1);
}
memcpy(ob+pos2, ib+pos1, count);

View file

@ -68,28 +68,28 @@ bzerr(int err)
{
switch (err) {
case BZ_SEQUENCE_ERROR:
fprintf(stderr, "Bzip2: Call sequence error, buggy code ?\n");
log_msg(LOG_ERR, 0, "Bzip2: Call sequence error, buggy code ?\n");
break;
case BZ_PARAM_ERROR:
fprintf(stderr, "Bzip2: Invalid parameter\n");
log_msg(LOG_ERR, 0, "Bzip2: Invalid parameter\n");
break;
case BZ_MEM_ERROR:
fprintf(stderr, "Bzip2: Out of memory\n");
log_msg(LOG_ERR, 0, "Bzip2: Out of memory\n");
break;
case BZ_DATA_ERROR:
fprintf(stderr, "Bzip2: Data integrity checksum error\n");
log_msg(LOG_ERR, 0, "Bzip2: Data integrity checksum error\n");
break;
case BZ_DATA_ERROR_MAGIC:
fprintf(stderr, "Bzip2: Invalid magic number in compressed buf\n");
log_msg(LOG_ERR, 0, "Bzip2: Invalid magic number in compressed buf\n");
break;
case BZ_OUTBUFF_FULL:
fprintf(stderr, "Bzip2: Output buffer overflow\n");
log_msg(LOG_ERR, 0, "Bzip2: Output buffer overflow\n");
break;
case BZ_CONFIG_ERROR:
fprintf(stderr, "Bzip2: Improper library config on platform\n");
log_msg(LOG_ERR, 0, "Bzip2: Improper library config on platform\n");
break;
default:
fprintf(stderr, "Bzip2: Unknown error code: %d\n", err);
log_msg(LOG_ERR, 0, "Bzip2: Unknown error code: %d\n", err);
}
}

View file

@ -113,14 +113,14 @@ aes_init(aes_ctx_t *ctx, uchar_t *salt, int saltlen, uchar_t *pwd, int pwd_len,
pickparams(&logN, &r, &p);
N = (uint64_t)(1) << logN;
if (crypto_scrypt(pwd, pwd_len, salt, saltlen, N, r, p, key, ctx->keylen)) {
fprintf(stderr, "Scrypt failed\n");
log_msg(LOG_ERR, 0, "Scrypt failed\n");
return (-1);
}
#else
rv = PKCS5_PBKDF2_HMAC(pwd, pwd_len, salt, saltlen, PBE_ROUNDS, EVP_sha256(),
ctx->keylen, key);
if (rv != ctx->keylen) {
fprintf(stderr, "Key size is %d bytes - should be %d bits\n", i, ctx->keylen);
log_msg(LOG_ERR, 0, "Key size is %d bytes - should be %d bits\n", i, ctx->keylen);
return (-1);
}
#endif
@ -177,7 +177,7 @@ aes_encrypt(aes_ctx_t *ctx, uchar_t *plaintext, uchar_t *ciphertext, uint64_t le
// Init counter mode AES from scrypt
strm = crypto_aesctr_init(&key, ctx->nonce + id);
if (!strm) {
fprintf(stderr, "Failed to init counter mode AES\n");
log_msg(LOG_ERR, 0, "Failed to init counter mode AES\n");
return (-1);
}
crypto_aesctr_stream(strm, plaintext, ciphertext, len);
@ -204,7 +204,7 @@ aes_decrypt(aes_ctx_t *ctx, uchar_t *ciphertext, uchar_t *plaintext, uint64_t le
// Init counter mode AES from scrypt
strm = crypto_aesctr_init(&key, ctx->nonce + id);
if (!strm) {
fprintf(stderr, "Failed to init counter mode AES\n");
log_msg(LOG_ERR, 0, "Failed to init counter mode AES\n");
return (-1);
}
crypto_aesctr_stream(strm, ciphertext, plaintext, len);

View file

@ -801,12 +801,12 @@ init_crypto(crypto_ctx_t *cctx, uchar_t *pwd, int pwd_len, int crypto_alg,
*/
if (crypto_alg == CRYPTO_ALG_AES) {
if (aes_init(actx, salt, 32, pwd, pwd_len, 0, enc_dec) != 0) {
fprintf(stderr, "Failed to initialize AES context\n");
log_msg(LOG_ERR, 0, "Failed to initialize AES context\n");
return (-1);
}
} else {
if (salsa20_init(sctx, salt, 32, pwd, pwd_len, 0, enc_dec) != 0) {
fprintf(stderr, "Failed to initialize SALSA20 context\n");
log_msg(LOG_ERR, 0, "Failed to initialize SALSA20 context\n");
return (-1);
}
}
@ -816,7 +816,7 @@ init_crypto(crypto_ctx_t *cctx, uchar_t *pwd, int pwd_len, int crypto_alg,
* Pass given nonce and salt.
*/
if (saltlen > MAX_SALTLEN) {
fprintf(stderr, "Salt too long. Max allowed length is %d\n",
log_msg(LOG_ERR, 0, "Salt too long. Max allowed length is %d\n",
MAX_SALTLEN);
free(actx);
return (-1);
@ -827,12 +827,12 @@ init_crypto(crypto_ctx_t *cctx, uchar_t *pwd, int pwd_len, int crypto_alg,
if (crypto_alg == CRYPTO_ALG_AES) {
if (aes_init(actx, cctx->salt, saltlen, pwd, pwd_len, U64_P(nonce),
enc_dec) != 0) {
fprintf(stderr, "Failed to initialize AES context\n");
log_msg(LOG_ERR, 0, "Failed to initialize AES context\n");
return (-1);
}
} else {
if (salsa20_init(sctx, salt, 32, pwd, pwd_len, nonce, enc_dec) != 0) {
fprintf(stderr, "Failed to initialize SALSA20 context\n");
log_msg(LOG_ERR, 0, "Failed to initialize SALSA20 context\n");
return (-1);
}
}
@ -847,7 +847,7 @@ init_crypto(crypto_ctx_t *cctx, uchar_t *pwd, int pwd_len, int crypto_alg,
actx = NULL;
sctx = NULL;
} else {
fprintf(stderr, "Unrecognized algorithm code: %d\n", crypto_alg);
log_msg(LOG_ERR, 0, "Unrecognized algorithm code: %d\n", crypto_alg);
return (-1);
}
return (0);
@ -869,7 +869,7 @@ crypto_buf(crypto_ctx_t *cctx, uchar_t *from, uchar_t *to, uint64_t bytes, uint6
return (salsa20_decrypt((salsa20_ctx_t *)(cctx->crypto_ctx), from, to, bytes, id));
}
} else {
fprintf(stderr, "Unrecognized algorithm code: %d\n", cctx->crypto_alg);
log_msg(LOG_ERR, 0, "Unrecognized algorithm code: %d\n", cctx->crypto_alg);
return (-1);
}
return (0);

View file

@ -75,20 +75,20 @@ salsa20_init(salsa20_ctx_t *ctx, uchar_t *salt, int saltlen, uchar_t *pwd, int p
uint64_t N;
if (XSALSA20_CRYPTO_NONCEBYTES % 8) {
fprintf(stderr, "XSALSA20_CRYPTO_NONCEBYTES is not a multiple of 8!\n");
log_msg(LOG_ERR, 0, "XSALSA20_CRYPTO_NONCEBYTES is not a multiple of 8!\n");
return (-1);
}
pickparams(&logN, &r, &p);
N = (uint64_t)(1) << logN;
if (crypto_scrypt(pwd, pwd_len, salt, saltlen, N, r, p, key, ctx->keylen)) {
fprintf(stderr, "Scrypt failed\n");
log_msg(LOG_ERR, 0, "Scrypt failed\n");
return (-1);
}
#else
rv = PKCS5_PBKDF2_HMAC(pwd, pwd_len, salt, saltlen, PBE_ROUNDS, EVP_sha256(),
ctx->keylen, key);
if (rv != ctx->keylen) {
fprintf(stderr, "Key size is %d bytes - should be %d bits\n", i, ctx->keylen);
log_msg(LOG_ERR, 0, "Key size is %d bytes - should be %d bits\n", i, ctx->keylen);
return (-1);
}
#endif

View file

@ -449,7 +449,7 @@ delta2_decode(uchar_t *src, uint64_t srclen, uchar_t *dst, uint64_t *dstlen)
last = pos + srclen;
olen = LE64(U64_P(pos));
if (*dstlen < olen) {
fprintf(stderr, "DELTA2 Decode: Destination buffer too small.\n");
log_msg(LOG_ERR, 0, "DELTA2 Decode: Destination buffer too small.\n");
return (-1);
}
@ -468,7 +468,7 @@ delta2_decode(uchar_t *src, uint64_t srclen, uchar_t *dst, uint64_t *dstlen)
rcnt = val & MSB_SETZERO_MASK;
pos += sizeof (rcnt);
if (out + rcnt > *dstlen) {
fprintf(stderr, "DELTA2 Decode(lit): Destination buffer overflow. Corrupt data.\n");
log_msg(LOG_ERR, 0, "DELTA2 Decode(lit): Destination buffer overflow. Corrupt data.\n");
return (-1);
}
memcpy(pos1, pos, rcnt);
@ -485,7 +485,7 @@ delta2_decode(uchar_t *src, uint64_t srclen, uchar_t *dst, uint64_t *dstlen)
delta = LE64(U64_P(pos));
pos += sizeof (delta);
if (out + rcnt > *dstlen) {
fprintf(stderr, "DELTA2 Decode(delta): Destination buffer overflow. Corrupt data.\n");
log_msg(LOG_ERR, 0, "DELTA2 Decode(delta): Destination buffer overflow. Corrupt data.\n");
return (-1);
}

View file

@ -46,19 +46,19 @@ static void
libbsc_err(int err) {
switch (err) {
case LIBBSC_BAD_PARAMETER:
fprintf(stderr, "LIBBSC: Bad Parameter.\n");
log_msg(LOG_ERR, 0, "LIBBSC: Bad Parameter.\n");
break;
case LIBBSC_NOT_ENOUGH_MEMORY:
fprintf(stderr, "LIBBSC: Out of memory.\n");
log_msg(LOG_ERR, 0, "LIBBSC: Out of memory.\n");
break;
case LIBBSC_NOT_SUPPORTED:
fprintf(stderr, "LIBBSC: Using unsupported feature.\n");
log_msg(LOG_ERR, 0, "LIBBSC: Using unsupported feature.\n");
break;
case LIBBSC_UNEXPECTED_EOB:
fprintf(stderr, "LIBBSC: Unexpected end of block.\n");
log_msg(LOG_ERR, 0, "LIBBSC: Unexpected end of block.\n");
break;
case LIBBSC_DATA_CORRUPT:
fprintf(stderr, "LIBBSC: Corrupt data.\n");
log_msg(LOG_ERR, 0, "LIBBSC: Corrupt data.\n");
break;
}
}
@ -96,7 +96,7 @@ libbsc_init(void **data, int *level, int nthreads, uint64_t chunksize,
int rv;
if (chunksize > BSC_MAX_CHUNK) {
fprintf(stderr, "Max allowed chunk size for LIBBSC is: %s \n",
log_msg(LOG_ERR, 0, "Max allowed chunk size for LIBBSC is: %s \n",
bytes_to_size(BSC_MAX_CHUNK));
return (1);
}

View file

@ -70,7 +70,7 @@ lz4_init(void **data, int *level, int nthreads, uint64_t chunksize,
int lev;
if (chunksize > LZ4_MAX_CHUNK) {
fprintf(stderr, "Max allowed chunk size for LZ4 is: %ld \n",
log_msg(LOG_ERR, 0, "Max allowed chunk size for LZ4 is: %ld \n",
LZ4_MAX_CHUNK);
return (1);
}

View file

@ -55,7 +55,7 @@ lz_fx_init(void **data, int *level, int nthreads, uint64_t chunksize,
int lev;
if (chunksize > UINT_MAX) {
fprintf(stderr, "Chunk size too big for LZFX.\n");
log_msg(LOG_ERR, 0, "Chunk size too big for LZFX.\n");
return (1);
}
lzdat = (struct lzfx_params *)slab_alloc(NULL, sizeof (struct lzfx_params));
@ -86,19 +86,19 @@ lz_fx_err(int err)
{
switch (err) {
case LZFX_ESIZE:
fprintf(stderr, "LZFX: Output buffer too small.\n");
log_msg(LOG_ERR, 0, "LZFX: Output buffer too small.\n");
break;
case LZFX_ECORRUPT:
fprintf(stderr, "LZFX: Corrupt data for decompression.\n");
log_msg(LOG_ERR, 0, "LZFX: Corrupt data for decompression.\n");
break;
case LZFX_EARGS:
fprintf(stderr, "LZFX: Invalid arguments.\n");
log_msg(LOG_ERR, 0, "LZFX: Invalid arguments.\n");
break;
case LZFX_ENOMEM:
fprintf(stderr, "LZFX: Out of memory when allocating hashtable.\n");
log_msg(LOG_ERR, 0, "LZFX: Out of memory when allocating hashtable.\n");
break;
default:
fprintf(stderr, "LZFX: Unknown error code: %d\n", err);
log_msg(LOG_ERR, 0, "LZFX: Unknown error code: %d\n", err);
}
}

View file

@ -152,36 +152,36 @@ lzerr(int err, int cmp)
{
switch (err) {
case SZ_ERROR_MEM:
fprintf(stderr, "LZMA: Memory allocation error\n");
log_msg(LOG_ERR, 0, "LZMA: Memory allocation error\n");
break;
case SZ_ERROR_PARAM:
fprintf(stderr, "LZMA: Incorrect paramater\n");
log_msg(LOG_ERR, 0, "LZMA: Incorrect paramater\n");
break;
case SZ_ERROR_WRITE:
fprintf(stderr, "LZMA: Write callback error\n");
log_msg(LOG_ERR, 0, "LZMA: Write callback error\n");
break;
case SZ_ERROR_PROGRESS:
fprintf(stderr, "LZMA: Progress callback errored\n");
log_msg(LOG_ERR, 0, "LZMA: Progress callback errored\n");
break;
case SZ_ERROR_INPUT_EOF:
fprintf(stderr, "LZMA: More compressed input bytes expected\n");
log_msg(LOG_ERR, 0, "LZMA: More compressed input bytes expected\n");
break;
case SZ_ERROR_OUTPUT_EOF:
/* This error is non-fatal during compression */
if (!cmp)
fprintf(stderr, "LZMA: Output buffer overflow\n");
log_msg(LOG_ERR, 0, "LZMA: Output buffer overflow\n");
break;
case SZ_ERROR_UNSUPPORTED:
fprintf(stderr, "LZMA: Unsupported properties\n");
log_msg(LOG_ERR, 0, "LZMA: Unsupported properties\n");
break;
case SZ_ERROR_DESTLEN:
fprintf(stderr, "LZMA: Output chunk size too small\n");
log_msg(LOG_ERR, 0, "LZMA: Output chunk size too small\n");
break;
case SZ_ERROR_DATA:
fprintf(stderr, "LZMA: Data Error\n");
log_msg(LOG_ERR, 0, "LZMA: Data Error\n");
break;
default:
fprintf(stderr, "LZMA: Unknown error code: %d\n", err);
log_msg(LOG_ERR, 0, "LZMA: Unknown error code: %d\n", err);
}
}

5
main.c
View file

@ -37,6 +37,7 @@
#include <libgen.h>
#include <pcompress.h>
#include <ctype.h>
#include <utils.h>
int
main(int argc, char *argv[])
@ -49,8 +50,8 @@ main(int argc, char *argv[])
err = init_pc_context(pctx, argc, argv);
if (err != 0 && err != 2) {
fprintf(stderr, "Invalid arguments to pcompress.\n");
fprintf(stderr, "Please see usage.\n");
log_msg(LOG_ERR, 0, "Invalid arguments to pcompress.\n");
log_msg(LOG_ERR, 0, "Please see usage.\n");
destroy_pc_context(pctx);
return (err);

View file

@ -170,18 +170,18 @@ usage(pc_ctx_t *pctx)
static void
show_compression_stats(pc_ctx_t *pctx)
{
fprintf(stderr, "\nCompression Statistics\n");
fprintf(stderr, "======================\n");
fprintf(stderr, "Total chunks : %u\n", pctx->chunk_num);
log_msg(LOG_INFO, 0, "\nCompression Statistics\n");
log_msg(LOG_INFO, 0, "======================\n");
log_msg(LOG_INFO, 0, "Total chunks : %u\n", pctx->chunk_num);
if (pctx->chunk_num == 0) {
fprintf(stderr, "No statistics to display.\n");
log_msg(LOG_INFO, 0, "No statistics to display.\n");
} else {
fprintf(stderr, "Best compressed chunk : %s(%.2f%%)\n",
log_msg(LOG_INFO, 0, "Best compressed chunk : %s(%.2f%%)\n",
bytes_to_size(pctx->smallest_chunk), (double)pctx->smallest_chunk/(double)pctx->chunksize*100);
fprintf(stderr, "Worst compressed chunk : %s(%.2f%%)\n",
log_msg(LOG_INFO, 0, "Worst compressed chunk : %s(%.2f%%)\n",
bytes_to_size(pctx->largest_chunk), (double)pctx->largest_chunk/(double)pctx->chunksize*100);
pctx->avg_chunk /= pctx->chunk_num;
fprintf(stderr, "Avg compressed chunk : %s(%.2f%%)\n\n",
log_msg(LOG_INFO, 0, "Avg compressed chunk : %s(%.2f%%)\n\n",
bytes_to_size(pctx->avg_chunk), (double)pctx->avg_chunk/(double)pctx->chunksize*100);
}
}
@ -272,7 +272,7 @@ preproc_compress(pc_ctx_t *pctx, compress_func_ptr cmp_func, void *src, uint64_t
/*
* Execution won't come here but just in case ...
*/
fprintf(stderr, "Invalid preprocessing mode\n");
log_msg(LOG_ERR, 0, "Invalid preprocessing mode\n");
return (-1);
}
@ -360,14 +360,14 @@ preproc_decompress(pc_ctx_t *pctx, compress_func_ptr dec_func, void *src, uint64
result = lzp_decompress((const uchar_t *)src, (uchar_t *)dst, srclen,
hashsize, LZP_DEFAULT_LZPMINLEN, 0);
if (result < 0) {
fprintf(stderr, "LZP decompression failed.\n");
log_msg(LOG_ERR, 0, "LZP decompression failed.\n");
return (-1);
}
*dstlen = result;
}
if (!(type & (PREPROC_COMPRESSED | PREPROC_TYPE_DELTA2 | PREPROC_TYPE_LZP)) && type > 0) {
fprintf(stderr, "Invalid preprocessing flags: %d\n", type);
log_msg(LOG_ERR, 0, "Invalid preprocessing flags: %d\n", type);
return (-1);
}
return (0);
@ -447,7 +447,7 @@ redo:
/*
* HMAC verification failure is fatal.
*/
fprintf(stderr, "Chunk %d, HMAC verification failed\n", tdat->id);
log_msg(LOG_ERR, 0, "Chunk %d, HMAC verification failed\n", tdat->id);
pctx->main_cancel = 1;
tdat->len_cmp = 0;
pctx->t_errored = 1;
@ -497,7 +497,7 @@ redo:
/*
* Header CRC32 verification failure is fatal.
*/
fprintf(stderr, "Chunk %d, Header CRC verification failed\n", tdat->id);
log_msg(LOG_ERR, 0, "Chunk %d, Header CRC verification failed\n", tdat->id);
pctx->main_cancel = 1;
tdat->len_cmp = 0;
pctx->t_errored = 1;
@ -545,7 +545,7 @@ redo:
}
if (rv == -1) {
tdat->len_cmp = 0;
fprintf(stderr, "ERROR: Chunk %d, decompression failed.\n", tdat->id);
log_msg(LOG_ERR, 0, "ERROR: Chunk %d, decompression failed.\n", tdat->id);
pctx->t_errored = 1;
goto cont;
}
@ -595,7 +595,7 @@ redo:
if (rv == -1) {
tdat->len_cmp = 0;
fprintf(stderr, "ERROR: Chunk %d, decompression failed.\n", tdat->id);
log_msg(LOG_ERR, 0, "ERROR: Chunk %d, decompression failed.\n", tdat->id);
pctx->t_errored = 1;
goto cont;
}
@ -609,7 +609,7 @@ redo:
rctx->cbuf = tdat->compressed_chunk;
dedupe_decompress(rctx, tdat->uncompressed_chunk, &(tdat->len_cmp));
if (!rctx->valid) {
fprintf(stderr, "ERROR: Chunk %d, dedup recovery failed.\n", tdat->id);
log_msg(LOG_ERR, 0, "ERROR: Chunk %d, dedup recovery failed.\n", tdat->id);
rv = -1;
tdat->len_cmp = 0;
pctx->t_errored = 1;
@ -641,7 +641,7 @@ redo:
compute_checksum(checksum, pctx->cksum, tdat->uncompressed_chunk, _chunksize, tdat->cksum_mt, 1);
if (memcmp(checksum, tdat->checksum, pctx->cksum_bytes) != 0) {
tdat->len_cmp = 0;
fprintf(stderr, "ERROR: Chunk %d, checksums do not match.\n", tdat->id);
log_msg(LOG_ERR, 0, "ERROR: Chunk %d, checksums do not match.\n", tdat->id);
pctx->t_errored = 1;
}
}
@ -723,12 +723,12 @@ start_decompress(pc_ctx_t *pctx, const char *filename, const char *to_filename)
sbuf.st_size = 0;
} else {
if ((compfd = open(filename, O_RDONLY, 0)) == -1) {
err_print(1, "Cannot open: %s", filename);
log_msg(LOG_ERR, 1, "Cannot open: %s", filename);
return (1);
}
if (fstat(compfd, &sbuf) == -1) {
err_print(1, "Cannot stat: %s", filename);
log_msg(LOG_ERR, 1, "Cannot stat: %s", filename);
return (1);
}
if (sbuf.st_size == 0)
@ -737,7 +737,7 @@ start_decompress(pc_ctx_t *pctx, const char *filename, const char *to_filename)
if ((uncompfd = open(to_filename, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR)) == -1) {
close(compfd);
err_print(1, "Cannot open: %s", to_filename);
log_msg(LOG_ERR, 1, "Cannot open: %s", to_filename);
return (1);
}
} else {
@ -762,9 +762,9 @@ start_decompress(pc_ctx_t *pctx, const char *filename, const char *to_filename)
}
if (init_algo(pctx, algorithm, 0) != 0) {
if (pctx->pipe_mode || filename == NULL)
fprintf(stderr, "Input stream is not pcompressed.\n");
log_msg(LOG_ERR, 0, "Input stream is not pcompressed.\n");
else
fprintf(stderr, "%s is not a pcompressed file.\n", filename);
log_msg(LOG_ERR, 0, "%s is not a pcompressed file.\n", filename);
UNCOMP_BAIL;
}
pctx->algo = algorithm;
@ -786,23 +786,23 @@ start_decompress(pc_ctx_t *pctx, const char *filename, const char *to_filename)
* Check for ridiculous values (malicious tampering or otherwise).
*/
if (version > VERSION) {
fprintf(stderr, "Cannot handle newer archive version %d, capability %d\n",
log_msg(LOG_ERR, 0, "Cannot handle newer archive version %d, capability %d\n",
version, VERSION);
err = 1;
goto uncomp_done;
}
if (chunksize > EIGHTY_PCT(get_total_ram())) {
fprintf(stderr, "Chunk size must not exceed 80%% of total RAM.\n");
log_msg(LOG_ERR, 0, "Chunk size must not exceed 80%% of total RAM.\n");
err = 1;
goto uncomp_done;
}
if (level > MAX_LEVEL || level < 0) {
fprintf(stderr, "Invalid compression level in header: %d\n", level);
log_msg(LOG_ERR, 0, "Invalid compression level in header: %d\n", level);
err = 1;
goto uncomp_done;
}
if (version < VERSION-3) {
fprintf(stderr, "Unsupported version: %d\n", version);
log_msg(LOG_ERR, 0, "Unsupported version: %d\n", version);
err = 1;
goto uncomp_done;
}
@ -825,14 +825,14 @@ start_decompress(pc_ctx_t *pctx, const char *filename, const char *to_filename)
if (flags & FLAG_DEDUP_FIXED) {
if (version > 7) {
if (pctx->pipe_mode) {
fprintf(stderr, "Global Deduplication is not supported with pipe mode.\n");
log_msg(LOG_ERR, 0, "Global Deduplication is not supported with pipe mode.\n");
err = 1;
goto uncomp_done;
}
pctx->enable_rabin_global = 1;
dedupe_flag = RABIN_DEDUPE_FILE_GLOBAL;
} else {
fprintf(stderr, "Invalid file deduplication flags.\n");
log_msg(LOG_ERR, 0, "Invalid file deduplication flags.\n");
err = 1;
goto uncomp_done;
}
@ -857,7 +857,7 @@ start_decompress(pc_ctx_t *pctx, const char *filename, const char *to_filename)
if (pctx->cksum == CKSUM_BLAKE512) pctx->cksum = CKSUM_SKEIN512;
}
if (get_checksum_props(NULL, &(pctx->cksum), &(pctx->cksum_bytes), &(pctx->mac_bytes), 1) == -1) {
fprintf(stderr, "Invalid checksum algorithm code: %d. File corrupt ?\n", pctx->cksum);
log_msg(LOG_ERR, 0, "Invalid checksum algorithm code: %d. File corrupt ?\n", pctx->cksum);
UNCOMP_BAIL;
}
@ -899,7 +899,7 @@ start_decompress(pc_ctx_t *pctx, const char *filename, const char *to_filename)
} else if (pctx->encrypt_type == CRYPTO_ALG_SALSA20) {
noncelen = XSALSA20_CRYPTO_NONCEBYTES;
} else {
fprintf(stderr, "Invalid Encryption algorithm code: %d. File corrupt ?\n",
log_msg(LOG_ERR, 0, "Invalid Encryption algorithm code: %d. File corrupt ?\n",
pctx->encrypt_type);
UNCOMP_BAIL;
}
@ -963,7 +963,7 @@ start_decompress(pc_ctx_t *pctx, const char *filename, const char *to_filename)
free(salt2);
memset(salt1, 0, saltlen);
free(salt1);
err_print(0, "Failed to get password.\n");
log_msg(LOG_ERR, 0, "Failed to get password.\n");
UNCOMP_BAIL;
}
} else if (!pctx->user_pw) {
@ -1001,7 +1001,7 @@ start_decompress(pc_ctx_t *pctx, const char *filename, const char *to_filename)
memset(salt1, 0, saltlen);
free(salt1);
close(uncompfd); unlink(to_filename);
err_print(0, "Failed to get password.\n");
log_msg(LOG_ERR, 0, "Failed to get password.\n");
UNCOMP_BAIL;
}
close(fd);
@ -1016,7 +1016,7 @@ start_decompress(pc_ctx_t *pctx, const char *filename, const char *to_filename)
free(salt1);
memset(pctx->user_pw, 0, pctx->user_pw_len);
close(uncompfd); unlink(to_filename);
err_print(0, "Failed to initialize crypto\n");
log_msg(LOG_ERR, 0, "Failed to initialize crypto\n");
UNCOMP_BAIL;
}
memset(pctx->user_pw, 0, pctx->user_pw_len);
@ -1031,7 +1031,7 @@ start_decompress(pc_ctx_t *pctx, const char *filename, const char *to_filename)
free(salt1);
memset(pw, 0, MAX_PW_LEN);
close(uncompfd); unlink(to_filename);
err_print(0, "Failed to initialize crypto\n");
log_msg(LOG_ERR, 0, "Failed to initialize crypto\n");
UNCOMP_BAIL;
}
memset(pw, 0, MAX_PW_LEN);
@ -1045,7 +1045,7 @@ start_decompress(pc_ctx_t *pctx, const char *filename, const char *to_filename)
*/
if (hmac_init(&hdr_mac, pctx->cksum, &(pctx->crypto_ctx)) == -1) {
close(uncompfd); unlink(to_filename);
err_print(0, "Cannot initialize header hmac.\n");
log_msg(LOG_ERR, 0, "Cannot initialize header hmac.\n");
UNCOMP_BAIL;
}
hmac_update(&hdr_mac, (uchar_t *)pctx->algo, ALGO_SZ);
@ -1072,7 +1072,7 @@ start_decompress(pc_ctx_t *pctx, const char *filename, const char *to_filename)
memset(n1, 0, noncelen);
if (memcmp(hdr_hash2, hdr_hash1, pctx->mac_bytes) != 0) {
close(uncompfd); unlink(to_filename);
err_print(0, "Header verification failed! File tampered or wrong password.\n");
log_msg(LOG_ERR, 0, "Header verification failed! File tampered or wrong password.\n");
UNCOMP_BAIL;
}
} else if (version >= 5) {
@ -1102,7 +1102,7 @@ start_decompress(pc_ctx_t *pctx, const char *filename, const char *to_filename)
crc2 = lzma_crc32((uchar_t *)&d2, sizeof (level), crc2);
if (crc1 != crc2) {
close(uncompfd); unlink(to_filename);
err_print(0, "Header verification failed! File tampered or wrong password.\n");
log_msg(LOG_ERR, 0, "Header verification failed! File tampered or wrong password.\n");
UNCOMP_BAIL;
}
}
@ -1116,9 +1116,9 @@ start_decompress(pc_ctx_t *pctx, const char *filename, const char *to_filename)
set_threadcounts(&props, &(pctx->nthreads), nprocs, DECOMPRESS_THREADS);
if (props.is_single_chunk)
pctx->nthreads = 1;
fprintf(stderr, "Scaling to %d thread", pctx->nthreads * props.nthreads);
if (pctx->nthreads * props.nthreads > 1) fprintf(stderr, "s");
fprintf(stderr, "\n");
log_msg(LOG_INFO, 0, "Scaling to %d thread", pctx->nthreads * props.nthreads);
if (pctx->nthreads * props.nthreads > 1) log_msg(LOG_INFO, 0, "s");
log_msg(LOG_INFO, 0, "\n");
nprocs = pctx->nthreads;
slab_cache_add(compressed_chunksize);
slab_cache_add(chunksize);
@ -1128,7 +1128,7 @@ start_decompress(pc_ctx_t *pctx, const char *filename, const char *to_filename)
for (i = 0; i < nprocs; i++) {
dary[i] = (struct cmp_data *)slab_alloc(NULL, sizeof (struct cmp_data));
if (!dary[i]) {
fprintf(stderr, "1: Out of memory\n");
log_msg(LOG_ERR, 0, "1: Out of memory\n");
UNCOMP_BAIL;
}
tdat = dary[i];
@ -1182,7 +1182,7 @@ start_decompress(pc_ctx_t *pctx, const char *filename, const char *to_filename)
if (pctx->encrypt_type) {
if (hmac_init(&tdat->chunk_hmac, pctx->cksum, &(pctx->crypto_ctx)) == -1) {
fprintf(stderr, "Cannot initialize chunk hmac.\n");
log_msg(LOG_ERR, 0, "Cannot initialize chunk hmac.\n");
UNCOMP_BAIL;
}
}
@ -1246,7 +1246,7 @@ start_decompress(pc_ctx_t *pctx, const char *filename, const char *to_filename)
if (rb != sizeof (tdat->len_cmp)) {
if (rb < 0) perror("Read: ");
else
fprintf(stderr, "Incomplete chunk %d header,"
log_msg(LOG_ERR, 0, "Incomplete chunk %d header,"
"file corrupt\n", pctx->chunk_num);
UNCOMP_BAIL;
}
@ -1257,7 +1257,7 @@ start_decompress(pc_ctx_t *pctx, const char *filename, const char *to_filename)
* Check for ridiculous length.
*/
if (tdat->len_cmp > chunksize + 256) {
fprintf(stderr, "Compressed length too big for chunk: %d\n",
log_msg(LOG_ERR, 0, "Compressed length too big for chunk: %d\n",
pctx->chunk_num);
UNCOMP_BAIL;
}
@ -1287,7 +1287,7 @@ start_decompress(pc_ctx_t *pctx, const char *filename, const char *to_filename)
tdat->uncompressed_chunk = (uchar_t *)slab_alloc(NULL,
chunksize);
if (!tdat->compressed_chunk || !tdat->uncompressed_chunk) {
fprintf(stderr, "2: Out of memory\n");
log_msg(LOG_ERR, 0, "2: Out of memory\n");
UNCOMP_BAIL;
}
tdat->cmp_seg = tdat->uncompressed_chunk;
@ -1310,7 +1310,7 @@ start_decompress(pc_ctx_t *pctx, const char *filename, const char *to_filename)
perror("Read: ");
UNCOMP_BAIL;
} else {
fprintf(stderr, "Incomplete chunk %d, file corrupt.\n",
log_msg(LOG_ERR, 0, "Incomplete chunk %d, file corrupt.\n",
pctx->chunk_num);
UNCOMP_BAIL;
}
@ -1739,7 +1739,7 @@ start_compress(pc_ctx_t *pctx, const char *filename, uint64_t chunksize, int lev
pw_len = get_pw_string(pw,
"Please enter encryption password", 1);
if (pw_len == -1) {
err_print(0, "Failed to get password.\n");
log_msg(LOG_ERR, 0, "Failed to get password.\n");
return (1);
}
} else if (!pctx->user_pw) {
@ -1769,7 +1769,7 @@ start_compress(pc_ctx_t *pctx, const char *filename, uint64_t chunksize, int lev
}
}
if (pw_len == -1) {
err_print(1, "Failed to get password.\n");
log_msg(LOG_ERR, 1, "Failed to get password.\n");
return (1);
}
close(fd);
@ -1778,7 +1778,7 @@ start_compress(pc_ctx_t *pctx, const char *filename, uint64_t chunksize, int lev
if (init_crypto(&(pctx->crypto_ctx), pctx->user_pw, pctx->user_pw_len, pctx->encrypt_type,
NULL, 0, pctx->keylen, 0, ENCRYPT_FLAG) == -1) {
memset(pctx->user_pw, 0, pctx->user_pw_len);
err_print(0, "Failed to initialize crypto\n");
log_msg(LOG_ERR, 0, "Failed to initialize crypto\n");
return (1);
}
memset(pctx->user_pw, 0, pctx->user_pw_len);
@ -1788,7 +1788,7 @@ start_compress(pc_ctx_t *pctx, const char *filename, uint64_t chunksize, int lev
if (init_crypto(&(pctx->crypto_ctx), pw, pw_len, pctx->encrypt_type, NULL,
0, pctx->keylen, 0, ENCRYPT_FLAG) == -1) {
memset(pw, 0, MAX_PW_LEN);
err_print(0, "Failed to initialize crypto\n");
log_msg(LOG_ERR, 0, "Failed to initialize crypto\n");
return (1);
}
memset(pw, 0, MAX_PW_LEN);
@ -1808,19 +1808,19 @@ start_compress(pc_ctx_t *pctx, const char *filename, uint64_t chunksize, int lev
if (!pctx->pipe_mode) {
char *tmp;
if ((uncompfd = open(filename, O_RDONLY, 0)) == -1) {
err_print(1, "Cannot open: %s", filename);
log_msg(LOG_ERR, 1, "Cannot open: %s", filename);
return (1);
}
if (fstat(uncompfd, &sbuf) == -1) {
close(uncompfd);
err_print(1, "Cannot stat: %s", filename);
log_msg(LOG_ERR, 1, "Cannot stat: %s", filename);
return (1);
}
if (!S_ISREG(sbuf.st_mode)) {
close(uncompfd);
err_print(0, "File %s is not a regular file.\n", filename);
log_msg(LOG_ERR, 0, "File %s is not a regular file.\n", filename);
return (1);
}
@ -1991,24 +1991,24 @@ start_compress(pc_ctx_t *pctx, const char *filename, uint64_t chunksize, int lev
flags |= pctx->encrypt_type;
set_threadcounts(&props, &(pctx->nthreads), nprocs, COMPRESS_THREADS);
fprintf(stderr, "Scaling to %d thread", pctx->nthreads * props.nthreads);
if (pctx->nthreads * props.nthreads > 1) fprintf(stderr, "s");
log_msg(LOG_INFO, 0, "Scaling to %d thread", pctx->nthreads * props.nthreads);
if (pctx->nthreads * props.nthreads > 1) log_msg(LOG_INFO, 0, "s");
nprocs = pctx->nthreads;
fprintf(stderr, "\n");
log_msg(LOG_INFO, 0, "\n");
dary = (struct cmp_data **)slab_calloc(NULL, nprocs, sizeof (struct cmp_data *));
if ((pctx->enable_rabin_scan || pctx->enable_fixed_scan))
cread_buf = (uchar_t *)slab_alloc(NULL, compressed_chunksize);
else
cread_buf = (uchar_t *)slab_alloc(NULL, chunksize);
if (!cread_buf) {
fprintf(stderr, "3: Out of memory\n");
log_msg(LOG_ERR, 0, "3: Out of memory\n");
COMP_BAIL;
}
for (i = 0; i < nprocs; i++) {
dary[i] = (struct cmp_data *)slab_alloc(NULL, sizeof (struct cmp_data));
if (!dary[i]) {
fprintf(stderr, "4: Out of memory\n");
log_msg(LOG_ERR, 0, "4: Out of memory\n");
COMP_BAIL;
}
tdat = dary[i];
@ -2042,7 +2042,7 @@ start_compress(pc_ctx_t *pctx, const char *filename, uint64_t chunksize, int lev
if (pctx->encrypt_type) {
if (hmac_init(&tdat->chunk_hmac, pctx->cksum, &(pctx->crypto_ctx)) == -1) {
fprintf(stderr, "Cannot initialize chunk hmac.\n");
log_msg(LOG_ERR, 0, "Cannot initialize chunk hmac.\n");
COMP_BAIL;
}
}
@ -2146,7 +2146,7 @@ start_compress(pc_ctx_t *pctx, const char *filename, uint64_t chunksize, int lev
unsigned int hlen;
if (hmac_init(&hdr_mac, pctx->cksum, &(pctx->crypto_ctx)) == -1) {
fprintf(stderr, "Cannot initialize header hmac.\n");
log_msg(LOG_ERR, 0, "Cannot initialize header hmac.\n");
COMP_BAIL;
}
hmac_update(&hdr_mac, cread_buf, pos - cread_buf);
@ -2240,7 +2240,7 @@ start_compress(pc_ctx_t *pctx, const char *filename, uint64_t chunksize, int lev
tdat->compressed_chunk = tdat->cmp_seg + COMPRESSED_CHUNKSZ +
pctx->cksum_bytes + pctx->mac_bytes;
if (!tdat->cmp_seg || !tdat->uncompressed_chunk) {
fprintf(stderr, "5: Out of memory\n");
log_msg(LOG_ERR, 0, "5: Out of memory\n");
COMP_BAIL;
}
}
@ -2344,9 +2344,9 @@ comp_done:
rm_fname(tmpfile1);
}
if (filename)
fprintf(stderr, "Error compressing file: %s\n", filename);
log_msg(LOG_ERR, 0, "Error compressing file: %s\n", filename);
else
fprintf(stderr, "Error compressing\n");
log_msg(LOG_ERR, 0, "Error compressing\n");
} else {
/*
* Write a trailer of zero chunk length.
@ -2613,7 +2613,7 @@ init_pc_context(pc_ctx_t *pctx, int argc, char *argv[])
pctx->do_compress = 1;
pctx->algo = optarg;
if (init_algo(pctx, pctx->algo, 1) != 0) {
err_print(0, "Invalid algorithm %s\n", optarg);
log_msg(LOG_ERR, 0, "Invalid algorithm %s\n", optarg);
return (1);
}
break;
@ -2621,21 +2621,21 @@ init_pc_context(pc_ctx_t *pctx, int argc, char *argv[])
case 's':
ovr = parse_numeric(&chunksize, optarg);
if (ovr == 1) {
err_print(0, "Chunk size too large %s\n", optarg);
log_msg(LOG_ERR, 0, "Chunk size too large %s\n", optarg);
return (1);
} else if (ovr == 2) {
err_print(0, "Invalid number %s\n", optarg);
log_msg(LOG_ERR, 0, "Invalid number %s\n", optarg);
return (1);
}
pctx->chunksize = chunksize;
if (pctx->chunksize < MIN_CHUNK) {
err_print(0, "Minimum chunk size is %ld\n", MIN_CHUNK);
log_msg(LOG_ERR, 0, "Minimum chunk size is %ld\n", MIN_CHUNK);
return (1);
}
if (pctx->chunksize > EIGHTY_PCT(get_total_ram())) {
err_print(0, "Chunk size must not exceed 80%% of total RAM.\n");
log_msg(LOG_ERR, 0, "Chunk size must not exceed 80%% of total RAM.\n");
return (1);
}
break;
@ -2643,7 +2643,7 @@ init_pc_context(pc_ctx_t *pctx, int argc, char *argv[])
case 'l':
pctx->level = atoi(optarg);
if (pctx->level < 0 || pctx->level > MAX_LEVEL) {
err_print(0, "Compression level should be in range 0 - 14\n");
log_msg(LOG_ERR, 0, "Compression level should be in range 0 - 14\n");
return (1);
}
break;
@ -2651,7 +2651,7 @@ init_pc_context(pc_ctx_t *pctx, int argc, char *argv[])
case 'B':
pctx->rab_blk_size = atoi(optarg);
if (pctx->rab_blk_size < 0 || pctx->rab_blk_size > 5) {
err_print(0, "Average Dedupe block size must be in range 0 (2k), 1 (4k) .. 5 (64k)\n");
log_msg(LOG_ERR, 0, "Average Dedupe block size must be in range 0 (2k), 1 (4k) .. 5 (64k)\n");
return (1);
}
break;
@ -2663,7 +2663,7 @@ init_pc_context(pc_ctx_t *pctx, int argc, char *argv[])
case 't':
pctx->nthreads = atoi(optarg);
if (pctx->nthreads < 1 || pctx->nthreads > 256) {
err_print(0, "Thread count should be in range 1 - 256\n");
log_msg(LOG_ERR, 0, "Thread count should be in range 1 - 256\n");
return (1);
}
break;
@ -2695,7 +2695,7 @@ init_pc_context(pc_ctx_t *pctx, int argc, char *argv[])
case 'e':
pctx->encrypt_type = get_crypto_alg(optarg);
if (pctx->encrypt_type == 0) {
err_print(0, "Invalid encryption algorithm. Should be AES or SALSA20.\n", optarg);
log_msg(LOG_ERR, 0, "Invalid encryption algorithm. Should be AES or SALSA20.\n", optarg);
return (1);
}
break;
@ -2720,7 +2720,7 @@ init_pc_context(pc_ctx_t *pctx, int argc, char *argv[])
case 'k':
pctx->keylen = atoi(optarg);
if ((pctx->keylen != 16 && pctx->keylen != 32) || pctx->keylen > MAX_KEYLEN) {
err_print(0, "Encryption KEY length should be 16 or 32.\n", optarg);
log_msg(LOG_ERR, 0, "Encryption KEY length should be 16 or 32.\n", optarg);
return (1);
}
break;
@ -2728,7 +2728,7 @@ init_pc_context(pc_ctx_t *pctx, int argc, char *argv[])
case 'S':
if (get_checksum_props(optarg, &(pctx->cksum), &(pctx->cksum_bytes),
&(pctx->mac_bytes), 0) == -1) {
err_print(0, "Invalid checksum type %s\n", optarg);
log_msg(LOG_ERR, 0, "Invalid checksum type %s\n", optarg);
return (1);
}
break;
@ -2766,28 +2766,28 @@ init_pc_context(pc_ctx_t *pctx, int argc, char *argv[])
*/
num_rem = argc - my_optind;
if (pctx->pipe_mode && num_rem > 0 ) {
fprintf(stderr, "Filename(s) unexpected for pipe mode\n");
log_msg(LOG_ERR, 0, "Filename(s) unexpected for pipe mode\n");
return (1);
}
if ((pctx->enable_rabin_scan || pctx->enable_fixed_scan) && !pctx->do_compress) {
fprintf(stderr, "Deduplication is only used during compression.\n");
log_msg(LOG_ERR, 0, "Deduplication is only used during compression.\n");
return (1);
}
if (!pctx->enable_rabin_scan)
pctx->enable_rabin_split = 0;
if (pctx->enable_fixed_scan && (pctx->enable_rabin_scan || pctx->enable_delta_encode || pctx->enable_rabin_split)) {
fprintf(stderr, "Rabin Deduplication and Fixed block Deduplication are mutually exclusive\n");
log_msg(LOG_ERR, 0, "Rabin Deduplication and Fixed block Deduplication are mutually exclusive\n");
return (1);
}
if (!pctx->do_compress && pctx->encrypt_type) {
fprintf(stderr, "Encryption only makes sense when compressing!\n");
log_msg(LOG_ERR, 0, "Encryption only makes sense when compressing!\n");
return (1);
} else if (pctx->pipe_mode && pctx->encrypt_type && !pctx->pwd_file) {
fprintf(stderr, "Pipe mode requires password to be provided in a file.\n");
log_msg(LOG_ERR, 0, "Pipe mode requires password to be provided in a file.\n");
return (1);
}
@ -2801,12 +2801,12 @@ init_pc_context(pc_ctx_t *pctx, int argc, char *argv[])
}
if (pctx->enable_rabin_global && pctx->enable_delta_encode) {
fprintf(stderr, "Global Deduplication does not support Delta Compression.\n");
log_msg(LOG_ERR, 0, "Global Deduplication does not support Delta Compression.\n");
return (1);
}
if (num_rem == 0 && !pctx->pipe_mode) {
fprintf(stderr, "Expected at least one filename.\n");
log_msg(LOG_ERR, 0, "Expected at least one filename.\n");
return (1);
} else if (num_rem == 1 || num_rem == 2) {
@ -2814,7 +2814,7 @@ init_pc_context(pc_ctx_t *pctx, int argc, char *argv[])
char apath[MAXPATHLEN];
if ((pctx->filename = realpath(argv[my_optind], NULL)) == NULL) {
err_print(1, "%s", argv[my_optind]);
log_msg(LOG_ERR, 1, "%s", argv[my_optind]);
return (1);
}
@ -2833,7 +2833,7 @@ init_pc_context(pc_ctx_t *pctx, int argc, char *argv[])
/* Check if compressed file exists */
if (pctx->to_filename != NULL) {
err_print(0, "Compressed file %s exists\n", pctx->to_filename);
log_msg(LOG_ERR, 0, "Compressed file %s exists\n", pctx->to_filename);
free((void *)(pctx->to_filename));
return (1);
}
@ -2845,14 +2845,14 @@ init_pc_context(pc_ctx_t *pctx, int argc, char *argv[])
pctx->filename = NULL;
} else {
if ((pctx->filename = realpath(argv[my_optind], NULL)) == NULL) {
err_print(1, "%s", argv[my_optind]);
log_msg(LOG_ERR, 1, "%s", argv[my_optind]);
return (1);
}
}
my_optind++;
if ((pctx->to_filename = realpath(argv[my_optind], NULL)) != NULL) {
free((void *)(pctx->to_filename));
err_print(0, "File %s exists\n", argv[my_optind]);
log_msg(LOG_ERR, 0, "File %s exists\n", argv[my_optind]);
return (1);
}
pctx->to_filename = argv[my_optind];
@ -2860,7 +2860,7 @@ init_pc_context(pc_ctx_t *pctx, int argc, char *argv[])
return (1);
}
} else if (num_rem > 2) {
fprintf(stderr, "Too many filenames.\n");
log_msg(LOG_ERR, 0, "Too many filenames.\n");
return (1);
}
pctx->main_cancel = 0;
@ -2869,7 +2869,7 @@ init_pc_context(pc_ctx_t *pctx, int argc, char *argv[])
get_checksum_props(DEFAULT_CKSUM, &(pctx->cksum), &(pctx->cksum_bytes), &(pctx->mac_bytes), 0);
if ((pctx->enable_rabin_scan || pctx->enable_fixed_scan) && pctx->cksum == CKSUM_CRC64) {
fprintf(stderr, "CRC64 checksum is not suitable for Deduplication.\n");
log_msg(LOG_ERR, 0, "CRC64 checksum is not suitable for Deduplication.\n");
return (1);
}

View file

@ -86,7 +86,7 @@ ppmd_init(void **data, int *level, int nthreads, uint64_t chunksize,
_ppmd->Base = 0;
_ppmd->Size = 0;
if (!Ppmd8_Alloc(_ppmd, ppmd8_mem_sz[*level], &g_Alloc)) {
fprintf(stderr, "PPMD: Out of memory.\n");
log_msg(LOG_ERR, 0, "PPMD: Out of memory.\n");
return (-1);
}
Ppmd8_Construct(_ppmd);

View file

@ -222,7 +222,7 @@ read_config(char *configfile, archive_config_t *cfg)
if (strncmp(line, "CHUNKSZ", 7) == 0) {
int ck = atoi(pos);
if (ck < MIN_CK || ck > MAX_CK) {
fprintf(stderr, "Invalid Chunk Size: %d\n", ck);
log_msg(LOG_ERR, 0, "Invalid Chunk Size: %d\n", ck);
fclose(fh);
return (1);
}
@ -233,7 +233,7 @@ read_config(char *configfile, archive_config_t *cfg)
if (stat(pos, &sb) == -1) {
if (errno != ENOENT) {
perror(" ");
fprintf(stderr, "Invalid ROOTDIR.\n");
log_msg(LOG_ERR, 0, "Invalid ROOTDIR.\n");
fclose(fh);
return (1);
} else {
@ -241,7 +241,7 @@ read_config(char *configfile, archive_config_t *cfg)
strncpy(cfg->rootdir, pos, PATH_MAX);
}
} else {
fprintf(stderr, "Invalid ROOTDIR. It already exists.\n");
log_msg(LOG_ERR, 0, "Invalid ROOTDIR. It already exists.\n");
fclose(fh);
return (1);
}
@ -250,12 +250,12 @@ read_config(char *configfile, archive_config_t *cfg)
ssize_t arch_sz;
ovr = parse_numeric(&arch_sz, pos);
if (ovr == 1) {
fprintf(stderr, "ARCHIVESZ value too large.\n");
log_msg(LOG_ERR, 0, "ARCHIVESZ value too large.\n");
fclose(fh);
return (1);
}
if (ovr == 2) {
fprintf(stderr, "Invalid ARCHIVESZ value.\n");
log_msg(LOG_ERR, 0, "Invalid ARCHIVESZ value.\n");
fclose(fh);
return (1);
}
@ -268,21 +268,21 @@ read_config(char *configfile, archive_config_t *cfg)
} else if (strcmp(pos, "yes") == 0) {
cfg->verify_chunks = 1;
} else {
fprintf(stderr, "Invalid VERIFY setting. Must be either yes or no.\n");
log_msg(LOG_ERR, 0, "Invalid VERIFY setting. Must be either yes or no.\n");
fclose(fh);
return (1);
}
} else if (strncmp(line, "COMPRESS", 8) == 0) {
cfg->algo = get_compress_algo(pos);
if (cfg->algo == COMPRESS_INVALID) {
fprintf(stderr, "Invalid COMPRESS setting.\n");
log_msg(LOG_ERR, 0, "Invalid COMPRESS setting.\n");
fclose(fh);
return (1);
}
} else if (strncmp(line, "CHUNK_CKSUM", 11) == 0) {
cfg->chunk_cksum_type = get_cksum_type(pos);
if (cfg->chunk_cksum_type == CKSUM_INVALID) {
fprintf(stderr, "Invalid CHUNK_CKSUM setting.\n");
log_msg(LOG_ERR, 0, "Invalid CHUNK_CKSUM setting.\n");
fclose(fh);
return (1);
}

View file

@ -61,7 +61,7 @@ init_global_db(char *configfile)
cfg = calloc(1, sizeof (archive_config_t));
if (!cfg) {
fprintf(stderr, "Memory allocation failure\n");
log_msg(LOG_ERR, 0, "Memory allocation failure\n");
return (NULL);
}
@ -215,7 +215,7 @@ init_global_db_s(char *path, char *tmppath, uint32_t chunksize, uint64_t user_ch
index_t *indx;
if (path != NULL) {
fprintf(stderr, "Disk based index not yet implemented.\n");
log_msg(LOG_WARN, 0, "Disk based index not yet implemented.\n");
return (NULL);
}
cfg = calloc(1, sizeof (archive_config_t));

View file

@ -226,7 +226,7 @@ create_dedupe_context(uint64_t chunksize, uint64_t real_chunksize, int rab_blk_s
if ((ck = getenv("PCOMPRESS_CHUNK_HASH_GLOBAL")) != NULL) {
if (get_checksum_props(ck, &chunk_cksum, &cksum_bytes, &mac_bytes, 1) != 0 ||
strcmp(ck, "CRC64") == 0) {
fprintf(stderr, "Invalid PCOMPRESS_CHUNK_HASH_GLOBAL.\n");
log_msg(LOG_ERR, 0, "Invalid PCOMPRESS_CHUNK_HASH_GLOBAL.\n");
chunk_cksum = DEFAULT_CHUNK_CKSUM;
pthread_mutex_unlock(&init_lock);
return (NULL);
@ -235,7 +235,7 @@ create_dedupe_context(uint64_t chunksize, uint64_t real_chunksize, int rab_blk_s
if (chunk_cksum == 0) {
chunk_cksum = DEFAULT_CHUNK_CKSUM;
if (get_checksum_props(NULL, &chunk_cksum, &cksum_bytes, &mac_bytes, 0) != 0) {
fprintf(stderr, "Invalid default chunk checksum: %d\n", DEFAULT_CHUNK_CKSUM);
log_msg(LOG_ERR, 0, "Invalid default chunk checksum: %d\n", DEFAULT_CHUNK_CKSUM);
pthread_mutex_unlock(&init_lock);
return (NULL);
}
@ -256,19 +256,19 @@ create_dedupe_context(uint64_t chunksize, uint64_t real_chunksize, int rab_blk_s
* Rabin window size must be power of 2 for optimization.
*/
if (!ISP2(RAB_POLYNOMIAL_WIN_SIZE)) {
fprintf(stderr, "Rabin window size must be a power of 2 in range 4 <= x <= 64\n");
log_msg(LOG_ERR, 0, "Rabin window size must be a power of 2 in range 4 <= x <= 64\n");
return (NULL);
}
if (arc) {
if (chunksize < RAB_MIN_CHUNK_SIZE_GLOBAL) {
fprintf(stderr, "Minimum chunk size for Global Dedup must be %" PRIu64 " bytes\n",
log_msg(LOG_ERR, 0, "Minimum chunk size for Global Dedup must be %" PRIu64 " bytes\n",
RAB_MIN_CHUNK_SIZE_GLOBAL);
return (NULL);
}
} else {
if (chunksize < RAB_MIN_CHUNK_SIZE) {
fprintf(stderr, "Minimum chunk size for Dedup must be %" PRIu64 " bytes\n",
log_msg(LOG_ERR, 0, "Minimum chunk size for Dedup must be %" PRIu64 " bytes\n",
RAB_MIN_CHUNK_SIZE);
return (NULL);
}
@ -324,7 +324,7 @@ create_dedupe_context(uint64_t chunksize, uint64_t real_chunksize, int rab_blk_s
++(ctx->blknum);
if (ctx->blknum > RABIN_MAX_BLOCKS) {
fprintf(stderr, "Chunk size too large for dedup.\n");
log_msg(LOG_ERR, 0, "Chunk size too large for dedup.\n");
destroy_dedupe_context(ctx);
return (NULL);
}
@ -340,7 +340,7 @@ create_dedupe_context(uint64_t chunksize, uint64_t real_chunksize, int rab_blk_s
}
if(ctx == NULL || ctx->current_window_data == NULL ||
(ctx->blocks == NULL && real_chunksize > 0 && dedupe_flag != RABIN_DEDUPE_FILE_GLOBAL)) {
fprintf(stderr,
log_msg(LOG_ERR, 0,
"Could not allocate rabin polynomial context, out of memory\n");
destroy_dedupe_context(ctx);
return (NULL);
@ -351,7 +351,7 @@ create_dedupe_context(uint64_t chunksize, uint64_t real_chunksize, int rab_blk_s
arc->sub_intervals,
arc->similarity_cksum_sz);
if (!ctx->similarity_cksums) {
fprintf(stderr,
log_msg(LOG_ERR, 0,
"Could not allocate dedupe context, out of memory\n");
destroy_dedupe_context(ctx);
return (NULL);
@ -365,7 +365,7 @@ create_dedupe_context(uint64_t chunksize, uint64_t real_chunksize, int rab_blk_s
// The lzma_data member is not needed during decompression
if (!(ctx->lzma_data) && op == COMPRESS) {
fprintf(stderr,
log_msg(LOG_ERR, 0,
"Could not initialize LZMA data for dedupe index, out of memory\n");
destroy_dedupe_context(ctx);
return (NULL);
@ -1123,7 +1123,7 @@ process_blocks:
offset = U64_P(sim_ck);
if (db_segcache_map(cfg, ctx->id, &o_blks, &offset,
(uchar_t **)&seg_blocks) == -1) {
fprintf(stderr, "** Segment cache mmap failed.\n");
log_msg(LOG_ERR, 0, "** Segment cache mmap failed.\n");
ctx->valid = 0;
return (0);
}
@ -1575,7 +1575,7 @@ dedupe_decompress(dedupe_context_t *ctx, uchar_t *buf, uint64_t *size)
len &= RABIN_INDEX_VALUE;
if (sz + len > data_sz) {
fprintf(stderr, "Dedup data overflows chunk.\n");
log_msg(LOG_ERR, 0, "Dedup data overflows chunk.\n");
ctx->valid = 0;
break;
}
@ -1675,14 +1675,14 @@ dedupe_decompress(dedupe_context_t *ctx, uchar_t *buf, uint64_t *size)
newsz = data_sz - sz;
rv = bspatch(buf + ctx->blocks[blk]->offset, buf + pos1, len, pos2, &newsz);
if (rv == 0) {
fprintf(stderr, "Failed to bspatch block.\n");
log_msg(LOG_ERR, 0, "Failed to bspatch block.\n");
ctx->valid = 0;
break;
}
pos2 += newsz;
sz += newsz;
if (sz > data_sz) {
fprintf(stderr, "Dedup data overflows chunk.\n");
log_msg(LOG_ERR, 0, "Dedup data overflows chunk.\n");
ctx->valid = 0;
break;
}
@ -1696,13 +1696,13 @@ dedupe_decompress(dedupe_context_t *ctx, uchar_t *buf, uint64_t *size)
pos2 += len;
sz += len;
if (sz > data_sz) {
fprintf(stderr, "Dedup data overflows chunk.\n");
log_msg(LOG_ERR, 0, "Dedup data overflows chunk.\n");
ctx->valid = 0;
break;
}
}
if (ctx->valid && sz < data_sz) {
fprintf(stderr, "Too little dedup data processed.\n");
log_msg(LOG_ERR, 0, "Too little dedup data processed.\n");
ctx->valid = 0;
}
*size = data_sz;

View file

@ -48,6 +48,8 @@
#include "utils.h"
processor_info_t proc_info;
static int cur_log_level = 1;
static log_dest_t ldest = {LOG_OUTPUT, LOG_INFO, NULL};
void
init_pcompress() {
@ -55,6 +57,7 @@ init_pcompress() {
XXH32_module_init();
}
/*
void
err_exit(int show_errno, const char *format, ...)
{
@ -83,6 +86,7 @@ err_print(int show_errno, const char *format, ...)
if (show_errno)
fprintf(stderr, "\nError: %s\n", strerror(err));
}
*/
/*
* Fetch the command name that started the current process.
@ -448,3 +452,46 @@ chk_dir(char *dir)
}
return (1);
}
void DLL_EXPORT
set_log_dest(log_dest_t *dest)
{
ldest.type = dest->type;
ldest.fd = dest->fd;
ldest.cb = dest->cb;
}
void DLL_EXPORT
set_log_level(int level)
{
if (level >= 0 && level < 10)
cur_log_level = level;
}
void DLL_EXPORT
log_msg(log_level_t log_level, int show_errno, const char *format, ...)
{
int err = errno, written;
va_list args;
char msg[1024];
if (log_level <= cur_log_level) return;
va_start(args, format);
written = vsnprintf(msg, 1024, format, args);
va_end(args);
if (written < 1024 && show_errno) {
snprintf(msg + written, 1024 - written, "\nError: %s\n", strerror(err));
}
if (ldest.type == LOG_OUTPUT) {
fputs(msg, stderr);
} else if (ldest.type == LOG_FILE) {
int rv;
rv = write(ldest.fd, msg, strlen(msg));
} else {
ldest.cb(msg);
}
}

View file

@ -260,6 +260,32 @@ typedef int (*deinit_func_ptr)(void **data);
typedef void (*stats_func_ptr)(int show);
typedef void (*props_func_ptr)(algo_props_t *data, int level, uint64_t chunksize);
/*
* Logging definitions.
*/
typedef enum {
LOG_OUTPUT,
LOG_FILE,
LOG_CALLBACK
} log_dest_type_t;
typedef enum {
LOG_ERR,
LOG_WARN,
LOG_INFO
} log_level_t;
typedef void (*log_callback_ptr)(char *msg);
typedef struct {
log_dest_type_t type;
int fd;
log_callback_ptr cb;
} log_dest_t;
void set_log_dest(log_dest_t *dest);
void set_log_level(int level);
void log_msg(log_level_t log_level, int show_errno, const char *format, ...);
/*
* Roundup v to the nearest power of 2. From Bit Twiddling Hacks:

View file

@ -116,27 +116,27 @@ void zerr(int ret, int cmp)
perror(" ");
break;
case Z_STREAM_ERROR:
fprintf(stderr, "Zlib: Invalid stream structure\n");
log_msg(LOG_ERR, 0, "Zlib: Invalid stream structure\n");
break;
case Z_DATA_ERROR:
fprintf(stderr, "Zlib: Invalid or incomplete deflate data\n");
log_msg(LOG_ERR, 0, "Zlib: Invalid or incomplete deflate data\n");
break;
case Z_MEM_ERROR:
fprintf(stderr, "Zlib: Out of memory\n");
log_msg(LOG_ERR, 0, "Zlib: Out of memory\n");
break;
case Z_VERSION_ERROR:
fprintf(stderr, "Zlib: Version mismatch!\n");
log_msg(LOG_ERR, 0, "Zlib: Version mismatch!\n");
break;
case Z_BUF_ERROR:
/* This error is non-fatal during compression. */
if (!cmp)
fprintf(stderr, "Zlib: Buffer error decompression failed.\n");
log_msg(LOG_ERR, 0, "Zlib: Buffer error decompression failed.\n");
break;
case Z_NEED_DICT:
fprintf(stderr, "Zlib: Need present dictionary.\n");
log_msg(LOG_ERR, 0, "Zlib: Need present dictionary.\n");
break;
default:
fprintf(stderr, "Zlib: Unknown error code: %d\n", ret);
log_msg(LOG_ERR, 0, "Zlib: Unknown error code: %d\n", ret);
}
}