Disk ARchive  2.4.12
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
generic_file.hpp
Go to the documentation of this file.
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 
00036 
00037 
00039 // IMPORTANT : THIS FILE MUST ALWAYS BE INCLUDE AFTER infinint.hpp   //
00040 //             (and infinint.hpp must be included too, always)       //
00042 #include "infinint.hpp"
00044 
00045 
00046 
00047 #ifndef GENERIC_FILE_HPP
00048 #define GENERIC_FILE_HPP
00049 
00050 
00051 #include "../my_config.h"
00052 
00053 extern "C"
00054 {
00055 #if HAVE_UNISTD_H
00056 #include <unistd.h>
00057 #endif
00058 } // end extern "C"
00059 
00060 #include "path.hpp"
00061 #include "integers.hpp"
00062 #include "thread_cancellation.hpp"
00063 #include "label.hpp"
00064 #include "crc.hpp"
00065 #include "user_interaction.hpp"
00066 #include "mem_ui.hpp"
00067 
00068 #include <string>
00069 
00070 namespace libdar
00071 {
00072 
00075 
00077     enum gf_mode
00078     {
00079         gf_read_only,  
00080         gf_write_only, 
00081         gf_read_write  
00082     };
00083 
00084 
00085     extern gf_mode generic_file_get_mode(S_I fd);
00086     extern const char * generic_file_get_name(gf_mode mode);
00087 
00089 
00101     class generic_file
00102     {
00103     public :
00105         generic_file(gf_mode m) { rw = m; terminated = false; enable_crc(false); checksum = NULL; };
00106 
00108         generic_file(const generic_file &ref) { copy_from(ref); };
00109 
00110 
00112 
00114         void terminate() const;
00115 
00116         virtual ~generic_file() { destroy(); };
00117 
00119         const generic_file & operator = (const generic_file & ref) { destroy(); copy_from(ref); return *this; };
00120 
00122         gf_mode get_mode() const { return rw; };
00123 
00125 
00131         U_I read(char *a, U_I size);
00132 
00134 
00136         void write(const char *a, U_I size);
00137 
00139 
00141         void write(const std::string & arg);
00142 
00144         S_I read_back(char &a);
00145 
00147         S_I read_forward(char &a) { if(terminated) throw SRC_BUG; return read(&a, 1); };
00148 
00150 
00154         virtual bool skip(const infinint & pos) = 0;
00155 
00157         virtual bool skip_to_eof() = 0;
00158 
00160         virtual bool skip_relative(S_I x) = 0;
00161 
00163         virtual infinint get_position() = 0;
00164 
00166         virtual void copy_to(generic_file &ref);
00167 
00169 
00174         virtual void copy_to(generic_file &ref, const infinint & crc_size, crc * & value);
00175 
00177         U_32 copy_to(generic_file &ref, U_32 size); // returns the number of byte effectively copied
00178 
00180         infinint copy_to(generic_file &ref, infinint size); // returns the number of byte effectively copied
00181 
00183 
00191         bool diff(generic_file & f, const infinint & crc_size, crc * & value);
00192 
00202         bool diff(generic_file & f, const infinint & crc_size, crc * & value, infinint & err_offset);
00203 
00205 
00207         void reset_crc(const infinint & width);
00208 
00210         bool crc_status() const { return active_read == &generic_file::read_crc; };
00211 
00213 
00217         crc *get_crc();
00218 
00220         void sync_write();
00221 
00222     protected :
00223         void set_mode(gf_mode x) { rw = x; };
00224 
00226 
00235         virtual U_I inherited_read(char *a, U_I size) = 0;
00236 
00238 
00242         virtual void inherited_write(const char *a, U_I size) = 0;
00243 
00244 
00246 
00249         virtual void inherited_sync_write() = 0;
00250 
00251 
00253 
00256         virtual void inherited_terminate() = 0;
00257 
00258 
00261         bool is_terminated() const { return terminated; };
00262 
00263     private :
00264         gf_mode rw;
00265         crc *checksum;
00266         bool terminated;
00267         U_I (generic_file::* active_read)(char *a, U_I size);
00268         void (generic_file::* active_write)(const char *a, U_I size);
00269 
00270         void enable_crc(bool mode);
00271 
00272         U_I read_crc(char *a, U_I size);
00273         void write_crc(const char *a, U_I size);
00274         void destroy();
00275         void copy_from(const generic_file & ref);
00276     };
00277 
00278 #define CONTEXT_INIT "init"
00279 #define CONTEXT_OP   "operation"
00280 #define CONTEXT_LAST_SLICE  "last_slice"
00281 
00283 
00298 
00299     class label;
00300 
00301     class contextual
00302     {
00303     public :
00304         contextual() { status = ""; };
00305         virtual ~contextual() {};
00306 
00307         virtual void set_info_status(const std::string & s) { status = s; };
00308         virtual std::string get_info_status() const { return status; };
00309         virtual bool is_an_old_start_end_archive() const = 0;
00310 
00311         virtual const label & get_data_name() const = 0;
00312 
00313     private:
00314         std::string status;
00315     };
00316 
00318 
00319 } // end of namespace
00320 
00321 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines