platform_beos.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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)
  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. //char buf[B_FILE_NAME_LENGTH];
  57. BDirectory directory;
  58. BEntry entry;
  59. BPath path;
  60. status_t rc;
  61. rc = vol.GetRootDirectory(&directory);
  62. BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
  63. rc = directory.GetEntry(&entry);
  64. BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
  65. rc = entry.GetPath(&path);
  66. BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
  67. const char *str = path.Path();
  68. BAIL_IF_MACRO(str == NULL, ERR_OS_ERROR, NULL); /* ?! */
  69. char *retval = (char *) allocator.Malloc(strlen(str) + 1);
  70. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  71. strcpy(retval, str);
  72. return(retval);
  73. } /* if */
  74. } /* while */
  75. return(NULL);
  76. } /* getMountPoint */
  77. /*
  78. * This function is lifted from Simple Directmedia Layer (SDL):
  79. * http://www.libsdl.org/
  80. */
  81. static void tryDir(const char *d, PHYSFS_StringCallback callback, void *data)
  82. {
  83. BDirectory dir;
  84. dir.SetTo(d);
  85. if (dir.InitCheck() != B_NO_ERROR)
  86. return;
  87. dir.Rewind();
  88. BEntry entry;
  89. while (dir.GetNextEntry(&entry) >= 0)
  90. {
  91. BPath path;
  92. const char *name;
  93. entry_ref e;
  94. if (entry.GetPath(&path) != B_NO_ERROR)
  95. continue;
  96. name = path.Path();
  97. if (entry.GetRef(&e) != B_NO_ERROR)
  98. continue;
  99. if (entry.IsDirectory())
  100. {
  101. if (strcmp(e.name, "floppy") != 0)
  102. tryDir(name, callback, data);
  103. } /* if */
  104. else
  105. {
  106. bool add_it = false;
  107. device_geometry g;
  108. if (strcmp(e.name, "raw") == 0) /* ignore partitions. */
  109. {
  110. const int devfd = open(name, O_RDONLY);
  111. if (devfd >= 0)
  112. {
  113. const int rc = ioctl(devfd, B_GET_GEOMETRY, &g, sizeof(g));
  114. close(devfd);
  115. if (rc >= 0)
  116. {
  117. if (g.device_type == B_CD)
  118. {
  119. char *mntpnt = getMountPoint(name);
  120. if (mntpnt != NULL)
  121. {
  122. callback(data, mntpnt);
  123. allocator.Free(mntpnt); /* !!! FIXME: lose this malloc! */
  124. } /* if */
  125. } /* if */
  126. } /* if */
  127. } /* if */
  128. } /* if */
  129. } /* else */
  130. } /* while */
  131. } /* tryDir */
  132. void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
  133. {
  134. tryDir("/dev/disk", cb, data);
  135. } /* __PHYSFS_platformDetectAvailableCDs */
  136. static team_id getTeamID(void)
  137. {
  138. thread_info info;
  139. thread_id tid = find_thread(NULL);
  140. get_thread_info(tid, &info);
  141. return(info.team);
  142. } /* getTeamID */
  143. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  144. {
  145. image_info info;
  146. int32 cookie = 0;
  147. while (get_next_image_info(0, &cookie, &info) == B_OK)
  148. {
  149. if (info.type == B_APP_IMAGE)
  150. break;
  151. } /* while */
  152. BEntry entry(info.name, true);
  153. BPath path;
  154. status_t rc = entry.GetPath(&path); /* (path) now has binary's path. */
  155. assert(rc == B_OK);
  156. rc = path.GetParent(&path); /* chop filename, keep directory. */
  157. assert(rc == B_OK);
  158. const char *str = path.Path();
  159. assert(str != NULL);
  160. char *retval = (char *) allocator.Malloc(strlen(str) + 1);
  161. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  162. strcpy(retval, str);
  163. return(retval);
  164. } /* __PHYSFS_platformCalcBaseDir */
  165. void *__PHYSFS_platformGetThreadID(void)
  166. {
  167. return((void *) find_thread(NULL));
  168. } /* __PHYSFS_platformGetThreadID */
  169. void *__PHYSFS_platformCreateMutex(void)
  170. {
  171. return(new BLocker("PhysicsFS lock", true));
  172. } /* __PHYSFS_platformCreateMutex */
  173. void __PHYSFS_platformDestroyMutex(void *mutex)
  174. {
  175. delete ((BLocker *) mutex);
  176. } /* __PHYSFS_platformDestroyMutex */
  177. int __PHYSFS_platformGrabMutex(void *mutex)
  178. {
  179. return ((BLocker *) mutex)->Lock() ? 1 : 0;
  180. } /* __PHYSFS_platformGrabMutex */
  181. void __PHYSFS_platformReleaseMutex(void *mutex)
  182. {
  183. ((BLocker *) mutex)->Unlock();
  184. } /* __PHYSFS_platformReleaseMutex */
  185. int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
  186. {
  187. return(0); /* just use malloc() and friends. */
  188. } /* __PHYSFS_platformSetDefaultAllocator */
  189. #endif /* PHYSFS_PLATFORM_BEOS */
  190. /* end of beos.cpp ... */