2007-03-21 06:03:17 +01:00
|
|
|
/*
|
|
|
|
* 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__
|
2017-07-12 05:47:48 +02:00
|
|
|
#include "physfs_internal.h"
|
2007-03-21 06:03:17 +01:00
|
|
|
|
2017-07-22 19:50:40 +02:00
|
|
|
#ifdef PHYSFS_PLATFORM_MACOS
|
2007-03-21 06:03:17 +01:00
|
|
|
|
2012-06-25 20:40:03 +02:00
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
|
|
|
|
#if !defined(PHYSFS_NO_CDROM_SUPPORT)
|
2017-07-12 05:47:48 +02:00
|
|
|
#include <IOKit/IOKitLib.h>
|
2007-03-21 06:03:17 +01:00
|
|
|
#include <IOKit/storage/IOMedia.h>
|
|
|
|
#include <IOKit/storage/IOCDMedia.h>
|
|
|
|
#include <IOKit/storage/IODVDMedia.h>
|
|
|
|
#include <sys/mount.h>
|
2012-06-25 20:40:03 +02:00
|
|
|
#endif
|
|
|
|
|
2007-03-21 06:03:17 +01:00
|
|
|
int __PHYSFS_platformInit(void)
|
|
|
|
{
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1; /* success. */
|
2007-03-21 06:03:17 +01:00
|
|
|
} /* __PHYSFS_platformInit */
|
|
|
|
|
|
|
|
|
|
|
|
int __PHYSFS_platformDeinit(void)
|
|
|
|
{
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1; /* always succeed. */
|
2007-03-21 06:03:17 +01:00
|
|
|
} /* __PHYSFS_platformDeinit */
|
|
|
|
|
|
|
|
|
2012-06-25 20:40:03 +02:00
|
|
|
|
2007-03-21 06:03:17 +01:00
|
|
|
/* CD-ROM detection code... */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Code based on sample from Apple Developer Connection:
|
2016-02-25 08:51:28 +01:00
|
|
|
* https://developer.apple.com/samplecode/Sample_Code/Devices_and_Hardware/Disks/VolumeToBSDNode/VolumeToBSDNode.c.htm
|
2007-03-21 06:03:17 +01:00
|
|
|
*/
|
|
|
|
|
2012-06-25 20:40:03 +02:00
|
|
|
#if !defined(PHYSFS_NO_CDROM_SUPPORT)
|
|
|
|
|
2007-03-21 06:03:17 +01:00
|
|
|
static int darwinIsWholeMedia(io_service_t service)
|
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
CFTypeRef wholeMedia;
|
|
|
|
|
|
|
|
if (!IOObjectConformsTo(service, kIOMediaClass))
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0;
|
2007-03-21 06:03:17 +01:00
|
|
|
|
|
|
|
wholeMedia = IORegistryEntryCreateCFProperty(service,
|
|
|
|
CFSTR(kIOMediaWholeKey),
|
2017-07-12 05:47:48 +02:00
|
|
|
NULL, 0);
|
2007-03-21 06:03:17 +01:00
|
|
|
if (wholeMedia == NULL)
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0;
|
2007-03-21 06:03:17 +01:00
|
|
|
|
|
|
|
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)
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0;
|
2007-03-21 06:03:17 +01:00
|
|
|
|
|
|
|
rc = IOServiceGetMatchingServices(masterPort, matchingDict, &iter);
|
|
|
|
if ((rc != KERN_SUCCESS) || (!iter))
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0;
|
2007-03-21 06:03:17 +01:00
|
|
|
|
|
|
|
service = IOIteratorNext(iter);
|
|
|
|
IOObjectRelease(iter);
|
|
|
|
if (!service)
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0;
|
2007-03-21 06:03:17 +01:00
|
|
|
|
|
|
|
rc = IORegistryEntryCreateIterator(service, kIOServicePlane,
|
|
|
|
kIORegistryIterateRecursively | kIORegistryIterateParents, &iter);
|
|
|
|
|
|
|
|
if (!iter)
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0;
|
2007-03-21 06:03:17 +01:00
|
|
|
|
|
|
|
if (rc != KERN_SUCCESS)
|
|
|
|
{
|
|
|
|
IOObjectRelease(iter);
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0;
|
2007-03-21 06:03:17 +01:00
|
|
|
} /* 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);
|
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2007-03-21 06:03:17 +01:00
|
|
|
} /* darwinIsMountedDisc */
|
|
|
|
|
2012-06-25 20:40:03 +02:00
|
|
|
#endif /* !defined(PHYSFS_NO_CDROM_SUPPORT) */
|
|
|
|
|
2007-03-21 06:03:17 +01:00
|
|
|
|
|
|
|
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
|
|
|
|
{
|
2012-06-25 20:40:03 +02:00
|
|
|
#if !defined(PHYSFS_NO_CDROM_SUPPORT)
|
2007-03-21 06:03:17 +01:00
|
|
|
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)
|
2017-07-06 17:51:41 +02:00
|
|
|
BAIL(PHYSFS_ERR_OS_ERROR, ) /*return void*/;
|
2007-03-21 06:03:17 +01:00
|
|
|
|
|
|
|
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 */
|
2012-06-25 20:40:03 +02:00
|
|
|
#endif /* !defined(PHYSFS_NO_CDROM_SUPPORT) */
|
2007-03-21 06:03:17 +01:00
|
|
|
} /* __PHYSFS_platformDetectAvailableCDs */
|
|
|
|
|
|
|
|
|
|
|
|
static char *convertCFString(CFStringRef cfstr)
|
|
|
|
{
|
|
|
|
CFIndex len = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfstr),
|
|
|
|
kCFStringEncodingUTF8) + 1;
|
|
|
|
char *retval = (char *) allocator.Malloc(len);
|
2017-07-06 17:51:41 +02:00
|
|
|
BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
2007-03-21 06:03:17 +01:00
|
|
|
|
|
|
|
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);
|
2017-07-06 17:51:41 +02:00
|
|
|
BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
2007-03-21 06:03:17 +01:00
|
|
|
} /* else */
|
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2007-03-21 06:03:17 +01:00
|
|
|
} /* convertCFString */
|
|
|
|
|
|
|
|
|
|
|
|
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
|
|
|
|
{
|
|
|
|
CFURLRef cfurl = NULL;
|
|
|
|
CFStringRef cfstr = NULL;
|
|
|
|
CFMutableStringRef cfmutstr = NULL;
|
|
|
|
char *retval = NULL;
|
|
|
|
|
2012-06-25 20:40:03 +02:00
|
|
|
cfurl = CFBundleCopyBundleURL(CFBundleGetMainBundle());
|
2017-07-06 17:51:41 +02:00
|
|
|
BAIL_IF(cfurl == NULL, PHYSFS_ERR_OS_ERROR, NULL);
|
2007-03-21 06:03:17 +01:00
|
|
|
cfstr = CFURLCopyFileSystemPath(cfurl, kCFURLPOSIXPathStyle);
|
|
|
|
CFRelease(cfurl);
|
2017-07-06 17:51:41 +02:00
|
|
|
BAIL_IF(!cfstr, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
2017-07-12 05:47:48 +02:00
|
|
|
cfmutstr = CFStringCreateMutableCopy(NULL, 0, cfstr);
|
2007-03-21 06:03:17 +01:00
|
|
|
CFRelease(cfstr);
|
2017-07-06 17:51:41 +02:00
|
|
|
BAIL_IF(!cfmutstr, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
2012-06-25 21:50:20 +02:00
|
|
|
CFStringAppendCString(cfmutstr, "/", kCFStringEncodingUTF8);
|
2007-03-21 06:03:17 +01:00
|
|
|
retval = convertCFString(cfmutstr);
|
|
|
|
CFRelease(cfmutstr);
|
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval; /* whew. */
|
2007-03-21 06:03:17 +01:00
|
|
|
} /* __PHYSFS_platformCalcBaseDir */
|
|
|
|
|
|
|
|
|
2012-03-22 04:30:50 +01:00
|
|
|
char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
|
|
|
|
{
|
|
|
|
/* !!! FIXME: there's a real API to determine this */
|
|
|
|
const char *userdir = __PHYSFS_getUserDir();
|
|
|
|
const char *append = "Library/Application Support/";
|
2012-03-22 04:59:43 +01:00
|
|
|
const size_t len = strlen(userdir) + strlen(append) + strlen(app) + 2;
|
2012-03-22 04:30:50 +01:00
|
|
|
char *retval = allocator.Malloc(len);
|
2017-07-06 17:51:41 +02:00
|
|
|
BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
2012-03-22 04:59:43 +01:00
|
|
|
snprintf(retval, len, "%s%s%s/", userdir, append, app);
|
2012-03-22 04:30:50 +01:00
|
|
|
return retval;
|
|
|
|
} /* __PHYSFS_platformCalcPrefDir */
|
|
|
|
|
2017-07-22 19:50:40 +02:00
|
|
|
#endif /* PHYSFS_PLATFORM_MACOS */
|
2007-03-21 06:03:17 +01:00
|
|
|
|
2017-07-22 19:19:57 +02:00
|
|
|
/* end of physfs_platform_macos.c ... */
|
2007-03-21 06:03:17 +01:00
|
|
|
|