physfs_platform_ogc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Wii/GameCube 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_OGC
  11. #include <unistd.h>
  12. #include <ctype.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <dirent.h>
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <stdlib.h>
  19. #include <sys/param.h>
  20. #include <time.h>
  21. #include <limits.h>
  22. #include <ogc/lwp.h>
  23. #include <ogc/mutex.h>
  24. #include <ogc/system.h>
  25. #include "physfs_internal.h"
  26. static PHYSFS_ErrorCode errcodeFromErrnoError(const int err)
  27. {
  28. switch (err)
  29. {
  30. case 0: return PHYSFS_ERR_OK;
  31. case EACCES: return PHYSFS_ERR_PERMISSION;
  32. case EPERM: return PHYSFS_ERR_PERMISSION;
  33. case EDQUOT: return PHYSFS_ERR_NO_SPACE;
  34. case EIO: return PHYSFS_ERR_IO;
  35. case ELOOP: return PHYSFS_ERR_SYMLINK_LOOP;
  36. case EMLINK: return PHYSFS_ERR_NO_SPACE;
  37. case ENAMETOOLONG: return PHYSFS_ERR_BAD_FILENAME;
  38. case ENOENT: return PHYSFS_ERR_NOT_FOUND;
  39. case ENOSPC: return PHYSFS_ERR_NO_SPACE;
  40. case ENOTDIR: return PHYSFS_ERR_NOT_FOUND;
  41. case EISDIR: return PHYSFS_ERR_NOT_A_FILE;
  42. case EROFS: return PHYSFS_ERR_READ_ONLY;
  43. case ETXTBSY: return PHYSFS_ERR_BUSY;
  44. case EBUSY: return PHYSFS_ERR_BUSY;
  45. case ENOMEM: return PHYSFS_ERR_OUT_OF_MEMORY;
  46. case ENOTEMPTY: return PHYSFS_ERR_DIR_NOT_EMPTY;
  47. default: return PHYSFS_ERR_OS_ERROR;
  48. } /* switch */
  49. } /* errcodeFromErrnoError */
  50. static inline PHYSFS_ErrorCode errcodeFromErrno(void)
  51. {
  52. return errcodeFromErrnoError(errno);
  53. } /* errcodeFromErrno */
  54. static inline char *buildSubdirPath(const char *subdir, size_t subdir_length)
  55. {
  56. const char *baseDir;
  57. char *retval;
  58. size_t length;
  59. baseDir = PHYSFS_getBaseDir();
  60. length = strlen(baseDir);
  61. retval = allocator.Malloc(length + subdir_length);
  62. BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  63. strcpy(retval, baseDir);
  64. strcpy(retval + length, subdir);
  65. return retval;
  66. }
  67. char *__PHYSFS_platformCalcUserDir(void)
  68. {
  69. static const char subdir[] = "userdata/";
  70. /* We don't have users on the Wii/GameCube. Just create a userdata folder
  71. * in the application's directory. */
  72. return buildSubdirPath(subdir, sizeof(subdir));
  73. } /* __PHYSFS_platformCalcUserDir */
  74. PHYSFS_EnumerateCallbackResult __PHYSFS_platformEnumerate(const char *dirname,
  75. PHYSFS_EnumerateCallback callback,
  76. const char *origdir, void *callbackdata)
  77. {
  78. DIR *dir;
  79. struct dirent *ent;
  80. PHYSFS_EnumerateCallbackResult retval = PHYSFS_ENUM_OK;
  81. dir = opendir(dirname);
  82. BAIL_IF(dir == NULL, errcodeFromErrno(), PHYSFS_ENUM_ERROR);
  83. while ((retval == PHYSFS_ENUM_OK) && ((ent = readdir(dir)) != NULL))
  84. {
  85. const char *name = ent->d_name;
  86. if (name[0] == '.') /* ignore "." and ".." */
  87. {
  88. if ((name[1] == '\0') || ((name[1] == '.') && (name[2] == '\0')))
  89. continue;
  90. } /* if */
  91. retval = callback(callbackdata, origdir, name);
  92. if (retval == PHYSFS_ENUM_ERROR)
  93. PHYSFS_setErrorCode(PHYSFS_ERR_APP_CALLBACK);
  94. } /* while */
  95. closedir(dir);
  96. return retval;
  97. } /* __PHYSFS_platformEnumerate */
  98. int __PHYSFS_platformMkDir(const char *path)
  99. {
  100. const int rc = mkdir(path, S_IRWXU);
  101. BAIL_IF(rc == -1, errcodeFromErrno(), 0);
  102. return 1;
  103. } /* __PHYSFS_platformMkDir */
  104. #if !defined(O_CLOEXEC) && defined(FD_CLOEXEC)
  105. static inline void set_CLOEXEC(int fildes)
  106. {
  107. int flags = fcntl(fildes, F_GETFD);
  108. if (flags != -1) {
  109. fcntl(fildes, F_SETFD, flags | FD_CLOEXEC);
  110. }
  111. }
  112. #endif
  113. static void *doOpen(const char *filename, int mode)
  114. {
  115. const int appending = (mode & O_APPEND);
  116. int fd;
  117. int *retval;
  118. errno = 0;
  119. /* O_APPEND doesn't actually behave as we'd like. */
  120. mode &= ~O_APPEND;
  121. #ifdef O_CLOEXEC
  122. /* Add O_CLOEXEC if defined */
  123. mode |= O_CLOEXEC;
  124. #endif
  125. do {
  126. fd = open(filename, mode, S_IRUSR | S_IWUSR);
  127. } while ((fd < 0) && (errno == EINTR));
  128. BAIL_IF(fd < 0, errcodeFromErrno(), NULL);
  129. #if !defined(O_CLOEXEC) && defined(FD_CLOEXEC)
  130. set_CLOEXEC(fd);
  131. #endif
  132. if (appending)
  133. {
  134. if (lseek(fd, 0, SEEK_END) < 0)
  135. {
  136. const int err = errno;
  137. close(fd);
  138. BAIL(errcodeFromErrnoError(err), NULL);
  139. } /* if */
  140. } /* if */
  141. retval = (int *) allocator.Malloc(sizeof (int));
  142. if (!retval)
  143. {
  144. close(fd);
  145. BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  146. } /* if */
  147. *retval = fd;
  148. return ((void *) retval);
  149. } /* doOpen */
  150. void *__PHYSFS_platformOpenRead(const char *filename)
  151. {
  152. return doOpen(filename, O_RDONLY);
  153. } /* __PHYSFS_platformOpenRead */
  154. void *__PHYSFS_platformOpenWrite(const char *filename)
  155. {
  156. return doOpen(filename, O_WRONLY | O_CREAT | O_TRUNC);
  157. } /* __PHYSFS_platformOpenWrite */
  158. void *__PHYSFS_platformOpenAppend(const char *filename)
  159. {
  160. return doOpen(filename, O_WRONLY | O_CREAT | O_APPEND);
  161. } /* __PHYSFS_platformOpenAppend */
  162. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  163. PHYSFS_uint64 len)
  164. {
  165. const int fd = *((int *) opaque);
  166. ssize_t rc = 0;
  167. if (!__PHYSFS_ui64FitsAddressSpace(len))
  168. BAIL(PHYSFS_ERR_INVALID_ARGUMENT, -1);
  169. do {
  170. rc = read(fd, buffer, (size_t) len);
  171. } while ((rc == -1) && (errno == EINTR));
  172. BAIL_IF(rc == -1, errcodeFromErrno(), -1);
  173. assert(rc >= 0);
  174. assert(rc <= len);
  175. return (PHYSFS_sint64) rc;
  176. } /* __PHYSFS_platformRead */
  177. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  178. PHYSFS_uint64 len)
  179. {
  180. const int fd = *((int *) opaque);
  181. ssize_t rc = 0;
  182. if (!__PHYSFS_ui64FitsAddressSpace(len))
  183. BAIL(PHYSFS_ERR_INVALID_ARGUMENT, -1);
  184. do {
  185. rc = write(fd, (void *) buffer, (size_t) len);
  186. } while ((rc == -1) && (errno == EINTR));
  187. BAIL_IF(rc == -1, errcodeFromErrno(), rc);
  188. assert(rc >= 0);
  189. assert(rc <= len);
  190. return (PHYSFS_sint64) rc;
  191. } /* __PHYSFS_platformWrite */
  192. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  193. {
  194. const int fd = *((int *) opaque);
  195. const off_t rc = lseek(fd, (off_t) pos, SEEK_SET);
  196. BAIL_IF(rc == -1, errcodeFromErrno(), 0);
  197. return 1;
  198. } /* __PHYSFS_platformSeek */
  199. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  200. {
  201. const int fd = *((int *) opaque);
  202. PHYSFS_sint64 retval;
  203. retval = (PHYSFS_sint64) lseek(fd, 0, SEEK_CUR);
  204. BAIL_IF(retval == -1, errcodeFromErrno(), -1);
  205. return retval;
  206. } /* __PHYSFS_platformTell */
  207. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  208. {
  209. const int fd = *((int *) opaque);
  210. struct stat statbuf;
  211. BAIL_IF(fstat(fd, &statbuf) == -1, errcodeFromErrno(), -1);
  212. return ((PHYSFS_sint64) statbuf.st_size);
  213. } /* __PHYSFS_platformFileLength */
  214. int __PHYSFS_platformFlush(void *opaque)
  215. {
  216. const int fd = *((int *) opaque);
  217. int rc = -1;
  218. if ((fcntl(fd, F_GETFL) & O_ACCMODE) != O_RDONLY) {
  219. do {
  220. rc = fsync(fd);
  221. } while ((rc == -1) && (errno == EINTR));
  222. BAIL_IF(rc == -1, errcodeFromErrno(), 0);
  223. }
  224. return 1;
  225. } /* __PHYSFS_platformFlush */
  226. void __PHYSFS_platformClose(void *opaque)
  227. {
  228. const int fd = *((int *) opaque);
  229. int rc = -1;
  230. do {
  231. rc = close(fd); /* we don't check this. You should have used flush! */
  232. } while ((rc == -1) && (errno == EINTR));
  233. allocator.Free(opaque);
  234. } /* __PHYSFS_platformClose */
  235. int __PHYSFS_platformDelete(const char *path)
  236. {
  237. BAIL_IF(remove(path) == -1, errcodeFromErrno(), 0);
  238. return 1;
  239. } /* __PHYSFS_platformDelete */
  240. int __PHYSFS_platformStat(const char *fname, PHYSFS_Stat *st, const int follow)
  241. {
  242. struct stat statbuf;
  243. const int rc = follow ? stat(fname, &statbuf) : lstat(fname, &statbuf);
  244. BAIL_IF(rc == -1, errcodeFromErrno(), 0);
  245. if (S_ISREG(statbuf.st_mode))
  246. {
  247. st->filetype = PHYSFS_FILETYPE_REGULAR;
  248. st->filesize = statbuf.st_size;
  249. } /* if */
  250. else if(S_ISDIR(statbuf.st_mode))
  251. {
  252. st->filetype = PHYSFS_FILETYPE_DIRECTORY;
  253. st->filesize = 0;
  254. } /* else if */
  255. else if(S_ISLNK(statbuf.st_mode))
  256. {
  257. st->filetype = PHYSFS_FILETYPE_SYMLINK;
  258. st->filesize = 0;
  259. } /* else if */
  260. else
  261. {
  262. st->filetype = PHYSFS_FILETYPE_OTHER;
  263. st->filesize = statbuf.st_size;
  264. } /* else */
  265. st->modtime = statbuf.st_mtime;
  266. st->createtime = statbuf.st_ctime;
  267. st->accesstime = statbuf.st_atime;
  268. st->readonly = (access(fname, W_OK) == -1);
  269. return 1;
  270. } /* __PHYSFS_platformStat */
  271. void *__PHYSFS_platformGetThreadID(void)
  272. {
  273. return (void *) LWP_GetSelf();
  274. } /* __PHYSFS_platformGetThreadID */
  275. void *__PHYSFS_platformCreateMutex(void)
  276. {
  277. mutex_t m;
  278. LWP_MutexInit(&m, true);
  279. return (void *) m;
  280. } /* __PHYSFS_platformCreateMutex */
  281. void __PHYSFS_platformDestroyMutex(void *mutex)
  282. {
  283. mutex_t m = (mutex_t) mutex;
  284. LWP_MutexDestroy(m);
  285. } /* __PHYSFS_platformDestroyMutex */
  286. int __PHYSFS_platformGrabMutex(void *mutex)
  287. {
  288. mutex_t m = (mutex_t) mutex;
  289. return LWP_MutexLock(m) == 0 ? 1 : 0;
  290. } /* __PHYSFS_platformGrabMutex */
  291. void __PHYSFS_platformReleaseMutex(void *mutex)
  292. {
  293. mutex_t m = (mutex_t) mutex;
  294. LWP_MutexUnlock(m);
  295. } /* __PHYSFS_platformReleaseMutex */
  296. int __PHYSFS_platformInit(const char *argv0)
  297. {
  298. return 1; /* always succeed. */
  299. } /* __PHYSFS_platformInit */
  300. void __PHYSFS_platformDeinit(void)
  301. {
  302. /* no-op */
  303. } /* __PHYSFS_platformDeinit */
  304. void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
  305. {
  306. } /* __PHYSFS_platformDetectAvailableCDs */
  307. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  308. {
  309. char *retval;
  310. const size_t bufsize = 128;
  311. retval = allocator.Malloc(bufsize);
  312. BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  313. if (getcwd(retval, bufsize - 1))
  314. {
  315. /* Make sure the path is slash-terminated */
  316. size_t length = strlen(retval);
  317. if (length > 0 && retval[length - 1] != '/')
  318. {
  319. retval[length++] = '/';
  320. retval[length] = '\0';
  321. }
  322. }
  323. else
  324. {
  325. strcpy(retval, "/");
  326. }
  327. return retval;
  328. } /* __PHYSFS_platformCalcBaseDir */
  329. char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
  330. {
  331. static const char subdir[] = "data/";
  332. return buildSubdirPath(subdir, sizeof(subdir));
  333. } /* __PHYSFS_platformCalcPrefDir */
  334. /* end of physfs_platform_unix.c ... */
  335. #endif /* PHYSFS_PLATFORM_OGC */
  336. /* end of physfs_platform_ogc.c ... */