annotate zlib/examples/gzlog.c @ 111:04ced10e8804

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