Added cmake
[amule.git] / src / FileArea.cpp
blobbe8e13864009950c7df97994234f92d26ffc47f9
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 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 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
35 #ifdef HAVE_SIGNAL_H
36 #include <signal.h>
37 #endif
39 #include "FileArea.h" // Interface declarations.
40 #include "FileAutoClose.h" // Needed for CFileAutoClose
42 #ifndef ENABLE_MMAP
43 # define ENABLE_MMAP 0
44 #endif
46 #if ENABLE_MMAP && defined(HAVE_MMAP) && defined(HAVE_SYSCONF) && (defined(HAVE__SC_PAGESIZE) || defined(HAVE__SC_PAGE_SIZE))
47 # define USE_MMAP
48 #endif
50 #ifdef USE_MMAP
52 #include <sys/mman.h>
54 #if defined(HAVE_SYSCONF) && defined(HAVE__SC_PAGESIZE)
55 static const long gs_pageSize = sysconf(_SC_PAGESIZE);
56 #elif defined(HAVE_SYSCONF) && defined(HAVE__SC_PAGE_SIZE)
57 static const long gs_pageSize = sysconf(_SC_PAGE_SIZE);
58 #endif
60 #endif /* USE_MMAP */
62 #if !defined(HAVE_SIGACTION) || !defined(SA_SIGINFO) || !defined(USE_MMAP) || defined(__UCLIBC__)
64 class CFileAreaSigHandler
66 public:
67 static void Init() {};
68 static void Add(CFileArea&) {};
69 static void Remove(CFileArea&) {};
70 private:
71 CFileAreaSigHandler() {};
74 #else
76 class CFileAreaSigHandler
78 public:
79 static void Init();
80 static void Add(CFileArea& area);
81 static void Remove(CFileArea& area);
82 private:
83 CFileAreaSigHandler() {};
84 static wxMutex mutex;
85 static CFileArea *first;
86 static bool initialized;
87 static struct sigaction old_segv, old_bus;
88 static void Handler(int sig, siginfo_t *info, void *ctx);
91 wxMutex CFileAreaSigHandler::mutex;
92 CFileArea * CFileAreaSigHandler::first;
93 bool CFileAreaSigHandler::initialized = false;
94 struct sigaction CFileAreaSigHandler::old_segv;
95 struct sigaction CFileAreaSigHandler::old_bus;
97 /* define MAP_ANONYMOUS for Mac OS X */
98 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
99 #define MAP_ANONYMOUS MAP_ANON
100 #endif
102 // Handle signals.
103 // The idea is to replace faulted memory with zeroes and mark
104 // the error in proper CFileArea
105 void CFileAreaSigHandler::Handler(int sig, siginfo_t *info, void *ctx)
107 CFileArea *cur;
108 // find the mapped section where violation occurred (if any)
110 wxMutexLocker lock(mutex);
111 cur = first;
112 while (cur) {
113 if (cur->m_mmap_buffer && info->si_addr >= cur->m_mmap_buffer && info->si_addr < cur->m_mmap_buffer + cur->m_length)
114 break;
115 cur = cur->m_next;
119 // mark error if found
120 if (cur && gs_pageSize > 0) {
121 cur->m_error = true;
122 char *start_addr = ((char *) info->si_addr) - (((unsigned long) info->si_addr) % gs_pageSize);
123 if (mmap(start_addr, gs_pageSize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) != MAP_FAILED)
124 return;
127 // call old handler
128 struct sigaction* sa = (sig == SIGSEGV) ? &old_segv : &old_bus;
129 if (sa->sa_flags & SA_SIGINFO)
130 sa->sa_sigaction(sig, info, ctx);
131 else if (sa->sa_handler == SIG_DFL || sa->sa_handler == SIG_IGN)
132 abort();
133 else
134 sa->sa_handler(sig);
137 void CFileAreaSigHandler::Init()
139 // init error handler if needed
140 wxMutexLocker lock(mutex);
141 if (initialized)
142 return;
144 // Set our new signal handler.
145 // Note that we safe old handlers (propably wx ones) in order
146 // to be able to call them if signal not handled as desired.
147 // These handler will be removed by wx code when wx will restore
148 // old ones
149 struct sigaction sa;
150 memset(&sa, 0, sizeof(sa));
151 sigemptyset(&sa.sa_mask);
152 sa.sa_sigaction = Handler;
153 sa.sa_flags = SA_NODEFER|SA_SIGINFO;
154 if (sigaction(SIGSEGV, &sa, &old_segv))
155 return;
156 if (sigaction(SIGBUS, &sa, &old_bus)) {
157 sigaction(SIGSEGV, &old_segv, NULL);
158 return;
160 initialized = true;
163 void CFileAreaSigHandler::Add(CFileArea& area)
165 wxMutexLocker lock(mutex);
166 area.m_next = first;
167 first = &area;
170 void CFileAreaSigHandler::Remove(CFileArea& area)
172 wxMutexLocker lock(mutex);
173 CFileArea **cur = &first;
174 while (*cur) {
175 if (*cur == &area) {
176 *cur = area.m_next;
177 area.m_next = NULL;
178 break;
180 cur = &(*cur)->m_next;
183 #endif
185 CFileArea::CFileArea()
186 : m_buffer(NULL), m_mmap_buffer(NULL), m_length(0), m_next(NULL), m_file(NULL), m_error(false)
188 CFileAreaSigHandler::Init();
192 CFileArea::~CFileArea()
194 Close();
195 CheckError();
198 bool CFileArea::Close()
200 if (m_buffer != NULL && m_mmap_buffer == NULL)
202 delete[] m_buffer;
203 m_buffer = NULL;
205 #ifdef USE_MMAP
206 if (m_mmap_buffer)
208 munmap(m_mmap_buffer, m_length);
209 // remove from list
210 CFileAreaSigHandler::Remove(*this);
211 m_buffer = NULL;
212 m_mmap_buffer = NULL;
213 if (m_file) {
214 m_file->Unlock();
215 m_file = NULL;
218 #endif
219 return true;
223 void CFileArea::ReadAt(CFileAutoClose& file, uint64 offset, size_t count)
225 Close();
227 #ifdef USE_MMAP
228 uint64 offEnd = offset + count;
229 if (gs_pageSize > 0 && offEnd < 0x100000000ull) {
230 uint64 offStart = offset & (~((uint64)gs_pageSize-1));
231 m_length = offEnd - offStart;
232 void *p = mmap(NULL, m_length, PROT_READ, MAP_SHARED, file.fd(), offStart);
233 if (p != MAP_FAILED) {
234 m_file = &file;
235 m_mmap_buffer = (byte*) p;
236 m_buffer = m_mmap_buffer + (offset - offStart);
238 // add to list to catch errors correctly
239 CFileAreaSigHandler::Add(*this);
240 return;
243 file.Unlock();
244 #endif
245 m_buffer = new byte[count];
246 file.ReadAt(m_buffer, offset, count);
249 #ifdef USE_MMAP
250 void CFileArea::StartWriteAt(CFileAutoClose& file, uint64 offset, size_t count)
252 Close();
254 uint64 offEnd = offset + count;
255 if (file.GetLength() >= offEnd && gs_pageSize > 0 && offEnd < 0x100000000ull) {
256 uint64 offStart = offset & (~((uint64)gs_pageSize-1));
257 m_length = offEnd - offStart;
258 void *p = mmap(NULL, m_length, PROT_READ|PROT_WRITE, MAP_SHARED, file.fd(), offStart);
259 if (p != MAP_FAILED)
261 m_file = &file;
262 m_mmap_buffer = (byte*) p;
263 m_buffer = m_mmap_buffer + (offset - offStart);
265 // add to list to catch errors correctly
266 CFileAreaSigHandler::Add(*this);
267 return;
269 file.Unlock();
271 m_buffer = new byte[count];
273 #else
274 void CFileArea::StartWriteAt(CFileAutoClose&, uint64, size_t count)
276 Close();
277 m_buffer = new byte[count];
279 #endif
282 bool CFileArea::FlushAt(CFileAutoClose& file, uint64 offset, size_t count)
284 if (!m_buffer)
285 return false;
287 #ifdef USE_MMAP
288 if (m_mmap_buffer) {
289 if (msync(m_mmap_buffer, m_length, MS_SYNC))
290 return false;
291 Close();
292 return true;
294 #endif
295 file.WriteAt(m_buffer, offset, count);
296 Close();
297 return true;
300 void CFileArea::CheckError()
302 bool err = m_error;
303 m_error = false;
304 if (err)
305 throw CIOFailureException(wxT("Read error, failed to read from file."));