physfs_platform_haiku.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. void __PHYSFS_platformDeinit(void)
  28. {
  29. /* no-op */
  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. const int devfd = open(name, O_RDONLY);
  87. if (devfd < 0)
  88. continue;
  89. device_geometry g;
  90. const int rc = ioctl(devfd, B_GET_GEOMETRY, &g, sizeof (g));
  91. close(devfd);
  92. if (rc < 0)
  93. continue;
  94. if (g.device_type != B_CD)
  95. continue;
  96. char mntpnt[B_FILE_NAME_LENGTH];
  97. if (getMountPoint(name, mntpnt, sizeof (mntpnt)))
  98. callback(data, mntpnt);
  99. } /* while */
  100. } /* tryDir */
  101. void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
  102. {
  103. tryDir("/dev/disk", cb, data);
  104. } /* __PHYSFS_platformDetectAvailableCDs */
  105. static team_id getTeamID(void)
  106. {
  107. thread_info info;
  108. thread_id tid = find_thread(NULL);
  109. get_thread_info(tid, &info);
  110. return info.team;
  111. } /* getTeamID */
  112. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  113. {
  114. image_info info;
  115. int32 cookie = 0;
  116. while (get_next_image_info(0, &cookie, &info) == B_OK)
  117. {
  118. if (info.type == B_APP_IMAGE)
  119. break;
  120. } /* while */
  121. BEntry entry(info.name, true);
  122. BPath path;
  123. status_t rc = entry.GetPath(&path); /* (path) now has binary's path. */
  124. assert(rc == B_OK);
  125. rc = path.GetParent(&path); /* chop filename, keep directory. */
  126. assert(rc == B_OK);
  127. const char *str = path.Path();
  128. assert(str != NULL);
  129. const size_t len = strlen(str);
  130. char *retval = (char *) allocator.Malloc(len + 2);
  131. BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  132. strcpy(retval, str);
  133. retval[len] = '/';
  134. retval[len+1] = '\0';
  135. return retval;
  136. } /* __PHYSFS_platformCalcBaseDir */
  137. char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
  138. {
  139. const char *userdir = __PHYSFS_getUserDir();
  140. const char *append = "config/settings/";
  141. const size_t len = strlen(userdir) + strlen(append) + strlen(app) + 2;
  142. char *retval = (char *) allocator.Malloc(len);
  143. BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  144. snprintf(retval, len, "%s%s%s/", userdir, append, app);
  145. return retval;
  146. } /* __PHYSFS_platformCalcPrefDir */
  147. #endif /* PHYSFS_PLATFORM_HAIKU */
  148. /* end of physfs_platform_haiku.cpp ... */