beos.cpp 6.6 KB

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