physfs_platform_posix.c 11 KB

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