physfs_platform_posix.c 10 KB

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