tarfile-ops-nt.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00039
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 }
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 }
00112