beos.cpp 7.2 KB

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