physfs_platform_posix.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. void __PHYSFS_platformEnumerateFiles(const char *dirname,
  102. PHYSFS_EnumFilesCallback callback,
  103. const char *origdir,
  104. void *callbackdata)
  105. {
  106. DIR *dir;
  107. struct dirent *ent;
  108. char *buf = NULL;
  109. errno = 0;
  110. dir = opendir(dirname);
  111. if (dir == NULL)
  112. {
  113. allocator.Free(buf);
  114. return;
  115. } /* if */
  116. while ((ent = readdir(dir)) != NULL)
  117. {
  118. if (strcmp(ent->d_name, ".") == 0)
  119. continue;
  120. else if (strcmp(ent->d_name, "..") == 0)
  121. continue;
  122. callback(callbackdata, origdir, ent->d_name);
  123. } /* while */
  124. allocator.Free(buf);
  125. closedir(dir);
  126. } /* __PHYSFS_platformEnumerateFiles */
  127. int __PHYSFS_platformMkDir(const char *path)
  128. {
  129. const int rc = mkdir(path, S_IRWXU);
  130. BAIL_IF(rc == -1, errcodeFromErrno(), 0);
  131. return 1;
  132. } /* __PHYSFS_platformMkDir */
  133. static void *doOpen(const char *filename, int mode)
  134. {
  135. const int appending = (mode & O_APPEND);
  136. int fd;
  137. int *retval;
  138. errno = 0;
  139. /* O_APPEND doesn't actually behave as we'd like. */
  140. mode &= ~O_APPEND;
  141. fd = open(filename, mode, S_IRUSR | S_IWUSR);
  142. BAIL_IF(fd < 0, errcodeFromErrno(), NULL);
  143. if (appending)
  144. {
  145. if (lseek(fd, 0, SEEK_END) < 0)
  146. {
  147. const int err = errno;
  148. close(fd);
  149. BAIL(errcodeFromErrnoError(err), NULL);
  150. } /* if */
  151. } /* if */
  152. retval = (int *) allocator.Malloc(sizeof (int));
  153. if (!retval)
  154. {
  155. close(fd);
  156. BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  157. } /* if */
  158. *retval = fd;
  159. return ((void *) retval);
  160. } /* doOpen */
  161. void *__PHYSFS_platformOpenRead(const char *filename)
  162. {
  163. return doOpen(filename, O_RDONLY);
  164. } /* __PHYSFS_platformOpenRead */
  165. void *__PHYSFS_platformOpenWrite(const char *filename)
  166. {
  167. return doOpen(filename, O_WRONLY | O_CREAT | O_TRUNC);
  168. } /* __PHYSFS_platformOpenWrite */
  169. void *__PHYSFS_platformOpenAppend(const char *filename)
  170. {
  171. return doOpen(filename, O_WRONLY | O_CREAT | O_APPEND);
  172. } /* __PHYSFS_platformOpenAppend */
  173. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  174. PHYSFS_uint64 len)
  175. {
  176. const int fd = *((int *) opaque);
  177. ssize_t rc = 0;
  178. if (!__PHYSFS_ui64FitsAddressSpace(len))
  179. BAIL(PHYSFS_ERR_INVALID_ARGUMENT, -1);
  180. rc = read(fd, buffer, (size_t) len);
  181. BAIL_IF(rc == -1, errcodeFromErrno(), -1);
  182. assert(rc >= 0);
  183. assert(rc <= len);
  184. return (PHYSFS_sint64) rc;
  185. } /* __PHYSFS_platformRead */
  186. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  187. PHYSFS_uint64 len)
  188. {
  189. const int fd = *((int *) opaque);
  190. ssize_t rc = 0;
  191. if (!__PHYSFS_ui64FitsAddressSpace(len))
  192. BAIL(PHYSFS_ERR_INVALID_ARGUMENT, -1);
  193. rc = write(fd, (void *) buffer, (size_t) len);
  194. BAIL_IF(rc == -1, errcodeFromErrno(), rc);
  195. assert(rc >= 0);
  196. assert(rc <= len);
  197. return (PHYSFS_sint64) rc;
  198. } /* __PHYSFS_platformWrite */
  199. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  200. {
  201. const int fd = *((int *) opaque);
  202. const int rc = lseek(fd, (off_t) pos, SEEK_SET);
  203. BAIL_IF(rc == -1, errcodeFromErrno(), 0);
  204. return 1;
  205. } /* __PHYSFS_platformSeek */
  206. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  207. {
  208. const int fd = *((int *) opaque);
  209. PHYSFS_sint64 retval;
  210. retval = (PHYSFS_sint64) lseek(fd, 0, SEEK_CUR);
  211. BAIL_IF(retval == -1, errcodeFromErrno(), -1);
  212. return retval;
  213. } /* __PHYSFS_platformTell */
  214. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  215. {
  216. const int fd = *((int *) opaque);
  217. struct stat statbuf;
  218. BAIL_IF(fstat(fd, &statbuf) == -1, errcodeFromErrno(), -1);
  219. return ((PHYSFS_sint64) statbuf.st_size);
  220. } /* __PHYSFS_platformFileLength */
  221. int __PHYSFS_platformFlush(void *opaque)
  222. {
  223. const int fd = *((int *) opaque);
  224. if ((fcntl(fd, F_GETFL) & O_ACCMODE) != O_RDONLY)
  225. BAIL_IF(fsync(fd) == -1, errcodeFromErrno(), 0);
  226. return 1;
  227. } /* __PHYSFS_platformFlush */
  228. void __PHYSFS_platformClose(void *opaque)
  229. {
  230. const int fd = *((int *) opaque);
  231. (void) close(fd); /* we don't check this. You should have used flush! */
  232. allocator.Free(opaque);
  233. } /* __PHYSFS_platformClose */
  234. int __PHYSFS_platformDelete(const char *path)
  235. {
  236. BAIL_IF(remove(path) == -1, errcodeFromErrno(), 0);
  237. return 1;
  238. } /* __PHYSFS_platformDelete */
  239. int __PHYSFS_platformStat(const char *filename, PHYSFS_Stat *st)
  240. {
  241. struct stat statbuf;
  242. BAIL_IF(lstat(filename, &statbuf) == -1, errcodeFromErrno(), 0);
  243. if (S_ISREG(statbuf.st_mode))
  244. {
  245. st->filetype = PHYSFS_FILETYPE_REGULAR;
  246. st->filesize = statbuf.st_size;
  247. } /* if */
  248. else if(S_ISDIR(statbuf.st_mode))
  249. {
  250. st->filetype = PHYSFS_FILETYPE_DIRECTORY;
  251. st->filesize = 0;
  252. } /* else if */
  253. else if(S_ISLNK(statbuf.st_mode))
  254. {
  255. st->filetype = PHYSFS_FILETYPE_SYMLINK;
  256. st->filesize = 0;
  257. } /* else if */
  258. else
  259. {
  260. st->filetype = PHYSFS_FILETYPE_OTHER;
  261. st->filesize = statbuf.st_size;
  262. } /* else */
  263. st->modtime = statbuf.st_mtime;
  264. st->createtime = statbuf.st_ctime;
  265. st->accesstime = statbuf.st_atime;
  266. /* !!! FIXME: maybe we should just report full permissions? */
  267. st->readonly = access(filename, W_OK);
  268. return 1;
  269. } /* __PHYSFS_platformStat */
  270. typedef struct
  271. {
  272. pthread_mutex_t mutex;
  273. pthread_t owner;
  274. PHYSFS_uint32 count;
  275. } PthreadMutex;
  276. void *__PHYSFS_platformGetThreadID(void)
  277. {
  278. return ( (void *) ((size_t) pthread_self()) );
  279. } /* __PHYSFS_platformGetThreadID */
  280. void *__PHYSFS_platformCreateMutex(void)
  281. {
  282. int rc;
  283. PthreadMutex *m = (PthreadMutex *) allocator.Malloc(sizeof (PthreadMutex));
  284. BAIL_IF(!m, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  285. rc = pthread_mutex_init(&m->mutex, NULL);
  286. if (rc != 0)
  287. {
  288. allocator.Free(m);
  289. BAIL(PHYSFS_ERR_OS_ERROR, NULL);
  290. } /* if */
  291. m->count = 0;
  292. m->owner = (pthread_t) 0xDEADBEEF;
  293. return ((void *) m);
  294. } /* __PHYSFS_platformCreateMutex */
  295. void __PHYSFS_platformDestroyMutex(void *mutex)
  296. {
  297. PthreadMutex *m = (PthreadMutex *) mutex;
  298. /* Destroying a locked mutex is a bug, but we'll try to be helpful. */
  299. if ((m->owner == pthread_self()) && (m->count > 0))
  300. pthread_mutex_unlock(&m->mutex);
  301. pthread_mutex_destroy(&m->mutex);
  302. allocator.Free(m);
  303. } /* __PHYSFS_platformDestroyMutex */
  304. int __PHYSFS_platformGrabMutex(void *mutex)
  305. {
  306. PthreadMutex *m = (PthreadMutex *) mutex;
  307. pthread_t tid = pthread_self();
  308. if (m->owner != tid)
  309. {
  310. if (pthread_mutex_lock(&m->mutex) != 0)
  311. return 0;
  312. m->owner = tid;
  313. } /* if */
  314. m->count++;
  315. return 1;
  316. } /* __PHYSFS_platformGrabMutex */
  317. void __PHYSFS_platformReleaseMutex(void *mutex)
  318. {
  319. PthreadMutex *m = (PthreadMutex *) mutex;
  320. assert(m->owner == pthread_self()); /* catch programming errors. */
  321. assert(m->count > 0); /* catch programming errors. */
  322. if (m->owner == pthread_self())
  323. {
  324. if (--m->count == 0)
  325. {
  326. m->owner = (pthread_t) 0xDEADBEEF;
  327. pthread_mutex_unlock(&m->mutex);
  328. } /* if */
  329. } /* if */
  330. } /* __PHYSFS_platformReleaseMutex */
  331. #endif /* PHYSFS_PLATFORM_POSIX */
  332. /* end of physfs_platform_posix.c ... */