00001 // AtomicFile.H 00002 00003 // Copyright (C) 2001, Compaq Computer Corporation 00004 00005 // This file is part of Vesta. 00006 00007 // Vesta is free software; you can redistribute it and/or 00008 // modify it under the terms of the GNU Lesser General Public 00009 // License as published by the Free Software Foundation; either 00010 // version 2.1 of the License, or (at your option) any later version. 00011 00012 // Vesta is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 // Lesser General Public License for more details. 00016 00017 // You should have received a copy of the GNU Lesser General Public 00018 // License along with Vesta; if not, write to the Free Software 00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 00021 // 00022 // Atomically create or replace files. 00023 // 00024 #ifndef _ATOMIC_FILE_H 00025 #define _ATOMIC_FILE_H 00026 00027 #include "FdStream.H" 00028 #include "FS.H" 00029 00030 // Open a new file under a temporary name. Rename it to its real name 00031 // when it is closed. The destructor closes the temporary file, but does 00032 // not rename the temporary file to the real name. To change the name, 00033 // you must invoke the "close" method explicitly. 00034 // 00035 // Temporary names are chosen in such a way that the temporaries can be found 00036 // and deleted by "AtomicFile::cleanup" if the program crashes with files 00037 // still open. 00038 // 00039 // The last component of the filename may not contain reserved_char, 00040 // nor may anything else in its directory. 00041 00042 class AtomicFile : public FS::OFdStream { 00043 char *tempname, *realname; 00044 public: 00045 void open(const char* name, std::ios::openmode mode, int prot = 0644) throw(); 00046 void close() throw(); 00047 ~AtomicFile() throw(); 00048 00049 // The following are in the class purely for name scoping purposes 00050 00051 // Remove old temporary files in the specified directory. Any name 00052 // containing reserved_char is assumed to be a temporary. Subdirectories 00053 // are not traversed. 00054 // 00055 static void cleanup(const char* dirname =".") throw(); 00056 static const char reserved_char; // ';' 00057 }; 00058 00059 namespace FS 00060 { 00061 // Same as the other variants in FS.[HC] 00062 void Close(AtomicFile &vaf) throw (FS::Failure); 00063 } 00064 00065 #endif // _ATOMIC_FILE_H