platform_posix.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. char *__PHYSFS_platformCurrentDir(void)
  176. {
  177. int allocSize = 64;
  178. char *retval = NULL;
  179. char *ptr;
  180. do
  181. {
  182. allocSize *= 2;
  183. ptr = (char *) allocator.Realloc(retval, allocSize);
  184. if (ptr == NULL)
  185. {
  186. if (retval != NULL)
  187. allocator.Free(retval);
  188. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  189. } /* if */
  190. retval = ptr;
  191. ptr = getcwd(retval, allocSize);
  192. } while (ptr == NULL && errno == ERANGE);
  193. if (ptr == NULL && errno)
  194. {
  195. /* getcwd() failed , for example current directory not existing... */
  196. if (retval != NULL)
  197. allocator.Free(retval);
  198. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  199. } /* if */
  200. /* try to shrink buffer... */
  201. ptr = (char *) allocator.Realloc(retval, strlen(retval) + 1);
  202. if (ptr != NULL)
  203. retval = ptr; /* oh well if it failed. */
  204. return retval;
  205. } /* __PHYSFS_platformCurrentDir */
  206. int __PHYSFS_platformMkDir(const char *path)
  207. {
  208. int rc;
  209. errno = 0;
  210. rc = mkdir(path, S_IRWXU);
  211. BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
  212. return 1;
  213. } /* __PHYSFS_platformMkDir */
  214. static void *doOpen(const char *filename, int mode)
  215. {
  216. const int appending = (mode & O_APPEND);
  217. int fd;
  218. int *retval;
  219. errno = 0;
  220. /* O_APPEND doesn't actually behave as we'd like. */
  221. mode &= ~O_APPEND;
  222. fd = open(filename, mode, S_IRUSR | S_IWUSR);
  223. BAIL_IF_MACRO(fd < 0, strerror(errno), NULL);
  224. if (appending)
  225. {
  226. if (lseek(fd, 0, SEEK_END) < 0)
  227. {
  228. close(fd);
  229. BAIL_MACRO(strerror(errno), NULL);
  230. } /* if */
  231. } /* if */
  232. retval = (int *) allocator.Malloc(sizeof (int));
  233. if (retval == NULL)
  234. {
  235. close(fd);
  236. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  237. } /* if */
  238. *retval = fd;
  239. return ((void *) retval);
  240. } /* doOpen */
  241. void *__PHYSFS_platformOpenRead(const char *filename)
  242. {
  243. return doOpen(filename, O_RDONLY);
  244. } /* __PHYSFS_platformOpenRead */
  245. void *__PHYSFS_platformOpenWrite(const char *filename)
  246. {
  247. return doOpen(filename, O_WRONLY | O_CREAT | O_TRUNC);
  248. } /* __PHYSFS_platformOpenWrite */
  249. void *__PHYSFS_platformOpenAppend(const char *filename)
  250. {
  251. return doOpen(filename, O_WRONLY | O_CREAT | O_APPEND);
  252. } /* __PHYSFS_platformOpenAppend */
  253. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  254. PHYSFS_uint64 len)
  255. {
  256. const int fd = *((int *) opaque);
  257. ssize_t rc = 0;
  258. BAIL_IF_MACRO(!__PHYSFS_ui64FitsAddressSpace(len),ERR_INVALID_ARGUMENT,-1);
  259. rc = read(fd, buffer, (size_t) len);
  260. BAIL_IF_MACRO(rc == -1, strerror(errno), (PHYSFS_sint64) rc);
  261. assert(rc >= 0);
  262. assert(rc <= len);
  263. return (PHYSFS_sint64) rc;
  264. } /* __PHYSFS_platformRead */
  265. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  266. PHYSFS_uint64 len)
  267. {
  268. const int fd = *((int *) opaque);
  269. ssize_t rc = 0;
  270. BAIL_IF_MACRO(!__PHYSFS_ui64FitsAddressSpace(len),ERR_INVALID_ARGUMENT,-1);
  271. rc = write(fd, (void *) buffer, (size_t) len);
  272. BAIL_IF_MACRO(rc == -1, strerror(errno), rc);
  273. assert(rc >= 0);
  274. assert(rc <= len);
  275. return (PHYSFS_sint64) rc;
  276. } /* __PHYSFS_platformWrite */
  277. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  278. {
  279. const int fd = *((int *) opaque);
  280. #ifdef PHYSFS_HAVE_LLSEEK
  281. unsigned long offset_high = ((pos >> 32) & 0xFFFFFFFF);
  282. unsigned long offset_low = (pos & 0xFFFFFFFF);
  283. loff_t retoffset;
  284. int rc = llseek(fd, offset_high, offset_low, &retoffset, SEEK_SET);
  285. BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
  286. #else
  287. BAIL_IF_MACRO(lseek(fd, (int) pos, SEEK_SET) == -1, strerror(errno), 0);
  288. #endif
  289. return 1;
  290. } /* __PHYSFS_platformSeek */
  291. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  292. {
  293. const int fd = *((int *) opaque);
  294. PHYSFS_sint64 retval;
  295. #ifdef PHYSFS_HAVE_LLSEEK
  296. loff_t retoffset;
  297. int rc = llseek(fd, 0, &retoffset, SEEK_CUR);
  298. BAIL_IF_MACRO(rc == -1, strerror(errno), -1);
  299. retval = (PHYSFS_sint64) retoffset;
  300. #else
  301. retval = (PHYSFS_sint64) lseek(fd, 0, SEEK_CUR);
  302. BAIL_IF_MACRO(retval == -1, strerror(errno), -1);
  303. #endif
  304. return retval;
  305. } /* __PHYSFS_platformTell */
  306. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  307. {
  308. const int fd = *((int *) opaque);
  309. struct stat statbuf;
  310. BAIL_IF_MACRO(fstat(fd, &statbuf) == -1, strerror(errno), -1);
  311. return ((PHYSFS_sint64) statbuf.st_size);
  312. } /* __PHYSFS_platformFileLength */
  313. int __PHYSFS_platformFlush(void *opaque)
  314. {
  315. const int fd = *((int *) opaque);
  316. BAIL_IF_MACRO(fsync(fd) == -1, strerror(errno), 0);
  317. return 1;
  318. } /* __PHYSFS_platformFlush */
  319. void __PHYSFS_platformClose(void *opaque)
  320. {
  321. const int fd = *((int *) opaque);
  322. (void) close(fd); /* we don't check this. You should have used flush! */
  323. allocator.Free(opaque);
  324. } /* __PHYSFS_platformClose */
  325. int __PHYSFS_platformDelete(const char *path)
  326. {
  327. BAIL_IF_MACRO(remove(path) == -1, strerror(errno), 0);
  328. return 1;
  329. } /* __PHYSFS_platformDelete */
  330. int __PHYSFS_platformStat(const char *filename, int *exists, PHYSFS_Stat *st)
  331. {
  332. struct stat statbuf;
  333. if (lstat(filename, &statbuf) == -1)
  334. {
  335. *exists = (errno == ENOENT);
  336. BAIL_MACRO(strerror(errno), 0);
  337. } /* if */
  338. *exists = 1;
  339. if (S_ISREG(statbuf.st_mode))
  340. {
  341. st->filetype = PHYSFS_FILETYPE_REGULAR;
  342. st->filesize = statbuf.st_size;
  343. } /* if */
  344. else if(S_ISDIR(statbuf.st_mode))
  345. {
  346. st->filetype = PHYSFS_FILETYPE_DIRECTORY;
  347. st->filesize = 0;
  348. } /* else if */
  349. else
  350. {
  351. st->filetype = PHYSFS_FILETYPE_OTHER;
  352. st->filesize = statbuf.st_size;
  353. } /* else */
  354. st->modtime = statbuf.st_mtime;
  355. st->createtime = statbuf.st_ctime;
  356. st->accesstime = statbuf.st_atime;
  357. /* !!! FIXME: maybe we should just report full permissions? */
  358. st->readonly = access(filename, W_OK);
  359. return 1;
  360. } /* __PHYSFS_platformStat */
  361. #ifndef PHYSFS_PLATFORM_BEOS /* BeOS has its own code in platform_beos.cpp */
  362. #if (defined PHYSFS_NO_THREAD_SUPPORT)
  363. void *__PHYSFS_platformGetThreadID(void) { return ((void *) 0x0001); }
  364. void *__PHYSFS_platformCreateMutex(void) { return ((void *) 0x0001); }
  365. void __PHYSFS_platformDestroyMutex(void *mutex) {}
  366. int __PHYSFS_platformGrabMutex(void *mutex) { return 1; }
  367. void __PHYSFS_platformReleaseMutex(void *mutex) {}
  368. #else
  369. typedef struct
  370. {
  371. pthread_mutex_t mutex;
  372. pthread_t owner;
  373. PHYSFS_uint32 count;
  374. } PthreadMutex;
  375. void *__PHYSFS_platformGetThreadID(void)
  376. {
  377. return ( (void *) ((size_t) pthread_self()) );
  378. } /* __PHYSFS_platformGetThreadID */
  379. void *__PHYSFS_platformCreateMutex(void)
  380. {
  381. int rc;
  382. PthreadMutex *m = (PthreadMutex *) allocator.Malloc(sizeof (PthreadMutex));
  383. BAIL_IF_MACRO(m == NULL, ERR_OUT_OF_MEMORY, NULL);
  384. rc = pthread_mutex_init(&m->mutex, NULL);
  385. if (rc != 0)
  386. {
  387. allocator.Free(m);
  388. BAIL_MACRO(strerror(rc), NULL);
  389. } /* if */
  390. m->count = 0;
  391. m->owner = (pthread_t) 0xDEADBEEF;
  392. return ((void *) m);
  393. } /* __PHYSFS_platformCreateMutex */
  394. void __PHYSFS_platformDestroyMutex(void *mutex)
  395. {
  396. PthreadMutex *m = (PthreadMutex *) mutex;
  397. /* Destroying a locked mutex is a bug, but we'll try to be helpful. */
  398. if ((m->owner == pthread_self()) && (m->count > 0))
  399. pthread_mutex_unlock(&m->mutex);
  400. pthread_mutex_destroy(&m->mutex);
  401. allocator.Free(m);
  402. } /* __PHYSFS_platformDestroyMutex */
  403. int __PHYSFS_platformGrabMutex(void *mutex)
  404. {
  405. PthreadMutex *m = (PthreadMutex *) mutex;
  406. pthread_t tid = pthread_self();
  407. if (m->owner != tid)
  408. {
  409. if (pthread_mutex_lock(&m->mutex) != 0)
  410. return 0;
  411. m->owner = tid;
  412. } /* if */
  413. m->count++;
  414. return 1;
  415. } /* __PHYSFS_platformGrabMutex */
  416. void __PHYSFS_platformReleaseMutex(void *mutex)
  417. {
  418. PthreadMutex *m = (PthreadMutex *) mutex;
  419. if (m->owner == pthread_self())
  420. {
  421. if (--m->count == 0)
  422. {
  423. m->owner = (pthread_t) 0xDEADBEEF;
  424. pthread_mutex_unlock(&m->mutex);
  425. } /* if */
  426. } /* if */
  427. } /* __PHYSFS_platformReleaseMutex */
  428. #endif /* !PHYSFS_NO_THREAD_SUPPORT */
  429. #endif /* !PHYSFS_PLATFORM_BEOS */
  430. #endif /* PHYSFS_PLATFORM_POSIX */
  431. /* end of posix.c ... */