Disk ARchive  2.6.2
Full featured and portable backup and archiving tool
generic_file.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2019 Denis Corbin
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 //
19 // to contact the author : http://dar.linux.free.fr/email.html
20 /*********************************************************************/
21 
36 
37 #ifndef GENERIC_FILE_HPP
38 #define GENERIC_FILE_HPP
39 
40 #include "../my_config.h"
41 
42 extern "C"
43 {
44 #if HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
47 } // end extern "C"
48 
49 #include "proto_generic_file.hpp"
50 #include "integers.hpp"
51 #include "crc.hpp"
52 #include "infinint.hpp"
53 #include "gf_mode.hpp"
54 
55 #include <string>
56 
57 namespace libdar
58 {
59 
62 
64 
77  {
78  public :
80  generic_file(gf_mode m) { rw = m; terminated = no_read_ahead = false; enable_crc(false); checksum = nullptr; };
81 
83  generic_file(const generic_file &ref) { copy_from(ref); };
84 
86  generic_file(generic_file && ref) noexcept { nullifyptr(); move_from(std::move(ref)); };
87 
89  generic_file & operator = (const generic_file & ref) { destroy(); copy_from(ref); return *this; };
90 
92  generic_file & operator = (generic_file && ref) noexcept { move_from(std::move(ref)); return *this; };
93 
95 
97  ~generic_file() noexcept(false) { destroy(); };
98 
100  void terminate();
101 
105  bool operator == (generic_file & ref);
106  bool operator != (generic_file & ref) { return ! (*this == ref); };
107 
109  gf_mode get_mode() const { return rw; };
110 
115  void read_ahead(const infinint & amount);
116 
123  void ignore_read_ahead(bool mode) { no_read_ahead = mode; };
124 
126  virtual U_I read(char *a, U_I size) override;
127 
129  virtual void write(const char *a, U_I size) override;
130 
132 
134  void write(const std::string & arg);
135 
137  S_I read_back(char &a);
138 
140  S_I read_forward(char &a) { if(terminated) throw SRC_BUG; return read(&a, 1); };
141 
142 
143  enum skippability { skip_backward, skip_forward };
144 
146 
151  virtual bool skippable(skippability direction, const infinint & amount) = 0;
152 
154 
158  virtual bool skip(const infinint & pos) = 0;
159 
161  virtual bool skip_to_eof() = 0;
162 
164  virtual bool skip_relative(S_I x) = 0;
165 
167  virtual infinint get_position() const = 0;
168 
170  virtual void copy_to(generic_file &ref);
171 
173 
178  virtual void copy_to(generic_file &ref, const infinint & crc_size, crc * & value);
179 
181  U_32 copy_to(generic_file &ref, U_32 size); // returns the number of byte effectively copied
182 
184  infinint copy_to(generic_file &ref, infinint size); // returns the number of byte effectively copied
185 
187 
198  bool diff(generic_file & f,
199  const infinint & me_read_ahead,
200  const infinint & you_read_ahead,
201  const infinint & crc_size,
202  crc * & value);
203 
216  bool diff(generic_file & f,
217  const infinint & me_read_ahead,
218  const infinint & you_read_ahead,
219  const infinint & crc_size,
220  crc * & value,
221  infinint & err_offset);
222 
224 
226  void reset_crc(const infinint & width);
227 
229  bool crc_status() const { return active_read == &generic_file::read_crc; };
230 
232 
236  crc *get_crc();
237 
239  void sync_write();
240 
242  void flush_read();
243 
244 
245  protected :
246  void set_mode(gf_mode x) { rw = x; };
247 
249 
253  virtual void inherited_read_ahead(const infinint & amount) = 0;
254 
255 
257 
266  virtual U_I inherited_read(char *a, U_I size) = 0;
267 
269 
273  virtual void inherited_write(const char *a, U_I size) = 0;
274 
275 
277 
281  virtual void inherited_sync_write() = 0;
282 
284 
289  virtual void inherited_flush_read() = 0;
290 
292 
295  virtual void inherited_terminate() = 0;
296 
297 
300  bool is_terminated() const { return terminated; };
301 
302  private :
303  gf_mode rw;
304  crc *checksum;
305  bool terminated;
306  bool no_read_ahead;
307  U_I (generic_file::* active_read)(char *a, U_I size);
308  void (generic_file::* active_write)(const char *a, U_I size);
309 
310  void enable_crc(bool mode);
311 
312  U_I read_crc(char *a, U_I size);
313  void write_crc(const char *a, U_I size);
314  void destroy();
315  void nullifyptr() noexcept { checksum = nullptr; };
316  void copy_from(const generic_file & ref);
317  void move_from(generic_file && ref) noexcept;
318  };
319 
321 
322 } // end of namespace
323 
324 #endif
generic modes to open file
virtual void inherited_write(const char *a, U_I size)=0
implementation of the write() operation
are defined here basic integer types that tend to be portable
virtual void write(const char *a, U_I size) override
write data to the generic_file inherited from proto_generic_file
class crc definition, used to handle Cyclic Redundancy Checks
bool diff(generic_file &f, const infinint &me_read_ahead, const infinint &you_read_ahead, const infinint &crc_size, crc *&value)
compares the contents with the object in argument
ancestor class of generic_file
virtual infinint get_position() const =0
get the current read/write position
generic_file(const generic_file &ref)
copy constructor
bool operator==(generic_file &ref)
void flush_read()
be ready to read at current position, reseting all pending data for reading, cached and in compressio...
virtual bool skip(const infinint &pos)=0
skip at the absolute position
precursor class of generic_file used to avoid cyclic dependencies with storage and infinint ...
void ignore_read_ahead(bool mode)
virtual void inherited_flush_read()=0
reset internal engine, flush caches in order to read the data at current position ...
gf_mode
generic_file openning modes
Definition: gf_mode.hpp:43
S_I read_back(char &a)
skip back one char, read on char and skip back one char
~generic_file() noexcept(false)
virtual destructor,
virtual bool skip_relative(S_I x)=0
skip relatively to the current position
crc * get_crc()
get CRC of the transfered date since last reset
virtual void inherited_sync_write()=0
write down any pending data
bool crc_status() const
to known whether CRC calculation is activated or not
generic_file & operator=(const generic_file &ref)
assignment operator
void read_ahead(const infinint &amount)
generic_file(generic_file &&ref) noexcept
move constructor
generic_file(gf_mode m)
main constructor
virtual U_I read(char *a, U_I size) override
read data from the generic_file inherited from proto_generic_file
bool is_terminated() const
virtual void inherited_terminate()=0
destructor-like call, except that it is allowed to throw exceptions
switch module to limitint (32 ou 64 bits integers) or infinint
virtual U_I inherited_read(char *a, U_I size)=0
implementation of read() operation
virtual void inherited_read_ahead(const infinint &amount)=0
tells the object that several calls to read() will follow to probably obtain at least the given amoun...
this is the interface class from which all other data transfer classes inherit
virtual void copy_to(generic_file &ref)
copy all data from current position to the object in argument
void sync_write()
write any pending data
void reset_crc(const infinint &width)
reset CRC on read or writen data
the arbitrary large positive integer class
gf_mode get_mode() const
retreive the openning mode for this object
pure virtual class defining interface of a CRC object
Definition: crc.hpp:46
virtual bool skip_to_eof()=0
skip to the end of file
void terminate()
destructor-like call, except that it is allowed to throw exceptions
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:46
virtual bool skippable(skippability direction, const infinint &amount)=0
whether the implementation is able to skip
S_I read_forward(char &a)
read one char