1
0

beos.cpp 7.3 KB

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