14e00368fSchristos /* gzlog.h 2*ed8eb4c2Schristos Copyright (C) 2004, 2008, 2012 Mark Adler, all rights reserved 3*ed8eb4c2Schristos version 2.2, 14 Aug 2012 44e00368fSchristos 54e00368fSchristos This software is provided 'as-is', without any express or implied 64e00368fSchristos warranty. In no event will the author be held liable for any damages 74e00368fSchristos arising from the use of this software. 84e00368fSchristos 94e00368fSchristos Permission is granted to anyone to use this software for any purpose, 104e00368fSchristos including commercial applications, and to alter it and redistribute it 114e00368fSchristos freely, subject to the following restrictions: 124e00368fSchristos 134e00368fSchristos 1. The origin of this software must not be misrepresented; you must not 144e00368fSchristos claim that you wrote the original software. If you use this software 154e00368fSchristos in a product, an acknowledgment in the product documentation would be 164e00368fSchristos appreciated but is not required. 174e00368fSchristos 2. Altered source versions must be plainly marked as such, and must not be 184e00368fSchristos misrepresented as being the original software. 194e00368fSchristos 3. This notice may not be removed or altered from any source distribution. 204e00368fSchristos 214e00368fSchristos Mark Adler madler@alumni.caltech.edu 224e00368fSchristos */ 234e00368fSchristos 244e00368fSchristos /* Version History: 254e00368fSchristos 1.0 26 Nov 2004 First version 264e00368fSchristos 2.0 25 Apr 2008 Complete redesign for recovery of interrupted operations 274e00368fSchristos Interface changed slightly in that now path is a prefix 284e00368fSchristos Compression now occurs as needed during gzlog_write() 294e00368fSchristos gzlog_write() now always leaves the log file as valid gzip 30*ed8eb4c2Schristos 2.1 8 Jul 2012 Fix argument checks in gzlog_compress() and gzlog_write() 31*ed8eb4c2Schristos 2.2 14 Aug 2012 Clean up signed comparisons 324e00368fSchristos */ 334e00368fSchristos 344e00368fSchristos /* 354e00368fSchristos The gzlog object allows writing short messages to a gzipped log file, 364e00368fSchristos opening the log file locked for small bursts, and then closing it. The log 374e00368fSchristos object works by appending stored (uncompressed) data to the gzip file until 384e00368fSchristos 1 MB has been accumulated. At that time, the stored data is compressed, and 394e00368fSchristos replaces the uncompressed data in the file. The log file is truncated to 404e00368fSchristos its new size at that time. After each write operation, the log file is a 414e00368fSchristos valid gzip file that can decompressed to recover what was written. 424e00368fSchristos 434e00368fSchristos The gzlog operations can be interupted at any point due to an application or 444e00368fSchristos system crash, and the log file will be recovered the next time the log is 454e00368fSchristos opened with gzlog_open(). 464e00368fSchristos */ 474e00368fSchristos 484e00368fSchristos #ifndef GZLOG_H 494e00368fSchristos #define GZLOG_H 504e00368fSchristos 514e00368fSchristos /* gzlog object type */ 524e00368fSchristos typedef void gzlog; 534e00368fSchristos 544e00368fSchristos /* Open a gzlog object, creating the log file if it does not exist. Return 554e00368fSchristos NULL on error. Note that gzlog_open() could take a while to complete if it 564e00368fSchristos has to wait to verify that a lock is stale (possibly for five minutes), or 574e00368fSchristos if there is significant contention with other instantiations of this object 584e00368fSchristos when locking the resource. path is the prefix of the file names created by 594e00368fSchristos this object. If path is "foo", then the log file will be "foo.gz", and 604e00368fSchristos other auxiliary files will be created and destroyed during the process: 614e00368fSchristos "foo.dict" for a compression dictionary, "foo.temp" for a temporary (next) 624e00368fSchristos dictionary, "foo.add" for data being added or compressed, "foo.lock" for the 634e00368fSchristos lock file, and "foo.repairs" to log recovery operations performed due to 644e00368fSchristos interrupted gzlog operations. A gzlog_open() followed by a gzlog_close() 654e00368fSchristos will recover a previously interrupted operation, if any. */ 664e00368fSchristos gzlog *gzlog_open(char *path); 674e00368fSchristos 684e00368fSchristos /* Write to a gzlog object. Return zero on success, -1 if there is a file i/o 694e00368fSchristos error on any of the gzlog files (this should not happen if gzlog_open() 704e00368fSchristos succeeded, unless the device has run out of space or leftover auxiliary 714e00368fSchristos files have permissions or ownership that prevent their use), -2 if there is 724e00368fSchristos a memory allocation failure, or -3 if the log argument is invalid (e.g. if 734e00368fSchristos it was not created by gzlog_open()). This function will write data to the 744e00368fSchristos file uncompressed, until 1 MB has been accumulated, at which time that data 754e00368fSchristos will be compressed. The log file will be a valid gzip file upon successful 764e00368fSchristos return. */ 774e00368fSchristos int gzlog_write(gzlog *log, void *data, size_t len); 784e00368fSchristos 794e00368fSchristos /* Force compression of any uncompressed data in the log. This should be used 804e00368fSchristos sparingly, if at all. The main application would be when a log file will 814e00368fSchristos not be appended to again. If this is used to compress frequently while 824e00368fSchristos appending, it will both significantly increase the execution time and 834e00368fSchristos reduce the compression ratio. The return codes are the same as for 844e00368fSchristos gzlog_write(). */ 854e00368fSchristos int gzlog_compress(gzlog *log); 864e00368fSchristos 874e00368fSchristos /* Close a gzlog object. Return zero on success, -3 if the log argument is 884e00368fSchristos invalid. The log object is freed, and so cannot be referenced again. */ 894e00368fSchristos int gzlog_close(gzlog *log); 904e00368fSchristos 914e00368fSchristos #endif 92