platform_posix.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. #if ((!defined PHYSFS_NO_THREAD_SUPPORT) && (!defined PHYSFS_PLATFORM_BEOS))
  20. #include <pthread.h>
  21. #endif
  22. #ifdef PHYSFS_HAVE_LLSEEK
  23. #include <linux/unistd.h>
  24. #endif
  25. #include "physfs_internal.h"
  26. const char *__PHYSFS_platformDirSeparator = "/";
  27. char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname)
  28. {
  29. const char *envr = getenv(varname);
  30. char *retval = NULL;
  31. if (envr != NULL)
  32. {
  33. retval = (char *) allocator.Malloc(strlen(envr) + 1);
  34. if (retval != NULL)
  35. strcpy(retval, envr);
  36. } /* if */
  37. return retval;
  38. } /* __PHYSFS_platformCopyEnvironmentVariable */
  39. static char *getUserNameByUID(void)
  40. {
  41. uid_t uid = getuid();
  42. struct passwd *pw;
  43. char *retval = NULL;
  44. pw = getpwuid(uid);
  45. if ((pw != NULL) && (pw->pw_name != NULL))
  46. {
  47. retval = (char *) allocator.Malloc(strlen(pw->pw_name) + 1);
  48. if (retval != NULL)
  49. strcpy(retval, pw->pw_name);
  50. } /* if */
  51. return retval;
  52. } /* getUserNameByUID */
  53. static char *getUserDirByUID(void)
  54. {
  55. uid_t uid = getuid();
  56. struct passwd *pw;
  57. char *retval = NULL;
  58. pw = getpwuid(uid);
  59. if ((pw != NULL) && (pw->pw_dir != NULL))
  60. {
  61. retval = (char *) allocator.Malloc(strlen(pw->pw_dir) + 1);
  62. if (retval != NULL)
  63. strcpy(retval, pw->pw_dir);
  64. } /* if */
  65. return retval;
  66. } /* getUserDirByUID */
  67. char *__PHYSFS_platformGetUserName(void)
  68. {
  69. char *retval = getUserNameByUID();
  70. if (retval == NULL)
  71. retval = __PHYSFS_platformCopyEnvironmentVariable("USER");
  72. return retval;
  73. } /* __PHYSFS_platformGetUserName */
  74. char *__PHYSFS_platformGetUserDir(void)
  75. {
  76. char *retval = __PHYSFS_platformCopyEnvironmentVariable("HOME");
  77. /* if the environment variable was set, make sure it's really a dir. */
  78. if (retval != NULL)
  79. {
  80. struct stat statbuf;
  81. if ((stat(retval, &statbuf) == -1) || (S_ISDIR(statbuf.st_mode) == 0))
  82. {
  83. allocator.Free(retval);
  84. retval = NULL;
  85. } /* if */
  86. } /* if */
  87. if (retval == NULL)
  88. retval = getUserDirByUID();
  89. return retval;
  90. } /* __PHYSFS_platformGetUserDir */
  91. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  92. const char *dirName,
  93. const char *append)
  94. {
  95. int len = ((prepend) ? strlen(prepend) : 0) +
  96. ((append) ? strlen(append) : 0) +
  97. strlen(dirName) + 1;
  98. char *retval = (char *) allocator.Malloc(len);
  99. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  100. /* platform-independent notation is Unix-style already. :) */
  101. if (prepend)
  102. strcpy(retval, prepend);
  103. else
  104. retval[0] = '\0';
  105. strcat(retval, dirName);
  106. if (append)
  107. strcat(retval, append);
  108. return retval;
  109. } /* __PHYSFS_platformCvtToDependent */
  110. void __PHYSFS_platformEnumerateFiles(const char *dirname,
  111. int omitSymLinks,
  112. PHYSFS_EnumFilesCallback callback,
  113. const char *origdir,
  114. void *callbackdata)
  115. {
  116. DIR *dir;
  117. struct dirent *ent;
  118. int bufsize = 0;
  119. char *buf = NULL;
  120. int dlen = 0;
  121. if (omitSymLinks) /* !!! FIXME: this malloc sucks. */
  122. {
  123. dlen = strlen(dirname);
  124. bufsize = dlen + 256;
  125. buf = (char *) allocator.Malloc(bufsize);
  126. if (buf == NULL)
  127. return;
  128. strcpy(buf, dirname);
  129. if (buf[dlen - 1] != '/')
  130. {
  131. buf[dlen++] = '/';
  132. buf[dlen] = '\0';
  133. } /* if */
  134. } /* if */
  135. errno = 0;
  136. dir = opendir(dirname);
  137. if (dir == NULL)
  138. {
  139. allocator.Free(buf);
  140. return;
  141. } /* if */
  142. while ((ent = readdir(dir)) != NULL)
  143. {
  144. if (strcmp(ent->d_name, ".") == 0)
  145. continue;
  146. if (strcmp(ent->d_name, "..") == 0)
  147. continue;
  148. if (omitSymLinks)
  149. {
  150. PHYSFS_Stat statbuf;
  151. int exists = 0;
  152. char *p;
  153. int len = strlen(ent->d_name) + dlen + 1;
  154. if (len > bufsize)
  155. {
  156. p = (char *) allocator.Realloc(buf, len);
  157. if (p == NULL)
  158. continue;
  159. buf = p;
  160. bufsize = len;
  161. } /* if */
  162. strcpy(buf + dlen, ent->d_name);
  163. if (!__PHYSFS_platformStat(buf, &exists, &statbuf))
  164. continue;
  165. else if (!exists)
  166. continue; /* probably can't happen, but just in case. */
  167. else if (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK)
  168. continue;
  169. } /* if */
  170. callback(callbackdata, origdir, ent->d_name);
  171. } /* while */
  172. allocator.Free(buf);
  173. closedir(dir);
  174. } /* __PHYSFS_platformEnumerateFiles */
  175. int __PHYSFS_platformMkDir(const char *path)
  176. {
  177. int rc;
  178. errno = 0;
  179. rc = mkdir(path, S_IRWXU);
  180. BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
  181. return 1;
  182. } /* __PHYSFS_platformMkDir */
  183. static void *doOpen(const char *filename, int mode)
  184. {
  185. const int appending = (mode & O_APPEND);
  186. int fd;
  187. int *retval;
  188. errno = 0;
  189. /* O_APPEND doesn't actually behave as we'd like. */
  190. mode &= ~O_APPEND;
  191. fd = open(filename, mode, S_IRUSR | S_IWUSR);
  192. BAIL_IF_MACRO(fd < 0, strerror(errno), NULL);
  193. if (appending)
  194. {
  195. if (lseek(fd, 0, SEEK_END) < 0)
  196. {
  197. close(fd);
  198. BAIL_MACRO(strerror(errno), NULL);
  199. } /* if */
  200. } /* if */
  201. retval = (int *) allocator.Malloc(sizeof (int));
  202. if (retval == NULL)
  203. {
  204. close(fd);
  205. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  206. } /* if */
  207. *retval = fd;
  208. return ((void *) retval);
  209. } /* doOpen */
  210. void *__PHYSFS_platformOpenRead(const char *filename)
  211. {
  212. return doOpen(filename, O_RDONLY);
  213. } /* __PHYSFS_platformOpenRead */
  214. void *__PHYSFS_platformOpenWrite(const char *filename)
  215. {
  216. return doOpen(filename, O_WRONLY | O_CREAT | O_TRUNC);
  217. } /* __PHYSFS_platformOpenWrite */
  218. void *__PHYSFS_platformOpenAppend(const char *filename)
  219. {
  220. return doOpen(filename, O_WRONLY | O_CREAT | O_APPEND);
  221. } /* __PHYSFS_platformOpenAppend */
  222. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  223. PHYSFS_uint64 len)
  224. {
  225. const int fd = *((int *) opaque);
  226. ssize_t rc = 0;
  227. BAIL_IF_MACRO(!__PHYSFS_ui64FitsAddressSpace(len),ERR_INVALID_ARGUMENT,-1);
  228. rc = read(fd, buffer, (size_t) len);
  229. BAIL_IF_MACRO(rc == -1, strerror(errno), (PHYSFS_sint64) rc);
  230. assert(rc >= 0);
  231. assert(rc <= len);
  232. return (PHYSFS_sint64) rc;
  233. } /* __PHYSFS_platformRead */
  234. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  235. PHYSFS_uint64 len)
  236. {
  237. const int fd = *((int *) opaque);
  238. ssize_t rc = 0;
  239. BAIL_IF_MACRO(!__PHYSFS_ui64FitsAddressSpace(len),ERR_INVALID_ARGUMENT,-1);
  240. rc = write(fd, (void *) buffer, (size_t) len);
  241. BAIL_IF_MACRO(rc == -1, strerror(errno), rc);
  242. assert(rc >= 0);
  243. assert(rc <= len);
  244. return (PHYSFS_sint64) rc;
  245. } /* __PHYSFS_platformWrite */
  246. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  247. {
  248. const int fd = *((int *) opaque);
  249. #ifdef PHYSFS_HAVE_LLSEEK
  250. unsigned long offset_high = ((pos >> 32) & 0xFFFFFFFF);
  251. unsigned long offset_low = (pos & 0xFFFFFFFF);
  252. loff_t retoffset;
  253. int rc = llseek(fd, offset_high, offset_low, &retoffset, SEEK_SET);
  254. BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
  255. #else
  256. BAIL_IF_MACRO(lseek(fd, (int) pos, SEEK_SET) == -1, strerror(errno), 0);
  257. #endif
  258. return 1;
  259. } /* __PHYSFS_platformSeek */
  260. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  261. {
  262. const int fd = *((int *) opaque);
  263. PHYSFS_sint64 retval;
  264. #ifdef PHYSFS_HAVE_LLSEEK
  265. loff_t retoffset;
  266. int rc = llseek(fd, 0, &retoffset, SEEK_CUR);
  267. BAIL_IF_MACRO(rc == -1, strerror(errno), -1);
  268. retval = (PHYSFS_sint64) retoffset;
  269. #else
  270. retval = (PHYSFS_sint64) lseek(fd, 0, SEEK_CUR);
  271. BAIL_IF_MACRO(retval == -1, strerror(errno), -1);
  272. #endif
  273. return retval;
  274. } /* __PHYSFS_platformTell */
  275. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  276. {
  277. const int fd = *((int *) opaque);
  278. struct stat statbuf;
  279. BAIL_IF_MACRO(fstat(fd, &statbuf) == -1, strerror(errno), -1);
  280. return ((PHYSFS_sint64) statbuf.st_size);
  281. } /* __PHYSFS_platformFileLength */
  282. int __PHYSFS_platformFlush(void *opaque)
  283. {
  284. const int fd = *((int *) opaque);
  285. BAIL_IF_MACRO(fsync(fd) == -1, strerror(errno), 0);
  286. return 1;
  287. } /* __PHYSFS_platformFlush */
  288. void __PHYSFS_platformClose(void *opaque)
  289. {
  290. const int fd = *((int *) opaque);
  291. (void) close(fd); /* we don't check this. You should have used flush! */
  292. allocator.Free(opaque);
  293. } /* __PHYSFS_platformClose */
  294. int __PHYSFS_platformDelete(const char *path)
  295. {
  296. BAIL_IF_MACRO(remove(path) == -1, strerror(errno), 0);
  297. return 1;
  298. } /* __PHYSFS_platformDelete */
  299. int __PHYSFS_platformStat(const char *filename, int *exists, PHYSFS_Stat *st)
  300. {
  301. struct stat statbuf;
  302. if (lstat(filename, &statbuf) == -1)
  303. {
  304. *exists = (errno == ENOENT);
  305. BAIL_MACRO(strerror(errno), 0);
  306. } /* if */
  307. *exists = 1;
  308. if (S_ISREG(statbuf.st_mode))
  309. {
  310. st->filetype = PHYSFS_FILETYPE_REGULAR;
  311. st->filesize = statbuf.st_size;
  312. } /* if */
  313. else if(S_ISDIR(statbuf.st_mode))
  314. {
  315. st->filetype = PHYSFS_FILETYPE_DIRECTORY;
  316. st->filesize = 0;
  317. } /* else if */
  318. else
  319. {
  320. st->filetype = PHYSFS_FILETYPE_OTHER;
  321. st->filesize = statbuf.st_size;
  322. } /* else */
  323. st->modtime = statbuf.st_mtime;
  324. st->createtime = statbuf.st_ctime;
  325. st->accesstime = statbuf.st_atime;
  326. /* !!! FIXME: maybe we should just report full permissions? */
  327. st->readonly = access(filename, W_OK);
  328. return 1;
  329. } /* __PHYSFS_platformStat */
  330. #ifndef PHYSFS_PLATFORM_BEOS /* BeOS has its own code in platform_beos.cpp */
  331. #if (defined PHYSFS_NO_THREAD_SUPPORT)
  332. void *__PHYSFS_platformGetThreadID(void) { return ((void *) 0x0001); }
  333. void *__PHYSFS_platformCreateMutex(void) { return ((void *) 0x0001); }
  334. void __PHYSFS_platformDestroyMutex(void *mutex) {}
  335. int __PHYSFS_platformGrabMutex(void *mutex) { return 1; }
  336. void __PHYSFS_platformReleaseMutex(void *mutex) {}
  337. #else
  338. typedef struct
  339. {
  340. pthread_mutex_t mutex;
  341. pthread_t owner;
  342. PHYSFS_uint32 count;
  343. } PthreadMutex;
  344. void *__PHYSFS_platformGetThreadID(void)
  345. {
  346. return ( (void *) ((size_t) pthread_self()) );
  347. } /* __PHYSFS_platformGetThreadID */
  348. void *__PHYSFS_platformCreateMutex(void)
  349. {
  350. int rc;
  351. PthreadMutex *m = (PthreadMutex *) allocator.Malloc(sizeof (PthreadMutex));
  352. BAIL_IF_MACRO(m == NULL, ERR_OUT_OF_MEMORY, NULL);
  353. rc = pthread_mutex_init(&m->mutex, NULL);
  354. if (rc != 0)
  355. {
  356. allocator.Free(m);
  357. BAIL_MACRO(strerror(rc), NULL);
  358. } /* if */
  359. m->count = 0;
  360. m->owner = (pthread_t) 0xDEADBEEF;
  361. return ((void *) m);
  362. } /* __PHYSFS_platformCreateMutex */
  363. void __PHYSFS_platformDestroyMutex(void *mutex)
  364. {
  365. PthreadMutex *m = (PthreadMutex *) mutex;
  366. /* Destroying a locked mutex is a bug, but we'll try to be helpful. */
  367. if ((m->owner == pthread_self()) && (m->count > 0))
  368. pthread_mutex_unlock(&m->mutex);
  369. pthread_mutex_destroy(&m->mutex);
  370. allocator.Free(m);
  371. } /* __PHYSFS_platformDestroyMutex */
  372. int __PHYSFS_platformGrabMutex(void *mutex)
  373. {
  374. PthreadMutex *m = (PthreadMutex *) mutex;
  375. pthread_t tid = pthread_self();
  376. if (m->owner != tid)
  377. {
  378. if (pthread_mutex_lock(&m->mutex) != 0)
  379. return 0;
  380. m->owner = tid;
  381. } /* if */
  382. m->count++;
  383. return 1;
  384. } /* __PHYSFS_platformGrabMutex */
  385. void __PHYSFS_platformReleaseMutex(void *mutex)
  386. {
  387. PthreadMutex *m = (PthreadMutex *) mutex;
  388. if (m->owner == pthread_self())
  389. {
  390. if (--m->count == 0)
  391. {
  392. m->owner = (pthread_t) 0xDEADBEEF;
  393. pthread_mutex_unlock(&m->mutex);
  394. } /* if */
  395. } /* if */
  396. } /* __PHYSFS_platformReleaseMutex */
  397. #endif /* !PHYSFS_NO_THREAD_SUPPORT */
  398. #endif /* !PHYSFS_PLATFORM_BEOS */
  399. #endif /* PHYSFS_PLATFORM_POSIX */
  400. /* end of posix.c ... */