Disk ARchive  2.4.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
generic_file.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2052 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 
39 // IMPORTANT : THIS FILE MUST ALWAYS BE INCLUDE AFTER infinint.hpp //
40 // (and infinint.hpp must be included too, always) //
42 #include "infinint.hpp"
44 
45 
46 
47 #ifndef GENERIC_FILE_HPP
48 #define GENERIC_FILE_HPP
49 
50 
51 #include "../my_config.h"
52 
53 extern "C"
54 {
55 #if HAVE_UNISTD_H
56 #include <unistd.h>
57 #endif
58 } // end extern "C"
59 
60 #include "path.hpp"
61 #include "integers.hpp"
62 #include "thread_cancellation.hpp"
63 #include "label.hpp"
64 #include "crc.hpp"
65 #include "user_interaction.hpp"
66 #include "mem_ui.hpp"
67 
68 #include <string>
69 
70 namespace libdar
71 {
72 
75 
77  enum gf_mode
78  {
82  };
83 
84 
85  extern gf_mode generic_file_get_mode(S_I fd);
86  extern const char * generic_file_get_name(gf_mode mode);
87 
89 
102  {
103  public :
105  generic_file(gf_mode m) { rw = m; terminated = false; enable_crc(false); checksum = NULL; };
106 
108  generic_file(const generic_file &ref) { copy_from(ref); };
109 
110 
112 
114  void terminate() const;
115 
116  virtual ~generic_file() { destroy(); };
117 
119  const generic_file & operator = (const generic_file & ref) { destroy(); copy_from(ref); return *this; };
120 
122  gf_mode get_mode() const { return rw; };
123 
125 
131  U_I read(char *a, U_I size);
132 
134 
136  void write(const char *a, U_I size);
137 
139 
141  void write(const std::string & arg);
142 
144  S_I read_back(char &a);
145 
147  S_I read_forward(char &a) { if(terminated) throw SRC_BUG; return read(&a, 1); };
148 
150 
154  virtual bool skip(const infinint & pos) = 0;
155 
157  virtual bool skip_to_eof() = 0;
158 
160  virtual bool skip_relative(S_I x) = 0;
161 
163  virtual infinint get_position() = 0;
164 
166  virtual void copy_to(generic_file &ref);
167 
169 
174  virtual void copy_to(generic_file &ref, const infinint & crc_size, crc * & value);
175 
177  U_32 copy_to(generic_file &ref, U_32 size); // returns the number of byte effectively copied
178 
180  infinint copy_to(generic_file &ref, infinint size); // returns the number of byte effectively copied
181 
183 
191  bool diff(generic_file & f, const infinint & crc_size, crc * & value);
192 
202  bool diff(generic_file & f, const infinint & crc_size, crc * & value, infinint & err_offset);
203 
205 
207  void reset_crc(const infinint & width);
208 
210  bool crc_status() const { return active_read == &generic_file::read_crc; };
211 
213 
217  crc *get_crc();
218 
220  void sync_write();
221 
222  protected :
223  void set_mode(gf_mode x) { rw = x; };
224 
226 
235  virtual U_I inherited_read(char *a, U_I size) = 0;
236 
238 
242  virtual void inherited_write(const char *a, U_I size) = 0;
243 
244 
246 
249  virtual void inherited_sync_write() = 0;
250 
251 
253 
256  virtual void inherited_terminate() = 0;
257 
258 
261  bool is_terminated() const { return terminated; };
262 
263  private :
264  gf_mode rw;
265  crc *checksum;
266  bool terminated;
267  U_I (generic_file::* active_read)(char *a, U_I size);
268  void (generic_file::* active_write)(const char *a, U_I size);
269 
270  void enable_crc(bool mode);
271 
272  U_I read_crc(char *a, U_I size);
273  void write_crc(const char *a, U_I size);
274  void destroy();
275  void copy_from(const generic_file & ref);
276  };
277 
278 #define CONTEXT_INIT "init"
279 #define CONTEXT_OP "operation"
280 #define CONTEXT_LAST_SLICE "last_slice"
281 
283 
298 
299  class label;
300 
301  class contextual
302  {
303  public :
304  contextual() { status = ""; };
305  virtual ~contextual() {};
306 
307  virtual void set_info_status(const std::string & s) { status = s; };
308  virtual std::string get_info_status() const { return status; };
309  virtual bool is_an_old_start_end_archive() const = 0;
310 
311  virtual const label & get_data_name() const = 0;
312 
313  private:
314  std::string status;
315  };
316 
318 
319 } // end of namespace
320 
321 #endif