beos.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * BeOS platform-dependent support routines for PhysicsFS.
  3. *
  4. * Please see the file LICENSE in the source's root directory.
  5. *
  6. * This file written by Ryan C. Gordon.
  7. */
  8. #ifdef __BEOS__
  9. #include <be/kernel/OS.h>
  10. #include <be/app/Roster.h>
  11. #include <be/storage/Volume.h>
  12. #include <be/storage/VolumeRoster.h>
  13. #include <be/storage/Directory.h>
  14. #include <be/storage/Entry.h>
  15. #include <be/storage/Path.h>
  16. #include <be/kernel/fs_info.h>
  17. #include <be/device/scsi.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #define __PHYSICSFS_INTERNAL__
  24. #include "physfs_internal.h"
  25. const char *__PHYSFS_platformDirSeparator = "/";
  26. int __PHYSFS_platformInit(void)
  27. {
  28. return(1); /* always succeed. */
  29. } /* __PHYSFS_platformInit */
  30. int __PHYSFS_platformDeinit(void)
  31. {
  32. return(1); /* always succeed. */
  33. } /* __PHYSFS_platformDeinit */
  34. static char *getMountPoint(const char *devname)
  35. {
  36. BVolumeRoster mounts;
  37. BVolume vol;
  38. mounts.Rewind();
  39. while (mounts.GetNextVolume(&vol) == B_NO_ERROR)
  40. {
  41. fs_info fsinfo;
  42. fs_stat_dev(vol.Device(), &fsinfo);
  43. if (strcmp(devname, fsinfo.device_name) == 0)
  44. {
  45. //char buf[B_FILE_NAME_LENGTH];
  46. BDirectory directory;
  47. BEntry entry;
  48. BPath path;
  49. status_t rc;
  50. rc = vol.GetRootDirectory(&directory);
  51. BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
  52. rc = directory.GetEntry(&entry);
  53. BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
  54. rc = entry.GetPath(&path);
  55. BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
  56. const char *str = path.Path();
  57. BAIL_IF_MACRO(str == NULL, ERR_OS_ERROR, NULL); /* ?! */
  58. char *retval = (char *) allocator.Malloc(strlen(str) + 1);
  59. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  60. strcpy(retval, str);
  61. return(retval);
  62. } /* if */
  63. } /* while */
  64. return(NULL);
  65. } /* getMountPoint */
  66. /*
  67. * This function is lifted from Simple Directmedia Layer (SDL):
  68. * http://www.libsdl.org/
  69. */
  70. static void tryDir(const char *d, PHYSFS_StringCallback callback, void *data)
  71. {
  72. BDirectory dir;
  73. dir.SetTo(d);
  74. if (dir.InitCheck() != B_NO_ERROR)
  75. return;
  76. dir.Rewind();
  77. BEntry entry;
  78. while (dir.GetNextEntry(&entry) >= 0)
  79. {
  80. BPath path;
  81. const char *name;
  82. entry_ref e;
  83. if (entry.GetPath(&path) != B_NO_ERROR)
  84. continue;
  85. name = path.Path();
  86. if (entry.GetRef(&e) != B_NO_ERROR)
  87. continue;
  88. if (entry.IsDirectory())
  89. {
  90. if (strcmp(e.name, "floppy") != 0)
  91. tryDir(name, callback, data);
  92. } /* if */
  93. else
  94. {
  95. bool add_it = false;
  96. int devfd;
  97. device_geometry g;
  98. if (strcmp(e.name, "raw") == 0) /* ignore partitions. */
  99. {
  100. int devfd = open(name, O_RDONLY);
  101. if (devfd >= 0)
  102. {
  103. if (ioctl(devfd, B_GET_GEOMETRY, &g, sizeof(g)) >= 0)
  104. {
  105. if (g.device_type == B_CD)
  106. {
  107. char *mntpnt = getMountPoint(name);
  108. if (mntpnt != NULL)
  109. {
  110. callback(data, mntpnt);
  111. allocator.Free(mntpnt); /* !!! FIXME: lose this malloc! */
  112. } /* if */
  113. } /* if */
  114. } /* if */
  115. } /* if */
  116. } /* if */
  117. close(devfd);
  118. } /* else */
  119. } /* while */
  120. } /* tryDir */
  121. void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
  122. {
  123. tryDir("/dev/disk", cb, data);
  124. } /* __PHYSFS_platformDetectAvailableCDs */
  125. static team_id getTeamID(void)
  126. {
  127. thread_info info;
  128. thread_id tid = find_thread(NULL);
  129. get_thread_info(tid, &info);
  130. return(info.team);
  131. } /* getMyTeamID */
  132. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  133. {
  134. /* in case there isn't a BApplication yet, we'll construct a roster. */
  135. BRoster roster;
  136. app_info info;
  137. status_t rc = roster.GetRunningAppInfo(getTeamID(), &info);
  138. BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
  139. BEntry entry(&(info.ref), true);
  140. BPath path;
  141. rc = entry.GetPath(&path); /* (path) now has binary's path. */
  142. assert(rc == B_OK);
  143. rc = path.GetParent(&path); /* chop filename, keep directory. */
  144. assert(rc == B_OK);
  145. const char *str = path.Path();
  146. assert(str != NULL);
  147. char *retval = (char *) allocator.Malloc(strlen(str) + 1);
  148. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  149. strcpy(retval, str);
  150. return(retval);
  151. } /* __PHYSFS_platformCalcBaseDir */
  152. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
  153. {
  154. return((PHYSFS_uint64) find_thread(NULL));
  155. } /* __PHYSFS_platformGetThreadID */
  156. /* Much like my college days, try to sleep for 10 milliseconds at a time... */
  157. void __PHYSFS_platformTimeslice(void)
  158. {
  159. snooze(10000); /* put thread to sleep for 10 milliseconds. */
  160. } /* __PHYSFS_platformTimeslice */
  161. char *__PHYSFS_platformRealPath(const char *path)
  162. {
  163. BPath normalized(path, NULL, true); /* force normalization of path. */
  164. const char *resolved_path = normalized.Path();
  165. BAIL_IF_MACRO(resolved_path == NULL, ERR_NO_SUCH_FILE, NULL);
  166. char *retval = (char *) allocator.Malloc(strlen(resolved_path) + 1);
  167. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  168. strcpy(retval, resolved_path);
  169. return(retval);
  170. } /* __PHYSFS_platformRealPath */
  171. void *__PHYSFS_platformCreateMutex(void)
  172. {
  173. sem_id *retval = (sem_id *) allocator.Malloc(sizeof (sem_id));
  174. sem_id rc;
  175. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  176. rc = create_sem(1, "PhysicsFS semaphore");
  177. if (rc < B_OK)
  178. {
  179. allocator.Free(retval);
  180. BAIL_MACRO(strerror(rc), NULL);
  181. } // if
  182. *retval = rc;
  183. return(retval);
  184. } /* __PHYSFS_platformCreateMutex */
  185. void __PHYSFS_platformDestroyMutex(void *mutex)
  186. {
  187. delete_sem( *((sem_id *) mutex) );
  188. allocator.Free(mutex);
  189. } /* __PHYSFS_platformDestroyMutex */
  190. int __PHYSFS_platformGrabMutex(void *mutex)
  191. {
  192. status_t rc = acquire_sem(*((sem_id *) mutex));
  193. BAIL_IF_MACRO(rc < B_OK, strerror(rc), 0);
  194. return(1);
  195. } /* __PHYSFS_platformGrabMutex */
  196. void __PHYSFS_platformReleaseMutex(void *mutex)
  197. {
  198. release_sem(*((sem_id *) mutex));
  199. } /* __PHYSFS_platformReleaseMutex */
  200. #endif
  201. /* end of beos.cpp ... */