1
0

posix.c 9.6 KB

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