beos.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. image_info info;
  149. int32 cookie = 0;
  150. while (get_next_image_info(0, &cookie, &info) == B_OK) {
  151. if (info.type == B_APP_IMAGE)
  152. break;
  153. }
  154. BEntry entry(info.name, true);
  155. BPath path;
  156. status_t rc = entry.GetPath(&path); /* (path) now has binary's path. */
  157. assert(rc == B_OK);
  158. rc = path.GetParent(&path); /* chop filename, keep directory. */
  159. assert(rc == B_OK);
  160. const char *str = path.Path();
  161. assert(str != NULL);
  162. char *retval = (char *) allocator.Malloc(strlen(str) + 1);
  163. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  164. strcpy(retval, str);
  165. return(retval);
  166. } /* __PHYSFS_platformCalcBaseDir */
  167. void *__PHYSFS_platformGetThreadID(void)
  168. {
  169. return((void *) find_thread(NULL));
  170. } /* __PHYSFS_platformGetThreadID */
  171. char *__PHYSFS_platformRealPath(const char *path)
  172. {
  173. BPath normalized(path, NULL, true); /* force normalization of path. */
  174. const char *resolved_path = normalized.Path();
  175. BAIL_IF_MACRO(resolved_path == NULL, ERR_NO_SUCH_FILE, NULL);
  176. char *retval = (char *) allocator.Malloc(strlen(resolved_path) + 1);
  177. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  178. strcpy(retval, resolved_path);
  179. return(retval);
  180. } /* __PHYSFS_platformRealPath */
  181. char *__PHYSFS_platformCurrentDir(void)
  182. {
  183. return(__PHYSFS_platformRealPath(".")); /* let BPath sort it out. */
  184. } /* __PHYSFS_platformCurrentDir */
  185. void *__PHYSFS_platformCreateMutex(void)
  186. {
  187. return(new BLocker("PhysicsFS lock", true));
  188. } /* __PHYSFS_platformCreateMutex */
  189. void __PHYSFS_platformDestroyMutex(void *mutex)
  190. {
  191. delete ((BLocker *) mutex);
  192. } /* __PHYSFS_platformDestroyMutex */
  193. int __PHYSFS_platformGrabMutex(void *mutex)
  194. {
  195. return ((BLocker *) mutex)->Lock() ? 1 : 0;
  196. } /* __PHYSFS_platformGrabMutex */
  197. void __PHYSFS_platformReleaseMutex(void *mutex)
  198. {
  199. ((BLocker *) mutex)->Unlock();
  200. } /* __PHYSFS_platformReleaseMutex */
  201. int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
  202. {
  203. return(0); /* just use malloc() and friends. */
  204. } /* __PHYSFS_platformSetDefaultAllocator */
  205. #endif /* PHYSFS_PLATFORM_BEOS */
  206. /* end of beos.cpp ... */