00001 // -*- c++ -*- 00002 // 00003 // $Id: file.h 2361 2009-06-11 15:06:39Z rafi $ 00004 // 00005 // Copyright (C) 2008, 2009 Rafael Ostertag 00006 // 00007 // This file is part of YAPET. 00008 // 00009 // YAPET is free software: you can redistribute it and/or modify it under the 00010 // terms of the GNU General Public License as published by the Free Software 00011 // Foundation, either version 3 of the License, or (at your option) any later 00012 // version. 00013 // 00014 // YAPET is distributed in the hope that it will be useful, but WITHOUT ANY 00015 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00016 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 00017 // details. 00018 // 00019 // You should have received a copy of the GNU General Public License along with 00020 // YAPET. If not, see <http://www.gnu.org/licenses/>. 00021 // 00022 // Additional permission under GNU GPL version 3 section 7 00023 // 00024 // If you modify this program, or any covered work, by linking or combining it 00025 // with the OpenSSL project's OpenSSL library (or a modified version of that 00026 // library), containing parts covered by the terms of the OpenSSL or SSLeay 00027 // licenses, Rafael Ostertag grants you additional permission to convey the 00028 // resulting work. Corresponding Source for a non-source form of such a 00029 // combination shall include the source code for the parts of OpenSSL used as 00030 // well as that of the covered work. 00031 // 00032 00033 #ifndef _FILE_H 00034 #define _FILE_H 00035 00036 #ifdef HAVE_CONFIG_H 00037 # include <config.h> 00038 #endif 00039 00040 #ifdef HAVE_STRING 00041 # include <string> 00042 #endif 00043 00044 #ifdef HAVE_LIST 00045 # include <list> 00046 #endif 00047 00048 #include "yapetexception.h" 00049 00050 #include "bdbuffer.h" 00051 #include "structs.h" 00052 #include "key.h" 00053 #include "partdec.h" 00054 00055 namespace YAPET { 00170 class File { 00171 private: 00177 int fd; 00183 std::string filename; 00193 time_t mtime; 00194 00195 00197 void openCreate() throw(YAPETException); 00199 void openNoCreate() throw(YAPETException); 00201 time_t lastModified() const throw(YAPETException); 00203 void seekCurr(off_t offset) const throw(YAPETException); 00205 void seekAbs(off_t offset) const throw(YAPETException); 00207 void preparePWSave() throw(YAPETException); 00208 00209 protected: 00216 struct WORD { 00217 uint8_t a; 00218 uint8_t b; 00219 }; 00220 00227 struct DWORD { 00228 WORD a; 00229 WORD b; 00230 }; 00231 00238 union ENDIAN { 00244 uint32_t abcd; 00245 DWORD dword; 00246 }; 00247 00248 #ifndef WORDS_BIGENDIAN 00251 uint32_t uint32_to_disk(uint32_t i) const; 00254 uint32_t uint32_from_disk(uint32_t i) const; 00255 #else 00256 00267 inline uint32_t uint32_to_disk(uint32_t i) const { return i; } 00280 inline uint32_t uint32_from_disk(uint32_t i) const { return i; } 00281 #endif // WORDS_BIGENDIAN 00282 00285 void seekDataSection() const throw(YAPETException); 00286 00288 BDBuffer* read() const throw(YAPETException); 00290 void write(const BDBuffer& buff, 00291 bool forceappend=false, 00292 bool forcewrite=false) 00293 throw(YAPETException, YAPETRetryException); 00295 bool isempty() const throw(YAPETException); 00297 void initFile(const Key& key) throw(YAPETException); 00299 void writeHeader(const Record<FileHeader>& header, 00300 const Key& key) 00301 throw(YAPETException); 00303 void writeHeader(const BDBuffer& enc_header) throw(YAPETException); 00305 BDBuffer* readHeader() const throw(YAPETException); 00307 void validateKey(const Key& key) 00308 throw(YAPETException,YAPETInvalidPasswordException); 00309 00310 public: 00312 File(const std::string& fn, 00313 const Key& key, 00314 bool create=false) 00315 throw(YAPETException); 00316 File(const File& f) throw(YAPETException); 00317 ~File(); 00318 00320 void save(std::list<PartDec>& records) throw(YAPETException); 00322 std::list<PartDec> read(const Key& key) const throw(YAPETException); 00324 std::string getFilename() const { return filename; } 00326 void setNewKey(const Key& oldkey, const Key& newkey) 00327 throw(YAPETException); 00328 time_t getMasterPWSet(const Key& key) const 00329 throw(YAPETException,YAPETInvalidPasswordException); 00331 const File& operator=(const File& f) throw(YAPETException); 00332 }; 00333 } 00334 #endif // _FILE_H