2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 1998 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
39 #include "FileArea.h" // Interface declarations.
40 #include "FileAutoClose.h" // Needed for CFileAutoClose
42 #ifdef HAVE_SYS_MMAN_H
46 #if !defined(HAVE_SIGACTION) || !defined(SA_SIGINFO) || !defined(HAVE_MMAP)
48 class CFileAreaSigHandler
51 static void Init() {};
52 static void Add(CFileArea
& area
) {};
53 static void Remove(CFileArea
& area
) {};
55 CFileAreaSigHandler() {};
60 class CFileAreaSigHandler
64 static void Add(CFileArea
& area
);
65 static void Remove(CFileArea
& area
);
67 CFileAreaSigHandler() {};
69 static CFileArea
*first
;
70 static bool initialized
;
71 static struct sigaction old_segv
, old_bus
;
72 static void Handler(int sig
, siginfo_t
*info
, void *ctx
);
75 wxMutex
CFileAreaSigHandler::mutex
;
76 CFileArea
* CFileAreaSigHandler::first
;
77 bool CFileAreaSigHandler::initialized
= false;
78 struct sigaction
CFileAreaSigHandler::old_segv
;
79 struct sigaction
CFileAreaSigHandler::old_bus
;
82 # if defined(HAVE_SYSCONF) && defined(HAVE__SC_PAGESIZE)
83 static const long gs_pageSize
= sysconf(_SC_PAGESIZE
);
84 # elif defined(HAVE_SYSCONF) && defined(HAVE__SC_PAGE_SIZE)
85 static const long gs_pageSize
= sysconf(_SC_PAGE_SIZE
);
86 # elif defined(HAVE_GETPAGESIZE)
87 static const int gs_pageSize
= getpagesize();
89 # error "Should use memory mapped files but don't know how to determine page size!"
93 /* define MAP_ANONYMOUS for Mac OS X */
94 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
95 #define MAP_ANONYMOUS MAP_ANON
99 // The idea is to replace faulted memory with zeroes and mark
100 // the error in proper CFileArea
101 void CFileAreaSigHandler::Handler(int sig
, siginfo_t
*info
, void *ctx
)
104 // find the mapped section where violation occurred (if any)
106 wxMutexLocker
lock(mutex
);
109 if (cur
->m_mmap_buffer
&& info
->si_addr
>= cur
->m_mmap_buffer
&& info
->si_addr
< cur
->m_mmap_buffer
+ cur
->m_length
)
115 // mark error if found
116 if (cur
&& gs_pageSize
> 0) {
118 char *start_addr
= ((char *) info
->si_addr
) - (((unsigned long) info
->si_addr
) % gs_pageSize
);
119 if (mmap(start_addr
, gs_pageSize
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
|MAP_FIXED
|MAP_ANONYMOUS
, -1, 0) != MAP_FAILED
)
124 struct sigaction
* sa
= (sig
== SIGSEGV
) ? &old_segv
: &old_bus
;
125 if (sa
->sa_flags
& SA_SIGINFO
)
126 sa
->sa_sigaction(sig
, info
, ctx
);
127 else if (sa
->sa_handler
== SIG_DFL
|| sa
->sa_handler
== SIG_IGN
)
133 void CFileAreaSigHandler::Init()
135 // init error handler if needed
136 wxMutexLocker
lock(mutex
);
140 // Set our new signal handler.
141 // Note that we safe old handlers (propably wx ones) in order
142 // to be able to call them if signal not handled as desired.
143 // These handler will be removed by wx code when wx will restore
146 memset(&sa
, 0, sizeof(sa
));
147 sigemptyset(&sa
.sa_mask
);
148 sa
.sa_sigaction
= Handler
;
149 sa
.sa_flags
= SA_NODEFER
|SA_SIGINFO
;
150 if (sigaction(SIGSEGV
, &sa
, &old_segv
))
152 if (sigaction(SIGBUS
, &sa
, &old_bus
)) {
153 sigaction(SIGSEGV
, &old_segv
, NULL
);
159 void CFileAreaSigHandler::Add(CFileArea
& area
)
161 wxMutexLocker
lock(mutex
);
166 void CFileAreaSigHandler::Remove(CFileArea
& area
)
168 wxMutexLocker
lock(mutex
);
169 CFileArea
**cur
= &first
;
176 cur
= &(*cur
)->m_next
;
181 CFileArea::CFileArea()
182 : m_buffer(NULL
), m_mmap_buffer(NULL
), m_length(0), m_next(NULL
), m_file(NULL
), m_error(false)
184 CFileAreaSigHandler::Init();
188 CFileArea::~CFileArea()
194 bool CFileArea::Close()
196 if (m_buffer
!= NULL
&& m_mmap_buffer
== NULL
)
204 munmap(m_mmap_buffer
, m_length
);
206 CFileAreaSigHandler::Remove(*this);
208 m_mmap_buffer
= NULL
;
219 void CFileArea::ReadAt(CFileAutoClose
& file
, uint64 offset
, size_t count
)
224 uint64 offEnd
= offset
+ count
;
225 if (gs_pageSize
> 0 && offEnd
< 0x100000000ull
) {
226 uint64 offStart
= offset
& (~((uint64
)gs_pageSize
-1));
227 m_length
= offEnd
- offStart
;
228 void *p
= mmap(NULL
, m_length
, PROT_READ
, MAP_SHARED
, file
.fd(), offStart
);
229 if (p
!= MAP_FAILED
) {
231 m_mmap_buffer
= (byte
*) p
;
232 m_buffer
= m_mmap_buffer
+ (offset
- offStart
);
234 // add to list to catch errors correctly
235 CFileAreaSigHandler::Add(*this);
241 m_buffer
= new byte
[count
];
242 file
.ReadAt(m_buffer
, offset
, count
);
245 void CFileArea::StartWriteAt(CFileAutoClose
& file
, uint64 offset
, size_t count
)
250 uint64 offEnd
= offset
+ count
;
251 if (file
.GetLength() >= offEnd
&& gs_pageSize
> 0 && offEnd
< 0x100000000ull
) {
252 uint64 offStart
= offset
& (~((uint64
)gs_pageSize
-1));
253 m_length
= offEnd
- offStart
;
254 void *p
= mmap(NULL
, m_length
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, file
.fd(), offStart
);
258 m_mmap_buffer
= (byte
*) p
;
259 m_buffer
= m_mmap_buffer
+ (offset
- offStart
);
261 // add to list to catch errors correctly
262 CFileAreaSigHandler::Add(*this);
268 m_buffer
= new byte
[count
];
272 bool CFileArea::FlushAt(CFileAutoClose
& file
, uint64 offset
, size_t count
)
279 if (msync(m_mmap_buffer
, m_length
, MS_SYNC
))
285 file
.WriteAt(m_buffer
, offset
, count
);
290 void CFileArea::CheckError()
295 throw CIOFailureException(wxT("Read error, failed to read from file."));