physfs_platform_posix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. #if !defined(O_CLOEXEC) && defined(FD_CLOEXEC)
  131. static inline void set_CLOEXEC(int fildes)
  132. {
  133. int flags = fcntl(fildes, F_GETFD);
  134. if (flags != -1) {
  135. fcntl(fildes, F_SETFD, flags | FD_CLOEXEC);
  136. }
  137. }
  138. #endif
  139. static void *doOpen(const char *filename, int mode)
  140. {
  141. const int appending = (mode & O_APPEND);
  142. int fd;
  143. int *retval;
  144. errno = 0;
  145. /* O_APPEND doesn't actually behave as we'd like. */
  146. mode &= ~O_APPEND;
  147. #ifdef O_CLOEXEC
  148. /* Add O_CLOEXEC if defined */
  149. mode |= O_CLOEXEC;
  150. #endif
  151. do {
  152. fd = open(filename, mode, S_IRUSR | S_IWUSR);
  153. } while ((fd < 0) && (errno == EINTR));
  154. BAIL_IF(fd < 0, errcodeFromErrno(), NULL);
  155. #if !defined(O_CLOEXEC) && defined(FD_CLOEXEC)
  156. set_CLOEXEC(fd);
  157. #endif
  158. if (appending)
  159. {
  160. if (lseek(fd, 0, SEEK_END) < 0)
  161. {
  162. const int err = errno;
  163. close(fd);
  164. BAIL(errcodeFromErrnoError(err), NULL);
  165. } /* if */
  166. } /* if */
  167. retval = (int *) allocator.Malloc(sizeof (int));
  168. if (!retval)
  169. {
  170. close(fd);
  171. BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  172. } /* if */
  173. *retval = fd;
  174. return ((void *) retval);
  175. } /* doOpen */
  176. void *__PHYSFS_platformOpenRead(const char *filename)
  177. {
  178. return doOpen(filename, O_RDONLY);
  179. } /* __PHYSFS_platformOpenRead */
  180. void *__PHYSFS_platformOpenWrite(const char *filename)
  181. {
  182. return doOpen(filename, O_WRONLY | O_CREAT | O_TRUNC);
  183. } /* __PHYSFS_platformOpenWrite */
  184. void *__PHYSFS_platformOpenAppend(const char *filename)
  185. {
  186. return doOpen(filename, O_WRONLY | O_CREAT | O_APPEND);
  187. } /* __PHYSFS_platformOpenAppend */
  188. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  189. PHYSFS_uint64 len)
  190. {
  191. const int fd = *((int *) opaque);
  192. ssize_t rc = 0;
  193. if (!__PHYSFS_ui64FitsAddressSpace(len))
  194. BAIL(PHYSFS_ERR_INVALID_ARGUMENT, -1);
  195. do {
  196. rc = read(fd, buffer, (size_t) len);
  197. } while ((rc == -1) && (errno == EINTR));
  198. BAIL_IF(rc == -1, errcodeFromErrno(), -1);
  199. assert(rc >= 0);
  200. assert(rc <= len);
  201. return (PHYSFS_sint64) rc;
  202. } /* __PHYSFS_platformRead */
  203. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  204. PHYSFS_uint64 len)
  205. {
  206. const int fd = *((int *) opaque);
  207. ssize_t rc = 0;
  208. if (!__PHYSFS_ui64FitsAddressSpace(len))
  209. BAIL(PHYSFS_ERR_INVALID_ARGUMENT, -1);
  210. do {
  211. rc = write(fd, (void *) buffer, (size_t) len);
  212. } while ((rc == -1) && (errno == EINTR));
  213. BAIL_IF(rc == -1, errcodeFromErrno(), rc);
  214. assert(rc >= 0);
  215. assert(rc <= len);
  216. return (PHYSFS_sint64) rc;
  217. } /* __PHYSFS_platformWrite */
  218. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  219. {
  220. const int fd = *((int *) opaque);
  221. const off_t rc = lseek(fd, (off_t) pos, SEEK_SET);
  222. BAIL_IF(rc == -1, errcodeFromErrno(), 0);
  223. return 1;
  224. } /* __PHYSFS_platformSeek */
  225. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  226. {
  227. const int fd = *((int *) opaque);
  228. PHYSFS_sint64 retval;
  229. retval = (PHYSFS_sint64) lseek(fd, 0, SEEK_CUR);
  230. BAIL_IF(retval == -1, errcodeFromErrno(), -1);
  231. return retval;
  232. } /* __PHYSFS_platformTell */
  233. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  234. {
  235. const int fd = *((int *) opaque);
  236. struct stat statbuf;
  237. BAIL_IF(fstat(fd, &statbuf) == -1, errcodeFromErrno(), -1);
  238. return ((PHYSFS_sint64) statbuf.st_size);
  239. } /* __PHYSFS_platformFileLength */
  240. int __PHYSFS_platformFlush(void *opaque)
  241. {
  242. const int fd = *((int *) opaque);
  243. int rc = -1;
  244. if ((fcntl(fd, F_GETFL) & O_ACCMODE) != O_RDONLY) {
  245. do {
  246. rc = fsync(fd);
  247. } while ((rc == -1) && (errno == EINTR));
  248. BAIL_IF(rc == -1, errcodeFromErrno(), 0);
  249. }
  250. return 1;
  251. } /* __PHYSFS_platformFlush */
  252. void __PHYSFS_platformClose(void *opaque)
  253. {
  254. const int fd = *((int *) opaque);
  255. int rc = -1;
  256. do {
  257. rc = close(fd); /* we don't check this. You should have used flush! */
  258. } while ((rc == -1) && (errno == EINTR));
  259. allocator.Free(opaque);
  260. } /* __PHYSFS_platformClose */
  261. int __PHYSFS_platformDelete(const char *path)
  262. {
  263. BAIL_IF(remove(path) == -1, errcodeFromErrno(), 0);
  264. return 1;
  265. } /* __PHYSFS_platformDelete */
  266. int __PHYSFS_platformStat(const char *fname, PHYSFS_Stat *st, const int follow)
  267. {
  268. struct stat statbuf;
  269. const int rc = follow ? stat(fname, &statbuf) : lstat(fname, &statbuf);
  270. BAIL_IF(rc == -1, errcodeFromErrno(), 0);
  271. if (S_ISREG(statbuf.st_mode))
  272. {
  273. st->filetype = PHYSFS_FILETYPE_REGULAR;
  274. st->filesize = statbuf.st_size;
  275. } /* if */
  276. else if(S_ISDIR(statbuf.st_mode))
  277. {
  278. st->filetype = PHYSFS_FILETYPE_DIRECTORY;
  279. st->filesize = 0;
  280. } /* else if */
  281. else if(S_ISLNK(statbuf.st_mode))
  282. {
  283. st->filetype = PHYSFS_FILETYPE_SYMLINK;
  284. st->filesize = 0;
  285. } /* else if */
  286. else
  287. {
  288. st->filetype = PHYSFS_FILETYPE_OTHER;
  289. st->filesize = statbuf.st_size;
  290. } /* else */
  291. st->modtime = statbuf.st_mtime;
  292. st->createtime = statbuf.st_ctime;
  293. st->accesstime = statbuf.st_atime;
  294. st->readonly = (access(fname, W_OK) == -1);
  295. return 1;
  296. } /* __PHYSFS_platformStat */
  297. typedef struct
  298. {
  299. pthread_mutex_t mutex;
  300. pthread_t owner;
  301. PHYSFS_uint32 count;
  302. } PthreadMutex;
  303. void *__PHYSFS_platformGetThreadID(void)
  304. {
  305. return ( (void *) ((size_t) pthread_self()) );
  306. } /* __PHYSFS_platformGetThreadID */
  307. void *__PHYSFS_platformCreateMutex(void)
  308. {
  309. int rc;
  310. PthreadMutex *m = (PthreadMutex *) allocator.Malloc(sizeof (PthreadMutex));
  311. BAIL_IF(!m, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  312. rc = pthread_mutex_init(&m->mutex, NULL);
  313. if (rc != 0)
  314. {
  315. allocator.Free(m);
  316. BAIL(PHYSFS_ERR_OS_ERROR, NULL);
  317. } /* if */
  318. m->count = 0;
  319. m->owner = (pthread_t) 0xDEADBEEF;
  320. return ((void *) m);
  321. } /* __PHYSFS_platformCreateMutex */
  322. void __PHYSFS_platformDestroyMutex(void *mutex)
  323. {
  324. PthreadMutex *m = (PthreadMutex *) mutex;
  325. /* Destroying a locked mutex is a bug, but we'll try to be helpful. */
  326. if ((m->owner == pthread_self()) && (m->count > 0))
  327. pthread_mutex_unlock(&m->mutex);
  328. pthread_mutex_destroy(&m->mutex);
  329. allocator.Free(m);
  330. } /* __PHYSFS_platformDestroyMutex */
  331. int __PHYSFS_platformGrabMutex(void *mutex)
  332. {
  333. PthreadMutex *m = (PthreadMutex *) mutex;
  334. pthread_t tid = pthread_self();
  335. if (m->owner != tid)
  336. {
  337. if (pthread_mutex_lock(&m->mutex) != 0)
  338. return 0;
  339. m->owner = tid;
  340. } /* if */
  341. m->count++;
  342. return 1;
  343. } /* __PHYSFS_platformGrabMutex */
  344. void __PHYSFS_platformReleaseMutex(void *mutex)
  345. {
  346. PthreadMutex *m = (PthreadMutex *) mutex;
  347. assert(m->owner == pthread_self()); /* catch programming errors. */
  348. assert(m->count > 0); /* catch programming errors. */
  349. if (m->owner == pthread_self())
  350. {
  351. if (--m->count == 0)
  352. {
  353. m->owner = (pthread_t) 0xDEADBEEF;
  354. pthread_mutex_unlock(&m->mutex);
  355. } /* if */
  356. } /* if */
  357. } /* __PHYSFS_platformReleaseMutex */
  358. #endif /* PHYSFS_PLATFORM_POSIX */
  359. /* end of physfs_platform_posix.c ... */