2 /// \file tarfile-ops-nt.cc
3 /// Non-thread safe operation functions for a libtar-compatible
4 /// zlib compression interface.
7 Copyright (C) 2007-2012, 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.
24 #include <sys/types.h>
35 namespace gztar_nonthread
{
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 )
51 if( index
>= gzArraySize
) {
52 gzFile
*h
= (gzFile
*) realloc(gzHandles
,
53 (gzArraySize
+ 100) * sizeof(gzFile
));
63 int fd
= open(file
, flags
, mode
);
67 gzFile gfd
= gzdopen(fd
, (flags
& O_WRONLY
) ? "wb9" : "rb");
73 gzHandles
[index
] = gfd
;
77 int close_compressed(int fd
)
79 unsigned int ufd
= fd
;
80 assert( ufd
< gzArraySize
);
81 int ret
= gzclose(gzHandles
[ufd
]);
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