# 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 : dar.linux@free.fr
    00020 /*********************************************************************/
    00021 // $Id: generic_file.hpp,v 1.25.2.3 2009/04/07 08:45:29 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 "user_interaction.hpp"
    00066 #include "thread_cancellation.hpp"
    00067
    00068 #include <string>
    00069
    00070 namespace libdar
    00071 {
    00072
    00075
    00076     const int CRC_SIZE = 2;
    00077     typedef char crc[CRC_SIZE];
    00078     extern void clear(crc & value);
    00079     extern void copy_crc(crc & dst, const crc & src);
    00080     extern bool same_crc(const crc &a, const crc &b);
    00081     extern std::string crc2str(const crc &a);
    00082
    00084     enum gf_mode
    00085     {
    00086         gf_read_only,
    00087         gf_write_only,
    00088         gf_read_write
    00089     };
    00090
    00091
    00092     extern gf_mode generic_file_get_mode(S_I fd);
    00093     extern const char * generic_file_get_name(gf_mode mode);
    00094
    00096
    00108     class generic_file
    00109     {
    00110     public :
    00112         generic_file(user_interaction & dialog, gf_mode m) { rw = m; clear(value); crc_offset = 0; enable_crc(false); gf_ui = dialog.clone(); };
    00114         generic_file(const generic_file &ref) { copy_from(ref); };
    00116         generic_file & operator = (const generic_file & ref) { detruire(); copy_from(ref); return *this; };
    00118         virtual ~generic_file() { detruire(); };
    00119
    00121         gf_mode get_mode() const { return rw; };
    00122
    00124         S_I read(char *a, size_t size);
    00126         S_I write(const char *a, size_t size);
    00128         S_I write(const std::string & arg);
    00130         S_I read_back(char &a);
    00132         S_I read_forward(char &a) { return read(&a, 1); };
    00134         virtual bool skip(const infinint & pos) = 0;
    00136         virtual bool skip_to_eof() = 0;
    00138         virtual bool skip_relative(S_I x) = 0;
    00140         virtual infinint get_position() = 0;
    00141
    00143         void copy_to(generic_file &ref);
    00145         void copy_to(generic_file &ref, crc & value);
    00147         U_32 copy_to(generic_file &ref, U_32 size); // returns the number of byte effectively copied
    00149         infinint copy_to(generic_file &ref, infinint size); // returns the number of byte effectively copied
    00151         bool diff(generic_file & f); // return true if arg differs from "this"
    00152
    00154         void reset_crc();
    00156         void get_crc(crc & val) { enable_crc(false); copy_crc(val, value); };
    00157
    00159         user_interaction & get_gf_ui() const { return *gf_ui; };
    00160
    00161     protected :
    00162         void set_mode(gf_mode x) { rw = x; };
    00163         virtual S_I inherited_read(char *a, size_t size) = 0;
    00164             // must provide as much byte as requested up to end of file
    00165             // stay blocked if not enough available
    00166             // returning zero or less than requested means end of file
    00167         virtual S_I inherited_write(const char *a, size_t size) = 0;
    00168             // must write all data or block or throw exceptions
    00169             // thus always returns the second argument
    00170
    00171     private :
    00172         gf_mode rw;
    00173         crc value;
    00174         S_I crc_offset;
    00175         user_interaction *gf_ui;
    00176         S_I (generic_file::* active_read)(char *a, size_t size);
    00177         S_I (generic_file::* active_write)(const char *a, size_t size);
    00178
    00179         void enable_crc(bool mode);
    00180         void compute_crc(const char *a, S_I size);
    00181         S_I read_crc(char *a, size_t size);
    00182         S_I write_crc(const char *a, size_t size);
    00183
    00184         void detruire() { if(gf_ui != NULL) delete gf_ui; };
    00185         void copy_from(const generic_file & ref);
    00186     };
    00187
    00189     class fichier : public generic_file, public thread_cancellation
    00190     {
    00191     public :
    00192         fichier(user_interaction & dialog, S_I fd);
    00193         fichier(user_interaction & dialog, const char *name, gf_mode m);
    00194         fichier(user_interaction & dialog, const path & chemin, gf_mode m);
    00195         ~fichier() { close(filedesc); };
    00196
    00197         infinint get_size() const;
    00198
    00199             // herite de generic_file
    00200         bool skip(const infinint & pos);
    00201         bool skip_to_eof();
    00202         bool skip_relative(S_I x);
    00203         infinint get_position();
    00204
    00205     protected :
    00206         S_I inherited_read(char *a, size_t size);
    00207         S_I inherited_write(const char *a, size_t size);
    00208
    00209     private :
    00210         S_I filedesc;
    00211
    00212         void open(const char *name, gf_mode m);
    00213     };
    00214
    00215 #define CONTEXT_INIT "init"
    00216 #define CONTEXT_OP   "operation"
    00217 #define CONTEXT_LAST_SLICE  "last_slice"
    00218
    00220
    00229     class contextual : public generic_file
    00230     {
    00231     public :
    00232         contextual(user_interaction & dialog, gf_mode m) : generic_file(dialog, m) {};
    00233
    00234         virtual void set_info_status(const std::string & s) = 0;
    00235         virtual std::string get_info_status() const = 0;
    00236     };
    00237
    00239
    00240 } // end of namespace
    00241
    00242 #endif