2 // This file is part of the aMule Project.
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 )
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
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
30 #include "config.h" // Needed for HAVE_SYS_PARAM_H
32 #ifdef HAVE_SYS_PARAM_H
33 #include <sys/param.h>
37 #if defined(__WINDOWS__) && !defined(__GNUWIN32__) && !defined(__WXWINE__) && !defined(__WXMICROWIN__)
40 # define WIN32_LEAN_AND_MEAN
45 # define NOGDICAPMASKS
63 #elif (defined(__UNIX__) || defined(__GNUWIN32__))
67 #elif (defined(__WXPM__))
69 #elif (defined(__WXSTUBS__))
70 // Have to ifdef this for different environments
72 #elif (defined(__WXMAC__))
74 int access( const char *path
, int mode
) { return 0 ; }
76 int _access( const char *path
, int mode
) { return 0 ; }
78 char* mktemp( char * path
) { return path
;}
81 # error "Please specify the header with file functions declarations."
84 // there is no distinction between text and binary files under Unix, so define
85 // O_BINARY as 0 if the system headers don't do it already
86 #if defined(__UNIX__) && !defined(O_BINARY)
90 #if defined(__WINDOWS__) && !wxCHECK_VERSION(3, 1, 0)
91 #include <wx/msw/mslu.h>
95 // The following defines handle different names across platforms,
96 // and ensures that we use 64b IO on windows (only 32b by default).
98 #define FLUSH_FD(x) _commit(x)
99 #define SEEK_FD(x, y, z) _lseeki64(x, y, z)
100 #define TELL_FD(x) _telli64(x)
102 #if (__MSVCRT_VERSION__ < 0x0601)
103 //#warning MSCVRT-Version smaller than 6.01
104 #define STAT_FD(x, y) _fstati64(x, y)
105 #define STAT_STRUCT struct _stati64
107 #define STAT_FD(x, y) _fstat64(x, y)
108 #define STAT_STRUCT struct __stat64
112 // We don't need to sync all meta-data, just the contents,
113 // so use fdatasync when possible (see man fdatasync).
114 #if defined(_POSIX_SYNCHRONIZED_IO) && (_POSIX_SYNCHRONIZED_IO > 0)
115 #define FLUSH_FD(x) fdatasync(x)
117 #define FLUSH_FD(x) fsync(x)
120 #define SEEK_FD(x, y, z) lseek(x, y, z)
121 #define TELL_FD(x) wxTell(x)
122 #define STAT_FD(x, y) fstat(x, y)
123 #define STAT_STRUCT struct stat
127 // This function is used to check if a syscall failed, in that case
128 // log an appropriate message containing the errno string.
129 inline void syscall_check(
131 const CPath
& filePath
,
132 const wxString
& what
)
135 AddDebugLogLineC(logCFile
,
136 CFormat(wxT("Error when %s (%s): %s"))
137 % what
% filePath
% wxSysErrorMsg());
142 CSeekFailureException::CSeekFailureException(const wxString
& desc
)
143 : CIOFailureException(wxT("SeekFailure"), desc
)
148 : m_fd(fd_invalid
), m_safeWrite(false)
152 CFile::CFile(const CPath
& fileName
, OpenMode mode
)
155 Open(fileName
, mode
);
159 CFile::CFile(const wxString
& fileName
, OpenMode mode
)
162 Open(fileName
, mode
);
169 // If the writing gets aborted, dtor is still called.
170 // In this case do NOT replace the original file with the
171 // probably broken new one!
178 int CFile::fd() const
184 bool CFile::IsOpened() const
186 return m_fd
!= fd_invalid
;
190 const CPath
& CFile::GetFilePath() const
196 bool CFile::Create(const CPath
& path
, bool overwrite
, int accessMode
)
198 if (!overwrite
&& path
.FileExists()) {
202 return Open(path
, write
, accessMode
);
205 bool CFile::Create(const wxString
& path
, bool overwrite
, int accessMode
)
207 return Create(CPath(path
), overwrite
, accessMode
);
211 bool CFile::Open(const wxString
& fileName
, OpenMode mode
, int accessMode
)
213 MULE_VALIDATE_PARAMS(fileName
.Length(), wxT("CFile: Cannot open, empty path."));
215 return Open(CPath(fileName
), mode
, accessMode
);
219 bool CFile::Open(const CPath
& fileName
, OpenMode mode
, int accessMode
)
221 MULE_VALIDATE_PARAMS(fileName
.IsOk(), wxT("CFile: Cannot open, empty path."));
228 m_filePath
= fileName
;
231 int flags
= O_BINARY
| O_LARGEFILE
;
233 int flags
= O_BINARY
;
241 if (fileName
.FileExists())
243 flags
|= O_WRONLY
| O_APPEND
;
246 //else: fall through as write_append is the same as write if the
247 // file doesn't exist
251 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
255 flags
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
256 m_filePath
= m_filePath
.AppendExt(wxT(".new"));
261 flags
|= O_WRONLY
| O_CREAT
| O_EXCL
;
269 // Windows needs wide character file names
271 m_fd
= _wopen(m_filePath
.GetRaw().c_str(), flags
, accessMode
);
273 Unicode2CharBuf tmpFileName
= filename2char(m_filePath
.GetRaw());
274 wxASSERT_MSG(tmpFileName
, wxT("Convertion failed in CFile::Open"));
275 m_fd
= open(tmpFileName
, flags
, accessMode
);
277 syscall_check(m_fd
!= fd_invalid
, m_filePath
, wxT("opening file"));
283 void CFile::Reopen(OpenMode mode
)
285 if (!Open(m_filePath
, mode
)) {
286 throw CIOFailureException(wxString(wxT("Error reopening file")));
293 MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot close closed file."));
295 bool closed
= (close(m_fd
) != -1);
296 syscall_check(closed
, m_filePath
, wxT("closing file"));
301 CPath
filePathTemp(m_filePath
);
302 m_filePath
= m_filePath
.RemoveExt(); // restore m_filePath for Reopen()
304 closed
= CPath::RenameFile(filePathTemp
, m_filePath
, true);
314 MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot flush closed file."));
316 bool flushed
= (FLUSH_FD(m_fd
) != -1);
317 syscall_check(flushed
, m_filePath
, wxT("flushing file"));
323 sint64
CFile::doRead(void* buffer
, size_t count
) const
325 MULE_VALIDATE_PARAMS(buffer
, wxT("CFile: Invalid buffer in read operation."));
326 MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot read from closed file."));
328 size_t totalRead
= 0;
329 while (totalRead
< count
) {
330 int current
= ::read(m_fd
, (char*)buffer
+ totalRead
, count
- totalRead
);
333 // Read error, nothing we can do other than abort.
334 throw CIOFailureException(wxString(wxT("Error reading from file: ")) + wxSysErrorMsg());
335 } else if ((totalRead
+ current
< count
) && Eof()) {
336 // We may fail to read the specified count in a couple
337 // of situations: EOF and interrupts. The check for EOF
338 // is needed to avoid inf. loops.
342 totalRead
+= current
;
349 sint64
CFile::doWrite(const void* buffer
, size_t nCount
)
351 MULE_VALIDATE_PARAMS(buffer
, wxT("CFile: Invalid buffer in write operation."));
352 MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot write to closed file."));
354 sint64 result
= ::write(m_fd
, buffer
, nCount
);
356 if (result
!= (sint64
)nCount
) {
357 throw CIOFailureException(wxString(wxT("Error writing to file: ")) + wxSysErrorMsg());
364 sint64
CFile::doSeek(sint64 offset
) const
366 MULE_VALIDATE_STATE(IsOpened(), wxT("Cannot seek on closed file."));
367 MULE_VALIDATE_PARAMS(offset
>= 0, wxT("Invalid position, must be positive."));
369 sint64 result
= SEEK_FD(m_fd
, offset
, SEEK_SET
);
371 if (result
== offset
) {
373 } else if (result
== wxInvalidOffset
) {
374 throw CSeekFailureException(wxString(wxT("Seeking failed: ")) + wxSysErrorMsg());
376 throw CSeekFailureException(wxT("Seeking returned incorrect position"));
381 uint64
CFile::GetPosition() const
383 MULE_VALIDATE_STATE(IsOpened(), wxT("Cannot get position in closed file."));
385 sint64 pos
= TELL_FD(m_fd
);
386 if (pos
== wxInvalidOffset
) {
387 throw CSeekFailureException(wxString(wxT("Failed to retrieve position in file: ")) + wxSysErrorMsg());
394 uint64
CFile::GetLength() const
396 MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot get length of closed file."));
399 if (STAT_FD(m_fd
, &buf
) == -1) {
400 throw CIOFailureException(wxString(wxT("Failed to retrieve length of file: ")) + wxSysErrorMsg());
407 uint64
CFile::GetAvailable() const
409 const uint64 length
= GetLength();
410 const uint64 position
= GetPosition();
412 // Safely handle seeking past EOF
413 if (position
< length
) {
414 return length
- position
;
417 // File is at or after EOF
422 bool CFile::SetLength(uint64 new_len
)
424 MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot set length when no file is open."));
428 // MSVC has a 64bit version
429 bool result
= _chsize_s(m_fd
, new_len
) == 0;
431 // MingW has an old runtime without it
432 bool result
= chsize(m_fd
, new_len
) == 0;
435 bool result
= ftruncate(m_fd
, new_len
) != -1;
438 syscall_check(result
, m_filePath
, wxT("truncating file"));
442 // File_checked_for_headers