# tarfile-ops-nt.cc

Go to the documentation of this file.

    00001 ///
    00002 /// \file       tarfile-ops-nt.cc
    00003 ///             Non-thread safe operation functions for a libtar-compatible
    00004 ///             zlib compression interface.
    00005
    00006 /*
    00007     Copyright (C) 2007-2011, Chris Frey <cdfrey@foursquare.net>
    00008
    00009     This program is free software; you can redistribute it and/or modify
    00010     it under the terms of the GNU General Public License as published by
    00011     the Free Software Foundation; either version 2 of the License, or
    00012     (at your option) any later version.
    00013
    00014     This program is distributed in the hope that it will be useful,
    00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00017
    00018     See the GNU General Public License in the COPYING file at the
    00019     root directory of this project for more details.
    00020 */
    00021
    00022 #include "tarfile.h"
    00023
    00024 #include <sys/types.h>
    00025 #include <sys/stat.h>
    00026 #include <fcntl.h>
    00027 #include <stdlib.h>
    00028
    00029 #include <zlib.h>
    00030
    00031 #include <assert.h>
    00032
    00033 namespace reuse {
    00034
    00035 namespace gztar_nonthread {
    00036
    00037         namespace {
    00038                 // array of compressed file handles... needed for architectures
    00039                 // where sizeof(int) != sizeof(gzFile)
    00040                 gzFile *gzHandles = 0;
    00041                 unsigned int gzArraySize = 0;
    00042         }
    00043
    00044         int open_compressed(const char *file, int flags, mode_t mode)
    00045         {
    00046                 unsigned int index = 0;
    00047                 for( ; index < gzArraySize; index++ ) {
    00048                         if( gzHandles[index] == 0 )
    00049                                 break;
    00050                 }
    00051                 if( index >= gzArraySize ) {
    00052                         gzFile *h = (gzFile*) realloc(gzHandles,
    00053                                 (gzArraySize + 100) * sizeof(gzFile));
    00054                         if( h ) {
    00055                                 gzHandles = h;
    00056                                 gzArraySize += 100;
    00057                         }
    00058                         else {
    00059                                 return -1;
    00060                         }
    00061                 }
    00062
    00063                 int fd = open(file, flags, mode);
    00064                 if( fd == -1 )
    00065                         return -1;
    00066
    00067                 gzFile gfd = gzdopen(fd, (flags & O_WRONLY) ? "wb9" : "rb");
    00068                 if( gfd == NULL ) {
    00069                         close(fd);
    00070                         return -1;
    00071                 }
    00072
    00073                 gzHandles[index] = gfd;
    00074                 return index;
    00075         }
    00076
    00077         int close_compressed(int fd)
    00078         {
    00079                 unsigned int ufd = fd;
    00080                 assert( ufd < gzArraySize );
    00081                 int ret = gzclose(gzHandles[ufd]);
    00082                 gzHandles[ufd] = 0;
    00083                 return ret;
    00084         }
    00085
    00086         ssize_t read_compressed(int fd, void *buf, size_t size)
    00087         {
    00088                 unsigned int ufd = fd;
    00089                 assert( ufd < gzArraySize );
    00090                 return gzread(gzHandles[ufd], buf, size);
    00091         }
    00092
    00093         ssize_t write_compressed(int fd, const void *buf, size_t size)
    00094         {
    00095                 unsigned int ufd = fd;
    00096                 assert( ufd < gzArraySize );
    00097                 return gzwrite(gzHandles[ufd], buf, size);
    00098         }
    00099
    00100 } // namespace gztar_nonthread
    00101
    00102
    00103 tartype_t gztar_ops_nonthread = {
    00104         (openfunc_t) gztar_nonthread::open_compressed,
    00105         gztar_nonthread::close_compressed,
    00106         gztar_nonthread::read_compressed,
    00107         gztar_nonthread::write_compressed
    00108 };
    00109
    00110
    00111 } // namespace reuse
    00112