physfs_platform_posix.c 11 KB

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