Skip to content

Latest commit

 

History

History
190 lines (148 loc) · 5.31 KB

physfs_platform_apple.m

File metadata and controls

190 lines (148 loc) · 5.31 KB
 
Aug 8, 2017
Aug 8, 2017
2
* Apple platform (macOS, iOS, watchOS, etc) support routines for PhysicsFS.
3
4
5
6
7
8
9
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#define __PHYSICSFS_INTERNAL__
Jul 12, 2017
Jul 12, 2017
10
#include "physfs_internal.h"
Aug 8, 2017
Aug 8, 2017
12
#ifdef PHYSFS_PLATFORM_APPLE
Aug 8, 2017
Aug 8, 2017
14
15
16
/* Foundation.h steps on these. :( */
#undef malloc
#undef free
Jun 25, 2012
Jun 25, 2012
17
Aug 8, 2017
Aug 8, 2017
18
#include <Foundation/Foundation.h>
Jun 25, 2012
Jun 25, 2012
19
20
21
int __PHYSFS_platformInit(void)
{
Jan 28, 2010
Jan 28, 2010
22
return 1; /* success. */
23
24
25
} /* __PHYSFS_platformInit */
Aug 6, 2017
Aug 6, 2017
26
void __PHYSFS_platformDeinit(void)
Aug 6, 2017
Aug 6, 2017
28
/* no-op */
29
30
31
} /* __PHYSFS_platformDeinit */
Aug 8, 2017
Aug 8, 2017
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
@autoreleasepool
{
NSString *path = [[NSBundle mainBundle] bundlePath];
BAIL_IF(!path, PHYSFS_ERR_OS_ERROR, NULL);
size_t len = [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
char *retval = (char *) allocator.Malloc(len + 2);
BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
[path getCString:retval maxLength:len+1 encoding:NSUTF8StringEncoding];
retval[len] = '/';
retval[len+1] = '\0';
return retval; /* whew. */
} /* @autoreleasepool */
} /* __PHYSFS_platformCalcBaseDir */
char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
{
@autoreleasepool
{
Aug 8, 2017
Aug 8, 2017
53
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, TRUE);
Aug 8, 2017
Aug 8, 2017
54
BAIL_IF(!paths, PHYSFS_ERR_OS_ERROR, NULL);
Aug 8, 2017
Aug 8, 2017
55
NSString *path = (NSString *) paths[0];
Aug 8, 2017
Aug 8, 2017
56
57
58
59
60
61
62
63
64
65
66
BAIL_IF(!path, PHYSFS_ERR_OS_ERROR, NULL);
size_t len = [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
const size_t applen = strlen(app);
char *retval = (char *) allocator.Malloc(len + applen + 3);
BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
[path getCString:retval maxLength:len+1 encoding:NSUTF8StringEncoding];
snprintf(retval + len, applen + 3, "/%s/", app);
return retval; /* whew. */
} /* @autoreleasepool */
} /* __PHYSFS_platformCalcPrefDir */
Jun 25, 2012
Jun 25, 2012
67
68
69
70
71
/* CD-ROM detection code... */
/*
* Code based on sample from Apple Developer Connection:
Feb 25, 2016
Feb 25, 2016
72
* https://developer.apple.com/samplecode/Sample_Code/Devices_and_Hardware/Disks/VolumeToBSDNode/VolumeToBSDNode.c.htm
Jun 25, 2012
Jun 25, 2012
75
76
#if !defined(PHYSFS_NO_CDROM_SUPPORT)
Aug 8, 2017
Aug 8, 2017
77
78
79
80
81
82
#include <IOKit/IOKitLib.h>
#include <IOKit/storage/IOMedia.h>
#include <IOKit/storage/IOCDMedia.h>
#include <IOKit/storage/IODVDMedia.h>
#include <sys/mount.h>
83
84
85
86
87
88
static int darwinIsWholeMedia(io_service_t service)
{
int retval = 0;
CFTypeRef wholeMedia;
if (!IOObjectConformsTo(service, kIOMediaClass))
Jan 28, 2010
Jan 28, 2010
89
return 0;
90
91
92
wholeMedia = IORegistryEntryCreateCFProperty(service,
CFSTR(kIOMediaWholeKey),
Jul 12, 2017
Jul 12, 2017
93
NULL, 0);
94
if (wholeMedia == NULL)
Jan 28, 2010
Jan 28, 2010
95
return 0;
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
113
return 0;
114
115
116
rc = IOServiceGetMatchingServices(masterPort, matchingDict, &iter);
if ((rc != KERN_SUCCESS) || (!iter))
Jan 28, 2010
Jan 28, 2010
117
return 0;
118
119
120
121
service = IOIteratorNext(iter);
IOObjectRelease(iter);
if (!service)
Jan 28, 2010
Jan 28, 2010
122
return 0;
123
124
125
126
127
rc = IORegistryEntryCreateIterator(service, kIOServicePlane,
kIORegistryIterateRecursively | kIORegistryIterateParents, &iter);
if (!iter)
Jan 28, 2010
Jan 28, 2010
128
return 0;
129
130
131
132
if (rc != KERN_SUCCESS)
{
IOObjectRelease(iter);
Jan 28, 2010
Jan 28, 2010
133
return 0;
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
} /* 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
154
return retval;
155
156
} /* darwinIsMountedDisc */
Jun 25, 2012
Jun 25, 2012
157
158
#endif /* !defined(PHYSFS_NO_CDROM_SUPPORT) */
159
160
161
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
{
Jun 25, 2012
Jun 25, 2012
162
#if !defined(PHYSFS_NO_CDROM_SUPPORT)
163
164
165
166
167
168
169
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)
Jul 6, 2017
Jul 6, 2017
170
BAIL(PHYSFS_ERR_OS_ERROR, ) /*return void*/;
171
172
173
174
175
176
177
178
179
180
181
182
183
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 */
Jun 25, 2012
Jun 25, 2012
184
#endif /* !defined(PHYSFS_NO_CDROM_SUPPORT) */
185
186
} /* __PHYSFS_platformDetectAvailableCDs */
Aug 8, 2017
Aug 8, 2017
187
#endif /* PHYSFS_PLATFORM_APPLE */
Aug 8, 2017
Aug 8, 2017
189
/* end of physfs_platform_apple.m ... */