libzypp  17.25.0
ZckStream.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
9 #include <zypp/base/ZckStream.h>
10 #include <zypp/base/String.h>
11 
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <errno.h>
17 
18 extern "C" {
19 #include <zck.h>
20 }
21 
22 namespace zypp {
23 
24  namespace detail {
25 
27  {
28  closeImpl();
29  }
30 
31  bool zckstreambufimpl::openImpl( const char *name_r, std::ios_base::openmode mode_r )
32  {
33  if ( isOpen() )
34  return false;
35 
36  if ( mode_r == std::ios_base::in ) {
37  _fd = ::open( name_r, O_RDONLY | O_CLOEXEC );
38  _isReading = true;
39 
40  } else if ( mode_r == std::ios_base::out ) {
41  _fd = ::open( name_r, O_WRONLY|O_CREAT|O_CLOEXEC, 0666 );
42  _isReading = false;
43  } else {
44  //unsupported mode
45  _lastErr = str::Format("ZChunk backend does not support the given open mode.");
46  return false;
47  }
48 
49  if ( _fd < 0 ) {
50  const int errSrv = errno;
51  _lastErr = str::Format("Opening file failed: %1%") % ::strerror( errSrv );
52  return false;
53  }
54 
55  _zContext = ::zck_create();
56  if ( !_zContext ) {
57  setError();
58  return false;
59  }
60 
61  if ( _isReading ) {
62  if ( !::zck_init_read( _zContext, _fd ) ) {
63  setError();
64  return false;
65  }
66  } else {
67  if ( !::zck_init_write( _zContext, _fd ) ) {
68  setError();
69  return false;
70  }
71  }
72 
73  _currfp = 0;
74  return true;
75  }
76 
78  {
79  if ( !isOpen() )
80  return true;
81 
82  bool success = true;
83 
84  if ( !zck_close( _zContext ) ) {
85  setError();
86  success = false;
87  }
88 
89  zck_free( &_zContext );
90  _zContext = nullptr;
91  ::close( _fd );
92  _fd = -1;
93  return success;
94  }
95 
97  {
98  if ( !zck_is_error( _zContext ) )
99  return;
100  _lastErr = zck_get_error( _zContext );
101  zck_clear_error( _zContext );
102  }
103 
104  std::streamsize zckstreambufimpl::readData(char *buffer_r, std::streamsize maxcount_r)
105  {
106  if ( !isOpen() || !canRead() )
107  return -1;
108 
109  ssize_t read = zck_read( _zContext, buffer_r, maxcount_r );
110  if ( read > 0 )
111  _currfp += read;
112  else
113  setError();
114 
115  return read;
116  }
117 
118  bool zckstreambufimpl::writeData(const char *buffer_r, std::streamsize count_r)
119  {
120  if ( !isOpen() || !canWrite() )
121  return false;
122 
123  ssize_t wrote = zck_write( _zContext, buffer_r, count_r );
124  if ( wrote > 0 )
125  _currfp += wrote;
126  else
127  setError();
128 
129  return wrote;
130  }
131 
133  {
134  return ( _fd >= 0 );
135  }
136 
138  {
139  return _isReading;
140  }
141 
143  {
144  return !_isReading;
145  }
146 
147  bool zckstreambufimpl::canSeek( std::ios_base::seekdir ) const
148  {
149  return false;
150  }
151 
152  off_t zckstreambufimpl::seekTo(off_t, std::ios_base::seekdir , std::ios_base::openmode)
153  {
154  return -1;
155  }
156 
158  {
159  return _currfp;
160  }
161  }
162 
163 }
zypp::detail::zckstreambufimpl::seekTo
off_t seekTo(off_t off_r, std::ios_base::seekdir way_r, std::ios_base::openmode omode_r)
Definition: ZckStream.cc:152
zypp::base::sysconfig::read
std::map< std::string, std::string > read(const Pathname &_path)
Read sysconfig file path_r and return (key,valye) pairs.
Definition: Sysconfig.cc:34
zypp::detail::zckstreambufimpl::_currfp
off_t _currfp
Definition: ZckStream.h:59
zypp::detail::zckstreambufimpl::readData
std::streamsize readData(char *buffer_r, std::streamsize maxcount_r)
Definition: ZckStream.cc:104
zypp::detail::zckstreambufimpl::closeImpl
bool closeImpl()
Definition: ZckStream.cc:77
zypp::detail::zckstreambufimpl::canWrite
bool canWrite() const
Definition: ZckStream.cc:142
zypp::str::strerror
std::string strerror(int errno_r)
Return string describing the error_r code.
Definition: String.cc:51
zypp::detail::zckstreambufimpl::isOpen
bool isOpen() const
Definition: ZckStream.cc:132
ZckStream.h
zypp::detail::zckstreambufimpl::tell
off_t tell() const
Definition: ZckStream.cc:157
zypp::detail::zckstreambufimpl::_lastErr
error_type _lastErr
Definition: ZckStream.h:60
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
zypp::detail::zckstreambufimpl::canRead
bool canRead() const
Definition: ZckStream.cc:137
zypp::detail::zckstreambufimpl::writeData
bool writeData(const char *buffer_r, std::streamsize count_r)
Definition: ZckStream.cc:118
zypp::detail::zckstreambufimpl::setError
void setError()
Definition: ZckStream.cc:96
zypp::detail::zckstreambufimpl::_fd
int _fd
Definition: ZckStream.h:56
zypp::detail::zckstreambufimpl::~zckstreambufimpl
~zckstreambufimpl()
Definition: ZckStream.cc:26
String.h
zypp::detail::zckstreambufimpl::openImpl
bool openImpl(const char *name_r, std::ios_base::openmode mode_r)
Definition: ZckStream.cc:31
zypp::str::Format
Convenient building of std::string with boost::format. Basically a boost::format autoconvertible to s...
Definition: String.h:249
zypp::detail::zckstreambufimpl::_isReading
bool _isReading
Definition: ZckStream.h:57
zypp::detail::zckstreambufimpl::_zContext
zckCtx * _zContext
Definition: ZckStream.h:58
zypp::detail::zckstreambufimpl::canSeek
bool canSeek(std::ios_base::seekdir way_r) const
Definition: ZckStream.cc:147