1 // 2 // This file is part of the aMule Project. 3 // 4 // Copyright (c) 2008-2011 aMule Team ( admin@amule.org / http://www.amule.org ) 5 // Copyright (c) 2008-2011 Stu Redman ( sturedman@amule.org ) 6 // 7 // Any parts of this program derived from the xMule, lMule or eMule project, 8 // or contributed by third-party developers are copyrighted by their 9 // respective authors. 10 // 11 // This program is free software; you can redistribute it and/or modify 12 // it under the terms of the GNU General Public License as published by 13 // the Free Software Foundation; either version 2 of the License, or 14 // (at your option) any later version. 15 // 16 // This program is distributed in the hope that it will be useful, 17 // but WITHOUT ANY WARRANTY; without even the implied warranty of 18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 // GNU General Public License for more details. 20 // 21 // You should have received a copy of the GNU General Public License 22 // along with this program; if not, write to the Free Software 23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 24 // 25 26 #ifndef FILEAUTOCLOSE_H 27 #define FILEAUTOCLOSE_H 28 29 #include "CFile.h" // Needed for CFile 30 31 /** 32 * This class encapsulates the CFile class. CFileAutoClose()33 * 34 * It allows to close the used file handle and reopen 35 * it on usage to minimize the number of used file handles. 36 * 37 */ 38 class CFileAutoClose 39 { 40 public: 41 42 /** 43 * Creates a closed file. 44 */ 45 CFileAutoClose(); 46 47 /** 48 * Constructor, calls Open on the specified file. 49 * 50 * To check if the file was successfully opened, a 51 * call to IsOpened() is required. 52 */ 53 CFileAutoClose(const CPath& path, CFile::OpenMode mode = CFile::read); 54 55 /** 56 * Request auto closing of the file handle. 57 * 58 * @param now true: close immediately false: close when timeout has expired 59 * @return True if the file has been (or has already been) autoclosed. 60 */ 61 bool Release(bool now = false); 62 63 /** 64 * Opens a file. 65 * 66 * @param path The full or relative path to the file. 67 * @param mode The opening mode (see CFile). 68 * @return True if the file was opened, false otherwise. 69 */ 70 bool Open(const CPath& path, CFile::OpenMode mode = CFile::read); 71 72 /** 73 * Calling Create is equivilant of calling open with OpenMode 'write'. 74 * 75 * @param overwrite Specifies if the target file should be overwritten, 76 * in case that it already exists. 77 * 78 * @see CFile::Open 79 */ 80 bool Create(const CPath& path, bool overwrite = false); 81 82 /** 83 * Closes the file. 84 * 85 * Note that calling Close on an closed file 86 * is an illegal operation. 87 */ 88 bool Close(); 89 90 /** 91 * @see CSafeFileIO::GetLength 92 * 93 * Note that calling GetLength on a closed file 94 * is an illegal operation. 95 */ 96 uint64 GetLength() const; 97 98 /** 99 * Resizes the file to the specified length. 100 * 101 */ 102 bool SetLength(uint64 newLength); 103 104 /** 105 * Returns the path of the currently opened file. 106 * 107 */ 108 const CPath& GetFilePath() const; 109 110 /** 111 * Returns true if the file is opened, false otherwise. 112 */ 113 bool IsOpened() const; 114 115 /** 116 * Reads 'count' bytes into 'buffer'. 117 * 118 * @param buffer The target buffer. 119 * @param offset The seek address in the file. 120 * @param count The number of bytes to read. 121 * 122 * See CFileDataIO::Read 123 */ 124 void ReadAt(void* buffer, uint64 offset, size_t count); 125 126 /** 127 * Write 'count' bytes from 'buffer' into the file. 128 * 129 * @param buffer The source-data buffer. 130 * @param offset The seek address in the file. 131 * @param count The number of bytes to write. 132 * 133 * See CFileDataIO::Write 134 */ 135 void WriteAt(const void* buffer, uint64 offset, size_t count); 136 137 /** 138 * Returns true when the file-position is past or at the end of the file. 139 */ 140 bool Eof(); 141 142 /** 143 * Returns the file descriptior assosiated with the file. 144 * 145 * This breaks the purpose of this class of course. 146 * Therefore the AutoClose mechanism is disabled when fd() is called. 147 * It's required for FileArea's mmap stuff. 148 * Currently FileArea objects are shortlived enough for this not being 149 * a problem anyway, but that might change in the future. 150 */ 151 int fd(); 152 153 /** 154 * Reenables AutoClose disabled by fd() before. 155 */ 156 void Unlock(); 157 158 private: 159 //! A CFileAutoClose is neither copyable nor assignable. 160 //@{ 161 CFileAutoClose(const CFileAutoClose&); 162 CFileAutoClose& operator=(const CFileAutoClose&); 163 //@} 164 165 /** 166 * Check if file was autoclosed, and reopen if needed. 167 */ 168 void Reopen(); 169 170 //! The wrapped CFile. 171 CFile m_file; 172 173 //! The mode used to open it. 174 CFile::OpenMode m_mode; 175 176 //! Is it temporarily closed? 177 bool m_autoClosed; 178 179 //! Autoclosing is disabled if != 0 180 uint16 m_locked; 181 182 //! Size before it was closed. 183 uint64 m_size; 184 185 //! Last access time (s) 186 uint32 m_lastAccess; 187 }; 188 189 190 #endif // FILEAUTOCLOSE_H 191 // File_checked_for_headers 192