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 beeing 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();
72 bool CFileAutoClose::Flush()
75 return m_file
.Flush();
79 uint64
CFileAutoClose::GetLength() const
81 return m_autoClosed
? m_size
: m_file
.GetLength();
84 bool CFileAutoClose::SetLength(uint64 newLength
)
87 return m_file
.SetLength(newLength
);
90 const CPath
& CFileAutoClose::GetFilePath() const
92 return m_file
.GetFilePath();
95 bool CFileAutoClose::IsOpened() const
97 return m_autoClosed
|| m_file
.IsOpened();
100 uint64
CFileAutoClose::Seek(sint64 offset
, wxSeekMode from
)
103 return m_file
.Seek(offset
, from
);
106 void CFileAutoClose::Read(void* buffer
, size_t count
)
109 return m_file
.Read(buffer
, count
);
112 void CFileAutoClose::Write(const void* buffer
, size_t count
)
115 return m_file
.Write(buffer
, count
);
118 bool CFileAutoClose::Eof()
124 uint64
CFileAutoClose::GetPosition()
127 return m_file
.GetPosition();
130 int CFileAutoClose::fd()
137 void CFileAutoClose::Unlock()
142 void CFileAutoClose::Reopen()
145 AddDebugLogLineN(logCFile
, wxT("Reopen AutoClosed file ") + GetFilePath().GetPrintable());
146 m_file
.Reopen(m_mode
); // throws on failure
147 // On open error m_autoClosed stays true, so if the app tries again
148 // it opens and throws again.
149 // Otherwise it would assert on an operation on a closed file and probably die.
150 m_autoClosed
= false;
152 m_lastAccess
= TheTime
;
155 bool CFileAutoClose::Release(bool now
)
158 && (now
|| TheTime
- m_lastAccess
>= ReleaseTime
)
160 && m_file
.IsOpened()) {
162 m_size
= m_file
.GetLength();
164 AddDebugLogLineN(logCFile
, wxT("AutoClosed file ") + GetFilePath().GetPrintable()
165 + (now
? wxT("(immediately)") : wxT("(timed)")));
170 // File_checked_for_headers