platform_beos.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Haiku 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_HAIKU
  11. #include <os/kernel/OS.h>
  12. #include <os/app/Roster.h>
  13. #include <os/storage/Volume.h>
  14. #include <os/storage/VolumeRoster.h>
  15. #include <os/storage/Directory.h>
  16. #include <os/storage/Entry.h>
  17. #include <os/storage/Path.h>
  18. #include <os/kernel/fs_info.h>
  19. #include <os/device/scsi.h>
  20. #include <errno.h>
  21. #include <unistd.h>
  22. #include "physfs_internal.h"
  23. int __PHYSFS_platformInit(void)
  24. {
  25. return 1; /* always succeed. */
  26. } /* __PHYSFS_platformInit */
  27. int __PHYSFS_platformDeinit(void)
  28. {
  29. return 1; /* always succeed. */
  30. } /* __PHYSFS_platformDeinit */
  31. static char *getMountPoint(const char *devname, char *buf, size_t bufsize)
  32. {
  33. BVolumeRoster mounts;
  34. BVolume vol;
  35. mounts.Rewind();
  36. while (mounts.GetNextVolume(&vol) == B_NO_ERROR)
  37. {
  38. fs_info fsinfo;
  39. fs_stat_dev(vol.Device(), &fsinfo);
  40. if (strcmp(devname, fsinfo.device_name) == 0)
  41. {
  42. BDirectory directory;
  43. BEntry entry;
  44. BPath path;
  45. const char *str;
  46. if ( (vol.GetRootDirectory(&directory) < B_OK) ||
  47. (directory.GetEntry(&entry) < B_OK) ||
  48. (entry.GetPath(&path) < B_OK) ||
  49. ( (str = path.Path()) == NULL) )
  50. return NULL;
  51. strncpy(buf, str, bufsize-1);
  52. buf[bufsize-1] = '\0';
  53. return buf;
  54. } /* if */
  55. } /* while */
  56. return NULL;
  57. } /* getMountPoint */
  58. /*
  59. * This function is lifted from Simple Directmedia Layer (SDL):
  60. * https://www.libsdl.org/ ... this is zlib-licensed code, too.
  61. */
  62. static void tryDir(const char *d, PHYSFS_StringCallback callback, void *data)
  63. {
  64. BDirectory dir;
  65. dir.SetTo(d);
  66. if (dir.InitCheck() != B_NO_ERROR)
  67. return;
  68. dir.Rewind();
  69. BEntry entry;
  70. while (dir.GetNextEntry(&entry) >= 0)
  71. {
  72. BPath path;
  73. const char *name;
  74. entry_ref e;
  75. if (entry.GetPath(&path) != B_NO_ERROR)
  76. continue;
  77. name = path.Path();
  78. if (entry.GetRef(&e) != B_NO_ERROR)
  79. continue;
  80. if (entry.IsDirectory())
  81. {
  82. if (strcmp(e.name, "floppy") != 0)
  83. tryDir(name, callback, data);
  84. continue;
  85. } /* if */
  86. if (strcmp(e.name, "raw") != 0) /* ignore partitions. */
  87. continue;
  88. const int devfd = open(name, O_RDONLY);
  89. if (devfd < 0)
  90. continue;
  91. device_geometry g;
  92. const int rc = ioctl(devfd, B_GET_GEOMETRY, &g, sizeof (g));
  93. close(devfd);
  94. if (rc < 0)
  95. continue;
  96. if (g.device_type != B_CD)
  97. continue;
  98. char mntpnt[B_FILE_NAME_LENGTH];
  99. if (getMountPoint(name, mntpnt, sizeof (mntpnt)))
  100. callback(data, mntpnt);
  101. } /* while */
  102. } /* tryDir */
  103. void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
  104. {
  105. tryDir("/dev/disk", cb, data);
  106. } /* __PHYSFS_platformDetectAvailableCDs */
  107. static team_id getTeamID(void)
  108. {
  109. thread_info info;
  110. thread_id tid = find_thread(NULL);
  111. get_thread_info(tid, &info);
  112. return info.team;
  113. } /* getTeamID */
  114. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  115. {
  116. image_info info;
  117. int32 cookie = 0;
  118. while (get_next_image_info(0, &cookie, &info) == B_OK)
  119. {
  120. if (info.type == B_APP_IMAGE)
  121. break;
  122. } /* while */
  123. BEntry entry(info.name, true);
  124. BPath path;
  125. status_t rc = entry.GetPath(&path); /* (path) now has binary's path. */
  126. assert(rc == B_OK);
  127. rc = path.GetParent(&path); /* chop filename, keep directory. */
  128. assert(rc == B_OK);
  129. const char *str = path.Path();
  130. assert(str != NULL);
  131. const size_t len = strlen(str);
  132. char *retval = (char *) allocator.Malloc(len + 2);
  133. BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  134. strcpy(retval, str);
  135. retval[len] = '/';
  136. retval[len+1] = '\0';
  137. return retval;
  138. } /* __PHYSFS_platformCalcBaseDir */
  139. char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
  140. {
  141. const char *userdir = __PHYSFS_getUserDir();
  142. const char *append = "config/settings/";
  143. const size_t len = strlen(userdir) + strlen(append) + strlen(app) + 2;
  144. char *retval = (char *) allocator.Malloc(len);
  145. BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  146. snprintf(retval, len, "%s%s%s/", userdir, append, app);
  147. return retval;
  148. } /* __PHYSFS_platformCalcPrefDir */
  149. #endif /* PHYSFS_PLATFORM_HAIKU */
  150. /* end of beos.cpp ... */