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