beos.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <errno.h>
  38. #include <unistd.h>
  39. #include "physfs_internal.h"
  40. int __PHYSFS_platformInit(void)
  41. {
  42. return(1); /* always succeed. */
  43. } /* __PHYSFS_platformInit */
  44. int __PHYSFS_platformDeinit(void)
  45. {
  46. return(1); /* always succeed. */
  47. } /* __PHYSFS_platformDeinit */
  48. static char *getMountPoint(const char *devname)
  49. {
  50. BVolumeRoster mounts;
  51. BVolume vol;
  52. mounts.Rewind();
  53. while (mounts.GetNextVolume(&vol) == B_NO_ERROR)
  54. {
  55. fs_info fsinfo;
  56. fs_stat_dev(vol.Device(), &fsinfo);
  57. if (strcmp(devname, fsinfo.device_name) == 0)
  58. {
  59. //char buf[B_FILE_NAME_LENGTH];
  60. BDirectory directory;
  61. BEntry entry;
  62. BPath path;
  63. status_t rc;
  64. rc = vol.GetRootDirectory(&directory);
  65. BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
  66. rc = directory.GetEntry(&entry);
  67. BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
  68. rc = entry.GetPath(&path);
  69. BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
  70. const char *str = path.Path();
  71. BAIL_IF_MACRO(str == NULL, ERR_OS_ERROR, NULL); /* ?! */
  72. char *retval = (char *) allocator.Malloc(strlen(str) + 1);
  73. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  74. strcpy(retval, str);
  75. return(retval);
  76. } /* if */
  77. } /* while */
  78. return(NULL);
  79. } /* getMountPoint */
  80. /*
  81. * This function is lifted from Simple Directmedia Layer (SDL):
  82. * http://www.libsdl.org/
  83. */
  84. static void tryDir(const char *d, PHYSFS_StringCallback callback, void *data)
  85. {
  86. BDirectory dir;
  87. dir.SetTo(d);
  88. if (dir.InitCheck() != B_NO_ERROR)
  89. return;
  90. dir.Rewind();
  91. BEntry entry;
  92. while (dir.GetNextEntry(&entry) >= 0)
  93. {
  94. BPath path;
  95. const char *name;
  96. entry_ref e;
  97. if (entry.GetPath(&path) != B_NO_ERROR)
  98. continue;
  99. name = path.Path();
  100. if (entry.GetRef(&e) != B_NO_ERROR)
  101. continue;
  102. if (entry.IsDirectory())
  103. {
  104. if (strcmp(e.name, "floppy") != 0)
  105. tryDir(name, callback, data);
  106. } /* if */
  107. else
  108. {
  109. bool add_it = false;
  110. int devfd;
  111. device_geometry g;
  112. if (strcmp(e.name, "raw") == 0) /* ignore partitions. */
  113. {
  114. int devfd = open(name, O_RDONLY);
  115. if (devfd >= 0)
  116. {
  117. if (ioctl(devfd, B_GET_GEOMETRY, &g, sizeof(g)) >= 0)
  118. {
  119. if (g.device_type == B_CD)
  120. {
  121. char *mntpnt = getMountPoint(name);
  122. if (mntpnt != NULL)
  123. {
  124. callback(data, mntpnt);
  125. allocator.Free(mntpnt); /* !!! FIXME: lose this malloc! */
  126. } /* if */
  127. } /* if */
  128. } /* if */
  129. } /* if */
  130. } /* if */
  131. close(devfd);
  132. } /* else */
  133. } /* while */
  134. } /* tryDir */
  135. void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
  136. {
  137. tryDir("/dev/disk", cb, data);
  138. } /* __PHYSFS_platformDetectAvailableCDs */
  139. static team_id getTeamID(void)
  140. {
  141. thread_info info;
  142. thread_id tid = find_thread(NULL);
  143. get_thread_info(tid, &info);
  144. return(info.team);
  145. } /* getTeamID */
  146. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  147. {
  148. /* in case there isn't a BApplication yet, we'll construct a roster. */
  149. BRoster roster;
  150. app_info info;
  151. status_t rc = roster.GetRunningAppInfo(getTeamID(), &info);
  152. BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
  153. BEntry entry(&(info.ref), true);
  154. BPath path;
  155. rc = entry.GetPath(&path); /* (path) now has binary's path. */
  156. assert(rc == B_OK);
  157. rc = path.GetParent(&path); /* chop filename, keep directory. */
  158. assert(rc == B_OK);
  159. const char *str = path.Path();
  160. assert(str != NULL);
  161. char *retval = (char *) allocator.Malloc(strlen(str) + 1);
  162. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  163. strcpy(retval, str);
  164. return(retval);
  165. } /* __PHYSFS_platformCalcBaseDir */
  166. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
  167. {
  168. return((PHYSFS_uint64) find_thread(NULL));
  169. } /* __PHYSFS_platformGetThreadID */
  170. char *__PHYSFS_platformRealPath(const char *path)
  171. {
  172. BPath normalized(path, NULL, true); /* force normalization of path. */
  173. const char *resolved_path = normalized.Path();
  174. BAIL_IF_MACRO(resolved_path == NULL, ERR_NO_SUCH_FILE, NULL);
  175. char *retval = (char *) allocator.Malloc(strlen(resolved_path) + 1);
  176. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  177. strcpy(retval, resolved_path);
  178. return(retval);
  179. } /* __PHYSFS_platformRealPath */
  180. char *__PHYSFS_platformCurrentDir(void)
  181. {
  182. return(__PHYSFS_platformRealPath(".")); /* let BPath sort it out. */
  183. } /* __PHYSFS_platformCurrentDir */
  184. void *__PHYSFS_platformCreateMutex(void)
  185. {
  186. return(new BLocker("PhysicsFS lock", true));
  187. } /* __PHYSFS_platformCreateMutex */
  188. void __PHYSFS_platformDestroyMutex(void *mutex)
  189. {
  190. delete ((BLocker *) mutex);
  191. } /* __PHYSFS_platformDestroyMutex */
  192. int __PHYSFS_platformGrabMutex(void *mutex)
  193. {
  194. return ((BLocker *) mutex)->Lock() ? 1 : 0;
  195. } /* __PHYSFS_platformGrabMutex */
  196. void __PHYSFS_platformReleaseMutex(void *mutex)
  197. {
  198. ((BLocker *) mutex)->Unlock();
  199. } /* __PHYSFS_platformReleaseMutex */
  200. int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
  201. {
  202. return(0); /* just use malloc() and friends. */
  203. } /* __PHYSFS_platformSetDefaultAllocator */
  204. #endif /* PHYSFS_PLATFORM_BEOS */
  205. /* end of beos.cpp ... */