Upstream tarball 9585
[amule.git] / src / FileAutoClose.cpp
blobc61f1d7384bd523edafabbb20c288492f1c794f7
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2008-2009 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2008-2009 Stu Redman ( sturedman@amule.org )
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.
20 //
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),
35 m_autoClosed(false),
36 m_locked(false),
37 m_size(0),
38 m_lastAccess(TheTime)
41 CFileAutoClose::CFileAutoClose(const CPath& path, CFile::OpenMode mode)
43 Open(path, mode);
46 bool CFileAutoClose::Open(const CPath& path, CFile::OpenMode mode)
48 m_mode = mode;
49 m_autoClosed = false;
50 m_locked = false;
51 m_size = 0;
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;
59 m_autoClosed = false;
60 m_lastAccess = TheTime;
61 return m_file.Create(path, overwrite);
64 bool CFileAutoClose::Close()
66 bool state = m_autoClosed ? true : m_file.Close();
67 m_autoClosed = false;
68 return state;
72 bool CFileAutoClose::Flush()
74 Reopen();
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)
86 Reopen();
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)
102 Reopen();
103 return m_file.Seek(offset, from);
106 void CFileAutoClose::Read(void* buffer, size_t count)
108 Reopen();
109 return m_file.Read(buffer, count);
112 void CFileAutoClose::Write(const void* buffer, size_t count)
114 Reopen();
115 return m_file.Write(buffer, count);
118 bool CFileAutoClose::Eof()
120 Reopen();
121 return m_file.Eof();
124 uint64 CFileAutoClose::GetPosition()
126 Reopen();
127 return m_file.GetPosition();
130 int CFileAutoClose::fd()
132 Reopen();
133 m_locked = true;
134 return m_file.fd();
137 void CFileAutoClose::Unlock()
139 m_locked = false;
142 void CFileAutoClose::Reopen()
144 if (m_autoClosed) {
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)
157 if (!m_autoClosed
158 && (now || TheTime - m_lastAccess >= ReleaseTime)
159 && !m_locked
160 && m_file.IsOpened()) {
161 m_autoClosed = true;
162 m_size = m_file.GetLength();
163 m_file.Close();
164 AddDebugLogLineN(logCFile, wxT("AutoClosed file ") + GetFilePath().GetPrintable()
165 + (now ? wxT("(immediately)") : wxT("(timed)")));
167 return m_autoClosed;
170 // File_checked_for_headers