macosx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. #define __PHYSICSFS_INTERNAL__
  9. #include "physfs_platforms.h"
  10. #ifdef PHYSFS_PLATFORM_MACOSX
  11. #include <Carbon/Carbon.h>
  12. #include <IOKit/storage/IOMedia.h>
  13. #include <IOKit/storage/IOCDMedia.h>
  14. #include <IOKit/storage/IODVDMedia.h>
  15. #include <sys/mount.h>
  16. #include <sys/stat.h>
  17. /* Seems to get defined in some system header... */
  18. #ifdef Free
  19. #undef Free
  20. #endif
  21. #include "physfs_internal.h"
  22. /* Wrap PHYSFS_Allocator in a CFAllocator... */
  23. static CFAllocatorRef cfallocator = NULL;
  24. CFStringRef cfallocDesc(const void *info)
  25. {
  26. return(CFStringCreateWithCString(cfallocator, "PhysicsFS",
  27. kCFStringEncodingASCII));
  28. } /* cfallocDesc */
  29. static void *cfallocMalloc(CFIndex allocSize, CFOptionFlags hint, void *info)
  30. {
  31. return allocator.Malloc(allocSize);
  32. } /* cfallocMalloc */
  33. static void cfallocFree(void *ptr, void *info)
  34. {
  35. allocator.Free(ptr);
  36. } /* cfallocFree */
  37. static void *cfallocRealloc(void *ptr, CFIndex newsize,
  38. CFOptionFlags hint, void *info)
  39. {
  40. if ((ptr == NULL) || (newsize <= 0))
  41. return NULL; /* ADC docs say you should always return NULL here. */
  42. return allocator.Realloc(ptr, newsize);
  43. } /* cfallocRealloc */
  44. int __PHYSFS_platformInit(void)
  45. {
  46. /* set up a CFAllocator, so Carbon can use the physfs allocator, too. */
  47. CFAllocatorContext ctx;
  48. memset(&ctx, '\0', sizeof (ctx));
  49. ctx.copyDescription = cfallocDesc;
  50. ctx.allocate = cfallocMalloc;
  51. ctx.reallocate = cfallocRealloc;
  52. ctx.deallocate = cfallocFree;
  53. cfallocator = CFAllocatorCreate(kCFAllocatorUseContext, &ctx);
  54. BAIL_IF_MACRO(cfallocator == NULL, ERR_OUT_OF_MEMORY, 0);
  55. return(1); /* success. */
  56. } /* __PHYSFS_platformInit */
  57. int __PHYSFS_platformDeinit(void)
  58. {
  59. CFRelease(cfallocator);
  60. cfallocator = NULL;
  61. return(1); /* always succeed. */
  62. } /* __PHYSFS_platformDeinit */
  63. /* CD-ROM detection code... */
  64. /*
  65. * Code based on sample from Apple Developer Connection:
  66. * http://developer.apple.com/samplecode/Sample_Code/Devices_and_Hardware/Disks/VolumeToBSDNode/VolumeToBSDNode.c.htm
  67. */
  68. static int darwinIsWholeMedia(io_service_t service)
  69. {
  70. int retval = 0;
  71. CFTypeRef wholeMedia;
  72. if (!IOObjectConformsTo(service, kIOMediaClass))
  73. return(0);
  74. wholeMedia = IORegistryEntryCreateCFProperty(service,
  75. CFSTR(kIOMediaWholeKey),
  76. cfallocator, 0);
  77. if (wholeMedia == NULL)
  78. return(0);
  79. retval = CFBooleanGetValue(wholeMedia);
  80. CFRelease(wholeMedia);
  81. return retval;
  82. } /* darwinIsWholeMedia */
  83. static int darwinIsMountedDisc(char *bsdName, mach_port_t masterPort)
  84. {
  85. int retval = 0;
  86. CFMutableDictionaryRef matchingDict;
  87. kern_return_t rc;
  88. io_iterator_t iter;
  89. io_service_t service;
  90. if ((matchingDict = IOBSDNameMatching(masterPort, 0, bsdName)) == NULL)
  91. return(0);
  92. rc = IOServiceGetMatchingServices(masterPort, matchingDict, &iter);
  93. if ((rc != KERN_SUCCESS) || (!iter))
  94. return(0);
  95. service = IOIteratorNext(iter);
  96. IOObjectRelease(iter);
  97. if (!service)
  98. return(0);
  99. rc = IORegistryEntryCreateIterator(service, kIOServicePlane,
  100. kIORegistryIterateRecursively | kIORegistryIterateParents, &iter);
  101. if (!iter)
  102. return(0);
  103. if (rc != KERN_SUCCESS)
  104. {
  105. IOObjectRelease(iter);
  106. return(0);
  107. } /* if */
  108. IOObjectRetain(service); /* add an extra object reference... */
  109. do
  110. {
  111. if (darwinIsWholeMedia(service))
  112. {
  113. if ( (IOObjectConformsTo(service, kIOCDMediaClass)) ||
  114. (IOObjectConformsTo(service, kIODVDMediaClass)) )
  115. {
  116. retval = 1;
  117. } /* if */
  118. } /* if */
  119. IOObjectRelease(service);
  120. } while ((service = IOIteratorNext(iter)) && (!retval));
  121. IOObjectRelease(iter);
  122. IOObjectRelease(service);
  123. return(retval);
  124. } /* darwinIsMountedDisc */
  125. void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
  126. {
  127. const char *devPrefix = "/dev/";
  128. const int prefixLen = strlen(devPrefix);
  129. mach_port_t masterPort = 0;
  130. struct statfs *mntbufp;
  131. int i, mounts;
  132. if (IOMasterPort(MACH_PORT_NULL, &masterPort) != KERN_SUCCESS)
  133. BAIL_MACRO(ERR_OS_ERROR, ) /*return void*/;
  134. mounts = getmntinfo(&mntbufp, MNT_WAIT); /* NOT THREAD SAFE! */
  135. for (i = 0; i < mounts; i++)
  136. {
  137. char *dev = mntbufp[i].f_mntfromname;
  138. char *mnt = mntbufp[i].f_mntonname;
  139. if (strncmp(dev, devPrefix, prefixLen) != 0) /* a virtual device? */
  140. continue;
  141. dev += prefixLen;
  142. if (darwinIsMountedDisc(dev, masterPort))
  143. cb(data, mnt);
  144. } /* for */
  145. } /* __PHYSFS_platformDetectAvailableCDs */
  146. static char *convertCFString(CFStringRef cfstr)
  147. {
  148. CFIndex len = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfstr),
  149. kCFStringEncodingUTF8) + 1;
  150. char *retval = (char *) allocator.Malloc(len);
  151. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  152. if (CFStringGetCString(cfstr, retval, len, kCFStringEncodingUTF8))
  153. {
  154. /* shrink overallocated buffer if possible... */
  155. CFIndex newlen = strlen(retval) + 1;
  156. if (newlen < len)
  157. {
  158. void *ptr = allocator.Realloc(retval, newlen);
  159. if (ptr != NULL)
  160. retval = (char *) ptr;
  161. } /* if */
  162. } /* if */
  163. else /* probably shouldn't fail, but just in case... */
  164. {
  165. allocator.Free(retval);
  166. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  167. } /* else */
  168. return(retval);
  169. } /* convertCFString */
  170. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  171. {
  172. ProcessSerialNumber psn = { 0, kCurrentProcess };
  173. struct stat statbuf;
  174. FSRef fsref;
  175. CFRange cfrange;
  176. CFURLRef cfurl = NULL;
  177. CFStringRef cfstr = NULL;
  178. CFMutableStringRef cfmutstr = NULL;
  179. char *retval = NULL;
  180. char *cstr = NULL;
  181. int rc = 0;
  182. BAIL_IF_MACRO(GetProcessBundleLocation(&psn, &fsref) != noErr, NULL, NULL);
  183. cfurl = CFURLCreateFromFSRef(cfallocator, &fsref);
  184. BAIL_IF_MACRO(cfurl == NULL, NULL, NULL);
  185. cfstr = CFURLCopyFileSystemPath(cfurl, kCFURLPOSIXPathStyle);
  186. CFRelease(cfurl);
  187. BAIL_IF_MACRO(cfstr == NULL, NULL, NULL);
  188. cfmutstr = CFStringCreateMutableCopy(cfallocator, 0, cfstr);
  189. CFRelease(cfstr);
  190. BAIL_IF_MACRO(cfmutstr == NULL, NULL, NULL);
  191. /* we have to decide if we got a binary's path, or the .app dir... */
  192. cstr = convertCFString(cfmutstr);
  193. if (cstr == NULL)
  194. {
  195. CFRelease(cfmutstr);
  196. return(NULL);
  197. } /* if */
  198. rc = stat(cstr, &statbuf);
  199. allocator.Free(cstr); /* done with this. */
  200. if (rc == -1)
  201. {
  202. CFRelease(cfmutstr);
  203. return(NULL); /* maybe default behaviour will work? */
  204. } /* if */
  205. if (S_ISREG(statbuf.st_mode))
  206. {
  207. /* Find last dirsep so we can chop the filename from the path. */
  208. cfrange = CFStringFind(cfmutstr, CFSTR("/"), kCFCompareBackwards);
  209. if (cfrange.location == kCFNotFound)
  210. {
  211. assert(0); /* shouldn't ever hit this... */
  212. CFRelease(cfmutstr);
  213. return(NULL);
  214. } /* if */
  215. /* chop the "/exename" from the end of the path string... */
  216. cfrange.length = CFStringGetLength(cfmutstr) - cfrange.location;
  217. CFStringDelete(cfmutstr, cfrange);
  218. /* If we're an Application Bundle, chop everything but the base. */
  219. cfrange = CFStringFind(cfmutstr, CFSTR("/Contents/MacOS"),
  220. kCFCompareCaseInsensitive |
  221. kCFCompareBackwards |
  222. kCFCompareAnchored);
  223. if (cfrange.location != kCFNotFound)
  224. CFStringDelete(cfmutstr, cfrange); /* chop that, too. */
  225. } /* if */
  226. retval = convertCFString(cfmutstr);
  227. CFRelease(cfmutstr);
  228. return(retval); /* whew. */
  229. } /* __PHYSFS_platformCalcBaseDir */
  230. /* !!! FIXME */
  231. #define osxerr(x) x
  232. char *__PHYSFS_platformRealPath(const char *path)
  233. {
  234. /* The symlink and relative path resolving happens in FSPathMakeRef() */
  235. FSRef fsref;
  236. CFURLRef cfurl = NULL;
  237. CFStringRef cfstr = NULL;
  238. char *retval = NULL;
  239. OSStatus rc = osxerr(FSPathMakeRef((UInt8 *) path, &fsref, NULL));
  240. BAIL_IF_MACRO(rc != noErr, NULL, NULL);
  241. /* Now get it to spit out a full path. */
  242. cfurl = CFURLCreateFromFSRef(cfallocator, &fsref);
  243. BAIL_IF_MACRO(cfurl == NULL, ERR_OUT_OF_MEMORY, NULL);
  244. cfstr = CFURLCopyFileSystemPath(cfurl, kCFURLPOSIXPathStyle);
  245. CFRelease(cfurl);
  246. BAIL_IF_MACRO(cfstr == NULL, ERR_OUT_OF_MEMORY, NULL);
  247. retval = convertCFString(cfstr);
  248. CFRelease(cfstr);
  249. return(retval);
  250. } /* __PHYSFS_platformRealPath */
  251. char *__PHYSFS_platformCurrentDir(void)
  252. {
  253. return(__PHYSFS_platformRealPath(".")); /* let CFURL sort it out. */
  254. } /* __PHYSFS_platformCurrentDir */
  255. /* Platform allocator uses default CFAllocator at PHYSFS_init() time. */
  256. static CFAllocatorRef cfallocdef = NULL;
  257. static int macosxAllocatorInit(void)
  258. {
  259. int retval = 0;
  260. cfallocdef = CFAllocatorGetDefault();
  261. retval = (cfallocdef != NULL);
  262. if (retval)
  263. CFRetain(cfallocdef);
  264. return(retval);
  265. } /* macosxAllocatorInit */
  266. static void macosxAllocatorDeinit(void)
  267. {
  268. if (cfallocdef != NULL)
  269. {
  270. CFRelease(cfallocdef);
  271. cfallocdef = NULL;
  272. } /* if */
  273. } /* macosxAllocatorDeinit */
  274. static void *macosxAllocatorMalloc(PHYSFS_uint64 s)
  275. {
  276. BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
  277. return(CFAllocatorAllocate(cfallocdef, (CFIndex) s, 0));
  278. } /* macosxAllocatorMalloc */
  279. static void *macosxAllocatorRealloc(void *ptr, PHYSFS_uint64 s)
  280. {
  281. BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
  282. return(CFAllocatorReallocate(cfallocdef, ptr, (CFIndex) s, 0));
  283. } /* macosxAllocatorRealloc */
  284. static void macosxAllocatorFree(void *ptr)
  285. {
  286. CFAllocatorDeallocate(cfallocdef, ptr);
  287. } /* macosxAllocatorFree */
  288. int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
  289. {
  290. allocator.Init = macosxAllocatorInit;
  291. allocator.Deinit = macosxAllocatorDeinit;
  292. allocator.Malloc = macosxAllocatorMalloc;
  293. allocator.Realloc = macosxAllocatorRealloc;
  294. allocator.Free = macosxAllocatorFree;
  295. return(1); /* return non-zero: we're supplying custom allocator. */
  296. } /* __PHYSFS_platformSetDefaultAllocator */
  297. void *__PHYSFS_platformGetThreadID(void)
  298. {
  299. return( (void *) ((size_t) MPCurrentTaskID()) );
  300. } /* __PHYSFS_platformGetThreadID */
  301. void *__PHYSFS_platformCreateMutex(void)
  302. {
  303. MPCriticalRegionID m = NULL;
  304. if (osxerr(MPCreateCriticalRegion(&m)) != noErr)
  305. return NULL;
  306. return m;
  307. } /* __PHYSFS_platformCreateMutex */
  308. void __PHYSFS_platformDestroyMutex(void *mutex)
  309. {
  310. MPCriticalRegionID m = (MPCriticalRegionID) mutex;
  311. MPDeleteCriticalRegion(m);
  312. } /* __PHYSFS_platformDestroyMutex */
  313. int __PHYSFS_platformGrabMutex(void *mutex)
  314. {
  315. MPCriticalRegionID m = (MPCriticalRegionID) mutex;
  316. if (MPEnterCriticalRegion(m, kDurationForever) != noErr)
  317. return(0);
  318. return(1);
  319. } /* __PHYSFS_platformGrabMutex */
  320. void __PHYSFS_platformReleaseMutex(void *mutex)
  321. {
  322. MPCriticalRegionID m = (MPCriticalRegionID) mutex;
  323. MPExitCriticalRegion(m);
  324. } /* __PHYSFS_platformReleaseMutex */
  325. #endif /* PHYSFS_PLATFORM_MACOSX */
  326. /* end of macosx.c ... */