platform_beos.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * BeOS platform-dependent 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_BEOS
  11. #ifdef PHYSFS_PLATFORM_HAIKU
  12. #include <os/kernel/OS.h>
  13. #include <os/app/Roster.h>
  14. #include <os/storage/Volume.h>
  15. #include <os/storage/VolumeRoster.h>
  16. #include <os/storage/Directory.h>
  17. #include <os/storage/Entry.h>
  18. #include <os/storage/Path.h>
  19. #include <os/kernel/fs_info.h>
  20. #include <os/device/scsi.h>
  21. #include <os/support/Locker.h>
  22. #else
  23. #include <be/kernel/OS.h>
  24. #include <be/app/Roster.h>
  25. #include <be/storage/Volume.h>
  26. #include <be/storage/VolumeRoster.h>
  27. #include <be/storage/Directory.h>
  28. #include <be/storage/Entry.h>
  29. #include <be/storage/Path.h>
  30. #include <be/kernel/fs_info.h>
  31. #include <be/device/scsi.h>
  32. #include <be/support/Locker.h>
  33. #endif
  34. #include <errno.h>
  35. #include <unistd.h>
  36. #include "physfs_internal.h"
  37. int __PHYSFS_platformInit(void)
  38. {
  39. return 1; /* always succeed. */
  40. } /* __PHYSFS_platformInit */
  41. int __PHYSFS_platformDeinit(void)
  42. {
  43. return 1; /* always succeed. */
  44. } /* __PHYSFS_platformDeinit */
  45. static char *getMountPoint(const char *devname, char *buf, size_t bufsize)
  46. {
  47. BVolumeRoster mounts;
  48. BVolume vol;
  49. mounts.Rewind();
  50. while (mounts.GetNextVolume(&vol) == B_NO_ERROR)
  51. {
  52. fs_info fsinfo;
  53. fs_stat_dev(vol.Device(), &fsinfo);
  54. if (strcmp(devname, fsinfo.device_name) == 0)
  55. {
  56. BDirectory directory;
  57. BEntry entry;
  58. BPath path;
  59. const char *str;
  60. if ( (vol.GetRootDirectory(&directory) < B_OK) ||
  61. (directory.GetEntry(&entry) < B_OK) ||
  62. (entry.GetPath(&path) < B_OK) ||
  63. ( (str = path.Path()) == NULL) )
  64. return NULL;
  65. strncpy(buf, str, bufsize-1);
  66. buf[bufsize-1] = '\0';
  67. return buf;
  68. } /* if */
  69. } /* while */
  70. return NULL;
  71. } /* getMountPoint */
  72. /*
  73. * This function is lifted from Simple Directmedia Layer (SDL):
  74. * http://www.libsdl.org/ ... this is zlib-licensed code, too.
  75. */
  76. static void tryDir(const char *d, PHYSFS_StringCallback callback, void *data)
  77. {
  78. BDirectory dir;
  79. dir.SetTo(d);
  80. if (dir.InitCheck() != B_NO_ERROR)
  81. return;
  82. dir.Rewind();
  83. BEntry entry;
  84. while (dir.GetNextEntry(&entry) >= 0)
  85. {
  86. BPath path;
  87. const char *name;
  88. entry_ref e;
  89. if (entry.GetPath(&path) != B_NO_ERROR)
  90. continue;
  91. name = path.Path();
  92. if (entry.GetRef(&e) != B_NO_ERROR)
  93. continue;
  94. if (entry.IsDirectory())
  95. {
  96. if (strcmp(e.name, "floppy") != 0)
  97. tryDir(name, callback, data);
  98. continue;
  99. } /* if */
  100. if (strcmp(e.name, "raw") != 0) /* ignore partitions. */
  101. continue;
  102. const int devfd = open(name, O_RDONLY);
  103. if (devfd < 0)
  104. continue;
  105. device_geometry g;
  106. const int rc = ioctl(devfd, B_GET_GEOMETRY, &g, sizeof (g));
  107. close(devfd);
  108. if (rc < 0)
  109. continue;
  110. if (g.device_type != B_CD)
  111. continue;
  112. char mntpnt[B_FILE_NAME_LENGTH];
  113. if (getMountPoint(name, mntpnt, sizeof (mntpnt)))
  114. callback(data, mntpnt);
  115. } /* while */
  116. } /* tryDir */
  117. void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
  118. {
  119. tryDir("/dev/disk", cb, data);
  120. } /* __PHYSFS_platformDetectAvailableCDs */
  121. static team_id getTeamID(void)
  122. {
  123. thread_info info;
  124. thread_id tid = find_thread(NULL);
  125. get_thread_info(tid, &info);
  126. return info.team;
  127. } /* getTeamID */
  128. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  129. {
  130. image_info info;
  131. int32 cookie = 0;
  132. while (get_next_image_info(0, &cookie, &info) == B_OK)
  133. {
  134. if (info.type == B_APP_IMAGE)
  135. break;
  136. } /* while */
  137. BEntry entry(info.name, true);
  138. BPath path;
  139. status_t rc = entry.GetPath(&path); /* (path) now has binary's path. */
  140. assert(rc == B_OK);
  141. rc = path.GetParent(&path); /* chop filename, keep directory. */
  142. assert(rc == B_OK);
  143. const char *str = path.Path();
  144. assert(str != NULL);
  145. char *retval = (char *) allocator.Malloc(strlen(str) + 1);
  146. BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  147. strcpy(retval, str);
  148. return retval;
  149. } /* __PHYSFS_platformCalcBaseDir */
  150. char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
  151. {
  152. /* !!! FIXME: there's a real API to determine this */
  153. const char *userdir = __PHYSFS_getUserDir();
  154. const char *append = "config/settings/";
  155. const size_t len = strlen(userdir) + strlen(append) + strlen(app) + 1;
  156. char *retval = allocator.Malloc(len);
  157. BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  158. snprintf(retval, len, "%s%s%s", userdir, append, app);
  159. return retval;
  160. } /* __PHYSFS_platformCalcPrefDir */
  161. void *__PHYSFS_platformGetThreadID(void)
  162. {
  163. return (void *) find_thread(NULL);
  164. } /* __PHYSFS_platformGetThreadID */
  165. void *__PHYSFS_platformCreateMutex(void)
  166. {
  167. return new BLocker("PhysicsFS lock", true);
  168. } /* __PHYSFS_platformCreateMutex */
  169. void __PHYSFS_platformDestroyMutex(void *mutex)
  170. {
  171. delete ((BLocker *) mutex);
  172. } /* __PHYSFS_platformDestroyMutex */
  173. int __PHYSFS_platformGrabMutex(void *mutex)
  174. {
  175. return ((BLocker *) mutex)->Lock() ? 1 : 0;
  176. } /* __PHYSFS_platformGrabMutex */
  177. void __PHYSFS_platformReleaseMutex(void *mutex)
  178. {
  179. ((BLocker *) mutex)->Unlock();
  180. } /* __PHYSFS_platformReleaseMutex */
  181. int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
  182. {
  183. return 0; /* just use malloc() and friends. */
  184. } /* __PHYSFS_platformSetDefaultAllocator */
  185. #endif /* PHYSFS_PLATFORM_BEOS */
  186. /* end of beos.cpp ... */