2 // This file is part of the aMule Project.
4 // Copyright (c) 2008-2009 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2008-2009 Stu Redman ( sturedman@amule.org )
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
26 #include "FileAutoClose.h"
27 #include "GetTickCount.h" // for TheTime
28 #include "Logger.h" // Needed for AddDebugLogLineM
31 static const uint32 ReleaseTime
= 600; // close file after 10 minutes of not being used
33 CFileAutoClose::CFileAutoClose()
34 : m_mode(CFile::read
),
41 CFileAutoClose::CFileAutoClose(const CPath
& path
, CFile::OpenMode mode
)
46 bool CFileAutoClose::Open(const CPath
& path
, CFile::OpenMode mode
)
52 m_lastAccess
= TheTime
;
53 return m_file
.Open(path
, mode
);
56 bool CFileAutoClose::Create(const CPath
& path
, bool overwrite
)
58 m_mode
= CFile::write
;
60 m_lastAccess
= TheTime
;
61 return m_file
.Create(path
, overwrite
);
64 bool CFileAutoClose::Close()
66 bool state
= m_autoClosed
? true : m_file
.Close();
71 uint64
CFileAutoClose::GetLength() const
73 return m_autoClosed
? m_size
: m_file
.GetLength();
76 bool CFileAutoClose::SetLength(uint64 newLength
)
79 return m_file
.SetLength(newLength
);
82 const CPath
& CFileAutoClose::GetFilePath() const
84 return m_file
.GetFilePath();
87 bool CFileAutoClose::IsOpened() const
89 return m_autoClosed
|| m_file
.IsOpened();
92 void CFileAutoClose::ReadAt(void* buffer
, uint64 offset
, size_t count
)
96 m_file
.Read(buffer
, count
);
99 void CFileAutoClose::WriteAt(const void* buffer
, uint64 offset
, size_t count
)
103 m_file
.Write(buffer
, count
);
106 bool CFileAutoClose::Eof()
112 int CFileAutoClose::fd()
119 void CFileAutoClose::Unlock()
124 void CFileAutoClose::Reopen()
127 AddDebugLogLineN(logCFile
, wxT("Reopen AutoClosed file ") + GetFilePath().GetPrintable());
128 m_file
.Reopen(m_mode
); // throws on failure
129 // On open error m_autoClosed stays true, so if the app tries again
130 // it opens and throws again.
131 // Otherwise it would assert on an operation on a closed file and probably die.
132 m_autoClosed
= false;
134 m_lastAccess
= TheTime
;
137 bool CFileAutoClose::Release(bool now
)
140 && (now
|| TheTime
- m_lastAccess
>= ReleaseTime
)
142 && m_file
.IsOpened()) {
144 m_size
= m_file
.GetLength();
146 AddDebugLogLineN(logCFile
, wxT("AutoClosed file ") + GetFilePath().GetPrintable()
147 + (now
? wxT("(immediately)") : wxT("(timed)")));
152 // File_checked_for_headers