beos.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. /* caller needs to malloc() mntpnt, and expect us to free() it. */
  38. static void addDisc(char *mntpnt, char ***discs, int *disccount)
  39. {
  40. char **tmp = (char **) realloc(*discs, sizeof (char *) * (*disccount + 1));
  41. if (tmp)
  42. {
  43. tmp[*disccount - 1] = mntpnt;
  44. *discs = tmp;
  45. (*disccount)++;
  46. } /* if */
  47. } /* addDisc */
  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 *) 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 *dirname, char ***discs, int *disccount)
  85. {
  86. BDirectory dir;
  87. dir.SetTo(dirname);
  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, discs, disccount);
  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. addDisc(mntpnt, discs, disccount);
  124. } /* if */
  125. } /* if */
  126. } /* if */
  127. } /* if */
  128. close(devfd);
  129. } /* else */
  130. } /* while */
  131. } /* tryDir */
  132. char **__PHYSFS_platformDetectAvailableCDs(void)
  133. {
  134. char **retval = (char **) malloc(sizeof (char *));
  135. int cd_count = 1; /* We count the NULL entry. */
  136. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  137. tryDir("/dev/disk", &retval, &cd_count);
  138. retval[cd_count - 1] = NULL;
  139. return(retval);
  140. } /* __PHYSFS_platformDetectAvailableCDs */
  141. static team_id getTeamID(void)
  142. {
  143. thread_info info;
  144. thread_id tid = find_thread(NULL);
  145. get_thread_info(tid, &info);
  146. return(info.team);
  147. } /* getMyTeamID */
  148. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  149. {
  150. /* in case there isn't a BApplication yet, we'll construct a roster. */
  151. BRoster roster;
  152. app_info info;
  153. status_t rc = roster.GetRunningAppInfo(getTeamID(), &info);
  154. BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
  155. BEntry entry(&(info.ref), true);
  156. BPath path;
  157. rc = entry.GetPath(&path); /* (path) now has binary's path. */
  158. assert(rc == B_OK);
  159. rc = path.GetParent(&path); /* chop filename, keep directory. */
  160. assert(rc == B_OK);
  161. const char *str = path.Path();
  162. assert(str != NULL);
  163. char *retval = (char *) malloc(strlen(str) + 1);
  164. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  165. strcpy(retval, str);
  166. return(retval);
  167. } /* __PHYSFS_platformCalcBaseDir */
  168. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
  169. {
  170. return((PHYSFS_uint64) find_thread(NULL));
  171. } /* __PHYSFS_platformGetThreadID */
  172. /* Much like my college days, try to sleep for 10 milliseconds at a time... */
  173. void __PHYSFS_platformTimeslice(void)
  174. {
  175. snooze(10000); /* put thread to sleep for 10 milliseconds. */
  176. } /* __PHYSFS_platformTimeslice */
  177. char *__PHYSFS_platformRealPath(const char *path)
  178. {
  179. char *str = (char *) alloca(strlen(path) + 1);
  180. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, NULL);
  181. strcpy(str, path);
  182. char *leaf = strrchr(str, '/');
  183. if (leaf != NULL)
  184. *(leaf++) = '\0';
  185. BPath normalized(str, leaf, true); /* force normalization of path. */
  186. const char *resolved_path = normalized.Path();
  187. BAIL_IF_MACRO(resolved_path == NULL, ERR_NO_SUCH_FILE, NULL);
  188. char *retval = (char *) malloc(strlen(resolved_path) + 1);
  189. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  190. strcpy(retval, resolved_path);
  191. return(retval);
  192. } /* __PHYSFS_platformRealPath */
  193. void *__PHYSFS_platformCreateMutex(void)
  194. {
  195. sem_id *retval = (sem_id *) malloc(sizeof (sem_id));
  196. sem_id rc;
  197. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  198. rc = create_sem(1, "PhysicsFS semaphore");
  199. if (rc < B_OK)
  200. {
  201. free(retval);
  202. BAIL_MACRO(strerror(rc), NULL);
  203. } // if
  204. *retval = rc;
  205. return(retval);
  206. } /* __PHYSFS_platformCreateMutex */
  207. void __PHYSFS_platformDestroyMutex(void *mutex)
  208. {
  209. delete_sem( *((sem_id *) mutex) );
  210. free(mutex);
  211. } /* __PHYSFS_platformDestroyMutex */
  212. int __PHYSFS_platformGrabMutex(void *mutex)
  213. {
  214. status_t rc = acquire_sem(*((sem_id *) mutex));
  215. BAIL_IF_MACRO(rc < B_OK, strerror(rc), 0);
  216. return(1);
  217. } /* __PHYSFS_platformGrabMutex */
  218. void __PHYSFS_platformReleaseMutex(void *mutex)
  219. {
  220. release_sem(*((sem_id *) mutex));
  221. } /* __PHYSFS_platformReleaseMutex */
  222. #endif
  223. /* end of beos.cpp ... */