posix.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. 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. int __PHYSFS_platformExists(const char *fname)
  92. {
  93. struct stat statbuf;
  94. BAIL_IF_MACRO(lstat(fname, &statbuf) == -1, strerror(errno), 0);
  95. return(1);
  96. } /* __PHYSFS_platformExists */
  97. int __PHYSFS_platformIsSymLink(const char *fname)
  98. {
  99. struct stat statbuf;
  100. BAIL_IF_MACRO(lstat(fname, &statbuf) == -1, strerror(errno), 0);
  101. return( (S_ISLNK(statbuf.st_mode)) ? 1 : 0 );
  102. } /* __PHYSFS_platformIsSymlink */
  103. int __PHYSFS_platformIsDirectory(const char *fname)
  104. {
  105. struct stat statbuf;
  106. BAIL_IF_MACRO(stat(fname, &statbuf) == -1, strerror(errno), 0);
  107. return( (S_ISDIR(statbuf.st_mode)) ? 1 : 0 );
  108. } /* __PHYSFS_platformIsDirectory */
  109. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  110. const char *dirName,
  111. const char *append)
  112. {
  113. int len = ((prepend) ? strlen(prepend) : 0) +
  114. ((append) ? strlen(append) : 0) +
  115. strlen(dirName) + 1;
  116. char *retval = (char *) allocator.Malloc(len);
  117. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  118. /* platform-independent notation is Unix-style already. :) */
  119. if (prepend)
  120. strcpy(retval, prepend);
  121. else
  122. retval[0] = '\0';
  123. strcat(retval, dirName);
  124. if (append)
  125. strcat(retval, append);
  126. return(retval);
  127. } /* __PHYSFS_platformCvtToDependent */
  128. void __PHYSFS_platformEnumerateFiles(const char *dirname,
  129. int omitSymLinks,
  130. PHYSFS_EnumFilesCallback callback,
  131. const char *origdir,
  132. void *callbackdata)
  133. {
  134. DIR *dir;
  135. struct dirent *ent;
  136. int bufsize = 0;
  137. char *buf = NULL;
  138. int dlen = 0;
  139. if (omitSymLinks) /* !!! FIXME: this malloc sucks. */
  140. {
  141. dlen = strlen(dirname);
  142. bufsize = dlen + 256;
  143. buf = (char *) allocator.Malloc(bufsize);
  144. if (buf == NULL)
  145. return;
  146. strcpy(buf, dirname);
  147. if (buf[dlen - 1] != '/')
  148. {
  149. buf[dlen++] = '/';
  150. buf[dlen] = '\0';
  151. } /* if */
  152. } /* if */
  153. errno = 0;
  154. dir = opendir(dirname);
  155. if (dir == NULL)
  156. {
  157. allocator.Free(buf);
  158. return;
  159. } /* if */
  160. while ((ent = readdir(dir)) != NULL)
  161. {
  162. if (strcmp(ent->d_name, ".") == 0)
  163. continue;
  164. if (strcmp(ent->d_name, "..") == 0)
  165. continue;
  166. if (omitSymLinks)
  167. {
  168. char *p;
  169. int len = strlen(ent->d_name) + dlen + 1;
  170. if (len > bufsize)
  171. {
  172. p = (char *) allocator.Realloc(buf, len);
  173. if (p == NULL)
  174. continue;
  175. buf = p;
  176. bufsize = len;
  177. } /* if */
  178. strcpy(buf + dlen, ent->d_name);
  179. if (__PHYSFS_platformIsSymLink(buf))
  180. continue;
  181. } /* if */
  182. callback(callbackdata, origdir, ent->d_name);
  183. } /* while */
  184. allocator.Free(buf);
  185. closedir(dir);
  186. } /* __PHYSFS_platformEnumerateFiles */
  187. int __PHYSFS_platformMkDir(const char *path)
  188. {
  189. int rc;
  190. errno = 0;
  191. rc = mkdir(path, S_IRWXU);
  192. BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
  193. return(1);
  194. } /* __PHYSFS_platformMkDir */
  195. static void *doOpen(const char *filename, int mode)
  196. {
  197. const int appending = (mode & O_APPEND);
  198. int fd;
  199. int *retval;
  200. errno = 0;
  201. /* O_APPEND doesn't actually behave as we'd like. */
  202. mode &= ~O_APPEND;
  203. fd = open(filename, mode, S_IRUSR | S_IWUSR);
  204. BAIL_IF_MACRO(fd < 0, strerror(errno), NULL);
  205. if (appending)
  206. {
  207. if (lseek(fd, 0, SEEK_END) < 0)
  208. {
  209. close(fd);
  210. BAIL_MACRO(strerror(errno), NULL);
  211. } /* if */
  212. } /* if */
  213. retval = (int *) allocator.Malloc(sizeof (int));
  214. if (retval == NULL)
  215. {
  216. close(fd);
  217. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  218. } /* if */
  219. *retval = fd;
  220. return((void *) retval);
  221. } /* doOpen */
  222. void *__PHYSFS_platformOpenRead(const char *filename)
  223. {
  224. return(doOpen(filename, O_RDONLY));
  225. } /* __PHYSFS_platformOpenRead */
  226. void *__PHYSFS_platformOpenWrite(const char *filename)
  227. {
  228. return(doOpen(filename, O_WRONLY | O_CREAT | O_TRUNC));
  229. } /* __PHYSFS_platformOpenWrite */
  230. void *__PHYSFS_platformOpenAppend(const char *filename)
  231. {
  232. return(doOpen(filename, O_WRONLY | O_CREAT | O_APPEND));
  233. } /* __PHYSFS_platformOpenAppend */
  234. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  235. PHYSFS_uint32 size, PHYSFS_uint32 count)
  236. {
  237. int fd = *((int *) opaque);
  238. int max = size * count;
  239. int rc = read(fd, buffer, max);
  240. BAIL_IF_MACRO(rc == -1, strerror(errno), rc);
  241. assert(rc <= max);
  242. if ((rc < max) && (size > 1))
  243. lseek(fd, -(rc % size), SEEK_CUR); /* rollback to object boundary. */
  244. return(rc / size);
  245. } /* __PHYSFS_platformRead */
  246. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  247. PHYSFS_uint32 size, PHYSFS_uint32 count)
  248. {
  249. int fd = *((int *) opaque);
  250. int max = size * count;
  251. int rc = write(fd, (void *) buffer, max);
  252. BAIL_IF_MACRO(rc == -1, strerror(errno), rc);
  253. assert(rc <= max);
  254. if ((rc < max) && (size > 1))
  255. lseek(fd, -(rc % size), SEEK_CUR); /* rollback to object boundary. */
  256. return(rc / size);
  257. } /* __PHYSFS_platformWrite */
  258. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  259. {
  260. int fd = *((int *) opaque);
  261. #ifdef PHYSFS_HAVE_LLSEEK
  262. unsigned long offset_high = ((pos >> 32) & 0xFFFFFFFF);
  263. unsigned long offset_low = (pos & 0xFFFFFFFF);
  264. loff_t retoffset;
  265. int rc = llseek(fd, offset_high, offset_low, &retoffset, SEEK_SET);
  266. BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
  267. #else
  268. BAIL_IF_MACRO(lseek(fd, (int) pos, SEEK_SET) == -1, strerror(errno), 0);
  269. #endif
  270. return(1);
  271. } /* __PHYSFS_platformSeek */
  272. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  273. {
  274. int fd = *((int *) opaque);
  275. PHYSFS_sint64 retval;
  276. #ifdef PHYSFS_HAVE_LLSEEK
  277. loff_t retoffset;
  278. int rc = llseek(fd, 0, &retoffset, SEEK_CUR);
  279. BAIL_IF_MACRO(rc == -1, strerror(errno), -1);
  280. retval = (PHYSFS_sint64) retoffset;
  281. #else
  282. retval = (PHYSFS_sint64) lseek(fd, 0, SEEK_CUR);
  283. BAIL_IF_MACRO(retval == -1, strerror(errno), -1);
  284. #endif
  285. return(retval);
  286. } /* __PHYSFS_platformTell */
  287. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  288. {
  289. int fd = *((int *) opaque);
  290. struct stat statbuf;
  291. BAIL_IF_MACRO(fstat(fd, &statbuf) == -1, strerror(errno), -1);
  292. return((PHYSFS_sint64) statbuf.st_size);
  293. } /* __PHYSFS_platformFileLength */
  294. int __PHYSFS_platformEOF(void *opaque)
  295. {
  296. PHYSFS_sint64 pos = __PHYSFS_platformTell(opaque);
  297. PHYSFS_sint64 len = __PHYSFS_platformFileLength(opaque);
  298. return((pos < 0) || (len < 0) || (pos >= len));
  299. } /* __PHYSFS_platformEOF */
  300. int __PHYSFS_platformFlush(void *opaque)
  301. {
  302. int fd = *((int *) opaque);
  303. BAIL_IF_MACRO(fsync(fd) == -1, strerror(errno), 0);
  304. return(1);
  305. } /* __PHYSFS_platformFlush */
  306. int __PHYSFS_platformClose(void *opaque)
  307. {
  308. int fd = *((int *) opaque);
  309. BAIL_IF_MACRO(close(fd) == -1, strerror(errno), 0);
  310. allocator.Free(opaque);
  311. return(1);
  312. } /* __PHYSFS_platformClose */
  313. int __PHYSFS_platformDelete(const char *path)
  314. {
  315. BAIL_IF_MACRO(remove(path) == -1, strerror(errno), 0);
  316. return(1);
  317. } /* __PHYSFS_platformDelete */
  318. PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
  319. {
  320. struct stat statbuf;
  321. BAIL_IF_MACRO(stat(fname, &statbuf) < 0, strerror(errno), -1);
  322. return statbuf.st_mtime;
  323. } /* __PHYSFS_platformGetLastModTime */
  324. #endif /* PHYSFS_PLATFORM_POSIX */
  325. /* end of posix.c ... */