kusano fc6ab3
/* gzlog.h
kusano fc6ab3
  Copyright (C) 2004, 2008, 2012 Mark Adler, all rights reserved
kusano fc6ab3
  version 2.2, 14 Aug 2012
kusano fc6ab3
kusano fc6ab3
  This software is provided 'as-is', without any express or implied
kusano fc6ab3
  warranty.  In no event will the author be held liable for any damages
kusano fc6ab3
  arising from the use of this software.
kusano fc6ab3
kusano fc6ab3
  Permission is granted to anyone to use this software for any purpose,
kusano fc6ab3
  including commercial applications, and to alter it and redistribute it
kusano fc6ab3
  freely, subject to the following restrictions:
kusano fc6ab3
kusano fc6ab3
  1. The origin of this software must not be misrepresented; you must not
kusano fc6ab3
     claim that you wrote the original software. If you use this software
kusano fc6ab3
     in a product, an acknowledgment in the product documentation would be
kusano fc6ab3
     appreciated but is not required.
kusano fc6ab3
  2. Altered source versions must be plainly marked as such, and must not be
kusano fc6ab3
     misrepresented as being the original software.
kusano fc6ab3
  3. This notice may not be removed or altered from any source distribution.
kusano fc6ab3
kusano fc6ab3
  Mark Adler    madler@alumni.caltech.edu
kusano fc6ab3
 */
kusano fc6ab3
kusano fc6ab3
/* Version History:
kusano fc6ab3
   1.0  26 Nov 2004  First version
kusano fc6ab3
   2.0  25 Apr 2008  Complete redesign for recovery of interrupted operations
kusano fc6ab3
                     Interface changed slightly in that now path is a prefix
kusano fc6ab3
                     Compression now occurs as needed during gzlog_write()
kusano fc6ab3
                     gzlog_write() now always leaves the log file as valid gzip
kusano fc6ab3
   2.1   8 Jul 2012  Fix argument checks in gzlog_compress() and gzlog_write()
kusano fc6ab3
   2.2  14 Aug 2012  Clean up signed comparisons
kusano fc6ab3
 */
kusano fc6ab3
kusano fc6ab3
/*
kusano fc6ab3
   The gzlog object allows writing short messages to a gzipped log file,
kusano fc6ab3
   opening the log file locked for small bursts, and then closing it.  The log
kusano fc6ab3
   object works by appending stored (uncompressed) data to the gzip file until
kusano fc6ab3
   1 MB has been accumulated.  At that time, the stored data is compressed, and
kusano fc6ab3
   replaces the uncompressed data in the file.  The log file is truncated to
kusano fc6ab3
   its new size at that time.  After each write operation, the log file is a
kusano fc6ab3
   valid gzip file that can decompressed to recover what was written.
kusano fc6ab3
kusano fc6ab3
   The gzlog operations can be interupted at any point due to an application or
kusano fc6ab3
   system crash, and the log file will be recovered the next time the log is
kusano fc6ab3
   opened with gzlog_open().
kusano fc6ab3
 */
kusano fc6ab3
kusano fc6ab3
#ifndef GZLOG_H
kusano fc6ab3
#define GZLOG_H
kusano fc6ab3
kusano fc6ab3
/* gzlog object type */
kusano fc6ab3
typedef void gzlog;
kusano fc6ab3
kusano fc6ab3
/* Open a gzlog object, creating the log file if it does not exist.  Return
kusano fc6ab3
   NULL on error.  Note that gzlog_open() could take a while to complete if it
kusano fc6ab3
   has to wait to verify that a lock is stale (possibly for five minutes), or
kusano fc6ab3
   if there is significant contention with other instantiations of this object
kusano fc6ab3
   when locking the resource.  path is the prefix of the file names created by
kusano fc6ab3
   this object.  If path is "foo", then the log file will be "foo.gz", and
kusano fc6ab3
   other auxiliary files will be created and destroyed during the process:
kusano fc6ab3
   "foo.dict" for a compression dictionary, "foo.temp" for a temporary (next)
kusano fc6ab3
   dictionary, "foo.add" for data being added or compressed, "foo.lock" for the
kusano fc6ab3
   lock file, and "foo.repairs" to log recovery operations performed due to
kusano fc6ab3
   interrupted gzlog operations.  A gzlog_open() followed by a gzlog_close()
kusano fc6ab3
   will recover a previously interrupted operation, if any. */
kusano fc6ab3
gzlog *gzlog_open(char *path);
kusano fc6ab3
kusano fc6ab3
/* Write to a gzlog object.  Return zero on success, -1 if there is a file i/o
kusano fc6ab3
   error on any of the gzlog files (this should not happen if gzlog_open()
kusano fc6ab3
   succeeded, unless the device has run out of space or leftover auxiliary
kusano fc6ab3
   files have permissions or ownership that prevent their use), -2 if there is
kusano fc6ab3
   a memory allocation failure, or -3 if the log argument is invalid (e.g. if
kusano fc6ab3
   it was not created by gzlog_open()).  This function will write data to the
kusano fc6ab3
   file uncompressed, until 1 MB has been accumulated, at which time that data
kusano fc6ab3
   will be compressed.  The log file will be a valid gzip file upon successful
kusano fc6ab3
   return. */
kusano fc6ab3
int gzlog_write(gzlog *log, void *data, size_t len);
kusano fc6ab3
kusano fc6ab3
/* Force compression of any uncompressed data in the log.  This should be used
kusano fc6ab3
   sparingly, if at all.  The main application would be when a log file will
kusano fc6ab3
   not be appended to again.  If this is used to compress frequently while
kusano fc6ab3
   appending, it will both significantly increase the execution time and
kusano fc6ab3
   reduce the compression ratio.  The return codes are the same as for
kusano fc6ab3
   gzlog_write(). */
kusano fc6ab3
int gzlog_compress(gzlog *log);
kusano fc6ab3
kusano fc6ab3
/* Close a gzlog object.  Return zero on success, -3 if the log argument is
kusano fc6ab3
   invalid.  The log object is freed, and so cannot be referenced again. */
kusano fc6ab3
int gzlog_close(gzlog *log);
kusano fc6ab3
kusano fc6ab3
#endif