libzypp  17.34.1
zerocopystreams.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \----------------------------------------------------------------------/
9 *
10 * This file contains private API, this might break at any time between releases.
11 * You have been warned!
12 *
13 * Some versions of protobuf lite do not export the simple zero copy streams,
14 * so for those we copied them from upstream.
15 */
16 #ifndef ZYPP_NG_RPC_ZEROCOPYSTREAMS_H_INCLUDED
17 #define ZYPP_NG_RPC_ZEROCOPYSTREAMS_H_INCLUDED
18 
19 #ifndef PROTOBUFLITE_HAS_NO_ZEROCOPYSTREAM
20 
21 // just use the one we get from libprotobuf
22 #include <google/protobuf/io/zero_copy_stream_impl.h>
23 
24 // pull them into our namespace
25 namespace zyppng {
28 }
29 
30 #else
31 
32 // Protocol Buffers - Google's data interchange format
33 // Copyright 2008 Google Inc. All rights reserved.
34 // https://developers.google.com/protocol-buffers/
35 //
36 // Redistribution and use in source and binary forms, with or without
37 // modification, are permitted provided that the following conditions are
38 // met:
39 //
40 // * Redistributions of source code must retain the above copyright
41 // notice, this list of conditions and the following disclaimer.
42 // * Redistributions in binary form must reproduce the above
43 // copyright notice, this list of conditions and the following disclaimer
44 // in the documentation and/or other materials provided with the
45 // distribution.
46 // * Neither the name of Google Inc. nor the names of its
47 // contributors may be used to endorse or promote products derived from
48 // this software without specific prior written permission.
49 //
50 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 
62 // Author: kenton@google.com (Kenton Varda)
63 // Based on original Protocol Buffers design by
64 // Sanjay Ghemawat, Jeff Dean, and others.
65 //
66 // This file contains common implementations of the interfaces defined in
67 // zero_copy_stream.h which are only included in the full (non-lite)
68 // protobuf library. These implementations include Unix file descriptors
69 // and C++ iostreams. See also: zero_copy_stream_impl_lite.h
70 
71 #include <string>
72 #include <iosfwd>
73 #include <google/protobuf/io/zero_copy_stream.h>
74 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
75 #include <google/protobuf/stubs/common.h>
76 
77 // pull into zyppng namespace, even though protobuf-lite does not export the symbols of those
78 // classes the headers still define them. So make sure we do not clash.
79 namespace zyppng {
80 
81 // A ZeroCopyInputStream which reads from a file descriptor.
82 //
83 // FileInputStream is preferred over using an ifstream with IstreamInputStream.
84 // The latter will introduce an extra layer of buffering, harming performance.
85 // Also, it's conceivable that FileInputStream could someday be enhanced
86 // to use zero-copy file descriptors on OSs which support them.
87 class FileInputStream : public google::protobuf::io::ZeroCopyInputStream {
88  public:
89  // Creates a stream that reads from the given Unix file descriptor.
90  // If a block_size is given, it specifies the number of bytes that
91  // should be read and returned with each call to Next(). Otherwise,
92  // a reasonable default is used.
93  explicit FileInputStream(int file_descriptor, int block_size = -1);
94 
95  // Flushes any buffers and closes the underlying file. Returns false if
96  // an error occurs during the process; use GetErrno() to examine the error.
97  // Even if an error occurs, the file descriptor is closed when this returns.
98  bool Close();
99 
100  // By default, the file descriptor is not closed when the stream is
101  // destroyed. Call SetCloseOnDelete(true) to change that. WARNING:
102  // This leaves no way for the caller to detect if close() fails. If
103  // detecting close() errors is important to you, you should arrange
104  // to close the descriptor yourself.
105  void SetCloseOnDelete(bool value) { copying_input_.SetCloseOnDelete(value); }
106 
107  // If an I/O error has occurred on this file descriptor, this is the
108  // errno from that error. Otherwise, this is zero. Once an error
109  // occurs, the stream is broken and all subsequent operations will
110  // fail.
111  int GetErrno() { return copying_input_.GetErrno(); }
112 
113  // implements ZeroCopyInputStream ----------------------------------
114  bool Next(const void** data, int* size);
115  void BackUp(int count);
116  bool Skip(int count);
117  google::protobuf::int64 ByteCount() const;
118 
119  private:
120  class CopyingFileInputStream : public google::protobuf::io::CopyingInputStream {
121  public:
122  CopyingFileInputStream(int file_descriptor);
123  ~CopyingFileInputStream();
124 
125  bool Close();
126  void SetCloseOnDelete(bool value) { close_on_delete_ = value; }
127  int GetErrno() { return errno_; }
128 
129  // implements CopyingInputStream ---------------------------------
130  int Read(void* buffer, int size);
131  int Skip(int count);
132 
133  private:
134  // The file descriptor.
135  const int file_;
136  bool close_on_delete_;
137  bool is_closed_;
138 
139  // The errno of the I/O error, if one has occurred. Otherwise, zero.
140  int errno_;
141 
142  // Did we try to seek once and fail? If so, we assume this file descriptor
143  // doesn't support seeking and won't try again.
144  bool previous_seek_failed_;
145 
146  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingFileInputStream);
147  };
148 
149  CopyingFileInputStream copying_input_;
150  google::protobuf::io::CopyingInputStreamAdaptor impl_;
151 
152  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileInputStream);
153 };
154 
155 // ===================================================================
156 
157 // A ZeroCopyOutputStream which writes to a file descriptor.
158 //
159 // FileOutputStream is preferred over using an ofstream with
160 // OstreamOutputStream. The latter will introduce an extra layer of buffering,
161 // harming performance. Also, it's conceivable that FileOutputStream could
162 // someday be enhanced to use zero-copy file descriptors on OSs which
163 // support them.
164 class FileOutputStream : public google::protobuf::io::ZeroCopyOutputStream {
165  public:
166  // Creates a stream that writes to the given Unix file descriptor.
167  // If a block_size is given, it specifies the size of the buffers
168  // that should be returned by Next(). Otherwise, a reasonable default
169  // is used.
170  explicit FileOutputStream(int file_descriptor, int block_size = -1);
171  ~FileOutputStream();
172 
173  // Flushes any buffers and closes the underlying file. Returns false if
174  // an error occurs during the process; use GetErrno() to examine the error.
175  // Even if an error occurs, the file descriptor is closed when this returns.
176  bool Close();
177 
178  // Flushes FileOutputStream's buffers but does not close the
179  // underlying file. No special measures are taken to ensure that
180  // underlying operating system file object is synchronized to disk.
181  bool Flush();
182 
183  // By default, the file descriptor is not closed when the stream is
184  // destroyed. Call SetCloseOnDelete(true) to change that. WARNING:
185  // This leaves no way for the caller to detect if close() fails. If
186  // detecting close() errors is important to you, you should arrange
187  // to close the descriptor yourself.
188  void SetCloseOnDelete(bool value) { copying_output_.SetCloseOnDelete(value); }
189 
190  // If an I/O error has occurred on this file descriptor, this is the
191  // errno from that error. Otherwise, this is zero. Once an error
192  // occurs, the stream is broken and all subsequent operations will
193  // fail.
194  int GetErrno() { return copying_output_.GetErrno(); }
195 
196  // implements ZeroCopyOutputStream ---------------------------------
197  bool Next(void** data, int* size);
198  void BackUp(int count);
199  google::protobuf::int64 ByteCount() const;
200 
201  private:
202  class CopyingFileOutputStream : public google::protobuf::io::CopyingOutputStream {
203  public:
204  CopyingFileOutputStream(int file_descriptor);
205  ~CopyingFileOutputStream();
206 
207  bool Close();
208  void SetCloseOnDelete(bool value) { close_on_delete_ = value; }
209  int GetErrno() { return errno_; }
210 
211  // implements CopyingOutputStream --------------------------------
212  bool Write(const void* buffer, int size);
213 
214  private:
215  // The file descriptor.
216  const int file_;
217  bool close_on_delete_;
218  bool is_closed_;
219 
220  // The errno of the I/O error, if one has occurred. Otherwise, zero.
221  int errno_;
222 
223  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingFileOutputStream);
224  };
225 
226  CopyingFileOutputStream copying_output_;
227  google::protobuf::io::CopyingOutputStreamAdaptor impl_;
228 
229  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileOutputStream);
230 };
231 
232 } // namespace
233 
234 #endif
235 
236 
237 
238 #endif
google::protobuf::io::FileInputStream FileInputStream
google::protobuf::io::FileOutputStream FileOutputStream