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