Disk ARchive  2.4.5
 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 // $Id: generic_file.hpp,v 1.50.2.3 2012/02/19 22:15:05 edrusb Rel $
22 //
23 /*********************************************************************/
24 
39 
40 
42 // IMPORTANT : THIS FILE MUST ALWAYS BE INCLUDE AFTER infinint.hpp //
43 // (and infinint.hpp must be included too, always) //
45 #include "infinint.hpp"
47 
48 
49 
50 #ifndef GENERIC_FILE_HPP
51 #define GENERIC_FILE_HPP
52 
53 
54 #include "../my_config.h"
55 
56 extern "C"
57 {
58 #if HAVE_UNISTD_H
59 #include <unistd.h>
60 #endif
61 } // end extern "C"
62 
63 #include "path.hpp"
64 #include "integers.hpp"
65 #include "thread_cancellation.hpp"
66 #include "label.hpp"
67 #include "crc.hpp"
68 #include "user_interaction.hpp"
69 #include "mem_ui.hpp"
70 
71 #include <string>
72 
73 namespace libdar
74 {
75 
78 
80  enum gf_mode
81  {
85  };
86 
87 
88  extern gf_mode generic_file_get_mode(S_I fd);
89  extern const char * generic_file_get_name(gf_mode mode);
90 
92 
105  {
106  public :
108  generic_file(gf_mode m) { rw = m; terminated = false; enable_crc(false); checksum = NULL; };
109 
111  generic_file(const generic_file &ref) { copy_from(ref); };
112 
113 
115 
117  void terminate() const;
118 
119  virtual ~generic_file() { destroy(); };
120 
122  const generic_file & operator = (const generic_file & ref) { destroy(); copy_from(ref); return *this; };
123 
125  gf_mode get_mode() const { return rw; };
126 
128 
134  U_I read(char *a, U_I size);
135 
137 
139  void write(const char *a, U_I size);
140 
142 
144  void write(const std::string & arg);
145 
147  S_I read_back(char &a);
148 
150  S_I read_forward(char &a) { if(terminated) throw SRC_BUG; return read(&a, 1); };
151 
153 
157  virtual bool skip(const infinint & pos) = 0;
158 
160  virtual bool skip_to_eof() = 0;
161 
163  virtual bool skip_relative(S_I x) = 0;
164 
166  virtual infinint get_position() = 0;
167 
169  virtual void copy_to(generic_file &ref);
170 
172 
177  virtual void copy_to(generic_file &ref, const infinint & crc_size, crc * & value);
178 
180  U_32 copy_to(generic_file &ref, U_32 size); // returns the number of byte effectively copied
181 
183  infinint copy_to(generic_file &ref, infinint size); // returns the number of byte effectively copied
184 
186 
194  bool diff(generic_file & f, const infinint & crc_size, crc * & value);
195 
197 
199  void reset_crc(const infinint & width);
200 
202  bool crc_status() const { return active_read == &generic_file::read_crc; };
203 
205 
209  crc *get_crc();
210 
212  void sync_write();
213 
214  protected :
215  void set_mode(gf_mode x) { rw = x; };
216 
218 
227  virtual U_I inherited_read(char *a, U_I size) = 0;
228 
230 
234  virtual void inherited_write(const char *a, U_I size) = 0;
235 
236 
238 
241  virtual void inherited_sync_write() = 0;
242 
243 
245 
248  virtual void inherited_terminate() = 0;
249 
250 
253  bool is_terminated() const { return terminated; };
254 
255  private :
256  gf_mode rw;
257  crc *checksum;
258  bool terminated;
259  U_I (generic_file::* active_read)(char *a, U_I size);
260  void (generic_file::* active_write)(const char *a, U_I size);
261 
262  void enable_crc(bool mode);
263 
264  U_I read_crc(char *a, U_I size);
265  void write_crc(const char *a, U_I size);
266  void destroy();
267  void copy_from(const generic_file & ref);
268  };
269 
270 #define CONTEXT_INIT "init"
271 #define CONTEXT_OP "operation"
272 #define CONTEXT_LAST_SLICE "last_slice"
273 
275 
290 
291  class label;
292 
293  class contextual
294  {
295  public :
296  contextual() { status = ""; };
297  virtual ~contextual() {};
298 
299  virtual void set_info_status(const std::string & s) { status = s; };
300  virtual std::string get_info_status() const { return status; };
301  virtual bool is_an_old_start_end_archive() const = 0;
302 
303  virtual const label & get_data_name() const = 0;
304 
305  private:
306  std::string status;
307  };
308 
310 
311 } // end of namespace
312 
313 #endif