1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377 | //
// This file is part of the aMule Project.
//
// Copyright (c) 2008-2011 aMule Team ( admin@amule.org / http://www.amule.org )
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//
#include "PlatformSpecific.h"
#include "config.h"
// NTFS Sparse Files (only for MSW)
#ifdef __WINDOWS__
#include "common/Format.h"
#include "Logger.h"
#include <winbase.h>
#include <winioctl.h>
#ifndef FSCTL_SET_SPARSE
# define FSCTL_SET_SPARSE CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 49, METHOD_BUFFERED, FILE_SPECIAL_ACCESS)
#endif
#ifndef FSCTL_SET_ZERO_DATA
# define FSCTL_SET_ZERO_DATA CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 50, METHOD_BUFFERED, FILE_WRITE_DATA)
#endif
// Create a message from a Windows error code
static wxString SystemError()
{
WCHAR * lpMsgBuf = NULL;
FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
0, // Default language
(LPWSTR) &lpMsgBuf,
0,
NULL
);
wxString ret(lpMsgBuf);
LocalFree(lpMsgBuf);
return ret;
}
// Create a file in sparse mode
bool PlatformSpecific::CreateSparseFile(const CPath& name, uint64_t size)
{
DWORD dwReturnedBytes=0;
HANDLE hd = CreateFileW(name.GetRaw().c_str(),
GENERIC_READ | GENERIC_WRITE,
0, // share - not shareable
NULL, // security - not inheritable
CREATE_ALWAYS,
FILE_ATTRIBUTE_ARCHIVE,
NULL);
if (hd == INVALID_HANDLE_VALUE) {
AddDebugLogLineC(logPartFile, CFormat(wxT("converting %s to sparse failed (OPEN): %s ")) % name % SystemError());
return false;
}
if (!DeviceIoControl(hd, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &dwReturnedBytes, NULL)) {
AddDebugLogLineC(logPartFile, CFormat(wxT("converting %s to sparse failed (SET_SPARSE): %s ")) % name % SystemError());
} else {
// FILE_ZERO_DATA_INFORMATION is not defined here
struct {
uint64 FileOffset;
uint64 BeyondFinalZero;
} fzdi;
fzdi.FileOffset = 0;
fzdi.BeyondFinalZero = size;
LARGE_INTEGER largo;
largo.QuadPart = size;
// zero the data
if (!DeviceIoControl(hd, FSCTL_SET_ZERO_DATA, (LPVOID) &fzdi, sizeof(fzdi), NULL, 0, &dwReturnedBytes, NULL)) {
AddDebugLogLineC(logPartFile, CFormat(wxT("converting %s to sparse failed (ZERO): %s")) % name % SystemError());
} else if (!SetFilePointerEx(hd, largo, NULL, FILE_BEGIN) || !SetEndOfFile(hd)) {
AddDebugLogLineC(logPartFile, CFormat(wxT("converting %s to sparse failed (SEEK): %s")) % name % SystemError());
}
}
CloseHandle(hd);
return true;
}
#else // non Windows systems don't need all this
#include "CFile.h"
bool PlatformSpecific::CreateSparseFile(const CPath& name, uint64_t WXUNUSED(size))
{
CFile f;
return f.Create(name.GetRaw(), true) && f.Close();
}
#endif
#ifdef __WINDOWS__
#include <wx/msw/registry.h>
#include <wx/utils.h>
// Get the max number of connections that the OS supports, or -1 for default
int PlatformSpecific::GetMaxConnections()
{
int maxconn = -1;
// Try to get the max connection value in the registry
wxRegKey key( wxT("HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Services\\VxD\\MSTCP\\MaxConnections") );
wxString value;
if ( key.Exists() ) {
value = key.QueryDefaultValue();
}
if ( !value.IsEmpty() && value.IsNumber() ) {
long mc;
value.ToLong(&mc);
maxconn = (int)mc;
} else {
switch (wxGetOsVersion()) {
case wxOS_WINDOWS_9X:
// This includes all Win9x versions
maxconn = 50;
break;
case wxOS_WINDOWS_NT:
// This includes NT based windows
maxconn = 500;
break;
default:
// Anything else. Let aMule decide...
break;
}
}
return maxconn;
}
#endif
#ifdef __WINDOWS__
#include <winbase.h>
#include <shlwapi.h>
static PlatformSpecific::EFSType doGetFilesystemType(const CPath& path)
{
wxWritableWCharBuffer pathRaw(path.GetRaw().wchar_str());
LPWSTR volume = pathRaw;
if (!PathStripToRootW(volume)) {
return PlatformSpecific::fsOther;
}
PathAddBackslashW(volume);
DWORD maximumComponentLength = 0;
DWORD filesystemFlags = 0;
WCHAR filesystemNameBuffer[128];
if (!GetVolumeInformationW(volume, NULL, 0, NULL, &maximumComponentLength, &filesystemFlags, filesystemNameBuffer, 128)) {
return PlatformSpecific::fsOther;
}
if (wxStrnicmp(filesystemNameBuffer, wxT("FAT"), 3) == 0) {
return PlatformSpecific::fsFAT;
} else if (wxStrcmp(filesystemNameBuffer, wxT("NTFS")) == 0) {
return PlatformSpecific::fsNTFS;
}
return PlatformSpecific::fsOther;
};
#elif defined(HAVE_GETMNTENT) && defined(HAVE_MNTENT_H)
#include <stdio.h>
#include <string.h>
#include <mntent.h>
#ifndef _PATH_MOUNTED
# define _PATH_MOUNTED "/etc/mtab"
#endif
#include <common/StringFunctions.h>
static PlatformSpecific::EFSType doGetFilesystemType(const CPath& path)
{
struct mntent *entry = NULL;
PlatformSpecific::EFSType retval = PlatformSpecific::fsOther;
FILE *mnttab = fopen(_PATH_MOUNTED, "r");
unsigned bestPrefixLen = 0;
if (mnttab == NULL) {
return PlatformSpecific::fsOther;
}
while ((entry = getmntent(mnttab)) != NULL) {
if (entry->mnt_dir) {
wxString dir = char2unicode(entry->mnt_dir);
if (dir == path.GetRaw().Mid(0, dir.Length())) {
if (dir.Length() >= bestPrefixLen) {
if (entry->mnt_type == NULL) {
break;
} else if (!strcmp(entry->mnt_type, "ntfs")) {
retval = PlatformSpecific::fsNTFS;
} else if (!strcmp(entry->mnt_type, "msdos") ||
!strcmp(entry->mnt_type, "umsdos") ||
!strcmp(entry->mnt_type, "vfat") ||
!strncmp(entry->mnt_type, "fat", 3)) {
retval = PlatformSpecific::fsFAT;
} else if (!strcmp(entry->mnt_type, "hfs")) {
retval = PlatformSpecific::fsHFS;
} else if (!strcmp(entry->mnt_type, "hpfs")) {
retval = PlatformSpecific::fsHPFS;
} else if (!strcmp(entry->mnt_type, "minix")) {
retval = PlatformSpecific::fsMINIX;
} /* Add more filesystem types here */
else if (dir.Length() > bestPrefixLen) {
retval = PlatformSpecific::fsOther;
}
bestPrefixLen = dir.Length();
}
}
}
}
fclose(mnttab);
return retval;
}
#elif defined(HAVE_GETMNTENT) && defined(HAVE_SYS_MNTENT_H) && defined(HAVE_SYS_MNTTAB_H)
#include <stdio.h>
#include <string.h>
#include <sys/mntent.h>
#include <sys/mnttab.h>
#ifndef MNTTAB
# define MNTTAB "/etc/mnttab"
#endif
#include <common/StringFunctions.h>
static PlatformSpecific::EFSType doGetFilesystemType(const CPath& path)
{
struct mnttab entryStatic;
struct mnttab *entry = &entryStatic;
PlatformSpecific::EFSType retval = PlatformSpecific::fsOther;
FILE *fmnttab = fopen(MNTTAB, "r");
unsigned bestPrefixLen = 0;
if (fmnttab == NULL) {
return PlatformSpecific::fsOther;
}
while (getmntent(fmnttab, entry) == 0) {
if (entry->mnt_mountp) {
wxString dir = char2unicode(entry->mnt_mountp);
if (dir == path.GetRaw().Mid(0, dir.Length())) {
if (dir.Length() >= bestPrefixLen) {
if (entry->mnt_fstype == NULL) {
break;
} else if (!strcmp(entry->mnt_fstype, MNTTYPE_PCFS)) {
retval = PlatformSpecific::fsFAT;
} else if (hasmntopt(entry, MNTOPT_NOLARGEFILES)) {
// MINIX is a file system that can handle special chars but has no large files.
retval = PlatformSpecific::fsMINIX;
} else if (dir.Length() > bestPrefixLen) {
retval = PlatformSpecific::fsOther;
}
bestPrefixLen = dir.Length();
}
}
}
}
fclose(fmnttab);
return retval;
}
#else
// No way to determine filesystem type, no restrictions apply.
static inline PlatformSpecific::EFSType doGetFilesystemType(const CPath& WXUNUSED(path))
{
return PlatformSpecific::fsOther;
}
#endif
#include <map>
#include <wx/thread.h>
PlatformSpecific::EFSType PlatformSpecific::GetFilesystemType(const CPath& path)
{
typedef std::map<wxString, EFSType> FSMap;
// Caching previous results, to speed up further checks.
static FSMap s_fscache;
// Lock used to ensure the integrity of the cache.
static wxMutex s_lock;
wxCHECK_MSG(path.IsOk(), fsOther, wxT("Invalid path in GetFilesystemType()"));
wxMutexLocker locker(s_lock);
FSMap::iterator it = s_fscache.find(path.GetRaw());
if (it != s_fscache.end()) {
return it->second;
}
return s_fscache[path.GetRaw()] = doGetFilesystemType(path);
}
// Power event vetoing
static bool m_preventingSleepMode = false;
#if defined(__WXMAC__) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1050 // 10.5 only
#include <IOKit/pwr_mgt/IOPMLib.h>
static IOPMAssertionID assertionID;
#endif
void PlatformSpecific::PreventSleepMode()
{
if (!m_preventingSleepMode) {
#ifdef _MSC_VER
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
m_preventingSleepMode = true;
// IOPMAssertionCreate has been introduced in Leopard (10.5) but deprecated starting from Snow Leopard(10.6)
// For more details see:
// - http://developer.apple.com/library/mac/#qa/qa1340/_index.html
// - http://www.cimgf.com/2009/10/14/the-journey-to-disabling-sleep-with-iokit/
#elif defined(__WXMAC__) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 // 10.6 only
CFStringRef reasonForActivity= CFSTR("Prevent Display Sleep");
IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep,
kIOPMAssertionLevelOn, reasonForActivity, &assertionID);
if (success == kIOReturnSuccess) {
// Correctly vetoed, flag so we don't do it again.
m_preventingSleepMode = true;
} else {
// May be should be better to trace in log?
}
#elif defined(__WXMAC__) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1050 // 10.5 only
IOReturn success = IOPMAssertionCreate(kIOPMAssertionTypeNoDisplaySleep,
kIOPMAssertionLevelOn, &assertionID);
if (success == kIOReturnSuccess) {
// Correctly vetoed, flag so we don't do it again.
m_preventingSleepMode = true;
} else {
// ??
}
#else
//#warning Power event vetoing not implemented.
// Not implemented
#endif
}
}
void PlatformSpecific::AllowSleepMode()
{
if (m_preventingSleepMode) {
#ifdef _MSC_VER
SetThreadExecutionState(ES_CONTINUOUS); // Clear the system request flag.
m_preventingSleepMode = false;
#elif defined(__WXMAC__) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1050 // 10.5 only
IOReturn success = IOPMAssertionRelease(assertionID);
if (success == kIOReturnSuccess) {
// Correctly restored, flag so we don't do it again.
m_preventingSleepMode = false;
} else {
// ??
}
#else
// Not implemented
#endif
}
}
|