beos.cpp 6.6 KB

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