xref: /minix3/common/dist/zlib/examples/gzlog.c (revision 44bedb31d842b4b0444105519bcf929a69fe2dc1)
1*44bedb31SLionel Sambuc /*	$NetBSD: gzlog.c,v 1.1.1.1 2006/01/14 20:11:09 christos Exp $	*/
2*44bedb31SLionel Sambuc 
3*44bedb31SLionel Sambuc /*
4*44bedb31SLionel Sambuc  * gzlog.c
5*44bedb31SLionel Sambuc  * Copyright (C) 2004 Mark Adler
6*44bedb31SLionel Sambuc  * For conditions of distribution and use, see copyright notice in gzlog.h
7*44bedb31SLionel Sambuc  * version 1.0, 26 Nov 2004
8*44bedb31SLionel Sambuc  *
9*44bedb31SLionel Sambuc  */
10*44bedb31SLionel Sambuc 
11*44bedb31SLionel Sambuc #include <string.h>             /* memcmp() */
12*44bedb31SLionel Sambuc #include <stdlib.h>             /* malloc(), free(), NULL */
13*44bedb31SLionel Sambuc #include <sys/types.h>          /* size_t, off_t */
14*44bedb31SLionel Sambuc #include <unistd.h>             /* read(), close(), sleep(), ftruncate(), */
15*44bedb31SLionel Sambuc                                 /* lseek() */
16*44bedb31SLionel Sambuc #include <fcntl.h>              /* open() */
17*44bedb31SLionel Sambuc #include <sys/file.h>           /* flock() */
18*44bedb31SLionel Sambuc #include "zlib.h"               /* deflateInit2(), deflate(), deflateEnd() */
19*44bedb31SLionel Sambuc 
20*44bedb31SLionel Sambuc #include "gzlog.h"              /* interface */
21*44bedb31SLionel Sambuc #define local static
22*44bedb31SLionel Sambuc 
23*44bedb31SLionel Sambuc /* log object structure */
24*44bedb31SLionel Sambuc typedef struct {
25*44bedb31SLionel Sambuc     int id;                 /* object identifier */
26*44bedb31SLionel Sambuc     int fd;                 /* log file descriptor */
27*44bedb31SLionel Sambuc     off_t extra;            /* offset of extra "ap" subfield */
28*44bedb31SLionel Sambuc     off_t mark_off;         /* offset of marked data */
29*44bedb31SLionel Sambuc     off_t last_off;         /* offset of last block */
30*44bedb31SLionel Sambuc     unsigned long crc;      /* uncompressed crc */
31*44bedb31SLionel Sambuc     unsigned long len;      /* uncompressed length (modulo 2^32) */
32*44bedb31SLionel Sambuc     unsigned stored;        /* length of current stored block */
33*44bedb31SLionel Sambuc } gz_log;
34*44bedb31SLionel Sambuc 
35*44bedb31SLionel Sambuc #define GZLOGID 19334       /* gz_log object identifier */
36*44bedb31SLionel Sambuc 
37*44bedb31SLionel Sambuc #define LOCK_RETRY 1            /* retry lock once a second */
38*44bedb31SLionel Sambuc #define LOCK_PATIENCE 1200      /* try about twenty minutes before forcing */
39*44bedb31SLionel Sambuc 
40*44bedb31SLionel Sambuc /* acquire a lock on a file */
lock(int fd)41*44bedb31SLionel Sambuc local int lock(int fd)
42*44bedb31SLionel Sambuc {
43*44bedb31SLionel Sambuc     int patience;
44*44bedb31SLionel Sambuc 
45*44bedb31SLionel Sambuc     /* try to lock every LOCK_RETRY seconds for LOCK_PATIENCE seconds */
46*44bedb31SLionel Sambuc     patience = LOCK_PATIENCE;
47*44bedb31SLionel Sambuc     do {
48*44bedb31SLionel Sambuc         if (flock(fd, LOCK_EX + LOCK_NB) == 0)
49*44bedb31SLionel Sambuc             return 0;
50*44bedb31SLionel Sambuc         (void)sleep(LOCK_RETRY);
51*44bedb31SLionel Sambuc         patience -= LOCK_RETRY;
52*44bedb31SLionel Sambuc     } while (patience > 0);
53*44bedb31SLionel Sambuc 
54*44bedb31SLionel Sambuc     /* we've run out of patience -- give up */
55*44bedb31SLionel Sambuc     return -1;
56*44bedb31SLionel Sambuc }
57*44bedb31SLionel Sambuc 
58*44bedb31SLionel Sambuc /* release lock */
unlock(int fd)59*44bedb31SLionel Sambuc local void unlock(int fd)
60*44bedb31SLionel Sambuc {
61*44bedb31SLionel Sambuc     (void)flock(fd, LOCK_UN);
62*44bedb31SLionel Sambuc }
63*44bedb31SLionel Sambuc 
64*44bedb31SLionel Sambuc /* release a log object */
log_clean(gz_log * log)65*44bedb31SLionel Sambuc local void log_clean(gz_log *log)
66*44bedb31SLionel Sambuc {
67*44bedb31SLionel Sambuc     unlock(log->fd);
68*44bedb31SLionel Sambuc     (void)close(log->fd);
69*44bedb31SLionel Sambuc     free(log);
70*44bedb31SLionel Sambuc }
71*44bedb31SLionel Sambuc 
72*44bedb31SLionel Sambuc /* read an unsigned long from a byte buffer little-endian */
make_ulg(unsigned char * buf)73*44bedb31SLionel Sambuc local unsigned long make_ulg(unsigned char *buf)
74*44bedb31SLionel Sambuc {
75*44bedb31SLionel Sambuc     int n;
76*44bedb31SLionel Sambuc     unsigned long val;
77*44bedb31SLionel Sambuc 
78*44bedb31SLionel Sambuc     val = (unsigned long)(*buf++);
79*44bedb31SLionel Sambuc     for (n = 8; n < 32; n += 8)
80*44bedb31SLionel Sambuc         val += (unsigned long)(*buf++) << n;
81*44bedb31SLionel Sambuc     return val;
82*44bedb31SLionel Sambuc }
83*44bedb31SLionel Sambuc 
84*44bedb31SLionel Sambuc /* read an off_t from a byte buffer little-endian */
make_off(unsigned char * buf)85*44bedb31SLionel Sambuc local off_t make_off(unsigned char *buf)
86*44bedb31SLionel Sambuc {
87*44bedb31SLionel Sambuc     int n;
88*44bedb31SLionel Sambuc     off_t val;
89*44bedb31SLionel Sambuc 
90*44bedb31SLionel Sambuc     val = (off_t)(*buf++);
91*44bedb31SLionel Sambuc     for (n = 8; n < 64; n += 8)
92*44bedb31SLionel Sambuc         val += (off_t)(*buf++) << n;
93*44bedb31SLionel Sambuc     return val;
94*44bedb31SLionel Sambuc }
95*44bedb31SLionel Sambuc 
96*44bedb31SLionel Sambuc /* write an unsigned long little-endian to byte buffer */
dice_ulg(unsigned long val,unsigned char * buf)97*44bedb31SLionel Sambuc local void dice_ulg(unsigned long val, unsigned char *buf)
98*44bedb31SLionel Sambuc {
99*44bedb31SLionel Sambuc     int n;
100*44bedb31SLionel Sambuc 
101*44bedb31SLionel Sambuc     for (n = 0; n < 4; n++) {
102*44bedb31SLionel Sambuc         *buf++ = val & 0xff;
103*44bedb31SLionel Sambuc         val >>= 8;
104*44bedb31SLionel Sambuc     }
105*44bedb31SLionel Sambuc }
106*44bedb31SLionel Sambuc 
107*44bedb31SLionel Sambuc /* write an off_t little-endian to byte buffer */
dice_off(off_t val,unsigned char * buf)108*44bedb31SLionel Sambuc local void dice_off(off_t val, unsigned char *buf)
109*44bedb31SLionel Sambuc {
110*44bedb31SLionel Sambuc     int n;
111*44bedb31SLionel Sambuc 
112*44bedb31SLionel Sambuc     for (n = 0; n < 8; n++) {
113*44bedb31SLionel Sambuc         *buf++ = val & 0xff;
114*44bedb31SLionel Sambuc         val >>= 8;
115*44bedb31SLionel Sambuc     }
116*44bedb31SLionel Sambuc }
117*44bedb31SLionel Sambuc 
118*44bedb31SLionel Sambuc /* initial, empty gzip file for appending */
119*44bedb31SLionel Sambuc local char empty_gz[] = {
120*44bedb31SLionel Sambuc     0x1f, 0x8b,                 /* magic gzip id */
121*44bedb31SLionel Sambuc     8,                          /* compression method is deflate */
122*44bedb31SLionel Sambuc     4,                          /* there is an extra field */
123*44bedb31SLionel Sambuc     0, 0, 0, 0,                 /* no modification time provided */
124*44bedb31SLionel Sambuc     0, 0xff,                    /* no extra flags, no OS */
125*44bedb31SLionel Sambuc     20, 0, 'a', 'p', 16, 0,     /* extra field with "ap" subfield */
126*44bedb31SLionel Sambuc     32, 0, 0, 0, 0, 0, 0, 0,    /* offset of uncompressed data */
127*44bedb31SLionel Sambuc     32, 0, 0, 0, 0, 0, 0, 0,    /* offset of last block */
128*44bedb31SLionel Sambuc     1, 0, 0, 0xff, 0xff,        /* empty stored block (last) */
129*44bedb31SLionel Sambuc     0, 0, 0, 0,                 /* crc */
130*44bedb31SLionel Sambuc     0, 0, 0, 0                  /* uncompressed length */
131*44bedb31SLionel Sambuc };
132*44bedb31SLionel Sambuc 
133*44bedb31SLionel Sambuc /* initialize a log object with locking */
gzlog_open(char * path)134*44bedb31SLionel Sambuc void *gzlog_open(char *path)
135*44bedb31SLionel Sambuc {
136*44bedb31SLionel Sambuc     unsigned xlen;
137*44bedb31SLionel Sambuc     unsigned char temp[20];
138*44bedb31SLionel Sambuc     unsigned sub_len;
139*44bedb31SLionel Sambuc     int good;
140*44bedb31SLionel Sambuc     gz_log *log;
141*44bedb31SLionel Sambuc 
142*44bedb31SLionel Sambuc     /* allocate log structure */
143*44bedb31SLionel Sambuc     log = malloc(sizeof(gz_log));
144*44bedb31SLionel Sambuc     if (log == NULL)
145*44bedb31SLionel Sambuc         return NULL;
146*44bedb31SLionel Sambuc     log->id = GZLOGID;
147*44bedb31SLionel Sambuc 
148*44bedb31SLionel Sambuc     /* open file, creating it if necessary, and locking it */
149*44bedb31SLionel Sambuc     log->fd = open(path, O_RDWR | O_CREAT, 0600);
150*44bedb31SLionel Sambuc     if (log->fd < 0) {
151*44bedb31SLionel Sambuc         free(log);
152*44bedb31SLionel Sambuc         return NULL;
153*44bedb31SLionel Sambuc     }
154*44bedb31SLionel Sambuc     if (lock(log->fd)) {
155*44bedb31SLionel Sambuc         close(log->fd);
156*44bedb31SLionel Sambuc         free(log);
157*44bedb31SLionel Sambuc         return NULL;
158*44bedb31SLionel Sambuc     }
159*44bedb31SLionel Sambuc 
160*44bedb31SLionel Sambuc     /* if file is empty, write new gzip stream */
161*44bedb31SLionel Sambuc     if (lseek(log->fd, 0, SEEK_END) == 0) {
162*44bedb31SLionel Sambuc         if (write(log->fd, empty_gz, sizeof(empty_gz)) != sizeof(empty_gz)) {
163*44bedb31SLionel Sambuc             log_clean(log);
164*44bedb31SLionel Sambuc             return NULL;
165*44bedb31SLionel Sambuc         }
166*44bedb31SLionel Sambuc     }
167*44bedb31SLionel Sambuc 
168*44bedb31SLionel Sambuc     /* check gzip header */
169*44bedb31SLionel Sambuc     (void)lseek(log->fd, 0, SEEK_SET);
170*44bedb31SLionel Sambuc     if (read(log->fd, temp, 12) != 12 || temp[0] != 0x1f ||
171*44bedb31SLionel Sambuc         temp[1] != 0x8b || temp[2] != 8 || (temp[3] & 4) == 0) {
172*44bedb31SLionel Sambuc         log_clean(log);
173*44bedb31SLionel Sambuc         return NULL;
174*44bedb31SLionel Sambuc     }
175*44bedb31SLionel Sambuc 
176*44bedb31SLionel Sambuc     /* process extra field to find "ap" sub-field */
177*44bedb31SLionel Sambuc     xlen = temp[10] + (temp[11] << 8);
178*44bedb31SLionel Sambuc     good = 0;
179*44bedb31SLionel Sambuc     while (xlen) {
180*44bedb31SLionel Sambuc         if (xlen < 4 || read(log->fd, temp, 4) != 4)
181*44bedb31SLionel Sambuc             break;
182*44bedb31SLionel Sambuc         sub_len = temp[2];
183*44bedb31SLionel Sambuc         sub_len += temp[3] << 8;
184*44bedb31SLionel Sambuc         xlen -= 4;
185*44bedb31SLionel Sambuc         if (memcmp(temp, "ap", 2) == 0 && sub_len == 16) {
186*44bedb31SLionel Sambuc             good = 1;
187*44bedb31SLionel Sambuc             break;
188*44bedb31SLionel Sambuc         }
189*44bedb31SLionel Sambuc         if (xlen < sub_len)
190*44bedb31SLionel Sambuc             break;
191*44bedb31SLionel Sambuc         (void)lseek(log->fd, sub_len, SEEK_CUR);
192*44bedb31SLionel Sambuc         xlen -= sub_len;
193*44bedb31SLionel Sambuc     }
194*44bedb31SLionel Sambuc     if (!good) {
195*44bedb31SLionel Sambuc         log_clean(log);
196*44bedb31SLionel Sambuc         return NULL;
197*44bedb31SLionel Sambuc     }
198*44bedb31SLionel Sambuc 
199*44bedb31SLionel Sambuc     /* read in "ap" sub-field */
200*44bedb31SLionel Sambuc     log->extra = lseek(log->fd, 0, SEEK_CUR);
201*44bedb31SLionel Sambuc     if (read(log->fd, temp, 16) != 16) {
202*44bedb31SLionel Sambuc         log_clean(log);
203*44bedb31SLionel Sambuc         return NULL;
204*44bedb31SLionel Sambuc     }
205*44bedb31SLionel Sambuc     log->mark_off = make_off(temp);
206*44bedb31SLionel Sambuc     log->last_off = make_off(temp + 8);
207*44bedb31SLionel Sambuc 
208*44bedb31SLionel Sambuc     /* get crc, length of gzip file */
209*44bedb31SLionel Sambuc     (void)lseek(log->fd, log->last_off, SEEK_SET);
210*44bedb31SLionel Sambuc     if (read(log->fd, temp, 13) != 13 ||
211*44bedb31SLionel Sambuc         memcmp(temp, "\001\000\000\377\377", 5) != 0) {
212*44bedb31SLionel Sambuc         log_clean(log);
213*44bedb31SLionel Sambuc         return NULL;
214*44bedb31SLionel Sambuc     }
215*44bedb31SLionel Sambuc     log->crc = make_ulg(temp + 5);
216*44bedb31SLionel Sambuc     log->len = make_ulg(temp + 9);
217*44bedb31SLionel Sambuc 
218*44bedb31SLionel Sambuc     /* set up to write over empty last block */
219*44bedb31SLionel Sambuc     (void)lseek(log->fd, log->last_off + 5, SEEK_SET);
220*44bedb31SLionel Sambuc     log->stored = 0;
221*44bedb31SLionel Sambuc     return (void *)log;
222*44bedb31SLionel Sambuc }
223*44bedb31SLionel Sambuc 
224*44bedb31SLionel Sambuc /* maximum amount to put in a stored block before starting a new one */
225*44bedb31SLionel Sambuc #define MAX_BLOCK 16384
226*44bedb31SLionel Sambuc 
227*44bedb31SLionel Sambuc /* write a block to a log object */
gzlog_write(void * obj,char * data,size_t len)228*44bedb31SLionel Sambuc int gzlog_write(void *obj, char *data, size_t len)
229*44bedb31SLionel Sambuc {
230*44bedb31SLionel Sambuc     size_t some;
231*44bedb31SLionel Sambuc     unsigned char temp[5];
232*44bedb31SLionel Sambuc     gz_log *log;
233*44bedb31SLionel Sambuc 
234*44bedb31SLionel Sambuc     /* check object */
235*44bedb31SLionel Sambuc     log = (gz_log *)obj;
236*44bedb31SLionel Sambuc     if (log == NULL || log->id != GZLOGID)
237*44bedb31SLionel Sambuc         return 1;
238*44bedb31SLionel Sambuc 
239*44bedb31SLionel Sambuc     /* write stored blocks until all of the input is written */
240*44bedb31SLionel Sambuc     do {
241*44bedb31SLionel Sambuc         some = MAX_BLOCK - log->stored;
242*44bedb31SLionel Sambuc         if (some > len)
243*44bedb31SLionel Sambuc             some = len;
244*44bedb31SLionel Sambuc         if (write(log->fd, data, some) != some)
245*44bedb31SLionel Sambuc             return 1;
246*44bedb31SLionel Sambuc         log->crc = crc32(log->crc, data, some);
247*44bedb31SLionel Sambuc         log->len += some;
248*44bedb31SLionel Sambuc         len -= some;
249*44bedb31SLionel Sambuc         data += some;
250*44bedb31SLionel Sambuc         log->stored += some;
251*44bedb31SLionel Sambuc 
252*44bedb31SLionel Sambuc         /* if the stored block is full, end it and start another */
253*44bedb31SLionel Sambuc         if (log->stored == MAX_BLOCK) {
254*44bedb31SLionel Sambuc             (void)lseek(log->fd, log->last_off, SEEK_SET);
255*44bedb31SLionel Sambuc             temp[0] = 0;
256*44bedb31SLionel Sambuc             dice_ulg(log->stored + ((unsigned long)(~log->stored) << 16),
257*44bedb31SLionel Sambuc                      temp + 1);
258*44bedb31SLionel Sambuc             if (write(log->fd, temp, 5) != 5)
259*44bedb31SLionel Sambuc                 return 1;
260*44bedb31SLionel Sambuc             log->last_off = lseek(log->fd, log->stored, SEEK_CUR);
261*44bedb31SLionel Sambuc             (void)lseek(log->fd, 5, SEEK_CUR);
262*44bedb31SLionel Sambuc             log->stored = 0;
263*44bedb31SLionel Sambuc         }
264*44bedb31SLionel Sambuc     } while (len);
265*44bedb31SLionel Sambuc     return 0;
266*44bedb31SLionel Sambuc }
267*44bedb31SLionel Sambuc 
268*44bedb31SLionel Sambuc /* recompress the remaining stored deflate data in place */
recomp(gz_log * log)269*44bedb31SLionel Sambuc local int recomp(gz_log *log)
270*44bedb31SLionel Sambuc {
271*44bedb31SLionel Sambuc     z_stream strm;
272*44bedb31SLionel Sambuc     size_t len, max;
273*44bedb31SLionel Sambuc     unsigned char *in;
274*44bedb31SLionel Sambuc     unsigned char *out;
275*44bedb31SLionel Sambuc     unsigned char temp[16];
276*44bedb31SLionel Sambuc 
277*44bedb31SLionel Sambuc     /* allocate space and read it all in (it's around 1 MB) */
278*44bedb31SLionel Sambuc     len = log->last_off - log->mark_off;
279*44bedb31SLionel Sambuc     max = len + (len >> 12) + (len >> 14) + 11;
280*44bedb31SLionel Sambuc     out = malloc(max);
281*44bedb31SLionel Sambuc     if (out == NULL)
282*44bedb31SLionel Sambuc         return 1;
283*44bedb31SLionel Sambuc     in = malloc(len);
284*44bedb31SLionel Sambuc     if (in == NULL) {
285*44bedb31SLionel Sambuc         free(out);
286*44bedb31SLionel Sambuc         return 1;
287*44bedb31SLionel Sambuc     }
288*44bedb31SLionel Sambuc     (void)lseek(log->fd, log->mark_off, SEEK_SET);
289*44bedb31SLionel Sambuc     if (read(log->fd, in, len) != len) {
290*44bedb31SLionel Sambuc         free(in);
291*44bedb31SLionel Sambuc         free(out);
292*44bedb31SLionel Sambuc         return 1;
293*44bedb31SLionel Sambuc     }
294*44bedb31SLionel Sambuc 
295*44bedb31SLionel Sambuc     /* recompress in memory, decoding stored data as we go */
296*44bedb31SLionel Sambuc     /* note: this assumes that unsigned is four bytes or more */
297*44bedb31SLionel Sambuc     /*       consider not making that assumption */
298*44bedb31SLionel Sambuc     strm.zalloc = Z_NULL;
299*44bedb31SLionel Sambuc     strm.zfree = Z_NULL;
300*44bedb31SLionel Sambuc     strm.opaque = Z_NULL;
301*44bedb31SLionel Sambuc     if (deflateInit2(&strm, Z_BEST_COMPRESSION, Z_DEFLATED, -15, 8,
302*44bedb31SLionel Sambuc         Z_DEFAULT_STRATEGY) != Z_OK) {
303*44bedb31SLionel Sambuc         free(in);
304*44bedb31SLionel Sambuc         free(out);
305*44bedb31SLionel Sambuc         return 1;
306*44bedb31SLionel Sambuc     }
307*44bedb31SLionel Sambuc     strm.next_in = in;
308*44bedb31SLionel Sambuc     strm.avail_out = max;
309*44bedb31SLionel Sambuc     strm.next_out = out;
310*44bedb31SLionel Sambuc     while (len >= 5) {
311*44bedb31SLionel Sambuc         if (strm.next_in[0] != 0)
312*44bedb31SLionel Sambuc             break;
313*44bedb31SLionel Sambuc         strm.avail_in = strm.next_in[1] + (strm.next_in[2] << 8);
314*44bedb31SLionel Sambuc         strm.next_in += 5;
315*44bedb31SLionel Sambuc         len -= 5;
316*44bedb31SLionel Sambuc         if (strm.avail_in != 0) {
317*44bedb31SLionel Sambuc             if (len < strm.avail_in)
318*44bedb31SLionel Sambuc                 break;
319*44bedb31SLionel Sambuc             len -= strm.avail_in;
320*44bedb31SLionel Sambuc             (void)deflate(&strm, Z_NO_FLUSH);
321*44bedb31SLionel Sambuc             if (strm.avail_in != 0 || strm.avail_out == 0)
322*44bedb31SLionel Sambuc                 break;
323*44bedb31SLionel Sambuc         }
324*44bedb31SLionel Sambuc     }
325*44bedb31SLionel Sambuc     (void)deflate(&strm, Z_SYNC_FLUSH);
326*44bedb31SLionel Sambuc     (void)deflateEnd(&strm);
327*44bedb31SLionel Sambuc     free(in);
328*44bedb31SLionel Sambuc     if (len != 0 || strm.avail_out == 0) {
329*44bedb31SLionel Sambuc         free(out);
330*44bedb31SLionel Sambuc         return 1;
331*44bedb31SLionel Sambuc     }
332*44bedb31SLionel Sambuc 
333*44bedb31SLionel Sambuc     /* overwrite stored data with compressed data */
334*44bedb31SLionel Sambuc     (void)lseek(log->fd, log->mark_off, SEEK_SET);
335*44bedb31SLionel Sambuc     len = max - strm.avail_out;
336*44bedb31SLionel Sambuc     if (write(log->fd, out, len) != len) {
337*44bedb31SLionel Sambuc         free(out);
338*44bedb31SLionel Sambuc         return 1;
339*44bedb31SLionel Sambuc     }
340*44bedb31SLionel Sambuc     free(out);
341*44bedb31SLionel Sambuc 
342*44bedb31SLionel Sambuc     /* write last empty block, crc, and length */
343*44bedb31SLionel Sambuc     log->mark_off = log->last_off = lseek(log->fd, 0, SEEK_CUR);
344*44bedb31SLionel Sambuc     temp[0] = 1;
345*44bedb31SLionel Sambuc     dice_ulg(0xffffL << 16, temp + 1);
346*44bedb31SLionel Sambuc     dice_ulg(log->crc, temp + 5);
347*44bedb31SLionel Sambuc     dice_ulg(log->len, temp + 9);
348*44bedb31SLionel Sambuc     if (write(log->fd, temp, 13) != 13)
349*44bedb31SLionel Sambuc         return 1;
350*44bedb31SLionel Sambuc 
351*44bedb31SLionel Sambuc     /* truncate file to discard remaining stored data and old trailer */
352*44bedb31SLionel Sambuc     ftruncate(log->fd, lseek(log->fd, 0, SEEK_CUR));
353*44bedb31SLionel Sambuc 
354*44bedb31SLionel Sambuc     /* update extra field to point to new last empty block */
355*44bedb31SLionel Sambuc     (void)lseek(log->fd, log->extra, SEEK_SET);
356*44bedb31SLionel Sambuc     dice_off(log->mark_off, temp);
357*44bedb31SLionel Sambuc     dice_off(log->last_off, temp + 8);
358*44bedb31SLionel Sambuc     if (write(log->fd, temp, 16) != 16)
359*44bedb31SLionel Sambuc         return 1;
360*44bedb31SLionel Sambuc     return 0;
361*44bedb31SLionel Sambuc }
362*44bedb31SLionel Sambuc 
363*44bedb31SLionel Sambuc /* maximum accumulation of stored blocks before compressing */
364*44bedb31SLionel Sambuc #define MAX_STORED 1048576
365*44bedb31SLionel Sambuc 
366*44bedb31SLionel Sambuc /* close log object */
gzlog_close(void * obj)367*44bedb31SLionel Sambuc int gzlog_close(void *obj)
368*44bedb31SLionel Sambuc {
369*44bedb31SLionel Sambuc     unsigned char temp[8];
370*44bedb31SLionel Sambuc     gz_log *log;
371*44bedb31SLionel Sambuc 
372*44bedb31SLionel Sambuc     /* check object */
373*44bedb31SLionel Sambuc     log = (gz_log *)obj;
374*44bedb31SLionel Sambuc     if (log == NULL || log->id != GZLOGID)
375*44bedb31SLionel Sambuc         return 1;
376*44bedb31SLionel Sambuc 
377*44bedb31SLionel Sambuc     /* go to start of most recent block being written */
378*44bedb31SLionel Sambuc     (void)lseek(log->fd, log->last_off, SEEK_SET);
379*44bedb31SLionel Sambuc 
380*44bedb31SLionel Sambuc     /* if some stuff was put there, update block */
381*44bedb31SLionel Sambuc     if (log->stored) {
382*44bedb31SLionel Sambuc         temp[0] = 0;
383*44bedb31SLionel Sambuc         dice_ulg(log->stored + ((unsigned long)(~log->stored) << 16),
384*44bedb31SLionel Sambuc                  temp + 1);
385*44bedb31SLionel Sambuc         if (write(log->fd, temp, 5) != 5)
386*44bedb31SLionel Sambuc             return 1;
387*44bedb31SLionel Sambuc         log->last_off = lseek(log->fd, log->stored, SEEK_CUR);
388*44bedb31SLionel Sambuc     }
389*44bedb31SLionel Sambuc 
390*44bedb31SLionel Sambuc     /* write last block (empty) */
391*44bedb31SLionel Sambuc     if (write(log->fd, "\001\000\000\377\377", 5) != 5)
392*44bedb31SLionel Sambuc         return 1;
393*44bedb31SLionel Sambuc 
394*44bedb31SLionel Sambuc     /* write updated crc and uncompressed length */
395*44bedb31SLionel Sambuc     dice_ulg(log->crc, temp);
396*44bedb31SLionel Sambuc     dice_ulg(log->len, temp + 4);
397*44bedb31SLionel Sambuc     if (write(log->fd, temp, 8) != 8)
398*44bedb31SLionel Sambuc         return 1;
399*44bedb31SLionel Sambuc 
400*44bedb31SLionel Sambuc     /* put offset of that last block in gzip extra block */
401*44bedb31SLionel Sambuc     (void)lseek(log->fd, log->extra + 8, SEEK_SET);
402*44bedb31SLionel Sambuc     dice_off(log->last_off, temp);
403*44bedb31SLionel Sambuc     if (write(log->fd, temp, 8) != 8)
404*44bedb31SLionel Sambuc         return 1;
405*44bedb31SLionel Sambuc 
406*44bedb31SLionel Sambuc     /* if more than 1 MB stored, then time to compress it */
407*44bedb31SLionel Sambuc     if (log->last_off - log->mark_off > MAX_STORED) {
408*44bedb31SLionel Sambuc         if (recomp(log))
409*44bedb31SLionel Sambuc             return 1;
410*44bedb31SLionel Sambuc     }
411*44bedb31SLionel Sambuc 
412*44bedb31SLionel Sambuc     /* unlock and close file */
413*44bedb31SLionel Sambuc     log_clean(log);
414*44bedb31SLionel Sambuc     return 0;
415*44bedb31SLionel Sambuc }
416