Disk ARchive
2.4.5
|
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$ 00022 // 00023 /*********************************************************************/ 00024 00025 #ifndef GENERIC_FILE_HPP 00026 #define GENERIC_FILE_HPP 00027 00028 #pragma interface 00029 00030 #include <unistd.h> 00031 #include "infinint.hpp" 00032 #include "path.hpp" 00033 #include "integers.hpp" 00034 00035 #define CRC_SIZE 2 00036 typedef char crc[CRC_SIZE]; 00037 extern void clear(crc & value); 00038 extern void copy_crc(crc & dst, const crc & src); 00039 extern bool same_crc(const crc &a, const crc &b); 00040 00041 enum gf_mode { gf_read_only, gf_write_only, gf_read_write }; 00042 00043 // note : 00044 // the read and write method are similar to the read and write system call 00045 // except that they never return negative values, but throw exception instead 00046 // returning zero means end of generic_file, and the call is blocking if 00047 // no data is available 00048 // write returns the number of bytes written, and never make partial writtingss 00049 // this it is blocked until all bytes are written or occures an exception 00050 // thus the returned value is always the value of the size argument. 00051 00052 extern gf_mode generic_file_get_mode(S_I fd); 00053 extern const string generic_file_get_name(gf_mode mode); 00054 00055 class generic_file 00056 { 00057 public : 00058 generic_file(gf_mode m) { rw = m; clear(value); crc_offset = 0; enable_crc(false); }; 00059 virtual ~generic_file() {}; 00060 00061 gf_mode get_mode() const { return rw; }; 00062 S_I read(char *a, size_t size); 00063 S_I write(char *a, size_t size); 00064 S_I read_back(char &a); 00065 virtual bool skip(const infinint & pos) = 0; 00066 virtual bool skip_to_eof() = 0; 00067 virtual bool skip_relative(S_I x) = 0; 00068 virtual infinint get_position() = 0; 00069 00070 void copy_to(generic_file &ref); 00071 void copy_to(generic_file &ref, crc & value); 00072 // generates CRC on copied data 00073 U_32 copy_to(generic_file &ref, U_32 size); // returns the number of byte effectively copied 00074 infinint copy_to(generic_file &ref, infinint size); // returns the number of byte effectively copied 00075 bool diff(generic_file & f); // return true if arg differs from "this" 00076 00077 // CRC on read or writen data 00078 void reset_crc(); 00079 void get_crc(crc & val) { enable_crc(false); copy_crc(val, value); }; 00080 00081 protected : 00082 void set_mode(gf_mode x) { rw = x; }; 00083 virtual S_I inherited_read(char *a, size_t size) = 0; 00084 // must provide as much byte as requested up to end of file 00085 // stay blocked if not enough available 00086 // returning zero or less than requested means end of file 00087 virtual S_I inherited_write(char *a, size_t size) = 0; 00088 // must write all data or block or throw exceptions 00089 // thus always returns the second argument 00090 00091 private : 00092 gf_mode rw; 00093 crc value; 00094 S_I crc_offset; 00095 S_I (generic_file::* active_read)(char *a, size_t size); 00096 S_I (generic_file::* active_write)(char *a, size_t size); 00097 00098 void enable_crc(bool mode); 00099 void compute_crc(char *a, S_I size); 00100 S_I read_crc(char *a, size_t size); 00101 S_I write_crc(char *a, size_t size); 00102 }; 00103 00104 class fichier : public generic_file 00105 { 00106 public : 00107 fichier(S_I fd); 00108 fichier(char *name, gf_mode m); 00109 fichier(const path & chemin, gf_mode m); 00110 ~fichier() { close(filedesc); }; 00111 00112 infinint get_size() const; 00113 00114 // herite de generic_file 00115 bool skip(const infinint & pos); 00116 bool skip_to_eof(); 00117 bool skip_relative(S_I x); 00118 infinint get_position(); 00119 00120 protected : 00121 S_I inherited_read(char *a, size_t size); 00122 S_I inherited_write(char *a, size_t size); 00123 00124 private : 00125 S_I filedesc; 00126 00127 void open(const char *name, gf_mode m); 00128 }; 00129 00130 #endif