Disk ARchive 2.4.2
|
00001 /*********************************************************************/ 00002 // dar - disk archive - a backup/restoration program 00003 // Copyright (C) 2002-2052 Denis Corbin 00004 // 00005 // This program is free software; you can redistribute it and/or 00006 // modify it under the terms of the GNU General Public License 00007 // as published by the Free Software Foundation; either version 2 00008 // of the License, or (at your option) any later version. 00009 // 00010 // This program is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with this program; if not, write to the Free Software 00017 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 // 00019 // to contact the author : http://dar.linux.free.fr/email.html 00020 /*********************************************************************/ 00021 // $Id: generic_file.hpp,v 1.50 2011/04/17 16:36:36 edrusb Rel $ 00022 // 00023 /*********************************************************************/ 00024 00039 00040 00042 // IMPORTANT : THIS FILE MUST ALWAYS BE INCLUDE AFTER infinint.hpp // 00043 // (and infinint.hpp must be included too, always) // 00045 #include "infinint.hpp" 00047 00048 00049 00050 #ifndef GENERIC_FILE_HPP 00051 #define GENERIC_FILE_HPP 00052 00053 00054 #include "../my_config.h" 00055 00056 extern "C" 00057 { 00058 #if HAVE_UNISTD_H 00059 #include <unistd.h> 00060 #endif 00061 } // end extern "C" 00062 00063 #include "path.hpp" 00064 #include "integers.hpp" 00065 #include "thread_cancellation.hpp" 00066 #include "label.hpp" 00067 #include "crc.hpp" 00068 #include "user_interaction.hpp" 00069 #include "mem_ui.hpp" 00070 00071 #include <string> 00072 00073 namespace libdar 00074 { 00075 00078 00080 enum gf_mode 00081 { 00082 gf_read_only, 00083 gf_write_only, 00084 gf_read_write 00085 }; 00086 00087 00088 extern gf_mode generic_file_get_mode(S_I fd); 00089 extern const char * generic_file_get_name(gf_mode mode); 00090 00092 00104 class generic_file 00105 { 00106 public : 00108 generic_file(gf_mode m) { rw = m; terminated = false; enable_crc(false); }; 00109 00111 generic_file(const generic_file &ref) { copy_from(ref); }; 00112 00113 00115 00117 void terminate() const; 00118 00119 virtual ~generic_file() {}; 00120 00122 const generic_file & operator = (const generic_file & ref) { copy_from(ref); return *this; }; 00123 00125 gf_mode get_mode() const { return rw; }; 00126 00128 00134 U_I read(char *a, U_I size); 00135 00137 00139 void write(const char *a, U_I size); 00140 00142 00144 void write(const std::string & arg); 00145 00147 S_I read_back(char &a); 00148 00150 S_I read_forward(char &a) { if(terminated) throw SRC_BUG; return read(&a, 1); }; 00151 00153 00157 virtual bool skip(const infinint & pos) = 0; 00158 00160 virtual bool skip_to_eof() = 0; 00161 00163 virtual bool skip_relative(S_I x) = 0; 00164 00166 virtual infinint get_position() = 0; 00167 00169 virtual void copy_to(generic_file &ref); 00170 00172 virtual void copy_to(generic_file &ref, crc & value); 00173 00175 U_32 copy_to(generic_file &ref, U_32 size); // returns the number of byte effectively copied 00176 00178 infinint copy_to(generic_file &ref, infinint size); // returns the number of byte effectively copied 00179 00181 00187 bool diff(generic_file & f, crc & value); 00188 00190 00192 void reset_crc(const infinint & width); 00193 00195 bool crc_status() const { return active_read == &generic_file::read_crc; }; 00196 00198 00201 void get_crc(crc & val) { enable_crc(false); val = checksum; }; 00202 00204 void sync_write(); 00205 00206 protected : 00207 void set_mode(gf_mode x) { rw = x; }; 00208 00210 00219 virtual U_I inherited_read(char *a, U_I size) = 0; 00220 00222 00226 virtual void inherited_write(const char *a, U_I size) = 0; 00227 00228 00230 00233 virtual void inherited_sync_write() = 0; 00234 00235 00237 00240 virtual void inherited_terminate() = 0; 00241 00242 00245 bool is_terminated() const { return terminated; }; 00246 00247 private : 00248 gf_mode rw; 00249 crc checksum; 00250 bool terminated; 00251 U_I (generic_file::* active_read)(char *a, U_I size); 00252 void (generic_file::* active_write)(const char *a, U_I size); 00253 00254 void enable_crc(bool mode); 00255 00256 U_I read_crc(char *a, U_I size); 00257 void write_crc(const char *a, U_I size); 00258 00259 void copy_from(const generic_file & ref); 00260 }; 00261 00262 #define CONTEXT_INIT "init" 00263 #define CONTEXT_OP "operation" 00264 #define CONTEXT_LAST_SLICE "last_slice" 00265 00267 00282 00283 class label; 00284 00285 class contextual 00286 { 00287 public : 00288 contextual() { status = ""; }; 00289 virtual ~contextual() {}; 00290 00291 virtual void set_info_status(const std::string & s) { status = s; }; 00292 virtual std::string get_info_status() const { return status; }; 00293 virtual bool is_an_old_start_end_archive() const = 0; 00294 00295 virtual const label & get_data_name() const = 0; 00296 private: 00297 std::string status; 00298 }; 00299 00301 00302 } // end of namespace 00303 00304 #endif