Merge various leak fixes
[amule.git] / src / CFile.cpp
blob33ac5b9e6b53b9a11e5dd61cfe4b448a9ff846c1
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 1998-2011 Vadim Zeitlin ( zeitlin@dptmaths.ens-cachan.fr )
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.
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.
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.
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
27 #include "CFile.h" // Interface declarations.
28 #include "Logger.h" // Needed for AddDebugLogLineC
29 #include <common/Path.h> // Needed for CPath
32 #ifdef HAVE_CONFIG_H
33 #include "config.h" // Needed for HAVE_SYS_PARAM_H
34 #endif
37 #ifdef HAVE_SYS_PARAM_H
38 #include <sys/param.h>
39 #endif
41 // standard
42 #if defined(__WINDOWS__) && !defined(__GNUWIN32__) && !defined(__WXWINE__) && !defined(__WXMICROWIN__)
43 # include <io.h>
44 # ifndef __SALFORDC__
45 # define WIN32_LEAN_AND_MEAN
46 # define NOSERVICE
47 # define NOIME
48 # define NOATOM
49 # define NOGDI
50 # define NOGDICAPMASKS
51 # define NOMETAFILE
52 #ifndef NOMINMAX
53 #define NOMINMAX
54 #endif
55 # define NOMSG
56 # define NOOPENFILE
57 # define NORASTEROPS
58 # define NOSCROLL
59 # define NOSOUND
60 # define NOSYSMETRICS
61 # define NOTEXTMETRIC
62 # define NOWH
63 # define NOCOMM
64 # define NOKANJI
65 # define NOCRYPT
66 # define NOMCX
67 # endif
68 #elif (defined(__UNIX__) || defined(__GNUWIN32__))
69 # ifdef __GNUWIN32__
70 # include <windows.h>
71 # endif
72 #elif (defined(__WXPM__))
73 # include <io.h>
74 #elif (defined(__WXSTUBS__))
75 // Have to ifdef this for different environments
76 # include <io.h>
77 #elif (defined(__WXMAC__))
78 #if __MSL__ < 0x6000
79 int access( const char *path, int mode ) { return 0 ; }
80 #else
81 int _access( const char *path, int mode ) { return 0 ; }
82 #endif
83 char* mktemp( char * path ) { return path ;}
84 # include <stat.h>
85 #else
86 # error "Please specify the header with file functions declarations."
87 #endif //Win/UNIX
89 // there is no distinction between text and binary files under Unix, so define
90 // O_BINARY as 0 if the system headers don't do it already
91 #if defined(__UNIX__) && !defined(O_BINARY)
92 # define O_BINARY (0)
93 #endif //__UNIX__
95 #if defined(__WINDOWS__) && !wxCHECK_VERSION(3, 1, 0)
96 #include <wx/msw/mslu.h>
97 #endif
100 // The following defines handle different names across platforms,
101 // and ensures that we use 64b IO on windows (only 32b by default).
102 #ifdef __WINDOWS__
103 #define FLUSH_FD(x) _commit(x)
104 #define SEEK_FD(x, y, z) _lseeki64(x, y, z)
105 #define TELL_FD(x) _telli64(x)
107 #if (__MSVCRT_VERSION__ < 0x0601)
108 //#warning MSCVRT-Version smaller than 6.01
109 #define STAT_FD(x, y) _fstati64(x, y)
110 #define STAT_STRUCT struct _stati64
111 #else
112 #define STAT_FD(x, y) _fstat64(x, y)
113 #define STAT_STRUCT struct __stat64
114 #endif
115 #else
117 // We don't need to sync all meta-data, just the contents,
118 // so use fdatasync when possible (see man fdatasync).
119 #if defined(_POSIX_SYNCHRONIZED_IO) && (_POSIX_SYNCHRONIZED_IO > 0)
120 #define FLUSH_FD(x) fdatasync(x)
121 #else
122 #define FLUSH_FD(x) fsync(x)
123 #endif
125 #define SEEK_FD(x, y, z) lseek(x, y, z)
126 #define TELL_FD(x) wxTell(x)
127 #define STAT_FD(x, y) fstat(x, y)
128 #define STAT_STRUCT struct stat
129 #endif
132 // This function is used to check if a syscall failed, in that case
133 // log an appropriate message containing the errno string.
134 inline void syscall_check(
135 bool check,
136 const CPath& filePath,
137 const wxString& what)
139 if (!check) {
140 AddDebugLogLineC(logCFile,
141 CFormat(wxT("Error when %s (%s): %s"))
142 % what % filePath % wxSysErrorMsg());
147 CSeekFailureException::CSeekFailureException(const wxString& desc)
148 : CIOFailureException(wxT("SeekFailure"), desc)
152 CFile::CFile()
153 : m_fd(fd_invalid), m_safeWrite(false)
157 CFile::CFile(const CPath& fileName, OpenMode mode)
158 : m_fd(fd_invalid)
160 Open(fileName, mode);
164 CFile::CFile(const wxString& fileName, OpenMode mode)
165 : m_fd(fd_invalid)
167 Open(fileName, mode);
171 CFile::~CFile()
173 if (IsOpened()) {
174 // If the writing gets aborted, dtor is still called.
175 // In this case do NOT replace the original file with the
176 // probably broken new one!
177 m_safeWrite = false;
178 Close();
183 int CFile::fd() const
185 return m_fd;
189 bool CFile::IsOpened() const
191 return m_fd != fd_invalid;
195 const CPath& CFile::GetFilePath() const
197 return m_filePath;
201 bool CFile::Create(const CPath& path, bool overwrite, int accessMode)
203 if (!overwrite && path.FileExists()) {
204 return false;
207 return Open(path, write, accessMode);
210 bool CFile::Create(const wxString& path, bool overwrite, int accessMode)
212 return Create(CPath(path), overwrite, accessMode);
216 bool CFile::Open(const wxString& fileName, OpenMode mode, int accessMode)
218 MULE_VALIDATE_PARAMS(fileName.Length(), wxT("CFile: Cannot open, empty path."));
220 return Open(CPath(fileName), mode, accessMode);
224 bool CFile::Open(const CPath& fileName, OpenMode mode, int accessMode)
226 MULE_VALIDATE_PARAMS(fileName.IsOk(), wxT("CFile: Cannot open, empty path."));
228 if (IsOpened()) {
229 Close();
232 m_safeWrite = false;
233 m_filePath = fileName;
235 #ifdef __linux__
236 int flags = O_BINARY | O_LARGEFILE;
237 #else
238 int flags = O_BINARY;
239 #endif
240 switch ( mode ) {
241 case read:
242 flags |= O_RDONLY;
243 break;
245 case write_append:
246 if (fileName.FileExists())
248 flags |= O_WRONLY | O_APPEND;
249 break;
251 //else: fall through as write_append is the same as write if the
252 // file doesn't exist
254 /* fall through */
255 case write:
256 flags |= O_WRONLY | O_CREAT | O_TRUNC;
257 break;
259 case write_safe:
260 flags |= O_WRONLY | O_CREAT | O_TRUNC;
261 m_filePath = m_filePath.AppendExt(wxT(".new"));
262 m_safeWrite = true;
263 break;
265 case write_excl:
266 flags |= O_WRONLY | O_CREAT | O_EXCL;
267 break;
269 case read_write:
270 flags |= O_RDWR;
271 break;
274 // Windows needs wide character file names
275 #ifdef __WINDOWS__
276 m_fd = _wopen(m_filePath.GetRaw().c_str(), flags, accessMode);
277 #else
278 Unicode2CharBuf tmpFileName = filename2char(m_filePath.GetRaw());
279 wxASSERT_MSG(tmpFileName, wxT("Convertion failed in CFile::Open"));
280 m_fd = open(tmpFileName, flags, accessMode);
281 #endif
282 syscall_check(m_fd != fd_invalid, m_filePath, wxT("opening file"));
284 return IsOpened();
288 void CFile::Reopen(OpenMode mode)
290 if (!Open(m_filePath, mode)) {
291 throw CIOFailureException(wxString(wxT("Error reopening file")));
296 bool CFile::Close()
298 MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot close closed file."));
300 bool closed = (close(m_fd) != -1);
301 syscall_check(closed, m_filePath, wxT("closing file"));
303 m_fd = fd_invalid;
305 if (m_safeWrite) {
306 CPath filePathTemp(m_filePath);
307 m_filePath = m_filePath.RemoveExt(); // restore m_filePath for Reopen()
308 if (closed) {
309 closed = CPath::RenameFile(filePathTemp, m_filePath, true);
313 return closed;
317 bool CFile::Flush()
319 MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot flush closed file."));
321 bool flushed = (FLUSH_FD(m_fd) != -1);
322 syscall_check(flushed, m_filePath, wxT("flushing file"));
324 return flushed;
328 sint64 CFile::doRead(void* buffer, size_t count) const
330 MULE_VALIDATE_PARAMS(buffer, wxT("CFile: Invalid buffer in read operation."));
331 MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot read from closed file."));
333 size_t totalRead = 0;
334 while (totalRead < count) {
335 int current = ::read(m_fd, (char*)buffer + totalRead, count - totalRead);
337 if (current == -1) {
338 // Read error, nothing we can do other than abort.
339 throw CIOFailureException(wxString(wxT("Error reading from file: ")) + wxSysErrorMsg());
340 } else if ((totalRead + current < count) && Eof()) {
341 // We may fail to read the specified count in a couple
342 // of situations: EOF and interrupts. The check for EOF
343 // is needed to avoid inf. loops.
344 break;
347 totalRead += current;
350 return totalRead;
354 sint64 CFile::doWrite(const void* buffer, size_t nCount)
356 MULE_VALIDATE_PARAMS(buffer, wxT("CFile: Invalid buffer in write operation."));
357 MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot write to closed file."));
359 sint64 result = ::write(m_fd, buffer, nCount);
361 if (result != (sint64)nCount) {
362 throw CIOFailureException(wxString(wxT("Error writing to file: ")) + wxSysErrorMsg());
365 return result;
369 sint64 CFile::doSeek(sint64 offset) const
371 MULE_VALIDATE_STATE(IsOpened(), wxT("Cannot seek on closed file."));
372 MULE_VALIDATE_PARAMS(offset >= 0, wxT("Invalid position, must be positive."));
374 sint64 result = SEEK_FD(m_fd, offset, SEEK_SET);
376 if (result == offset) {
377 return result;
378 } else if (result == wxInvalidOffset) {
379 throw CSeekFailureException(wxString(wxT("Seeking failed: ")) + wxSysErrorMsg());
380 } else {
381 throw CSeekFailureException(wxT("Seeking returned incorrect position"));
386 uint64 CFile::GetPosition() const
388 MULE_VALIDATE_STATE(IsOpened(), wxT("Cannot get position in closed file."));
390 sint64 pos = TELL_FD(m_fd);
391 if (pos == wxInvalidOffset) {
392 throw CSeekFailureException(wxString(wxT("Failed to retrieve position in file: ")) + wxSysErrorMsg());
395 return pos;
399 uint64 CFile::GetLength() const
401 MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot get length of closed file."));
403 STAT_STRUCT buf;
404 if (STAT_FD(m_fd, &buf) == -1) {
405 throw CIOFailureException(wxString(wxT("Failed to retrieve length of file: ")) + wxSysErrorMsg());
408 return buf.st_size;
412 uint64 CFile::GetAvailable() const
414 const uint64 length = GetLength();
415 const uint64 position = GetPosition();
417 // Safely handle seeking past EOF
418 if (position < length) {
419 return length - position;
422 // File is at or after EOF
423 return 0;
427 bool CFile::SetLength(uint64 new_len)
429 MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot set length when no file is open."));
431 #ifdef __WINDOWS__
432 #ifdef _MSC_VER
433 // MSVC has a 64bit version
434 bool result = _chsize_s(m_fd, new_len) == 0;
435 #else
436 // MingW has an old runtime without it
437 bool result = chsize(m_fd, new_len) == 0;
438 #endif
439 #else
440 bool result = ftruncate(m_fd, new_len) != -1;
441 #endif
443 syscall_check(result, m_filePath, wxT("truncating file"));
445 return result;
447 // File_checked_for_headers