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