Bumped copyright dates for 2013
[barry.git] / src / tarfile-ops-nt.cc
bloba77329f48259dbaa71304b7a0c1d73710804cc26
1 ///
2 /// \file tarfile-ops-nt.cc
3 /// Non-thread safe operation functions for a libtar-compatible
4 /// zlib compression interface.
6 /*
7 Copyright (C) 2007-2013, Chris Frey <cdfrey@foursquare.net>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "tarfile.h"
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <stdlib.h>
29 #include <zlib.h>
31 #include <assert.h>
33 namespace reuse {
35 namespace gztar_nonthread {
37 namespace {
38 // array of compressed file handles... needed for architectures
39 // where sizeof(int) != sizeof(gzFile)
40 gzFile *gzHandles = 0;
41 unsigned int gzArraySize = 0;
44 int open_compressed(const char *file, int flags, mode_t mode)
46 unsigned int index = 0;
47 for( ; index < gzArraySize; index++ ) {
48 if( gzHandles[index] == 0 )
49 break;
51 if( index >= gzArraySize ) {
52 gzFile *h = (gzFile*) realloc(gzHandles,
53 (gzArraySize + 100) * sizeof(gzFile));
54 if( h ) {
55 gzHandles = h;
56 gzArraySize += 100;
58 else {
59 return -1;
63 int fd = open(file, flags, mode);
64 if( fd == -1 )
65 return -1;
67 gzFile gfd = gzdopen(fd, (flags & O_WRONLY) ? "wb9" : "rb");
68 if( gfd == NULL ) {
69 close(fd);
70 return -1;
73 gzHandles[index] = gfd;
74 return index;
77 int close_compressed(int fd)
79 unsigned int ufd = fd;
80 assert( ufd < gzArraySize );
81 int ret = gzclose(gzHandles[ufd]);
82 gzHandles[ufd] = 0;
83 return ret;
86 ssize_t read_compressed(int fd, void *buf, size_t size)
88 unsigned int ufd = fd;
89 assert( ufd < gzArraySize );
90 return gzread(gzHandles[ufd], buf, size);
93 ssize_t write_compressed(int fd, const void *buf, size_t size)
95 unsigned int ufd = fd;
96 assert( ufd < gzArraySize );
97 return gzwrite(gzHandles[ufd], buf, size);
100 } // namespace gztar_nonthread
103 tartype_t gztar_ops_nonthread = {
104 (openfunc_t) gztar_nonthread::open_compressed,
105 gztar_nonthread::close_compressed,
106 gztar_nonthread::read_compressed,
107 gztar_nonthread::write_compressed
111 } // namespace reuse