1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
10 | (C) SuSE Linux Products GmbH |
11 \----------------------------------------------------------------------/
15 Author: Michael Andres <ma@suse.de>
16 Maintainer: Michael Andres <ma@suse.de>
18 Purpose: Streams reading and writing gzip files.
24 #include "zypp/base/LogControl.h"
25 #include "zypp/base/LogTools.h"
28 #include "zypp/base/GzStream.h"
30 #include <sys/types.h>
34 ///////////////////////////////////////////////////////////////////
36 { /////////////////////////////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////
38 namespace gzstream_detail
39 { /////////////////////////////////////////////////////////////////
42 ///////////////////////////////////////////////////////////////////
44 // CLASS NAME : ZlibError
46 ///////////////////////////////////////////////////////////////////
48 ///////////////////////////////////////////////////////////////////
50 // METHOD NAME : ZlibError::strerror
51 // METHOD TYPE : std::string
54 ZlibError::strerror() const
56 std::string ret = ( _zError ? ::zError( _zError ) : "OK" );
57 if ( _zError == Z_ERRNO )
58 ret += std::string("(") + ::strerror( _errno ) + ")";
62 ///////////////////////////////////////////////////////////////////
64 // CLASS NAME : fgzstreambuf
66 ///////////////////////////////////////////////////////////////////
68 ///////////////////////////////////////////////////////////////////
70 // METHOD NAME : gzstreambufimpl::openImpl
74 gzstreambufimpl::openImpl(const char *name_r, std::ios_base::openmode mode_r)
79 // we expect gzdopen to handle errors of ::open
80 if ( mode_r == std::ios_base::in )
82 _fd = ::open( name_r, O_RDONLY | O_CLOEXEC );
83 _file = gzdopen( _fd, "rb" );
85 else if ( mode_r == std::ios_base::out )
87 _fd = ::open( name_r, O_WRONLY|O_CREAT|O_CLOEXEC, 0666 );
88 _file = gzdopen( _fd, "wb" );
90 // else: not supported
104 ///////////////////////////////////////////////////////////////////
106 // METHOD NAME : gzstreambufimpl::closeImpl
107 // METHOD TYPE : bool
110 gzstreambufimpl::closeImpl()
117 // it also closes _fd, fine
118 int r = gzclose( _file );
122 // DONT call setZError() here, as _file is no longer valid
124 _error._errno = errno;
130 _mode = std::ios_base::openmode(0);
137 ///////////////////////////////////////////////////////////////////
139 // METHOD NAME : gzstreambufimpl::readData
140 // METHOD TYPE : std::streamsize
143 gzstreambufimpl::readData( char * buffer_r, std::streamsize maxcount_r )
145 int read = gzread( _file, buffer_r, maxcount_r );
151 ///////////////////////////////////////////////////////////////////
153 // METHOD NAME : gzstreambufimpl::writeData
154 // METHOD TYPE : bool
157 gzstreambufimpl::writeData( const char * buffer_r, std::streamsize count_r )
162 if ( (written = gzwrite( _file, buffer_r, count_r )) == 0 )
165 return( written == count_r );
168 ///////////////////////////////////////////////////////////////////
170 // METHOD NAME : gzstreambufimpl::seekTo
171 // METHOD TYPE : off_t
174 gzstreambufimpl::seekTo( off_t off_r, std::ios_base::seekdir way_r, std::ios_base::openmode )
176 z_off_t ret = gzseek( _file, off_r, way_r );
182 ///////////////////////////////////////////////////////////////////
184 // METHOD NAME : gzstreambufimpl::tell
185 // METHOD TYPE : off_t
188 gzstreambufimpl::tell() const
190 z_off_t ret = gztell( _file );
197 gzstreambufimpl::compressed_tell() const
199 off_t pos = lseek (_fd, 0, SEEK_CUR);
200 // hopefully the conversion is ok
204 /////////////////////////////////////////////////////////////////
205 } // namespace gzstream_detail
206 ///////////////////////////////////////////////////////////////////
207 /////////////////////////////////////////////////////////////////
209 ///////////////////////////////////////////////////////////////////