posix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <ctype.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <pwd.h>
  19. #include <dirent.h>
  20. #include <errno.h>
  21. #include <fcntl.h>
  22. #ifdef PHYSFS_HAVE_LLSEEK
  23. #include <linux/unistd.h>
  24. #endif
  25. #include "physfs_internal.h"
  26. char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname)
  27. {
  28. const char *envr = getenv(varname);
  29. char *retval = NULL;
  30. if (envr != NULL)
  31. {
  32. retval = (char *) allocator.Malloc(strlen(envr) + 1);
  33. if (retval != NULL)
  34. strcpy(retval, envr);
  35. } /* if */
  36. return(retval);
  37. } /* __PHYSFS_platformCopyEnvironmentVariable */
  38. static char *getUserNameByUID(void)
  39. {
  40. uid_t uid = getuid();
  41. struct passwd *pw;
  42. char *retval = NULL;
  43. pw = getpwuid(uid);
  44. if ((pw != NULL) && (pw->pw_name != NULL))
  45. {
  46. retval = (char *) allocator.Malloc(strlen(pw->pw_name) + 1);
  47. if (retval != NULL)
  48. strcpy(retval, pw->pw_name);
  49. } /* if */
  50. return(retval);
  51. } /* getUserNameByUID */
  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))
  59. {
  60. retval = (char *) allocator.Malloc(strlen(pw->pw_dir) + 1);
  61. if (retval != NULL)
  62. strcpy(retval, pw->pw_dir);
  63. } /* if */
  64. return(retval);
  65. } /* getUserDirByUID */
  66. char *__PHYSFS_platformGetUserName(void)
  67. {
  68. char *retval = getUserNameByUID();
  69. if (retval == NULL)
  70. retval = __PHYSFS_platformCopyEnvironmentVariable("USER");
  71. return(retval);
  72. } /* __PHYSFS_platformGetUserName */
  73. char *__PHYSFS_platformGetUserDir(void)
  74. {
  75. char *retval = __PHYSFS_platformCopyEnvironmentVariable("HOME");
  76. if (retval == NULL)
  77. retval = getUserDirByUID();
  78. return(retval);
  79. } /* __PHYSFS_platformGetUserDir */
  80. int __PHYSFS_platformExists(const char *fname)
  81. {
  82. struct stat statbuf;
  83. BAIL_IF_MACRO(lstat(fname, &statbuf) == -1, strerror(errno), 0);
  84. return(1);
  85. } /* __PHYSFS_platformExists */
  86. int __PHYSFS_platformIsSymLink(const char *fname)
  87. {
  88. struct stat statbuf;
  89. BAIL_IF_MACRO(lstat(fname, &statbuf) == -1, strerror(errno), 0);
  90. return( (S_ISLNK(statbuf.st_mode)) ? 1 : 0 );
  91. } /* __PHYSFS_platformIsSymlink */
  92. int __PHYSFS_platformIsDirectory(const char *fname)
  93. {
  94. struct stat statbuf;
  95. BAIL_IF_MACRO(stat(fname, &statbuf) == -1, strerror(errno), 0);
  96. return( (S_ISDIR(statbuf.st_mode)) ? 1 : 0 );
  97. } /* __PHYSFS_platformIsDirectory */
  98. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  99. const char *dirName,
  100. const char *append)
  101. {
  102. int len = ((prepend) ? strlen(prepend) : 0) +
  103. ((append) ? strlen(append) : 0) +
  104. strlen(dirName) + 1;
  105. char *retval = (char *) allocator.Malloc(len);
  106. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  107. /* platform-independent notation is Unix-style already. :) */
  108. if (prepend)
  109. strcpy(retval, prepend);
  110. else
  111. retval[0] = '\0';
  112. strcat(retval, dirName);
  113. if (append)
  114. strcat(retval, append);
  115. return(retval);
  116. } /* __PHYSFS_platformCvtToDependent */
  117. void __PHYSFS_platformEnumerateFiles(const char *dirname,
  118. int omitSymLinks,
  119. PHYSFS_EnumFilesCallback callback,
  120. const char *origdir,
  121. void *callbackdata)
  122. {
  123. DIR *dir;
  124. struct dirent *ent;
  125. int bufsize = 0;
  126. char *buf = NULL;
  127. int dlen = 0;
  128. if (omitSymLinks) /* !!! FIXME: this malloc sucks. */
  129. {
  130. dlen = strlen(dirname);
  131. bufsize = dlen + 256;
  132. buf = (char *) allocator.Malloc(bufsize);
  133. if (buf == NULL)
  134. return;
  135. strcpy(buf, dirname);
  136. if (buf[dlen - 1] != '/')
  137. {
  138. buf[dlen++] = '/';
  139. buf[dlen] = '\0';
  140. } /* if */
  141. } /* if */
  142. errno = 0;
  143. dir = opendir(dirname);
  144. if (dir == NULL)
  145. {
  146. if (buf != NULL)
  147. allocator.Free(buf);
  148. return;
  149. } /* if */
  150. while ((ent = readdir(dir)) != NULL)
  151. {
  152. if (strcmp(ent->d_name, ".") == 0)
  153. continue;
  154. if (strcmp(ent->d_name, "..") == 0)
  155. continue;
  156. if (omitSymLinks)
  157. {
  158. char *p;
  159. int len = strlen(ent->d_name) + dlen + 1;
  160. if (len > bufsize)
  161. {
  162. p = (char *) allocator.Realloc(buf, len);
  163. if (p == NULL)
  164. continue;
  165. buf = p;
  166. bufsize = len;
  167. } /* if */
  168. strcpy(buf + dlen, ent->d_name);
  169. if (__PHYSFS_platformIsSymLink(buf))
  170. continue;
  171. } /* if */
  172. callback(callbackdata, origdir, ent->d_name);
  173. } /* while */
  174. if (buf != NULL)
  175. allocator.Free(buf);
  176. closedir(dir);
  177. } /* __PHYSFS_platformEnumerateFiles */
  178. char *__PHYSFS_platformCurrentDir(void)
  179. {
  180. int allocSize = 0;
  181. char *retval = NULL;
  182. char *ptr;
  183. do
  184. {
  185. allocSize += 100;
  186. ptr = (char *) allocator.Realloc(retval, allocSize);
  187. if (ptr == NULL)
  188. {
  189. if (retval != NULL)
  190. allocator.Free(retval);
  191. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  192. } /* if */
  193. retval = ptr;
  194. ptr = getcwd(retval, allocSize);
  195. } while (ptr == NULL && errno == ERANGE);
  196. if (ptr == NULL && errno)
  197. {
  198. /*
  199. * getcwd() failed for some reason, for example current
  200. * directory not existing.
  201. */
  202. if (retval != NULL)
  203. allocator.Free(retval);
  204. BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
  205. } /* if */
  206. return(retval);
  207. } /* __PHYSFS_platformCurrentDir */
  208. int __PHYSFS_platformMkDir(const char *path)
  209. {
  210. int rc;
  211. errno = 0;
  212. rc = mkdir(path, S_IRWXU);
  213. BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
  214. return(1);
  215. } /* __PHYSFS_platformMkDir */
  216. static void *doOpen(const char *filename, int mode)
  217. {
  218. int fd;
  219. int *retval;
  220. errno = 0;
  221. fd = open(filename, mode, S_IRUSR | S_IWUSR);
  222. BAIL_IF_MACRO(fd < 0, strerror(errno), NULL);
  223. retval = (int *) allocator.Malloc(sizeof (int));
  224. if (retval == NULL)
  225. {
  226. close(fd);
  227. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  228. } /* if */
  229. *retval = fd;
  230. return((void *) retval);
  231. } /* doOpen */
  232. void *__PHYSFS_platformOpenRead(const char *filename)
  233. {
  234. return(doOpen(filename, O_RDONLY));
  235. } /* __PHYSFS_platformOpenRead */
  236. void *__PHYSFS_platformOpenWrite(const char *filename)
  237. {
  238. return(doOpen(filename, O_WRONLY | O_CREAT | O_TRUNC));
  239. } /* __PHYSFS_platformOpenWrite */
  240. void *__PHYSFS_platformOpenAppend(const char *filename)
  241. {
  242. return(doOpen(filename, O_WRONLY | O_CREAT | O_APPEND));
  243. } /* __PHYSFS_platformOpenAppend */
  244. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  245. PHYSFS_uint32 size, PHYSFS_uint32 count)
  246. {
  247. int fd = *((int *) opaque);
  248. int max = size * count;
  249. int rc = read(fd, buffer, max);
  250. BAIL_IF_MACRO(rc == -1, strerror(errno), rc);
  251. assert(rc <= max);
  252. if ((rc < max) && (size > 1))
  253. lseek(fd, -(rc % size), SEEK_CUR); /* rollback to object boundary. */
  254. return(rc / size);
  255. } /* __PHYSFS_platformRead */
  256. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  257. PHYSFS_uint32 size, PHYSFS_uint32 count)
  258. {
  259. int fd = *((int *) opaque);
  260. int max = size * count;
  261. int rc = write(fd, (void *) buffer, max);
  262. BAIL_IF_MACRO(rc == -1, strerror(errno), rc);
  263. assert(rc <= max);
  264. if ((rc < max) && (size > 1))
  265. lseek(fd, -(rc % size), SEEK_CUR); /* rollback to object boundary. */
  266. return(rc / size);
  267. } /* __PHYSFS_platformWrite */
  268. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  269. {
  270. int fd = *((int *) opaque);
  271. #ifdef PHYSFS_HAVE_LLSEEK
  272. unsigned long offset_high = ((pos >> 32) & 0xFFFFFFFF);
  273. unsigned long offset_low = (pos & 0xFFFFFFFF);
  274. loff_t retoffset;
  275. int rc = llseek(fd, offset_high, offset_low, &retoffset, SEEK_SET);
  276. BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
  277. #else
  278. BAIL_IF_MACRO(lseek(fd, (int) pos, SEEK_SET) == -1, strerror(errno), 0);
  279. #endif
  280. return(1);
  281. } /* __PHYSFS_platformSeek */
  282. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  283. {
  284. int fd = *((int *) opaque);
  285. PHYSFS_sint64 retval;
  286. #ifdef PHYSFS_HAVE_LLSEEK
  287. loff_t retoffset;
  288. int rc = llseek(fd, 0, &retoffset, SEEK_CUR);
  289. BAIL_IF_MACRO(rc == -1, strerror(errno), -1);
  290. retval = (PHYSFS_sint64) retoffset;
  291. #else
  292. retval = (PHYSFS_sint64) lseek(fd, 0, SEEK_CUR);
  293. BAIL_IF_MACRO(retval == -1, strerror(errno), -1);
  294. #endif
  295. return(retval);
  296. } /* __PHYSFS_platformTell */
  297. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  298. {
  299. int fd = *((int *) opaque);
  300. struct stat statbuf;
  301. BAIL_IF_MACRO(fstat(fd, &statbuf) == -1, strerror(errno), -1);
  302. return((PHYSFS_sint64) statbuf.st_size);
  303. } /* __PHYSFS_platformFileLength */
  304. int __PHYSFS_platformEOF(void *opaque)
  305. {
  306. PHYSFS_sint64 pos = __PHYSFS_platformTell(opaque);
  307. PHYSFS_sint64 len = __PHYSFS_platformFileLength(opaque);
  308. return(pos >= len);
  309. } /* __PHYSFS_platformEOF */
  310. int __PHYSFS_platformFlush(void *opaque)
  311. {
  312. int fd = *((int *) opaque);
  313. BAIL_IF_MACRO(fsync(fd) == -1, strerror(errno), 0);
  314. return(1);
  315. } /* __PHYSFS_platformFlush */
  316. int __PHYSFS_platformClose(void *opaque)
  317. {
  318. int fd = *((int *) opaque);
  319. BAIL_IF_MACRO(close(fd) == -1, strerror(errno), 0);
  320. allocator.Free(opaque);
  321. return(1);
  322. } /* __PHYSFS_platformClose */
  323. int __PHYSFS_platformDelete(const char *path)
  324. {
  325. BAIL_IF_MACRO(remove(path) == -1, strerror(errno), 0);
  326. return(1);
  327. } /* __PHYSFS_platformDelete */
  328. PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
  329. {
  330. struct stat statbuf;
  331. BAIL_IF_MACRO(stat(fname, &statbuf) < 0, strerror(errno), -1);
  332. return statbuf.st_mtime;
  333. } /* __PHYSFS_platformGetLastModTime */
  334. int __PHYSFS_platformAllocatorInit(void)
  335. {
  336. return(1); /* always succeeds. */
  337. } /* __PHYSFS_platformAllocatorInit */
  338. void __PHYSFS_platformAllocatorDeinit(void)
  339. {
  340. /* no-op */
  341. } /* __PHYSFS_platformAllocatorInit */
  342. void *__PHYSFS_platformAllocatorMalloc(PHYSFS_uint64 s)
  343. {
  344. BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
  345. #undef malloc
  346. return(malloc((size_t) s));
  347. } /* __PHYSFS_platformMalloc */
  348. void *__PHYSFS_platformAllocatorRealloc(void *ptr, PHYSFS_uint64 s)
  349. {
  350. BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
  351. #undef realloc
  352. return(realloc(ptr, (size_t) s));
  353. } /* __PHYSFS_platformRealloc */
  354. void __PHYSFS_platformAllocatorFree(void *ptr)
  355. {
  356. #undef free
  357. free(ptr);
  358. } /* __PHYSFS_platformAllocatorFree */
  359. #endif /* PHYSFS_PLATFORM_POSIX */
  360. /* end of posix.c ... */