Skip to content

Latest commit

 

History

History
381 lines (298 loc) · 10.6 KB

platform_macosx.c

File metadata and controls

381 lines (298 loc) · 10.6 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
* Mac OS X support routines for PhysicsFS.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#define __PHYSICSFS_INTERNAL__
#include "physfs_platforms.h"
#ifdef PHYSFS_PLATFORM_MACOSX
#include <Carbon/Carbon.h>
#include <IOKit/storage/IOMedia.h>
#include <IOKit/storage/IOCDMedia.h>
#include <IOKit/storage/IODVDMedia.h>
#include <sys/mount.h>
Jul 10, 2011
Jul 10, 2011
19
#include <sys/stat.h>
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
/* Seems to get defined in some system header... */
#ifdef Free
#undef Free
#endif
#include "physfs_internal.h"
/* Wrap PHYSFS_Allocator in a CFAllocator... */
static CFAllocatorRef cfallocator = NULL;
CFStringRef cfallocDesc(const void *info)
{
return(CFStringCreateWithCString(cfallocator, "PhysicsFS",
kCFStringEncodingASCII));
} /* cfallocDesc */
static void *cfallocMalloc(CFIndex allocSize, CFOptionFlags hint, void *info)
{
return allocator.Malloc(allocSize);
} /* cfallocMalloc */
static void cfallocFree(void *ptr, void *info)
{
allocator.Free(ptr);
} /* cfallocFree */
static void *cfallocRealloc(void *ptr, CFIndex newsize,
CFOptionFlags hint, void *info)
{
if ((ptr == NULL) || (newsize <= 0))
return NULL; /* ADC docs say you should always return NULL here. */
return allocator.Realloc(ptr, newsize);
} /* cfallocRealloc */
int __PHYSFS_platformInit(void)
{
/* set up a CFAllocator, so Carbon can use the physfs allocator, too. */
CFAllocatorContext ctx;
memset(&ctx, '\0', sizeof (ctx));
ctx.copyDescription = cfallocDesc;
ctx.allocate = cfallocMalloc;
ctx.reallocate = cfallocRealloc;
ctx.deallocate = cfallocFree;
cfallocator = CFAllocatorCreate(kCFAllocatorUseContext, &ctx);
BAIL_IF_MACRO(cfallocator == NULL, ERR_OUT_OF_MEMORY, 0);
Jan 28, 2010
Jan 28, 2010
71
return 1; /* success. */
72
73
74
75
76
77
78
} /* __PHYSFS_platformInit */
int __PHYSFS_platformDeinit(void)
{
CFRelease(cfallocator);
cfallocator = NULL;
Jan 28, 2010
Jan 28, 2010
79
return 1; /* always succeed. */
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
} /* __PHYSFS_platformDeinit */
/* CD-ROM detection code... */
/*
* Code based on sample from Apple Developer Connection:
* http://developer.apple.com/samplecode/Sample_Code/Devices_and_Hardware/Disks/VolumeToBSDNode/VolumeToBSDNode.c.htm
*/
static int darwinIsWholeMedia(io_service_t service)
{
int retval = 0;
CFTypeRef wholeMedia;
if (!IOObjectConformsTo(service, kIOMediaClass))
Jan 28, 2010
Jan 28, 2010
96
return 0;
97
98
99
100
101
wholeMedia = IORegistryEntryCreateCFProperty(service,
CFSTR(kIOMediaWholeKey),
cfallocator, 0);
if (wholeMedia == NULL)
Jan 28, 2010
Jan 28, 2010
102
return 0;
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
retval = CFBooleanGetValue(wholeMedia);
CFRelease(wholeMedia);
return retval;
} /* darwinIsWholeMedia */
static int darwinIsMountedDisc(char *bsdName, mach_port_t masterPort)
{
int retval = 0;
CFMutableDictionaryRef matchingDict;
kern_return_t rc;
io_iterator_t iter;
io_service_t service;
if ((matchingDict = IOBSDNameMatching(masterPort, 0, bsdName)) == NULL)
Jan 28, 2010
Jan 28, 2010
120
return 0;
121
122
123
rc = IOServiceGetMatchingServices(masterPort, matchingDict, &iter);
if ((rc != KERN_SUCCESS) || (!iter))
Jan 28, 2010
Jan 28, 2010
124
return 0;
125
126
127
128
service = IOIteratorNext(iter);
IOObjectRelease(iter);
if (!service)
Jan 28, 2010
Jan 28, 2010
129
return 0;
130
131
132
133
134
rc = IORegistryEntryCreateIterator(service, kIOServicePlane,
kIORegistryIterateRecursively | kIORegistryIterateParents, &iter);
if (!iter)
Jan 28, 2010
Jan 28, 2010
135
return 0;
136
137
138
139
if (rc != KERN_SUCCESS)
{
IOObjectRelease(iter);
Jan 28, 2010
Jan 28, 2010
140
return 0;
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
} /* if */
IOObjectRetain(service); /* add an extra object reference... */
do
{
if (darwinIsWholeMedia(service))
{
if ( (IOObjectConformsTo(service, kIOCDMediaClass)) ||
(IOObjectConformsTo(service, kIODVDMediaClass)) )
{
retval = 1;
} /* if */
} /* if */
IOObjectRelease(service);
} while ((service = IOIteratorNext(iter)) && (!retval));
IOObjectRelease(iter);
IOObjectRelease(service);
Jan 28, 2010
Jan 28, 2010
161
return retval;
162
163
164
165
166
167
168
169
170
171
172
173
} /* darwinIsMountedDisc */
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
{
const char *devPrefix = "/dev/";
const int prefixLen = strlen(devPrefix);
mach_port_t masterPort = 0;
struct statfs *mntbufp;
int i, mounts;
if (IOMasterPort(MACH_PORT_NULL, &masterPort) != KERN_SUCCESS)
Mar 29, 2007
Mar 29, 2007
174
BAIL_MACRO(ERR_OS_ERROR, ) /*return void*/;
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
mounts = getmntinfo(&mntbufp, MNT_WAIT); /* NOT THREAD SAFE! */
for (i = 0; i < mounts; i++)
{
char *dev = mntbufp[i].f_mntfromname;
char *mnt = mntbufp[i].f_mntonname;
if (strncmp(dev, devPrefix, prefixLen) != 0) /* a virtual device? */
continue;
dev += prefixLen;
if (darwinIsMountedDisc(dev, masterPort))
cb(data, mnt);
} /* for */
} /* __PHYSFS_platformDetectAvailableCDs */
static char *convertCFString(CFStringRef cfstr)
{
CFIndex len = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfstr),
kCFStringEncodingUTF8) + 1;
char *retval = (char *) allocator.Malloc(len);
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
if (CFStringGetCString(cfstr, retval, len, kCFStringEncodingUTF8))
{
/* shrink overallocated buffer if possible... */
CFIndex newlen = strlen(retval) + 1;
if (newlen < len)
{
void *ptr = allocator.Realloc(retval, newlen);
if (ptr != NULL)
retval = (char *) ptr;
} /* if */
} /* if */
else /* probably shouldn't fail, but just in case... */
{
allocator.Free(retval);
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
} /* else */
Jan 28, 2010
Jan 28, 2010
216
return retval;
217
218
219
220
221
222
} /* convertCFString */
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
ProcessSerialNumber psn = { 0, kCurrentProcess };
Jul 10, 2011
Jul 10, 2011
223
struct stat statbuf;
224
225
226
227
228
229
FSRef fsref;
CFRange cfrange;
CFURLRef cfurl = NULL;
CFStringRef cfstr = NULL;
CFMutableStringRef cfmutstr = NULL;
char *retval = NULL;
Jul 10, 2011
Jul 10, 2011
230
231
char *cstr = NULL;
int rc = 0;
232
233
234
235
236
237
238
239
240
241
242
BAIL_IF_MACRO(GetProcessBundleLocation(&psn, &fsref) != noErr, NULL, NULL);
cfurl = CFURLCreateFromFSRef(cfallocator, &fsref);
BAIL_IF_MACRO(cfurl == NULL, NULL, NULL);
cfstr = CFURLCopyFileSystemPath(cfurl, kCFURLPOSIXPathStyle);
CFRelease(cfurl);
BAIL_IF_MACRO(cfstr == NULL, NULL, NULL);
cfmutstr = CFStringCreateMutableCopy(cfallocator, 0, cfstr);
CFRelease(cfstr);
BAIL_IF_MACRO(cfmutstr == NULL, NULL, NULL);
Jul 10, 2011
Jul 10, 2011
243
244
245
/* we have to decide if we got a binary's path, or the .app dir... */
cstr = convertCFString(cfmutstr);
if (cstr == NULL)
246
247
{
CFRelease(cfmutstr);
Jan 28, 2010
Jan 28, 2010
248
return NULL;
Jul 10, 2011
Jul 10, 2011
251
252
253
254
255
256
257
rc = stat(cstr, &statbuf);
allocator.Free(cstr); /* done with this. */
if (rc == -1)
{
CFRelease(cfmutstr);
return NULL; /* maybe default behaviour will work? */
} /* if */
Jul 10, 2011
Jul 10, 2011
259
260
261
262
263
264
265
266
267
268
269
270
271
272
if (S_ISREG(statbuf.st_mode))
{
/* Find last dirsep so we can chop the filename from the path. */
cfrange = CFStringFind(cfmutstr, CFSTR("/"), kCFCompareBackwards);
if (cfrange.location == kCFNotFound)
{
assert(0); /* shouldn't ever hit this... */
CFRelease(cfmutstr);
return NULL;
} /* if */
/* chop the "/exename" from the end of the path string... */
cfrange.length = CFStringGetLength(cfmutstr) - cfrange.location;
CFStringDelete(cfmutstr, cfrange);
Jul 10, 2011
Jul 10, 2011
274
275
276
277
278
279
280
281
282
/* If we're an Application Bundle, chop everything but the base. */
cfrange = CFStringFind(cfmutstr, CFSTR("/Contents/MacOS"),
kCFCompareCaseInsensitive |
kCFCompareBackwards |
kCFCompareAnchored);
if (cfrange.location != kCFNotFound)
CFStringDelete(cfmutstr, cfrange); /* chop that, too. */
} /* if */
283
284
285
286
retval = convertCFString(cfmutstr);
CFRelease(cfmutstr);
Jan 28, 2010
Jan 28, 2010
287
return retval; /* whew. */
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
} /* __PHYSFS_platformCalcBaseDir */
/* !!! FIXME */
#define osxerr(x) x
char *__PHYSFS_platformRealPath(const char *path)
{
/* The symlink and relative path resolving happens in FSPathMakeRef() */
FSRef fsref;
CFURLRef cfurl = NULL;
CFStringRef cfstr = NULL;
char *retval = NULL;
OSStatus rc = osxerr(FSPathMakeRef((UInt8 *) path, &fsref, NULL));
BAIL_IF_MACRO(rc != noErr, NULL, NULL);
/* Now get it to spit out a full path. */
cfurl = CFURLCreateFromFSRef(cfallocator, &fsref);
BAIL_IF_MACRO(cfurl == NULL, ERR_OUT_OF_MEMORY, NULL);
cfstr = CFURLCopyFileSystemPath(cfurl, kCFURLPOSIXPathStyle);
CFRelease(cfurl);
BAIL_IF_MACRO(cfstr == NULL, ERR_OUT_OF_MEMORY, NULL);
retval = convertCFString(cfstr);
CFRelease(cfstr);
Jan 28, 2010
Jan 28, 2010
313
return retval;
314
315
316
317
318
} /* __PHYSFS_platformRealPath */
char *__PHYSFS_platformCurrentDir(void)
{
Jan 28, 2010
Jan 28, 2010
319
return __PHYSFS_platformRealPath("."); /* let CFURL sort it out. */
320
321
322
323
324
325
326
327
328
329
330
331
332
333
} /* __PHYSFS_platformCurrentDir */
/* Platform allocator uses default CFAllocator at PHYSFS_init() time. */
static CFAllocatorRef cfallocdef = NULL;
static int macosxAllocatorInit(void)
{
int retval = 0;
cfallocdef = CFAllocatorGetDefault();
retval = (cfallocdef != NULL);
if (retval)
CFRetain(cfallocdef);
Jan 28, 2010
Jan 28, 2010
334
return retval;
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
} /* macosxAllocatorInit */
static void macosxAllocatorDeinit(void)
{
if (cfallocdef != NULL)
{
CFRelease(cfallocdef);
cfallocdef = NULL;
} /* if */
} /* macosxAllocatorDeinit */
static void *macosxAllocatorMalloc(PHYSFS_uint64 s)
{
Aug 24, 2010
Aug 24, 2010
350
BAIL_IF_MACRO(!__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
Jan 28, 2010
Jan 28, 2010
351
return CFAllocatorAllocate(cfallocdef, (CFIndex) s, 0);
352
353
354
355
356
} /* macosxAllocatorMalloc */
static void *macosxAllocatorRealloc(void *ptr, PHYSFS_uint64 s)
{
Aug 24, 2010
Aug 24, 2010
357
BAIL_IF_MACRO(!__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
Jan 28, 2010
Jan 28, 2010
358
return CFAllocatorReallocate(cfallocdef, ptr, (CFIndex) s, 0);
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
} /* macosxAllocatorRealloc */
static void macosxAllocatorFree(void *ptr)
{
CFAllocatorDeallocate(cfallocdef, ptr);
} /* macosxAllocatorFree */
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
{
allocator.Init = macosxAllocatorInit;
allocator.Deinit = macosxAllocatorDeinit;
allocator.Malloc = macosxAllocatorMalloc;
allocator.Realloc = macosxAllocatorRealloc;
allocator.Free = macosxAllocatorFree;
Jan 28, 2010
Jan 28, 2010
375
return 1; /* return non-zero: we're supplying custom allocator. */
376
377
378
379
380
} /* __PHYSFS_platformSetDefaultAllocator */
#endif /* PHYSFS_PLATFORM_MACOSX */
/* end of macosx.c ... */