beos.cpp 7.2 KB

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