Image-PNG-Simple
view release on metacpan or search on metacpan
zlib-1.2.8/examples/gzlog.c view on Meta::CPAN
provide a dictionary for the next invocation of this procedure.
- Rewrite the extra field marking foo.gz with a compression in process.
* If there is no data provided to compress (due to a missing foo.add file
when recovering), reconstruct and truncate the foo.gz file to contain
only the previous compressed data and proceed to the step after the next
one. Otherwise ...
- Compress the data with the dictionary in foo.dict, and write to the
foo.gz file starting at the bit immediately following the last previously
compressed block. If there is no foo.dict, proceed anyway with the
compression at slightly reduced efficiency. (For the foo.dict file to be
missing requires some external failure beyond simply the interruption of
a compress operation.) During this process, the foo.lock file is
periodically touched to assure that that file is not considered stale by
another process before we're done. The deflation is terminated with a
non-last empty static block (10 bits long), that is then located and
written over by a last-bit-set empty stored block.
- Append the crc and length of the data in the gzip file (previously
calculated during the append operations).
- Write over the extra field with the updated stored block offsets, bits
back, crc's, and lengths, and mark foo.gz as in process for a replacement
of the dictionary.
@ Delete the foo.add file.
- Replace foo.dict with foo.temp.
- Write over the extra field, marking foo.gz as complete.
Recovery procedure:
- If not a replace recovery, read in the foo.add file, and provide that data
to the appropriate recovery below. If there is no foo.add file, provide
a zero data length to the recovery. In that case, the append recovery
restores the foo.gz to the previous compressed + uncompressed data state.
For the the compress recovery, a missing foo.add file results in foo.gz
being restored to the previous compressed-only data state.
- Append recovery:
- Pick up append at + step above
- Compress recovery:
- Pick up compress at * step above
- Replace recovery:
- Pick up compress at @ step above
- Log the repair with a date stamp in foo.repairs
*/
#include <sys/types.h>
#include <stdio.h> /* rename, fopen, fprintf, fclose */
#include <stdlib.h> /* malloc, free */
#include <string.h> /* strlen, strrchr, strcpy, strncpy, strcmp */
#include <fcntl.h> /* open */
#include <unistd.h> /* lseek, read, write, close, unlink, sleep, */
/* ftruncate, fsync */
#include <errno.h> /* errno */
#include <time.h> /* time, ctime */
#include <sys/stat.h> /* stat */
#include <sys/time.h> /* utimes */
#include "zlib.h" /* crc32 */
#include "gzlog.h" /* header for external access */
#define local static
typedef unsigned int uint;
typedef unsigned long ulong;
/* Macro for debugging to deterministically force recovery operations */
#ifdef DEBUG
#include <setjmp.h> /* longjmp */
jmp_buf gzlog_jump; /* where to go back to */
int gzlog_bail = 0; /* which point to bail at (1..8) */
int gzlog_count = -1; /* number of times through to wait */
# define BAIL(n) do { if (n == gzlog_bail && gzlog_count-- == 0) \
longjmp(gzlog_jump, gzlog_bail); } while (0)
#else
# define BAIL(n)
#endif
/* how old the lock file can be in seconds before considering it stale */
#define PATIENCE 300
/* maximum stored block size in Kbytes -- must be in 1..63 */
#define MAX_STORE 16
/* number of stored Kbytes to trigger compression (must be >= 32 to allow
dictionary construction, and <= 204 * MAX_STORE, in order for >> 10 to
discard the stored block headers contribution of five bytes each) */
#define TRIGGER 1024
/* size of a deflate dictionary (this cannot be changed) */
#define DICT 32768U
/* values for the operation (2 bits) */
#define NO_OP 0
#define APPEND_OP 1
#define COMPRESS_OP 2
#define REPLACE_OP 3
/* macros to extract little-endian integers from an unsigned byte buffer */
#define PULL2(p) ((p)[0]+((uint)((p)[1])<<8))
#define PULL4(p) (PULL2(p)+((ulong)PULL2(p+2)<<16))
#define PULL8(p) (PULL4(p)+((off_t)PULL4(p+4)<<32))
/* macros to store integers into a byte buffer in little-endian order */
#define PUT2(p,a) do {(p)[0]=a;(p)[1]=(a)>>8;} while(0)
#define PUT4(p,a) do {PUT2(p,a);PUT2(p+2,a>>16);} while(0)
#define PUT8(p,a) do {PUT4(p,a);PUT4(p+4,a>>32);} while(0)
/* internal structure for log information */
#define LOGID "\106\035\172" /* should be three non-zero characters */
struct log {
char id[4]; /* contains LOGID to detect inadvertent overwrites */
int fd; /* file descriptor for .gz file, opened read/write */
char *path; /* allocated path, e.g. "/var/log/foo" or "foo" */
char *end; /* end of path, for appending suffices such as ".gz" */
off_t first; /* offset of first stored block first length byte */
int back; /* location of first block id in bits back from first */
uint stored; /* bytes currently in last stored block */
off_t last; /* offset of last stored block first length byte */
ulong ccrc; /* crc of compressed data */
ulong clen; /* length (modulo 2^32) of compressed data */
ulong tcrc; /* crc of total data */
ulong tlen; /* length (modulo 2^32) of total data */
time_t lock; /* last modify time of our lock file */
};
/* gzip header for gzlog */
( run in 1.373 second using v1.01-cache-2.11-cpan-39bf76dae61 )