annotate src/zlib-1.2.8/examples/gzlog.c @ 56:af97cad61ff0

Add updated build of PortAudio for OSX
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 03 Jan 2017 15:10:52 +0000
parents 5ea0608b923f
children
rev   line source
Chris@43 1 /*
Chris@43 2 * gzlog.c
Chris@43 3 * Copyright (C) 2004, 2008, 2012 Mark Adler, all rights reserved
Chris@43 4 * For conditions of distribution and use, see copyright notice in gzlog.h
Chris@43 5 * version 2.2, 14 Aug 2012
Chris@43 6 */
Chris@43 7
Chris@43 8 /*
Chris@43 9 gzlog provides a mechanism for frequently appending short strings to a gzip
Chris@43 10 file that is efficient both in execution time and compression ratio. The
Chris@43 11 strategy is to write the short strings in an uncompressed form to the end of
Chris@43 12 the gzip file, only compressing when the amount of uncompressed data has
Chris@43 13 reached a given threshold.
Chris@43 14
Chris@43 15 gzlog also provides protection against interruptions in the process due to
Chris@43 16 system crashes. The status of the operation is recorded in an extra field
Chris@43 17 in the gzip file, and is only updated once the gzip file is brought to a
Chris@43 18 valid state. The last data to be appended or compressed is saved in an
Chris@43 19 auxiliary file, so that if the operation is interrupted, it can be completed
Chris@43 20 the next time an append operation is attempted.
Chris@43 21
Chris@43 22 gzlog maintains another auxiliary file with the last 32K of data from the
Chris@43 23 compressed portion, which is preloaded for the compression of the subsequent
Chris@43 24 data. This minimizes the impact to the compression ratio of appending.
Chris@43 25 */
Chris@43 26
Chris@43 27 /*
Chris@43 28 Operations Concept:
Chris@43 29
Chris@43 30 Files (log name "foo"):
Chris@43 31 foo.gz -- gzip file with the complete log
Chris@43 32 foo.add -- last message to append or last data to compress
Chris@43 33 foo.dict -- dictionary of the last 32K of data for next compression
Chris@43 34 foo.temp -- temporary dictionary file for compression after this one
Chris@43 35 foo.lock -- lock file for reading and writing the other files
Chris@43 36 foo.repairs -- log file for log file recovery operations (not compressed)
Chris@43 37
Chris@43 38 gzip file structure:
Chris@43 39 - fixed-length (no file name) header with extra field (see below)
Chris@43 40 - compressed data ending initially with empty stored block
Chris@43 41 - uncompressed data filling out originally empty stored block and
Chris@43 42 subsequent stored blocks as needed (16K max each)
Chris@43 43 - gzip trailer
Chris@43 44 - no junk at end (no other gzip streams)
Chris@43 45
Chris@43 46 When appending data, the information in the first three items above plus the
Chris@43 47 foo.add file are sufficient to recover an interrupted append operation. The
Chris@43 48 extra field has the necessary information to restore the start of the last
Chris@43 49 stored block and determine where to append the data in the foo.add file, as
Chris@43 50 well as the crc and length of the gzip data before the append operation.
Chris@43 51
Chris@43 52 The foo.add file is created before the gzip file is marked for append, and
Chris@43 53 deleted after the gzip file is marked as complete. So if the append
Chris@43 54 operation is interrupted, the data to add will still be there. If due to
Chris@43 55 some external force, the foo.add file gets deleted between when the append
Chris@43 56 operation was interrupted and when recovery is attempted, the gzip file will
Chris@43 57 still be restored, but without the appended data.
Chris@43 58
Chris@43 59 When compressing data, the information in the first two items above plus the
Chris@43 60 foo.add file are sufficient to recover an interrupted compress operation.
Chris@43 61 The extra field has the necessary information to find the end of the
Chris@43 62 compressed data, and contains both the crc and length of just the compressed
Chris@43 63 data and of the complete set of data including the contents of the foo.add
Chris@43 64 file.
Chris@43 65
Chris@43 66 Again, the foo.add file is maintained during the compress operation in case
Chris@43 67 of an interruption. If in the unlikely event the foo.add file with the data
Chris@43 68 to be compressed is missing due to some external force, a gzip file with
Chris@43 69 just the previous compressed data will be reconstructed. In this case, all
Chris@43 70 of the data that was to be compressed is lost (approximately one megabyte).
Chris@43 71 This will not occur if all that happened was an interruption of the compress
Chris@43 72 operation.
Chris@43 73
Chris@43 74 The third state that is marked is the replacement of the old dictionary with
Chris@43 75 the new dictionary after a compress operation. Once compression is
Chris@43 76 complete, the gzip file is marked as being in the replace state. This
Chris@43 77 completes the gzip file, so an interrupt after being so marked does not
Chris@43 78 result in recompression. Then the dictionary file is replaced, and the gzip
Chris@43 79 file is marked as completed. This state prevents the possibility of
Chris@43 80 restarting compression with the wrong dictionary file.
Chris@43 81
Chris@43 82 All three operations are wrapped by a lock/unlock procedure. In order to
Chris@43 83 gain exclusive access to the log files, first a foo.lock file must be
Chris@43 84 exclusively created. When all operations are complete, the lock is
Chris@43 85 released by deleting the foo.lock file. If when attempting to create the
Chris@43 86 lock file, it already exists and the modify time of the lock file is more
Chris@43 87 than five minutes old (set by the PATIENCE define below), then the old
Chris@43 88 lock file is considered stale and deleted, and the exclusive creation of
Chris@43 89 the lock file is retried. To assure that there are no false assessments
Chris@43 90 of the staleness of the lock file, the operations periodically touch the
Chris@43 91 lock file to update the modified date.
Chris@43 92
Chris@43 93 Following is the definition of the extra field with all of the information
Chris@43 94 required to enable the above append and compress operations and their
Chris@43 95 recovery if interrupted. Multi-byte values are stored little endian
Chris@43 96 (consistent with the gzip format). File pointers are eight bytes long.
Chris@43 97 The crc's and lengths for the gzip trailer are four bytes long. (Note that
Chris@43 98 the length at the end of a gzip file is used for error checking only, and
Chris@43 99 for large files is actually the length modulo 2^32.) The stored block
Chris@43 100 length is two bytes long. The gzip extra field two-byte identification is
Chris@43 101 "ap" for append. It is assumed that writing the extra field to the file is
Chris@43 102 an "atomic" operation. That is, either all of the extra field is written
Chris@43 103 to the file, or none of it is, if the operation is interrupted right at the
Chris@43 104 point of updating the extra field. This is a reasonable assumption, since
Chris@43 105 the extra field is within the first 52 bytes of the file, which is smaller
Chris@43 106 than any expected block size for a mass storage device (usually 512 bytes or
Chris@43 107 larger).
Chris@43 108
Chris@43 109 Extra field (35 bytes):
Chris@43 110 - Pointer to first stored block length -- this points to the two-byte length
Chris@43 111 of the first stored block, which is followed by the two-byte, one's
Chris@43 112 complement of that length. The stored block length is preceded by the
Chris@43 113 three-bit header of the stored block, which is the actual start of the
Chris@43 114 stored block in the deflate format. See the bit offset field below.
Chris@43 115 - Pointer to the last stored block length. This is the same as above, but
Chris@43 116 for the last stored block of the uncompressed data in the gzip file.
Chris@43 117 Initially this is the same as the first stored block length pointer.
Chris@43 118 When the stored block gets to 16K (see the MAX_STORE define), then a new
Chris@43 119 stored block as added, at which point the last stored block length pointer
Chris@43 120 is different from the first stored block length pointer. When they are
Chris@43 121 different, the first bit of the last stored block header is eight bits, or
Chris@43 122 one byte back from the block length.
Chris@43 123 - Compressed data crc and length. This is the crc and length of the data
Chris@43 124 that is in the compressed portion of the deflate stream. These are used
Chris@43 125 only in the event that the foo.add file containing the data to compress is
Chris@43 126 lost after a compress operation is interrupted.
Chris@43 127 - Total data crc and length. This is the crc and length of all of the data
Chris@43 128 stored in the gzip file, compressed and uncompressed. It is used to
Chris@43 129 reconstruct the gzip trailer when compressing, as well as when recovering
Chris@43 130 interrupted operations.
Chris@43 131 - Final stored block length. This is used to quickly find where to append,
Chris@43 132 and allows the restoration of the original final stored block state when
Chris@43 133 an append operation is interrupted.
Chris@43 134 - First stored block start as the number of bits back from the final stored
Chris@43 135 block first length byte. This value is in the range of 3..10, and is
Chris@43 136 stored as the low three bits of the final byte of the extra field after
Chris@43 137 subtracting three (0..7). This allows the last-block bit of the stored
Chris@43 138 block header to be updated when a new stored block is added, for the case
Chris@43 139 when the first stored block and the last stored block are the same. (When
Chris@43 140 they are different, the numbers of bits back is known to be eight.) This
Chris@43 141 also allows for new compressed data to be appended to the old compressed
Chris@43 142 data in the compress operation, overwriting the previous first stored
Chris@43 143 block, or for the compressed data to be terminated and a valid gzip file
Chris@43 144 reconstructed on the off chance that a compression operation was
Chris@43 145 interrupted and the data to compress in the foo.add file was deleted.
Chris@43 146 - The operation in process. This is the next two bits in the last byte (the
Chris@43 147 bits under the mask 0x18). The are interpreted as 0: nothing in process,
Chris@43 148 1: append in process, 2: compress in process, 3: replace in process.
Chris@43 149 - The top three bits of the last byte in the extra field are reserved and
Chris@43 150 are currently set to zero.
Chris@43 151
Chris@43 152 Main procedure:
Chris@43 153 - Exclusively create the foo.lock file using the O_CREAT and O_EXCL modes of
Chris@43 154 the system open() call. If the modify time of an existing lock file is
Chris@43 155 more than PATIENCE seconds old, then the lock file is deleted and the
Chris@43 156 exclusive create is retried.
Chris@43 157 - Load the extra field from the foo.gz file, and see if an operation was in
Chris@43 158 progress but not completed. If so, apply the recovery procedure below.
Chris@43 159 - Perform the append procedure with the provided data.
Chris@43 160 - If the uncompressed data in the foo.gz file is 1MB or more, apply the
Chris@43 161 compress procedure.
Chris@43 162 - Delete the foo.lock file.
Chris@43 163
Chris@43 164 Append procedure:
Chris@43 165 - Put what to append in the foo.add file so that the operation can be
Chris@43 166 restarted if this procedure is interrupted.
Chris@43 167 - Mark the foo.gz extra field with the append operation in progress.
Chris@43 168 + Restore the original last-block bit and stored block length of the last
Chris@43 169 stored block from the information in the extra field, in case a previous
Chris@43 170 append operation was interrupted.
Chris@43 171 - Append the provided data to the last stored block, creating new stored
Chris@43 172 blocks as needed and updating the stored blocks last-block bits and
Chris@43 173 lengths.
Chris@43 174 - Update the crc and length with the new data, and write the gzip trailer.
Chris@43 175 - Write over the extra field (with a single write operation) with the new
Chris@43 176 pointers, lengths, and crc's, and mark the gzip file as not in process.
Chris@43 177 Though there is still a foo.add file, it will be ignored since nothing
Chris@43 178 is in process. If a foo.add file is leftover from a previously
Chris@43 179 completed operation, it is truncated when writing new data to it.
Chris@43 180 - Delete the foo.add file.
Chris@43 181
Chris@43 182 Compress and replace procedures:
Chris@43 183 - Read all of the uncompressed data in the stored blocks in foo.gz and write
Chris@43 184 it to foo.add. Also write foo.temp with the last 32K of that data to
Chris@43 185 provide a dictionary for the next invocation of this procedure.
Chris@43 186 - Rewrite the extra field marking foo.gz with a compression in process.
Chris@43 187 * If there is no data provided to compress (due to a missing foo.add file
Chris@43 188 when recovering), reconstruct and truncate the foo.gz file to contain
Chris@43 189 only the previous compressed data and proceed to the step after the next
Chris@43 190 one. Otherwise ...
Chris@43 191 - Compress the data with the dictionary in foo.dict, and write to the
Chris@43 192 foo.gz file starting at the bit immediately following the last previously
Chris@43 193 compressed block. If there is no foo.dict, proceed anyway with the
Chris@43 194 compression at slightly reduced efficiency. (For the foo.dict file to be
Chris@43 195 missing requires some external failure beyond simply the interruption of
Chris@43 196 a compress operation.) During this process, the foo.lock file is
Chris@43 197 periodically touched to assure that that file is not considered stale by
Chris@43 198 another process before we're done. The deflation is terminated with a
Chris@43 199 non-last empty static block (10 bits long), that is then located and
Chris@43 200 written over by a last-bit-set empty stored block.
Chris@43 201 - Append the crc and length of the data in the gzip file (previously
Chris@43 202 calculated during the append operations).
Chris@43 203 - Write over the extra field with the updated stored block offsets, bits
Chris@43 204 back, crc's, and lengths, and mark foo.gz as in process for a replacement
Chris@43 205 of the dictionary.
Chris@43 206 @ Delete the foo.add file.
Chris@43 207 - Replace foo.dict with foo.temp.
Chris@43 208 - Write over the extra field, marking foo.gz as complete.
Chris@43 209
Chris@43 210 Recovery procedure:
Chris@43 211 - If not a replace recovery, read in the foo.add file, and provide that data
Chris@43 212 to the appropriate recovery below. If there is no foo.add file, provide
Chris@43 213 a zero data length to the recovery. In that case, the append recovery
Chris@43 214 restores the foo.gz to the previous compressed + uncompressed data state.
Chris@43 215 For the the compress recovery, a missing foo.add file results in foo.gz
Chris@43 216 being restored to the previous compressed-only data state.
Chris@43 217 - Append recovery:
Chris@43 218 - Pick up append at + step above
Chris@43 219 - Compress recovery:
Chris@43 220 - Pick up compress at * step above
Chris@43 221 - Replace recovery:
Chris@43 222 - Pick up compress at @ step above
Chris@43 223 - Log the repair with a date stamp in foo.repairs
Chris@43 224 */
Chris@43 225
Chris@43 226 #include <sys/types.h>
Chris@43 227 #include <stdio.h> /* rename, fopen, fprintf, fclose */
Chris@43 228 #include <stdlib.h> /* malloc, free */
Chris@43 229 #include <string.h> /* strlen, strrchr, strcpy, strncpy, strcmp */
Chris@43 230 #include <fcntl.h> /* open */
Chris@43 231 #include <unistd.h> /* lseek, read, write, close, unlink, sleep, */
Chris@43 232 /* ftruncate, fsync */
Chris@43 233 #include <errno.h> /* errno */
Chris@43 234 #include <time.h> /* time, ctime */
Chris@43 235 #include <sys/stat.h> /* stat */
Chris@43 236 #include <sys/time.h> /* utimes */
Chris@43 237 #include "zlib.h" /* crc32 */
Chris@43 238
Chris@43 239 #include "gzlog.h" /* header for external access */
Chris@43 240
Chris@43 241 #define local static
Chris@43 242 typedef unsigned int uint;
Chris@43 243 typedef unsigned long ulong;
Chris@43 244
Chris@43 245 /* Macro for debugging to deterministically force recovery operations */
Chris@43 246 #ifdef DEBUG
Chris@43 247 #include <setjmp.h> /* longjmp */
Chris@43 248 jmp_buf gzlog_jump; /* where to go back to */
Chris@43 249 int gzlog_bail = 0; /* which point to bail at (1..8) */
Chris@43 250 int gzlog_count = -1; /* number of times through to wait */
Chris@43 251 # define BAIL(n) do { if (n == gzlog_bail && gzlog_count-- == 0) \
Chris@43 252 longjmp(gzlog_jump, gzlog_bail); } while (0)
Chris@43 253 #else
Chris@43 254 # define BAIL(n)
Chris@43 255 #endif
Chris@43 256
Chris@43 257 /* how old the lock file can be in seconds before considering it stale */
Chris@43 258 #define PATIENCE 300
Chris@43 259
Chris@43 260 /* maximum stored block size in Kbytes -- must be in 1..63 */
Chris@43 261 #define MAX_STORE 16
Chris@43 262
Chris@43 263 /* number of stored Kbytes to trigger compression (must be >= 32 to allow
Chris@43 264 dictionary construction, and <= 204 * MAX_STORE, in order for >> 10 to
Chris@43 265 discard the stored block headers contribution of five bytes each) */
Chris@43 266 #define TRIGGER 1024
Chris@43 267
Chris@43 268 /* size of a deflate dictionary (this cannot be changed) */
Chris@43 269 #define DICT 32768U
Chris@43 270
Chris@43 271 /* values for the operation (2 bits) */
Chris@43 272 #define NO_OP 0
Chris@43 273 #define APPEND_OP 1
Chris@43 274 #define COMPRESS_OP 2
Chris@43 275 #define REPLACE_OP 3
Chris@43 276
Chris@43 277 /* macros to extract little-endian integers from an unsigned byte buffer */
Chris@43 278 #define PULL2(p) ((p)[0]+((uint)((p)[1])<<8))
Chris@43 279 #define PULL4(p) (PULL2(p)+((ulong)PULL2(p+2)<<16))
Chris@43 280 #define PULL8(p) (PULL4(p)+((off_t)PULL4(p+4)<<32))
Chris@43 281
Chris@43 282 /* macros to store integers into a byte buffer in little-endian order */
Chris@43 283 #define PUT2(p,a) do {(p)[0]=a;(p)[1]=(a)>>8;} while(0)
Chris@43 284 #define PUT4(p,a) do {PUT2(p,a);PUT2(p+2,a>>16);} while(0)
Chris@43 285 #define PUT8(p,a) do {PUT4(p,a);PUT4(p+4,a>>32);} while(0)
Chris@43 286
Chris@43 287 /* internal structure for log information */
Chris@43 288 #define LOGID "\106\035\172" /* should be three non-zero characters */
Chris@43 289 struct log {
Chris@43 290 char id[4]; /* contains LOGID to detect inadvertent overwrites */
Chris@43 291 int fd; /* file descriptor for .gz file, opened read/write */
Chris@43 292 char *path; /* allocated path, e.g. "/var/log/foo" or "foo" */
Chris@43 293 char *end; /* end of path, for appending suffices such as ".gz" */
Chris@43 294 off_t first; /* offset of first stored block first length byte */
Chris@43 295 int back; /* location of first block id in bits back from first */
Chris@43 296 uint stored; /* bytes currently in last stored block */
Chris@43 297 off_t last; /* offset of last stored block first length byte */
Chris@43 298 ulong ccrc; /* crc of compressed data */
Chris@43 299 ulong clen; /* length (modulo 2^32) of compressed data */
Chris@43 300 ulong tcrc; /* crc of total data */
Chris@43 301 ulong tlen; /* length (modulo 2^32) of total data */
Chris@43 302 time_t lock; /* last modify time of our lock file */
Chris@43 303 };
Chris@43 304
Chris@43 305 /* gzip header for gzlog */
Chris@43 306 local unsigned char log_gzhead[] = {
Chris@43 307 0x1f, 0x8b, /* magic gzip id */
Chris@43 308 8, /* compression method is deflate */
Chris@43 309 4, /* there is an extra field (no file name) */
Chris@43 310 0, 0, 0, 0, /* no modification time provided */
Chris@43 311 0, 0xff, /* no extra flags, no OS specified */
Chris@43 312 39, 0, 'a', 'p', 35, 0 /* extra field with "ap" subfield */
Chris@43 313 /* 35 is EXTRA, 39 is EXTRA + 4 */
Chris@43 314 };
Chris@43 315
Chris@43 316 #define HEAD sizeof(log_gzhead) /* should be 16 */
Chris@43 317
Chris@43 318 /* initial gzip extra field content (52 == HEAD + EXTRA + 1) */
Chris@43 319 local unsigned char log_gzext[] = {
Chris@43 320 52, 0, 0, 0, 0, 0, 0, 0, /* offset of first stored block length */
Chris@43 321 52, 0, 0, 0, 0, 0, 0, 0, /* offset of last stored block length */
Chris@43 322 0, 0, 0, 0, 0, 0, 0, 0, /* compressed data crc and length */
Chris@43 323 0, 0, 0, 0, 0, 0, 0, 0, /* total data crc and length */
Chris@43 324 0, 0, /* final stored block data length */
Chris@43 325 5 /* op is NO_OP, last bit 8 bits back */
Chris@43 326 };
Chris@43 327
Chris@43 328 #define EXTRA sizeof(log_gzext) /* should be 35 */
Chris@43 329
Chris@43 330 /* initial gzip data and trailer */
Chris@43 331 local unsigned char log_gzbody[] = {
Chris@43 332 1, 0, 0, 0xff, 0xff, /* empty stored block (last) */
Chris@43 333 0, 0, 0, 0, /* crc */
Chris@43 334 0, 0, 0, 0 /* uncompressed length */
Chris@43 335 };
Chris@43 336
Chris@43 337 #define BODY sizeof(log_gzbody)
Chris@43 338
Chris@43 339 /* Exclusively create foo.lock in order to negotiate exclusive access to the
Chris@43 340 foo.* files. If the modify time of an existing lock file is greater than
Chris@43 341 PATIENCE seconds in the past, then consider the lock file to have been
Chris@43 342 abandoned, delete it, and try the exclusive create again. Save the lock
Chris@43 343 file modify time for verification of ownership. Return 0 on success, or -1
Chris@43 344 on failure, usually due to an access restriction or invalid path. Note that
Chris@43 345 if stat() or unlink() fails, it may be due to another process noticing the
Chris@43 346 abandoned lock file a smidge sooner and deleting it, so those are not
Chris@43 347 flagged as an error. */
Chris@43 348 local int log_lock(struct log *log)
Chris@43 349 {
Chris@43 350 int fd;
Chris@43 351 struct stat st;
Chris@43 352
Chris@43 353 strcpy(log->end, ".lock");
Chris@43 354 while ((fd = open(log->path, O_CREAT | O_EXCL, 0644)) < 0) {
Chris@43 355 if (errno != EEXIST)
Chris@43 356 return -1;
Chris@43 357 if (stat(log->path, &st) == 0 && time(NULL) - st.st_mtime > PATIENCE) {
Chris@43 358 unlink(log->path);
Chris@43 359 continue;
Chris@43 360 }
Chris@43 361 sleep(2); /* relinquish the CPU for two seconds while waiting */
Chris@43 362 }
Chris@43 363 close(fd);
Chris@43 364 if (stat(log->path, &st) == 0)
Chris@43 365 log->lock = st.st_mtime;
Chris@43 366 return 0;
Chris@43 367 }
Chris@43 368
Chris@43 369 /* Update the modify time of the lock file to now, in order to prevent another
Chris@43 370 task from thinking that the lock is stale. Save the lock file modify time
Chris@43 371 for verification of ownership. */
Chris@43 372 local void log_touch(struct log *log)
Chris@43 373 {
Chris@43 374 struct stat st;
Chris@43 375
Chris@43 376 strcpy(log->end, ".lock");
Chris@43 377 utimes(log->path, NULL);
Chris@43 378 if (stat(log->path, &st) == 0)
Chris@43 379 log->lock = st.st_mtime;
Chris@43 380 }
Chris@43 381
Chris@43 382 /* Check the log file modify time against what is expected. Return true if
Chris@43 383 this is not our lock. If it is our lock, touch it to keep it. */
Chris@43 384 local int log_check(struct log *log)
Chris@43 385 {
Chris@43 386 struct stat st;
Chris@43 387
Chris@43 388 strcpy(log->end, ".lock");
Chris@43 389 if (stat(log->path, &st) || st.st_mtime != log->lock)
Chris@43 390 return 1;
Chris@43 391 log_touch(log);
Chris@43 392 return 0;
Chris@43 393 }
Chris@43 394
Chris@43 395 /* Unlock a previously acquired lock, but only if it's ours. */
Chris@43 396 local void log_unlock(struct log *log)
Chris@43 397 {
Chris@43 398 if (log_check(log))
Chris@43 399 return;
Chris@43 400 strcpy(log->end, ".lock");
Chris@43 401 unlink(log->path);
Chris@43 402 log->lock = 0;
Chris@43 403 }
Chris@43 404
Chris@43 405 /* Check the gzip header and read in the extra field, filling in the values in
Chris@43 406 the log structure. Return op on success or -1 if the gzip header was not as
Chris@43 407 expected. op is the current operation in progress last written to the extra
Chris@43 408 field. This assumes that the gzip file has already been opened, with the
Chris@43 409 file descriptor log->fd. */
Chris@43 410 local int log_head(struct log *log)
Chris@43 411 {
Chris@43 412 int op;
Chris@43 413 unsigned char buf[HEAD + EXTRA];
Chris@43 414
Chris@43 415 if (lseek(log->fd, 0, SEEK_SET) < 0 ||
Chris@43 416 read(log->fd, buf, HEAD + EXTRA) != HEAD + EXTRA ||
Chris@43 417 memcmp(buf, log_gzhead, HEAD)) {
Chris@43 418 return -1;
Chris@43 419 }
Chris@43 420 log->first = PULL8(buf + HEAD);
Chris@43 421 log->last = PULL8(buf + HEAD + 8);
Chris@43 422 log->ccrc = PULL4(buf + HEAD + 16);
Chris@43 423 log->clen = PULL4(buf + HEAD + 20);
Chris@43 424 log->tcrc = PULL4(buf + HEAD + 24);
Chris@43 425 log->tlen = PULL4(buf + HEAD + 28);
Chris@43 426 log->stored = PULL2(buf + HEAD + 32);
Chris@43 427 log->back = 3 + (buf[HEAD + 34] & 7);
Chris@43 428 op = (buf[HEAD + 34] >> 3) & 3;
Chris@43 429 return op;
Chris@43 430 }
Chris@43 431
Chris@43 432 /* Write over the extra field contents, marking the operation as op. Use fsync
Chris@43 433 to assure that the device is written to, and in the requested order. This
Chris@43 434 operation, and only this operation, is assumed to be atomic in order to
Chris@43 435 assure that the log is recoverable in the event of an interruption at any
Chris@43 436 point in the process. Return -1 if the write to foo.gz failed. */
Chris@43 437 local int log_mark(struct log *log, int op)
Chris@43 438 {
Chris@43 439 int ret;
Chris@43 440 unsigned char ext[EXTRA];
Chris@43 441
Chris@43 442 PUT8(ext, log->first);
Chris@43 443 PUT8(ext + 8, log->last);
Chris@43 444 PUT4(ext + 16, log->ccrc);
Chris@43 445 PUT4(ext + 20, log->clen);
Chris@43 446 PUT4(ext + 24, log->tcrc);
Chris@43 447 PUT4(ext + 28, log->tlen);
Chris@43 448 PUT2(ext + 32, log->stored);
Chris@43 449 ext[34] = log->back - 3 + (op << 3);
Chris@43 450 fsync(log->fd);
Chris@43 451 ret = lseek(log->fd, HEAD, SEEK_SET) < 0 ||
Chris@43 452 write(log->fd, ext, EXTRA) != EXTRA ? -1 : 0;
Chris@43 453 fsync(log->fd);
Chris@43 454 return ret;
Chris@43 455 }
Chris@43 456
Chris@43 457 /* Rewrite the last block header bits and subsequent zero bits to get to a byte
Chris@43 458 boundary, setting the last block bit if last is true, and then write the
Chris@43 459 remainder of the stored block header (length and one's complement). Leave
Chris@43 460 the file pointer after the end of the last stored block data. Return -1 if
Chris@43 461 there is a read or write failure on the foo.gz file */
Chris@43 462 local int log_last(struct log *log, int last)
Chris@43 463 {
Chris@43 464 int back, len, mask;
Chris@43 465 unsigned char buf[6];
Chris@43 466
Chris@43 467 /* determine the locations of the bytes and bits to modify */
Chris@43 468 back = log->last == log->first ? log->back : 8;
Chris@43 469 len = back > 8 ? 2 : 1; /* bytes back from log->last */
Chris@43 470 mask = 0x80 >> ((back - 1) & 7); /* mask for block last-bit */
Chris@43 471
Chris@43 472 /* get the byte to modify (one or two back) into buf[0] -- don't need to
Chris@43 473 read the byte if the last-bit is eight bits back, since in that case
Chris@43 474 the entire byte will be modified */
Chris@43 475 buf[0] = 0;
Chris@43 476 if (back != 8 && (lseek(log->fd, log->last - len, SEEK_SET) < 0 ||
Chris@43 477 read(log->fd, buf, 1) != 1))
Chris@43 478 return -1;
Chris@43 479
Chris@43 480 /* change the last-bit of the last stored block as requested -- note
Chris@43 481 that all bits above the last-bit are set to zero, per the type bits
Chris@43 482 of a stored block being 00 and per the convention that the bits to
Chris@43 483 bring the stream to a byte boundary are also zeros */
Chris@43 484 buf[1] = 0;
Chris@43 485 buf[2 - len] = (*buf & (mask - 1)) + (last ? mask : 0);
Chris@43 486
Chris@43 487 /* write the modified stored block header and lengths, move the file
Chris@43 488 pointer to after the last stored block data */
Chris@43 489 PUT2(buf + 2, log->stored);
Chris@43 490 PUT2(buf + 4, log->stored ^ 0xffff);
Chris@43 491 return lseek(log->fd, log->last - len, SEEK_SET) < 0 ||
Chris@43 492 write(log->fd, buf + 2 - len, len + 4) != len + 4 ||
Chris@43 493 lseek(log->fd, log->stored, SEEK_CUR) < 0 ? -1 : 0;
Chris@43 494 }
Chris@43 495
Chris@43 496 /* Append len bytes from data to the locked and open log file. len may be zero
Chris@43 497 if recovering and no .add file was found. In that case, the previous state
Chris@43 498 of the foo.gz file is restored. The data is appended uncompressed in
Chris@43 499 deflate stored blocks. Return -1 if there was an error reading or writing
Chris@43 500 the foo.gz file. */
Chris@43 501 local int log_append(struct log *log, unsigned char *data, size_t len)
Chris@43 502 {
Chris@43 503 uint put;
Chris@43 504 off_t end;
Chris@43 505 unsigned char buf[8];
Chris@43 506
Chris@43 507 /* set the last block last-bit and length, in case recovering an
Chris@43 508 interrupted append, then position the file pointer to append to the
Chris@43 509 block */
Chris@43 510 if (log_last(log, 1))
Chris@43 511 return -1;
Chris@43 512
Chris@43 513 /* append, adding stored blocks and updating the offset of the last stored
Chris@43 514 block as needed, and update the total crc and length */
Chris@43 515 while (len) {
Chris@43 516 /* append as much as we can to the last block */
Chris@43 517 put = (MAX_STORE << 10) - log->stored;
Chris@43 518 if (put > len)
Chris@43 519 put = (uint)len;
Chris@43 520 if (put) {
Chris@43 521 if (write(log->fd, data, put) != put)
Chris@43 522 return -1;
Chris@43 523 BAIL(1);
Chris@43 524 log->tcrc = crc32(log->tcrc, data, put);
Chris@43 525 log->tlen += put;
Chris@43 526 log->stored += put;
Chris@43 527 data += put;
Chris@43 528 len -= put;
Chris@43 529 }
Chris@43 530
Chris@43 531 /* if we need to, add a new empty stored block */
Chris@43 532 if (len) {
Chris@43 533 /* mark current block as not last */
Chris@43 534 if (log_last(log, 0))
Chris@43 535 return -1;
Chris@43 536
Chris@43 537 /* point to new, empty stored block */
Chris@43 538 log->last += 4 + log->stored + 1;
Chris@43 539 log->stored = 0;
Chris@43 540 }
Chris@43 541
Chris@43 542 /* mark last block as last, update its length */
Chris@43 543 if (log_last(log, 1))
Chris@43 544 return -1;
Chris@43 545 BAIL(2);
Chris@43 546 }
Chris@43 547
Chris@43 548 /* write the new crc and length trailer, and truncate just in case (could
Chris@43 549 be recovering from partial append with a missing foo.add file) */
Chris@43 550 PUT4(buf, log->tcrc);
Chris@43 551 PUT4(buf + 4, log->tlen);
Chris@43 552 if (write(log->fd, buf, 8) != 8 ||
Chris@43 553 (end = lseek(log->fd, 0, SEEK_CUR)) < 0 || ftruncate(log->fd, end))
Chris@43 554 return -1;
Chris@43 555
Chris@43 556 /* write the extra field, marking the log file as done, delete .add file */
Chris@43 557 if (log_mark(log, NO_OP))
Chris@43 558 return -1;
Chris@43 559 strcpy(log->end, ".add");
Chris@43 560 unlink(log->path); /* ignore error, since may not exist */
Chris@43 561 return 0;
Chris@43 562 }
Chris@43 563
Chris@43 564 /* Replace the foo.dict file with the foo.temp file. Also delete the foo.add
Chris@43 565 file, since the compress operation may have been interrupted before that was
Chris@43 566 done. Returns 1 if memory could not be allocated, or -1 if reading or
Chris@43 567 writing foo.gz fails, or if the rename fails for some reason other than
Chris@43 568 foo.temp not existing. foo.temp not existing is a permitted error, since
Chris@43 569 the replace operation may have been interrupted after the rename is done,
Chris@43 570 but before foo.gz is marked as complete. */
Chris@43 571 local int log_replace(struct log *log)
Chris@43 572 {
Chris@43 573 int ret;
Chris@43 574 char *dest;
Chris@43 575
Chris@43 576 /* delete foo.add file */
Chris@43 577 strcpy(log->end, ".add");
Chris@43 578 unlink(log->path); /* ignore error, since may not exist */
Chris@43 579 BAIL(3);
Chris@43 580
Chris@43 581 /* rename foo.name to foo.dict, replacing foo.dict if it exists */
Chris@43 582 strcpy(log->end, ".dict");
Chris@43 583 dest = malloc(strlen(log->path) + 1);
Chris@43 584 if (dest == NULL)
Chris@43 585 return -2;
Chris@43 586 strcpy(dest, log->path);
Chris@43 587 strcpy(log->end, ".temp");
Chris@43 588 ret = rename(log->path, dest);
Chris@43 589 free(dest);
Chris@43 590 if (ret && errno != ENOENT)
Chris@43 591 return -1;
Chris@43 592 BAIL(4);
Chris@43 593
Chris@43 594 /* mark the foo.gz file as done */
Chris@43 595 return log_mark(log, NO_OP);
Chris@43 596 }
Chris@43 597
Chris@43 598 /* Compress the len bytes at data and append the compressed data to the
Chris@43 599 foo.gz deflate data immediately after the previous compressed data. This
Chris@43 600 overwrites the previous uncompressed data, which was stored in foo.add
Chris@43 601 and is the data provided in data[0..len-1]. If this operation is
Chris@43 602 interrupted, it picks up at the start of this routine, with the foo.add
Chris@43 603 file read in again. If there is no data to compress (len == 0), then we
Chris@43 604 simply terminate the foo.gz file after the previously compressed data,
Chris@43 605 appending a final empty stored block and the gzip trailer. Return -1 if
Chris@43 606 reading or writing the log.gz file failed, or -2 if there was a memory
Chris@43 607 allocation failure. */
Chris@43 608 local int log_compress(struct log *log, unsigned char *data, size_t len)
Chris@43 609 {
Chris@43 610 int fd;
Chris@43 611 uint got, max;
Chris@43 612 ssize_t dict;
Chris@43 613 off_t end;
Chris@43 614 z_stream strm;
Chris@43 615 unsigned char buf[DICT];
Chris@43 616
Chris@43 617 /* compress and append compressed data */
Chris@43 618 if (len) {
Chris@43 619 /* set up for deflate, allocating memory */
Chris@43 620 strm.zalloc = Z_NULL;
Chris@43 621 strm.zfree = Z_NULL;
Chris@43 622 strm.opaque = Z_NULL;
Chris@43 623 if (deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -15, 8,
Chris@43 624 Z_DEFAULT_STRATEGY) != Z_OK)
Chris@43 625 return -2;
Chris@43 626
Chris@43 627 /* read in dictionary (last 32K of data that was compressed) */
Chris@43 628 strcpy(log->end, ".dict");
Chris@43 629 fd = open(log->path, O_RDONLY, 0);
Chris@43 630 if (fd >= 0) {
Chris@43 631 dict = read(fd, buf, DICT);
Chris@43 632 close(fd);
Chris@43 633 if (dict < 0) {
Chris@43 634 deflateEnd(&strm);
Chris@43 635 return -1;
Chris@43 636 }
Chris@43 637 if (dict)
Chris@43 638 deflateSetDictionary(&strm, buf, (uint)dict);
Chris@43 639 }
Chris@43 640 log_touch(log);
Chris@43 641
Chris@43 642 /* prime deflate with last bits of previous block, position write
Chris@43 643 pointer to write those bits and overwrite what follows */
Chris@43 644 if (lseek(log->fd, log->first - (log->back > 8 ? 2 : 1),
Chris@43 645 SEEK_SET) < 0 ||
Chris@43 646 read(log->fd, buf, 1) != 1 || lseek(log->fd, -1, SEEK_CUR) < 0) {
Chris@43 647 deflateEnd(&strm);
Chris@43 648 return -1;
Chris@43 649 }
Chris@43 650 deflatePrime(&strm, (8 - log->back) & 7, *buf);
Chris@43 651
Chris@43 652 /* compress, finishing with a partial non-last empty static block */
Chris@43 653 strm.next_in = data;
Chris@43 654 max = (((uint)0 - 1) >> 1) + 1; /* in case int smaller than size_t */
Chris@43 655 do {
Chris@43 656 strm.avail_in = len > max ? max : (uint)len;
Chris@43 657 len -= strm.avail_in;
Chris@43 658 do {
Chris@43 659 strm.avail_out = DICT;
Chris@43 660 strm.next_out = buf;
Chris@43 661 deflate(&strm, len ? Z_NO_FLUSH : Z_PARTIAL_FLUSH);
Chris@43 662 got = DICT - strm.avail_out;
Chris@43 663 if (got && write(log->fd, buf, got) != got) {
Chris@43 664 deflateEnd(&strm);
Chris@43 665 return -1;
Chris@43 666 }
Chris@43 667 log_touch(log);
Chris@43 668 } while (strm.avail_out == 0);
Chris@43 669 } while (len);
Chris@43 670 deflateEnd(&strm);
Chris@43 671 BAIL(5);
Chris@43 672
Chris@43 673 /* find start of empty static block -- scanning backwards the first one
Chris@43 674 bit is the second bit of the block, if the last byte is zero, then
Chris@43 675 we know the byte before that has a one in the top bit, since an
Chris@43 676 empty static block is ten bits long */
Chris@43 677 if ((log->first = lseek(log->fd, -1, SEEK_CUR)) < 0 ||
Chris@43 678 read(log->fd, buf, 1) != 1)
Chris@43 679 return -1;
Chris@43 680 log->first++;
Chris@43 681 if (*buf) {
Chris@43 682 log->back = 1;
Chris@43 683 while ((*buf & ((uint)1 << (8 - log->back++))) == 0)
Chris@43 684 ; /* guaranteed to terminate, since *buf != 0 */
Chris@43 685 }
Chris@43 686 else
Chris@43 687 log->back = 10;
Chris@43 688
Chris@43 689 /* update compressed crc and length */
Chris@43 690 log->ccrc = log->tcrc;
Chris@43 691 log->clen = log->tlen;
Chris@43 692 }
Chris@43 693 else {
Chris@43 694 /* no data to compress -- fix up existing gzip stream */
Chris@43 695 log->tcrc = log->ccrc;
Chris@43 696 log->tlen = log->clen;
Chris@43 697 }
Chris@43 698
Chris@43 699 /* complete and truncate gzip stream */
Chris@43 700 log->last = log->first;
Chris@43 701 log->stored = 0;
Chris@43 702 PUT4(buf, log->tcrc);
Chris@43 703 PUT4(buf + 4, log->tlen);
Chris@43 704 if (log_last(log, 1) || write(log->fd, buf, 8) != 8 ||
Chris@43 705 (end = lseek(log->fd, 0, SEEK_CUR)) < 0 || ftruncate(log->fd, end))
Chris@43 706 return -1;
Chris@43 707 BAIL(6);
Chris@43 708
Chris@43 709 /* mark as being in the replace operation */
Chris@43 710 if (log_mark(log, REPLACE_OP))
Chris@43 711 return -1;
Chris@43 712
Chris@43 713 /* execute the replace operation and mark the file as done */
Chris@43 714 return log_replace(log);
Chris@43 715 }
Chris@43 716
Chris@43 717 /* log a repair record to the .repairs file */
Chris@43 718 local void log_log(struct log *log, int op, char *record)
Chris@43 719 {
Chris@43 720 time_t now;
Chris@43 721 FILE *rec;
Chris@43 722
Chris@43 723 now = time(NULL);
Chris@43 724 strcpy(log->end, ".repairs");
Chris@43 725 rec = fopen(log->path, "a");
Chris@43 726 if (rec == NULL)
Chris@43 727 return;
Chris@43 728 fprintf(rec, "%.24s %s recovery: %s\n", ctime(&now), op == APPEND_OP ?
Chris@43 729 "append" : (op == COMPRESS_OP ? "compress" : "replace"), record);
Chris@43 730 fclose(rec);
Chris@43 731 return;
Chris@43 732 }
Chris@43 733
Chris@43 734 /* Recover the interrupted operation op. First read foo.add for recovering an
Chris@43 735 append or compress operation. Return -1 if there was an error reading or
Chris@43 736 writing foo.gz or reading an existing foo.add, or -2 if there was a memory
Chris@43 737 allocation failure. */
Chris@43 738 local int log_recover(struct log *log, int op)
Chris@43 739 {
Chris@43 740 int fd, ret = 0;
Chris@43 741 unsigned char *data = NULL;
Chris@43 742 size_t len = 0;
Chris@43 743 struct stat st;
Chris@43 744
Chris@43 745 /* log recovery */
Chris@43 746 log_log(log, op, "start");
Chris@43 747
Chris@43 748 /* load foo.add file if expected and present */
Chris@43 749 if (op == APPEND_OP || op == COMPRESS_OP) {
Chris@43 750 strcpy(log->end, ".add");
Chris@43 751 if (stat(log->path, &st) == 0 && st.st_size) {
Chris@43 752 len = (size_t)(st.st_size);
Chris@43 753 if ((off_t)len != st.st_size ||
Chris@43 754 (data = malloc(st.st_size)) == NULL) {
Chris@43 755 log_log(log, op, "allocation failure");
Chris@43 756 return -2;
Chris@43 757 }
Chris@43 758 if ((fd = open(log->path, O_RDONLY, 0)) < 0) {
Chris@43 759 log_log(log, op, ".add file read failure");
Chris@43 760 return -1;
Chris@43 761 }
Chris@43 762 ret = (size_t)read(fd, data, len) != len;
Chris@43 763 close(fd);
Chris@43 764 if (ret) {
Chris@43 765 log_log(log, op, ".add file read failure");
Chris@43 766 return -1;
Chris@43 767 }
Chris@43 768 log_log(log, op, "loaded .add file");
Chris@43 769 }
Chris@43 770 else
Chris@43 771 log_log(log, op, "missing .add file!");
Chris@43 772 }
Chris@43 773
Chris@43 774 /* recover the interrupted operation */
Chris@43 775 switch (op) {
Chris@43 776 case APPEND_OP:
Chris@43 777 ret = log_append(log, data, len);
Chris@43 778 break;
Chris@43 779 case COMPRESS_OP:
Chris@43 780 ret = log_compress(log, data, len);
Chris@43 781 break;
Chris@43 782 case REPLACE_OP:
Chris@43 783 ret = log_replace(log);
Chris@43 784 }
Chris@43 785
Chris@43 786 /* log status */
Chris@43 787 log_log(log, op, ret ? "failure" : "complete");
Chris@43 788
Chris@43 789 /* clean up */
Chris@43 790 if (data != NULL)
Chris@43 791 free(data);
Chris@43 792 return ret;
Chris@43 793 }
Chris@43 794
Chris@43 795 /* Close the foo.gz file (if open) and release the lock. */
Chris@43 796 local void log_close(struct log *log)
Chris@43 797 {
Chris@43 798 if (log->fd >= 0)
Chris@43 799 close(log->fd);
Chris@43 800 log->fd = -1;
Chris@43 801 log_unlock(log);
Chris@43 802 }
Chris@43 803
Chris@43 804 /* Open foo.gz, verify the header, and load the extra field contents, after
Chris@43 805 first creating the foo.lock file to gain exclusive access to the foo.*
Chris@43 806 files. If foo.gz does not exist or is empty, then write the initial header,
Chris@43 807 extra, and body content of an empty foo.gz log file. If there is an error
Chris@43 808 creating the lock file due to access restrictions, or an error reading or
Chris@43 809 writing the foo.gz file, or if the foo.gz file is not a proper log file for
Chris@43 810 this object (e.g. not a gzip file or does not contain the expected extra
Chris@43 811 field), then return true. If there is an error, the lock is released.
Chris@43 812 Otherwise, the lock is left in place. */
Chris@43 813 local int log_open(struct log *log)
Chris@43 814 {
Chris@43 815 int op;
Chris@43 816
Chris@43 817 /* release open file resource if left over -- can occur if lock lost
Chris@43 818 between gzlog_open() and gzlog_write() */
Chris@43 819 if (log->fd >= 0)
Chris@43 820 close(log->fd);
Chris@43 821 log->fd = -1;
Chris@43 822
Chris@43 823 /* negotiate exclusive access */
Chris@43 824 if (log_lock(log) < 0)
Chris@43 825 return -1;
Chris@43 826
Chris@43 827 /* open the log file, foo.gz */
Chris@43 828 strcpy(log->end, ".gz");
Chris@43 829 log->fd = open(log->path, O_RDWR | O_CREAT, 0644);
Chris@43 830 if (log->fd < 0) {
Chris@43 831 log_close(log);
Chris@43 832 return -1;
Chris@43 833 }
Chris@43 834
Chris@43 835 /* if new, initialize foo.gz with an empty log, delete old dictionary */
Chris@43 836 if (lseek(log->fd, 0, SEEK_END) == 0) {
Chris@43 837 if (write(log->fd, log_gzhead, HEAD) != HEAD ||
Chris@43 838 write(log->fd, log_gzext, EXTRA) != EXTRA ||
Chris@43 839 write(log->fd, log_gzbody, BODY) != BODY) {
Chris@43 840 log_close(log);
Chris@43 841 return -1;
Chris@43 842 }
Chris@43 843 strcpy(log->end, ".dict");
Chris@43 844 unlink(log->path);
Chris@43 845 }
Chris@43 846
Chris@43 847 /* verify log file and load extra field information */
Chris@43 848 if ((op = log_head(log)) < 0) {
Chris@43 849 log_close(log);
Chris@43 850 return -1;
Chris@43 851 }
Chris@43 852
Chris@43 853 /* check for interrupted process and if so, recover */
Chris@43 854 if (op != NO_OP && log_recover(log, op)) {
Chris@43 855 log_close(log);
Chris@43 856 return -1;
Chris@43 857 }
Chris@43 858
Chris@43 859 /* touch the lock file to prevent another process from grabbing it */
Chris@43 860 log_touch(log);
Chris@43 861 return 0;
Chris@43 862 }
Chris@43 863
Chris@43 864 /* See gzlog.h for the description of the external methods below */
Chris@43 865 gzlog *gzlog_open(char *path)
Chris@43 866 {
Chris@43 867 size_t n;
Chris@43 868 struct log *log;
Chris@43 869
Chris@43 870 /* check arguments */
Chris@43 871 if (path == NULL || *path == 0)
Chris@43 872 return NULL;
Chris@43 873
Chris@43 874 /* allocate and initialize log structure */
Chris@43 875 log = malloc(sizeof(struct log));
Chris@43 876 if (log == NULL)
Chris@43 877 return NULL;
Chris@43 878 strcpy(log->id, LOGID);
Chris@43 879 log->fd = -1;
Chris@43 880
Chris@43 881 /* save path and end of path for name construction */
Chris@43 882 n = strlen(path);
Chris@43 883 log->path = malloc(n + 9); /* allow for ".repairs" */
Chris@43 884 if (log->path == NULL) {
Chris@43 885 free(log);
Chris@43 886 return NULL;
Chris@43 887 }
Chris@43 888 strcpy(log->path, path);
Chris@43 889 log->end = log->path + n;
Chris@43 890
Chris@43 891 /* gain exclusive access and verify log file -- may perform a
Chris@43 892 recovery operation if needed */
Chris@43 893 if (log_open(log)) {
Chris@43 894 free(log->path);
Chris@43 895 free(log);
Chris@43 896 return NULL;
Chris@43 897 }
Chris@43 898
Chris@43 899 /* return pointer to log structure */
Chris@43 900 return log;
Chris@43 901 }
Chris@43 902
Chris@43 903 /* gzlog_compress() return values:
Chris@43 904 0: all good
Chris@43 905 -1: file i/o error (usually access issue)
Chris@43 906 -2: memory allocation failure
Chris@43 907 -3: invalid log pointer argument */
Chris@43 908 int gzlog_compress(gzlog *logd)
Chris@43 909 {
Chris@43 910 int fd, ret;
Chris@43 911 uint block;
Chris@43 912 size_t len, next;
Chris@43 913 unsigned char *data, buf[5];
Chris@43 914 struct log *log = logd;
Chris@43 915
Chris@43 916 /* check arguments */
Chris@43 917 if (log == NULL || strcmp(log->id, LOGID))
Chris@43 918 return -3;
Chris@43 919
Chris@43 920 /* see if we lost the lock -- if so get it again and reload the extra
Chris@43 921 field information (it probably changed), recover last operation if
Chris@43 922 necessary */
Chris@43 923 if (log_check(log) && log_open(log))
Chris@43 924 return -1;
Chris@43 925
Chris@43 926 /* create space for uncompressed data */
Chris@43 927 len = ((size_t)(log->last - log->first) & ~(((size_t)1 << 10) - 1)) +
Chris@43 928 log->stored;
Chris@43 929 if ((data = malloc(len)) == NULL)
Chris@43 930 return -2;
Chris@43 931
Chris@43 932 /* do statement here is just a cheap trick for error handling */
Chris@43 933 do {
Chris@43 934 /* read in the uncompressed data */
Chris@43 935 if (lseek(log->fd, log->first - 1, SEEK_SET) < 0)
Chris@43 936 break;
Chris@43 937 next = 0;
Chris@43 938 while (next < len) {
Chris@43 939 if (read(log->fd, buf, 5) != 5)
Chris@43 940 break;
Chris@43 941 block = PULL2(buf + 1);
Chris@43 942 if (next + block > len ||
Chris@43 943 read(log->fd, (char *)data + next, block) != block)
Chris@43 944 break;
Chris@43 945 next += block;
Chris@43 946 }
Chris@43 947 if (lseek(log->fd, 0, SEEK_CUR) != log->last + 4 + log->stored)
Chris@43 948 break;
Chris@43 949 log_touch(log);
Chris@43 950
Chris@43 951 /* write the uncompressed data to the .add file */
Chris@43 952 strcpy(log->end, ".add");
Chris@43 953 fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
Chris@43 954 if (fd < 0)
Chris@43 955 break;
Chris@43 956 ret = (size_t)write(fd, data, len) != len;
Chris@43 957 if (ret | close(fd))
Chris@43 958 break;
Chris@43 959 log_touch(log);
Chris@43 960
Chris@43 961 /* write the dictionary for the next compress to the .temp file */
Chris@43 962 strcpy(log->end, ".temp");
Chris@43 963 fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
Chris@43 964 if (fd < 0)
Chris@43 965 break;
Chris@43 966 next = DICT > len ? len : DICT;
Chris@43 967 ret = (size_t)write(fd, (char *)data + len - next, next) != next;
Chris@43 968 if (ret | close(fd))
Chris@43 969 break;
Chris@43 970 log_touch(log);
Chris@43 971
Chris@43 972 /* roll back to compressed data, mark the compress in progress */
Chris@43 973 log->last = log->first;
Chris@43 974 log->stored = 0;
Chris@43 975 if (log_mark(log, COMPRESS_OP))
Chris@43 976 break;
Chris@43 977 BAIL(7);
Chris@43 978
Chris@43 979 /* compress and append the data (clears mark) */
Chris@43 980 ret = log_compress(log, data, len);
Chris@43 981 free(data);
Chris@43 982 return ret;
Chris@43 983 } while (0);
Chris@43 984
Chris@43 985 /* broke out of do above on i/o error */
Chris@43 986 free(data);
Chris@43 987 return -1;
Chris@43 988 }
Chris@43 989
Chris@43 990 /* gzlog_write() return values:
Chris@43 991 0: all good
Chris@43 992 -1: file i/o error (usually access issue)
Chris@43 993 -2: memory allocation failure
Chris@43 994 -3: invalid log pointer argument */
Chris@43 995 int gzlog_write(gzlog *logd, void *data, size_t len)
Chris@43 996 {
Chris@43 997 int fd, ret;
Chris@43 998 struct log *log = logd;
Chris@43 999
Chris@43 1000 /* check arguments */
Chris@43 1001 if (log == NULL || strcmp(log->id, LOGID))
Chris@43 1002 return -3;
Chris@43 1003 if (data == NULL || len <= 0)
Chris@43 1004 return 0;
Chris@43 1005
Chris@43 1006 /* see if we lost the lock -- if so get it again and reload the extra
Chris@43 1007 field information (it probably changed), recover last operation if
Chris@43 1008 necessary */
Chris@43 1009 if (log_check(log) && log_open(log))
Chris@43 1010 return -1;
Chris@43 1011
Chris@43 1012 /* create and write .add file */
Chris@43 1013 strcpy(log->end, ".add");
Chris@43 1014 fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
Chris@43 1015 if (fd < 0)
Chris@43 1016 return -1;
Chris@43 1017 ret = (size_t)write(fd, data, len) != len;
Chris@43 1018 if (ret | close(fd))
Chris@43 1019 return -1;
Chris@43 1020 log_touch(log);
Chris@43 1021
Chris@43 1022 /* mark log file with append in progress */
Chris@43 1023 if (log_mark(log, APPEND_OP))
Chris@43 1024 return -1;
Chris@43 1025 BAIL(8);
Chris@43 1026
Chris@43 1027 /* append data (clears mark) */
Chris@43 1028 if (log_append(log, data, len))
Chris@43 1029 return -1;
Chris@43 1030
Chris@43 1031 /* check to see if it's time to compress -- if not, then done */
Chris@43 1032 if (((log->last - log->first) >> 10) + (log->stored >> 10) < TRIGGER)
Chris@43 1033 return 0;
Chris@43 1034
Chris@43 1035 /* time to compress */
Chris@43 1036 return gzlog_compress(log);
Chris@43 1037 }
Chris@43 1038
Chris@43 1039 /* gzlog_close() return values:
Chris@43 1040 0: ok
Chris@43 1041 -3: invalid log pointer argument */
Chris@43 1042 int gzlog_close(gzlog *logd)
Chris@43 1043 {
Chris@43 1044 struct log *log = logd;
Chris@43 1045
Chris@43 1046 /* check arguments */
Chris@43 1047 if (log == NULL || strcmp(log->id, LOGID))
Chris@43 1048 return -3;
Chris@43 1049
Chris@43 1050 /* close the log file and release the lock */
Chris@43 1051 log_close(log);
Chris@43 1052
Chris@43 1053 /* free structure and return */
Chris@43 1054 if (log->path != NULL)
Chris@43 1055 free(log->path);
Chris@43 1056 strcpy(log->id, "bad");
Chris@43 1057 free(log);
Chris@43 1058 return 0;
Chris@43 1059 }