Disk ARchive  2.5.2
Full featured and portable backup and archiving tool
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 "on_pool.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 
00085     extern const char * generic_file_get_name(gf_mode mode);
00086 
00088 
00100     class generic_file : public on_pool
00101     {
00102     public :
00104         generic_file(gf_mode m) { rw = m; terminated = no_read_ahead = false; enable_crc(false); checksum = nullptr; };
00105 
00107     generic_file(const generic_file &ref) { copy_from(ref); };
00108 
00110     virtual ~generic_file() throw(Ebug) { destroy(); };
00111 
00113     void terminate() const;
00114 
00115 
00117     const generic_file & operator = (const generic_file & ref) { destroy(); copy_from(ref); return *this; };
00118 
00120         gf_mode get_mode() const { return rw; };
00121 
00126     virtual void read_ahead(const infinint & amount);
00127 
00134     void ignore_read_ahead(bool mode) { no_read_ahead = mode; };
00135 
00137 
00143         U_I read(char *a, U_I size);
00144 
00146 
00148         void write(const char *a, U_I size);
00149 
00151 
00153         void write(const std::string & arg);
00154 
00156         S_I read_back(char &a);
00157 
00159     S_I read_forward(char &a) { if(terminated) throw SRC_BUG; return read(&a, 1); };
00160 
00161 
00162     enum skippability { skip_backward, skip_forward };
00163 
00169     virtual bool skippable(skippability direction, const infinint & amount) = 0;
00170 
00176         virtual bool skip(const infinint & pos) = 0;
00177 
00179         virtual bool skip_to_eof() = 0;
00180 
00182         virtual bool skip_relative(S_I x) = 0;
00183 
00185         virtual infinint get_position() const = 0;
00186 
00188         virtual void copy_to(generic_file &ref);
00189 
00191 
00196         virtual void copy_to(generic_file &ref, const infinint & crc_size, crc * & value);
00197 
00199         U_32 copy_to(generic_file &ref, U_32 size); // returns the number of byte effectively copied
00200 
00202         infinint copy_to(generic_file &ref, infinint size); // returns the number of byte effectively copied
00203 
00205 
00215         bool diff(generic_file & f,
00216           const infinint & me_read_ahead,
00217           const infinint & you_read_ahead,
00218           const infinint & crc_size,
00219           crc * & value);
00220 
00232     bool diff(generic_file & f,
00233           const infinint & me_read_ahead,
00234           const infinint & you_read_ahead,
00235           const infinint & crc_size,
00236           crc * & value,
00237           infinint & err_offset);
00238 
00240 
00242         void reset_crc(const infinint & width);
00243 
00245     bool crc_status() const { return active_read == &generic_file::read_crc; };
00246 
00248 
00252         crc *get_crc();
00253 
00255     void sync_write();
00256 
00258     void flush_read();
00259 
00260 
00261     protected :
00262         void set_mode(gf_mode x) { rw = x; };
00263 
00269     virtual void inherited_read_ahead(const infinint & amount) = 0;
00270 
00271 
00273 
00282         virtual U_I inherited_read(char *a, U_I size) = 0;
00283 
00285 
00289         virtual void inherited_write(const char *a, U_I size) = 0;
00290 
00291 
00293 
00297     virtual void inherited_sync_write() = 0;
00298 
00305     virtual void inherited_flush_read() = 0;
00306 
00308 
00311     virtual void inherited_terminate() = 0;
00312 
00313 
00316     bool is_terminated() const { return terminated; };
00317 
00318     private :
00319         gf_mode rw;
00320         crc *checksum;
00321     bool terminated;
00322     bool no_read_ahead;
00323         U_I (generic_file::* active_read)(char *a, U_I size);
00324         void (generic_file::* active_write)(const char *a, U_I size);
00325 
00326         void enable_crc(bool mode);
00327 
00328         U_I read_crc(char *a, U_I size);
00329         void write_crc(const char *a, U_I size);
00330     void destroy();
00331     void copy_from(const generic_file & ref);
00332     };
00333 
00334 #define CONTEXT_INIT "init"
00335 #define CONTEXT_OP   "operation"
00336 #define CONTEXT_LAST_SLICE  "last_slice"
00337 
00339 
00354 
00355     class label;
00356 
00357     class contextual
00358     {
00359     public :
00360     contextual() { status = ""; };
00361     virtual ~contextual() throw(Ebug) {};
00362 
00368     virtual void set_info_status(const std::string & s) { status = s; };
00369 
00371         virtual std::string get_info_status() const { return status; };
00372 
00374     virtual bool is_an_old_start_end_archive() const = 0;
00375 
00380     virtual const label & get_data_name() const = 0;
00381 
00382     private:
00383     std::string status;
00384     };
00385 
00387 
00388 } // end of namespace
00389 
00390 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines