platform_posix.c 11 KB

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