author | nemo |
Mon, 20 May 2013 18:33:38 -0400 | |
changeset 9021 | 2a2e85e08c7d |
parent 8928 | 10a79d89ca31 |
child 9183 | 4cd7c7aafea7 |
permissions | -rw-r--r-- |
7768 | 1 |
/* |
2 |
* Mac OS X support routines for PhysicsFS. |
|
3 |
* |
|
4 |
* Please see the file LICENSE.txt in the source's root directory. |
|
5 |
* |
|
6 |
* This file written by Ryan C. Gordon. |
|
7 |
*/ |
|
8 |
||
9 |
#define __PHYSICSFS_INTERNAL__ |
|
10 |
#include "physfs_platforms.h" |
|
11 |
||
12 |
#ifdef PHYSFS_PLATFORM_MACOSX |
|
13 |
||
14 |
#include <CoreFoundation/CoreFoundation.h> |
|
15 |
||
16 |
#if !defined(PHYSFS_NO_CDROM_SUPPORT) |
|
17 |
#include <Carbon/Carbon.h> /* !!! FIXME */ |
|
18 |
#include <IOKit/storage/IOMedia.h> |
|
19 |
#include <IOKit/storage/IOCDMedia.h> |
|
20 |
#include <IOKit/storage/IODVDMedia.h> |
|
21 |
#include <sys/mount.h> |
|
22 |
#endif |
|
23 |
||
24 |
/* Seems to get defined in some system header... */ |
|
25 |
#ifdef Free |
|
26 |
#undef Free |
|
27 |
#endif |
|
28 |
||
29 |
#include "physfs_internal.h" |
|
30 |
||
31 |
||
8928
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
32 |
#if defined(__APPLE__) |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
33 |
#if defined(TARGET_OS_MAC) && MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4 |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
34 |
/* __eprintf shouldn't have been made visible from libstdc++, or anywhere, but |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
35 |
on Mac OS X 10.4 it was defined in libstdc++.6.0.3.dylib; so on that platform |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
36 |
we have to keep defining it to keep binary compatibility. |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
37 |
We can't just put the libgcc version in the export list, because that |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
38 |
doesn't work; once a symbol is marked as hidden, it stays that way. */ |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
39 |
|
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
40 |
void __eprintf (const char *string, const char *expression, |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
41 |
unsigned int line, const char *filename) |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
42 |
{ |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
43 |
fprintf(stderr, string, expression, line, filename); |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
44 |
fflush(stderr); |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
45 |
abort(); |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
46 |
} |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
47 |
#endif |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
48 |
#endif /* __APPLE__ */ |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
49 |
|
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
50 |
|
7768 | 51 |
/* Wrap PHYSFS_Allocator in a CFAllocator... */ |
52 |
static CFAllocatorRef cfallocator = NULL; |
|
53 |
||
54 |
static CFStringRef cfallocDesc(const void *info) |
|
55 |
{ |
|
56 |
return CFStringCreateWithCString(cfallocator, "PhysicsFS", |
|
57 |
kCFStringEncodingASCII); |
|
58 |
} /* cfallocDesc */ |
|
59 |
||
60 |
||
61 |
static void *cfallocMalloc(CFIndex allocSize, CFOptionFlags hint, void *info) |
|
62 |
{ |
|
63 |
return allocator.Malloc(allocSize); |
|
64 |
} /* cfallocMalloc */ |
|
65 |
||
66 |
||
67 |
static void cfallocFree(void *ptr, void *info) |
|
68 |
{ |
|
69 |
allocator.Free(ptr); |
|
70 |
} /* cfallocFree */ |
|
71 |
||
72 |
||
73 |
static void *cfallocRealloc(void *ptr, CFIndex newsize, |
|
74 |
CFOptionFlags hint, void *info) |
|
75 |
{ |
|
76 |
if ((ptr == NULL) || (newsize <= 0)) |
|
77 |
return NULL; /* ADC docs say you should always return NULL here. */ |
|
78 |
return allocator.Realloc(ptr, newsize); |
|
79 |
} /* cfallocRealloc */ |
|
80 |
||
81 |
||
82 |
int __PHYSFS_platformInit(void) |
|
83 |
{ |
|
84 |
/* set up a CFAllocator, so Carbon can use the physfs allocator, too. */ |
|
85 |
CFAllocatorContext ctx; |
|
86 |
memset(&ctx, '\0', sizeof (ctx)); |
|
87 |
ctx.copyDescription = cfallocDesc; |
|
88 |
ctx.allocate = cfallocMalloc; |
|
89 |
ctx.reallocate = cfallocRealloc; |
|
90 |
ctx.deallocate = cfallocFree; |
|
91 |
cfallocator = CFAllocatorCreate(kCFAllocatorUseContext, &ctx); |
|
92 |
BAIL_IF_MACRO(!cfallocator, PHYSFS_ERR_OUT_OF_MEMORY, 0); |
|
93 |
return 1; /* success. */ |
|
94 |
} /* __PHYSFS_platformInit */ |
|
95 |
||
96 |
||
97 |
int __PHYSFS_platformDeinit(void) |
|
98 |
{ |
|
99 |
CFRelease(cfallocator); |
|
100 |
cfallocator = NULL; |
|
101 |
return 1; /* always succeed. */ |
|
102 |
} /* __PHYSFS_platformDeinit */ |
|
103 |
||
104 |
||
105 |
||
106 |
/* CD-ROM detection code... */ |
|
107 |
||
108 |
/* |
|
109 |
* Code based on sample from Apple Developer Connection: |
|
110 |
* http://developer.apple.com/samplecode/Sample_Code/Devices_and_Hardware/Disks/VolumeToBSDNode/VolumeToBSDNode.c.htm |
|
111 |
*/ |
|
112 |
||
113 |
#if !defined(PHYSFS_NO_CDROM_SUPPORT) |
|
114 |
||
115 |
static int darwinIsWholeMedia(io_service_t service) |
|
116 |
{ |
|
117 |
int retval = 0; |
|
118 |
CFTypeRef wholeMedia; |
|
119 |
||
120 |
if (!IOObjectConformsTo(service, kIOMediaClass)) |
|
121 |
return 0; |
|
122 |
||
123 |
wholeMedia = IORegistryEntryCreateCFProperty(service, |
|
124 |
CFSTR(kIOMediaWholeKey), |
|
125 |
cfallocator, 0); |
|
126 |
if (wholeMedia == NULL) |
|
127 |
return 0; |
|
128 |
||
129 |
retval = CFBooleanGetValue(wholeMedia); |
|
130 |
CFRelease(wholeMedia); |
|
131 |
||
132 |
return retval; |
|
133 |
} /* darwinIsWholeMedia */ |
|
134 |
||
135 |
||
136 |
static int darwinIsMountedDisc(char *bsdName, mach_port_t masterPort) |
|
137 |
{ |
|
138 |
int retval = 0; |
|
139 |
CFMutableDictionaryRef matchingDict; |
|
140 |
kern_return_t rc; |
|
141 |
io_iterator_t iter; |
|
142 |
io_service_t service; |
|
143 |
||
144 |
if ((matchingDict = IOBSDNameMatching(masterPort, 0, bsdName)) == NULL) |
|
145 |
return 0; |
|
146 |
||
147 |
rc = IOServiceGetMatchingServices(masterPort, matchingDict, &iter); |
|
148 |
if ((rc != KERN_SUCCESS) || (!iter)) |
|
149 |
return 0; |
|
150 |
||
151 |
service = IOIteratorNext(iter); |
|
152 |
IOObjectRelease(iter); |
|
153 |
if (!service) |
|
154 |
return 0; |
|
155 |
||
156 |
rc = IORegistryEntryCreateIterator(service, kIOServicePlane, |
|
157 |
kIORegistryIterateRecursively | kIORegistryIterateParents, &iter); |
|
158 |
||
159 |
if (!iter) |
|
160 |
return 0; |
|
161 |
||
162 |
if (rc != KERN_SUCCESS) |
|
163 |
{ |
|
164 |
IOObjectRelease(iter); |
|
165 |
return 0; |
|
166 |
} /* if */ |
|
167 |
||
168 |
IOObjectRetain(service); /* add an extra object reference... */ |
|
169 |
||
170 |
do |
|
171 |
{ |
|
172 |
if (darwinIsWholeMedia(service)) |
|
173 |
{ |
|
174 |
if ( (IOObjectConformsTo(service, kIOCDMediaClass)) || |
|
175 |
(IOObjectConformsTo(service, kIODVDMediaClass)) ) |
|
176 |
{ |
|
177 |
retval = 1; |
|
178 |
} /* if */ |
|
179 |
} /* if */ |
|
180 |
IOObjectRelease(service); |
|
181 |
} while ((service = IOIteratorNext(iter)) && (!retval)); |
|
182 |
||
183 |
IOObjectRelease(iter); |
|
184 |
IOObjectRelease(service); |
|
185 |
||
186 |
return retval; |
|
187 |
} /* darwinIsMountedDisc */ |
|
188 |
||
189 |
#endif /* !defined(PHYSFS_NO_CDROM_SUPPORT) */ |
|
190 |
||
191 |
||
192 |
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data) |
|
193 |
{ |
|
194 |
#if !defined(PHYSFS_NO_CDROM_SUPPORT) |
|
195 |
const char *devPrefix = "/dev/"; |
|
196 |
const int prefixLen = strlen(devPrefix); |
|
197 |
mach_port_t masterPort = 0; |
|
198 |
struct statfs *mntbufp; |
|
199 |
int i, mounts; |
|
200 |
||
201 |
if (IOMasterPort(MACH_PORT_NULL, &masterPort) != KERN_SUCCESS) |
|
202 |
BAIL_MACRO(PHYSFS_ERR_OS_ERROR, ) /*return void*/; |
|
203 |
||
204 |
mounts = getmntinfo(&mntbufp, MNT_WAIT); /* NOT THREAD SAFE! */ |
|
205 |
for (i = 0; i < mounts; i++) |
|
206 |
{ |
|
207 |
char *dev = mntbufp[i].f_mntfromname; |
|
208 |
char *mnt = mntbufp[i].f_mntonname; |
|
209 |
if (strncmp(dev, devPrefix, prefixLen) != 0) /* a virtual device? */ |
|
210 |
continue; |
|
211 |
||
212 |
dev += prefixLen; |
|
213 |
if (darwinIsMountedDisc(dev, masterPort)) |
|
214 |
cb(data, mnt); |
|
215 |
} /* for */ |
|
216 |
#endif /* !defined(PHYSFS_NO_CDROM_SUPPORT) */ |
|
217 |
} /* __PHYSFS_platformDetectAvailableCDs */ |
|
218 |
||
219 |
||
220 |
static char *convertCFString(CFStringRef cfstr) |
|
221 |
{ |
|
222 |
CFIndex len = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfstr), |
|
223 |
kCFStringEncodingUTF8) + 1; |
|
224 |
char *retval = (char *) allocator.Malloc(len); |
|
225 |
BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL); |
|
226 |
||
227 |
if (CFStringGetCString(cfstr, retval, len, kCFStringEncodingUTF8)) |
|
228 |
{ |
|
229 |
/* shrink overallocated buffer if possible... */ |
|
230 |
CFIndex newlen = strlen(retval) + 1; |
|
231 |
if (newlen < len) |
|
232 |
{ |
|
233 |
void *ptr = allocator.Realloc(retval, newlen); |
|
234 |
if (ptr != NULL) |
|
235 |
retval = (char *) ptr; |
|
236 |
} /* if */ |
|
237 |
} /* if */ |
|
238 |
||
239 |
else /* probably shouldn't fail, but just in case... */ |
|
240 |
{ |
|
241 |
allocator.Free(retval); |
|
242 |
BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL); |
|
243 |
} /* else */ |
|
244 |
||
245 |
return retval; |
|
246 |
} /* convertCFString */ |
|
247 |
||
248 |
||
249 |
char *__PHYSFS_platformCalcBaseDir(const char *argv0) |
|
250 |
{ |
|
251 |
CFURLRef cfurl = NULL; |
|
252 |
CFStringRef cfstr = NULL; |
|
253 |
CFMutableStringRef cfmutstr = NULL; |
|
254 |
char *retval = NULL; |
|
255 |
||
256 |
cfurl = CFBundleCopyBundleURL(CFBundleGetMainBundle()); |
|
257 |
BAIL_IF_MACRO(cfurl == NULL, PHYSFS_ERR_OS_ERROR, NULL); |
|
258 |
cfstr = CFURLCopyFileSystemPath(cfurl, kCFURLPOSIXPathStyle); |
|
259 |
CFRelease(cfurl); |
|
260 |
BAIL_IF_MACRO(!cfstr, PHYSFS_ERR_OUT_OF_MEMORY, NULL); |
|
261 |
cfmutstr = CFStringCreateMutableCopy(cfallocator, 0, cfstr); |
|
262 |
CFRelease(cfstr); |
|
263 |
BAIL_IF_MACRO(!cfmutstr, PHYSFS_ERR_OUT_OF_MEMORY, NULL); |
|
264 |
CFStringAppendCString(cfmutstr, "/", kCFStringEncodingUTF8); |
|
265 |
retval = convertCFString(cfmutstr); |
|
266 |
CFRelease(cfmutstr); |
|
267 |
||
268 |
return retval; /* whew. */ |
|
269 |
} /* __PHYSFS_platformCalcBaseDir */ |
|
270 |
||
271 |
||
272 |
char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app) |
|
273 |
{ |
|
274 |
/* !!! FIXME: there's a real API to determine this */ |
|
275 |
const char *userdir = __PHYSFS_getUserDir(); |
|
276 |
const char *append = "Library/Application Support/"; |
|
277 |
const size_t len = strlen(userdir) + strlen(append) + strlen(app) + 2; |
|
278 |
char *retval = allocator.Malloc(len); |
|
279 |
BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL); |
|
280 |
snprintf(retval, len, "%s%s%s/", userdir, append, app); |
|
281 |
return retval; |
|
282 |
} /* __PHYSFS_platformCalcPrefDir */ |
|
283 |
||
284 |
||
285 |
/* Platform allocator uses default CFAllocator at PHYSFS_init() time. */ |
|
286 |
||
287 |
static CFAllocatorRef cfallocdef = NULL; |
|
288 |
||
289 |
static int macosxAllocatorInit(void) |
|
290 |
{ |
|
291 |
int retval = 0; |
|
292 |
cfallocdef = CFAllocatorGetDefault(); |
|
293 |
retval = (cfallocdef != NULL); |
|
294 |
if (retval) |
|
295 |
CFRetain(cfallocdef); |
|
296 |
return retval; |
|
297 |
} /* macosxAllocatorInit */ |
|
298 |
||
299 |
||
300 |
static void macosxAllocatorDeinit(void) |
|
301 |
{ |
|
302 |
if (cfallocdef != NULL) |
|
303 |
{ |
|
304 |
CFRelease(cfallocdef); |
|
305 |
cfallocdef = NULL; |
|
306 |
} /* if */ |
|
307 |
} /* macosxAllocatorDeinit */ |
|
308 |
||
309 |
||
310 |
static void *macosxAllocatorMalloc(PHYSFS_uint64 s) |
|
311 |
{ |
|
312 |
if (!__PHYSFS_ui64FitsAddressSpace(s)) |
|
313 |
BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL); |
|
314 |
return CFAllocatorAllocate(cfallocdef, (CFIndex) s, 0); |
|
315 |
} /* macosxAllocatorMalloc */ |
|
316 |
||
317 |
||
318 |
static void *macosxAllocatorRealloc(void *ptr, PHYSFS_uint64 s) |
|
319 |
{ |
|
320 |
if (!__PHYSFS_ui64FitsAddressSpace(s)) |
|
321 |
BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL); |
|
322 |
return CFAllocatorReallocate(cfallocdef, ptr, (CFIndex) s, 0); |
|
323 |
} /* macosxAllocatorRealloc */ |
|
324 |
||
325 |
||
326 |
static void macosxAllocatorFree(void *ptr) |
|
327 |
{ |
|
328 |
CFAllocatorDeallocate(cfallocdef, ptr); |
|
329 |
} /* macosxAllocatorFree */ |
|
330 |
||
331 |
||
332 |
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a) |
|
333 |
{ |
|
334 |
allocator.Init = macosxAllocatorInit; |
|
335 |
allocator.Deinit = macosxAllocatorDeinit; |
|
336 |
allocator.Malloc = macosxAllocatorMalloc; |
|
337 |
allocator.Realloc = macosxAllocatorRealloc; |
|
338 |
allocator.Free = macosxAllocatorFree; |
|
339 |
return 1; /* return non-zero: we're supplying custom allocator. */ |
|
340 |
} /* __PHYSFS_platformSetDefaultAllocator */ |
|
341 |
||
342 |
#endif /* PHYSFS_PLATFORM_MACOSX */ |
|
343 |
||
344 |
/* end of macosx.c ... */ |
|
345 |