platform_posix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. BAIL_IF_MACRO(fsync(fd) == -1, errcodeFromErrno(), 0);
  227. return 1;
  228. } /* __PHYSFS_platformFlush */
  229. void __PHYSFS_platformClose(void *opaque)
  230. {
  231. const int fd = *((int *) opaque);
  232. (void) close(fd); /* we don't check this. You should have used flush! */
  233. allocator.Free(opaque);
  234. } /* __PHYSFS_platformClose */
  235. int __PHYSFS_platformDelete(const char *path)
  236. {
  237. BAIL_IF_MACRO(remove(path) == -1, errcodeFromErrno(), 0);
  238. return 1;
  239. } /* __PHYSFS_platformDelete */
  240. int __PHYSFS_platformStat(const char *filename, int *exists, PHYSFS_Stat *st)
  241. {
  242. struct stat statbuf;
  243. if (lstat(filename, &statbuf) == -1)
  244. {
  245. *exists = (errno != ENOENT);
  246. BAIL_MACRO(errcodeFromErrno(), 0);
  247. } /* if */
  248. *exists = 1;
  249. if (S_ISREG(statbuf.st_mode))
  250. {
  251. st->filetype = PHYSFS_FILETYPE_REGULAR;
  252. st->filesize = statbuf.st_size;
  253. } /* if */
  254. else if(S_ISDIR(statbuf.st_mode))
  255. {
  256. st->filetype = PHYSFS_FILETYPE_DIRECTORY;
  257. st->filesize = 0;
  258. } /* else if */
  259. else if(S_ISLNK(statbuf.st_mode))
  260. {
  261. st->filetype = PHYSFS_FILETYPE_SYMLINK;
  262. st->filesize = 0;
  263. } /* else if */
  264. else
  265. {
  266. st->filetype = PHYSFS_FILETYPE_OTHER;
  267. st->filesize = statbuf.st_size;
  268. } /* else */
  269. st->modtime = statbuf.st_mtime;
  270. st->createtime = statbuf.st_ctime;
  271. st->accesstime = statbuf.st_atime;
  272. /* !!! FIXME: maybe we should just report full permissions? */
  273. st->readonly = access(filename, W_OK);
  274. return 1;
  275. } /* __PHYSFS_platformStat */
  276. #ifndef PHYSFS_PLATFORM_BEOS /* BeOS has its own code in platform_beos.cpp */
  277. #if (defined PHYSFS_NO_THREAD_SUPPORT)
  278. void *__PHYSFS_platformGetThreadID(void) { return ((void *) 0x0001); }
  279. void *__PHYSFS_platformCreateMutex(void) { return ((void *) 0x0001); }
  280. void __PHYSFS_platformDestroyMutex(void *mutex) {}
  281. int __PHYSFS_platformGrabMutex(void *mutex) { return 1; }
  282. void __PHYSFS_platformReleaseMutex(void *mutex) {}
  283. #else
  284. typedef struct
  285. {
  286. pthread_mutex_t mutex;
  287. pthread_t owner;
  288. PHYSFS_uint32 count;
  289. } PthreadMutex;
  290. void *__PHYSFS_platformGetThreadID(void)
  291. {
  292. return ( (void *) ((size_t) pthread_self()) );
  293. } /* __PHYSFS_platformGetThreadID */
  294. void *__PHYSFS_platformCreateMutex(void)
  295. {
  296. int rc;
  297. PthreadMutex *m = (PthreadMutex *) allocator.Malloc(sizeof (PthreadMutex));
  298. BAIL_IF_MACRO(!m, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  299. rc = pthread_mutex_init(&m->mutex, NULL);
  300. if (rc != 0)
  301. {
  302. allocator.Free(m);
  303. BAIL_MACRO(PHYSFS_ERR_OS_ERROR, NULL);
  304. } /* if */
  305. m->count = 0;
  306. m->owner = (pthread_t) 0xDEADBEEF;
  307. return ((void *) m);
  308. } /* __PHYSFS_platformCreateMutex */
  309. void __PHYSFS_platformDestroyMutex(void *mutex)
  310. {
  311. PthreadMutex *m = (PthreadMutex *) mutex;
  312. /* Destroying a locked mutex is a bug, but we'll try to be helpful. */
  313. if ((m->owner == pthread_self()) && (m->count > 0))
  314. pthread_mutex_unlock(&m->mutex);
  315. pthread_mutex_destroy(&m->mutex);
  316. allocator.Free(m);
  317. } /* __PHYSFS_platformDestroyMutex */
  318. int __PHYSFS_platformGrabMutex(void *mutex)
  319. {
  320. PthreadMutex *m = (PthreadMutex *) mutex;
  321. pthread_t tid = pthread_self();
  322. if (m->owner != tid)
  323. {
  324. if (pthread_mutex_lock(&m->mutex) != 0)
  325. return 0;
  326. m->owner = tid;
  327. } /* if */
  328. m->count++;
  329. return 1;
  330. } /* __PHYSFS_platformGrabMutex */
  331. void __PHYSFS_platformReleaseMutex(void *mutex)
  332. {
  333. PthreadMutex *m = (PthreadMutex *) mutex;
  334. assert(m->owner == pthread_self()); /* catch programming errors. */
  335. assert(m->count > 0); /* catch programming errors. */
  336. if (m->owner == pthread_self())
  337. {
  338. if (--m->count == 0)
  339. {
  340. m->owner = (pthread_t) 0xDEADBEEF;
  341. pthread_mutex_unlock(&m->mutex);
  342. } /* if */
  343. } /* if */
  344. } /* __PHYSFS_platformReleaseMutex */
  345. #endif /* !PHYSFS_NO_THREAD_SUPPORT */
  346. #endif /* !PHYSFS_PLATFORM_BEOS */
  347. #endif /* PHYSFS_PLATFORM_POSIX */
  348. /* end of posix.c ... */