1*16dce513Schristos /* gzlog.h 2*16dce513Schristos Copyright (C) 2004, 2008, 2012 Mark Adler, all rights reserved 3*16dce513Schristos version 2.2, 14 Aug 2012 4*16dce513Schristos 5*16dce513Schristos This software is provided 'as-is', without any express or implied 6*16dce513Schristos warranty. In no event will the author be held liable for any damages 7*16dce513Schristos arising from the use of this software. 8*16dce513Schristos 9*16dce513Schristos Permission is granted to anyone to use this software for any purpose, 10*16dce513Schristos including commercial applications, and to alter it and redistribute it 11*16dce513Schristos freely, subject to the following restrictions: 12*16dce513Schristos 13*16dce513Schristos 1. The origin of this software must not be misrepresented; you must not 14*16dce513Schristos claim that you wrote the original software. If you use this software 15*16dce513Schristos in a product, an acknowledgment in the product documentation would be 16*16dce513Schristos appreciated but is not required. 17*16dce513Schristos 2. Altered source versions must be plainly marked as such, and must not be 18*16dce513Schristos misrepresented as being the original software. 19*16dce513Schristos 3. This notice may not be removed or altered from any source distribution. 20*16dce513Schristos 21*16dce513Schristos Mark Adler madler@alumni.caltech.edu 22*16dce513Schristos */ 23*16dce513Schristos 24*16dce513Schristos /* Version History: 25*16dce513Schristos 1.0 26 Nov 2004 First version 26*16dce513Schristos 2.0 25 Apr 2008 Complete redesign for recovery of interrupted operations 27*16dce513Schristos Interface changed slightly in that now path is a prefix 28*16dce513Schristos Compression now occurs as needed during gzlog_write() 29*16dce513Schristos gzlog_write() now always leaves the log file as valid gzip 30*16dce513Schristos 2.1 8 Jul 2012 Fix argument checks in gzlog_compress() and gzlog_write() 31*16dce513Schristos 2.2 14 Aug 2012 Clean up signed comparisons 32*16dce513Schristos */ 33*16dce513Schristos 34*16dce513Schristos /* 35*16dce513Schristos The gzlog object allows writing short messages to a gzipped log file, 36*16dce513Schristos opening the log file locked for small bursts, and then closing it. The log 37*16dce513Schristos object works by appending stored (uncompressed) data to the gzip file until 38*16dce513Schristos 1 MB has been accumulated. At that time, the stored data is compressed, and 39*16dce513Schristos replaces the uncompressed data in the file. The log file is truncated to 40*16dce513Schristos its new size at that time. After each write operation, the log file is a 41*16dce513Schristos valid gzip file that can decompressed to recover what was written. 42*16dce513Schristos 43*16dce513Schristos The gzlog operations can be interupted at any point due to an application or 44*16dce513Schristos system crash, and the log file will be recovered the next time the log is 45*16dce513Schristos opened with gzlog_open(). 46*16dce513Schristos */ 47*16dce513Schristos 48*16dce513Schristos #ifndef GZLOG_H 49*16dce513Schristos #define GZLOG_H 50*16dce513Schristos 51*16dce513Schristos /* gzlog object type */ 52*16dce513Schristos typedef void gzlog; 53*16dce513Schristos 54*16dce513Schristos /* Open a gzlog object, creating the log file if it does not exist. Return 55*16dce513Schristos NULL on error. Note that gzlog_open() could take a while to complete if it 56*16dce513Schristos has to wait to verify that a lock is stale (possibly for five minutes), or 57*16dce513Schristos if there is significant contention with other instantiations of this object 58*16dce513Schristos when locking the resource. path is the prefix of the file names created by 59*16dce513Schristos this object. If path is "foo", then the log file will be "foo.gz", and 60*16dce513Schristos other auxiliary files will be created and destroyed during the process: 61*16dce513Schristos "foo.dict" for a compression dictionary, "foo.temp" for a temporary (next) 62*16dce513Schristos dictionary, "foo.add" for data being added or compressed, "foo.lock" for the 63*16dce513Schristos lock file, and "foo.repairs" to log recovery operations performed due to 64*16dce513Schristos interrupted gzlog operations. A gzlog_open() followed by a gzlog_close() 65*16dce513Schristos will recover a previously interrupted operation, if any. */ 66*16dce513Schristos gzlog *gzlog_open(char *path); 67*16dce513Schristos 68*16dce513Schristos /* Write to a gzlog object. Return zero on success, -1 if there is a file i/o 69*16dce513Schristos error on any of the gzlog files (this should not happen if gzlog_open() 70*16dce513Schristos succeeded, unless the device has run out of space or leftover auxiliary 71*16dce513Schristos files have permissions or ownership that prevent their use), -2 if there is 72*16dce513Schristos a memory allocation failure, or -3 if the log argument is invalid (e.g. if 73*16dce513Schristos it was not created by gzlog_open()). This function will write data to the 74*16dce513Schristos file uncompressed, until 1 MB has been accumulated, at which time that data 75*16dce513Schristos will be compressed. The log file will be a valid gzip file upon successful 76*16dce513Schristos return. */ 77*16dce513Schristos int gzlog_write(gzlog *log, void *data, size_t len); 78*16dce513Schristos 79*16dce513Schristos /* Force compression of any uncompressed data in the log. This should be used 80*16dce513Schristos sparingly, if at all. The main application would be when a log file will 81*16dce513Schristos not be appended to again. If this is used to compress frequently while 82*16dce513Schristos appending, it will both significantly increase the execution time and 83*16dce513Schristos reduce the compression ratio. The return codes are the same as for 84*16dce513Schristos gzlog_write(). */ 85*16dce513Schristos int gzlog_compress(gzlog *log); 86*16dce513Schristos 87*16dce513Schristos /* Close a gzlog object. Return zero on success, -3 if the log argument is 88*16dce513Schristos invalid. The log object is freed, and so cannot be referenced again. */ 89*16dce513Schristos int gzlog_close(gzlog *log); 90*16dce513Schristos 91*16dce513Schristos #endif 92