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