posix.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. const int appending = (mode & O_APPEND);
  188. int fd;
  189. int *retval;
  190. errno = 0;
  191. /* O_APPEND doesn't actually behave as we'd like. */
  192. mode &= ~O_APPEND;
  193. fd = open(filename, mode, S_IRUSR | S_IWUSR);
  194. BAIL_IF_MACRO(fd < 0, strerror(errno), NULL);
  195. if (appending)
  196. {
  197. if (lseek(fd, 0, SEEK_END) < 0)
  198. {
  199. close(fd);
  200. BAIL_MACRO(strerror(errno), NULL);
  201. } /* if */
  202. } /* if */
  203. retval = (int *) allocator.Malloc(sizeof (int));
  204. if (retval == NULL)
  205. {
  206. close(fd);
  207. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  208. } /* if */
  209. *retval = fd;
  210. return((void *) retval);
  211. } /* doOpen */
  212. void *__PHYSFS_platformOpenRead(const char *filename)
  213. {
  214. return(doOpen(filename, O_RDONLY));
  215. } /* __PHYSFS_platformOpenRead */
  216. void *__PHYSFS_platformOpenWrite(const char *filename)
  217. {
  218. return(doOpen(filename, O_WRONLY | O_CREAT | O_TRUNC));
  219. } /* __PHYSFS_platformOpenWrite */
  220. void *__PHYSFS_platformOpenAppend(const char *filename)
  221. {
  222. return(doOpen(filename, O_WRONLY | O_CREAT | O_APPEND));
  223. } /* __PHYSFS_platformOpenAppend */
  224. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  225. PHYSFS_uint32 size, PHYSFS_uint32 count)
  226. {
  227. int fd = *((int *) opaque);
  228. int max = size * count;
  229. int rc = read(fd, buffer, max);
  230. BAIL_IF_MACRO(rc == -1, strerror(errno), rc);
  231. assert(rc <= max);
  232. if ((rc < max) && (size > 1))
  233. lseek(fd, -(rc % size), SEEK_CUR); /* rollback to object boundary. */
  234. return(rc / size);
  235. } /* __PHYSFS_platformRead */
  236. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  237. PHYSFS_uint32 size, PHYSFS_uint32 count)
  238. {
  239. int fd = *((int *) opaque);
  240. int max = size * count;
  241. int rc = write(fd, (void *) buffer, max);
  242. BAIL_IF_MACRO(rc == -1, strerror(errno), rc);
  243. assert(rc <= max);
  244. if ((rc < max) && (size > 1))
  245. lseek(fd, -(rc % size), SEEK_CUR); /* rollback to object boundary. */
  246. return(rc / size);
  247. } /* __PHYSFS_platformWrite */
  248. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  249. {
  250. int fd = *((int *) opaque);
  251. #ifdef PHYSFS_HAVE_LLSEEK
  252. unsigned long offset_high = ((pos >> 32) & 0xFFFFFFFF);
  253. unsigned long offset_low = (pos & 0xFFFFFFFF);
  254. loff_t retoffset;
  255. int rc = llseek(fd, offset_high, offset_low, &retoffset, SEEK_SET);
  256. BAIL_IF_MACRO(rc == -1, strerror(errno), 0);
  257. #else
  258. BAIL_IF_MACRO(lseek(fd, (int) pos, SEEK_SET) == -1, strerror(errno), 0);
  259. #endif
  260. return(1);
  261. } /* __PHYSFS_platformSeek */
  262. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  263. {
  264. int fd = *((int *) opaque);
  265. PHYSFS_sint64 retval;
  266. #ifdef PHYSFS_HAVE_LLSEEK
  267. loff_t retoffset;
  268. int rc = llseek(fd, 0, &retoffset, SEEK_CUR);
  269. BAIL_IF_MACRO(rc == -1, strerror(errno), -1);
  270. retval = (PHYSFS_sint64) retoffset;
  271. #else
  272. retval = (PHYSFS_sint64) lseek(fd, 0, SEEK_CUR);
  273. BAIL_IF_MACRO(retval == -1, strerror(errno), -1);
  274. #endif
  275. return(retval);
  276. } /* __PHYSFS_platformTell */
  277. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  278. {
  279. int fd = *((int *) opaque);
  280. struct stat statbuf;
  281. BAIL_IF_MACRO(fstat(fd, &statbuf) == -1, strerror(errno), -1);
  282. return((PHYSFS_sint64) statbuf.st_size);
  283. } /* __PHYSFS_platformFileLength */
  284. int __PHYSFS_platformEOF(void *opaque)
  285. {
  286. PHYSFS_sint64 pos = __PHYSFS_platformTell(opaque);
  287. PHYSFS_sint64 len = __PHYSFS_platformFileLength(opaque);
  288. return(pos >= len);
  289. } /* __PHYSFS_platformEOF */
  290. int __PHYSFS_platformFlush(void *opaque)
  291. {
  292. int fd = *((int *) opaque);
  293. BAIL_IF_MACRO(fsync(fd) == -1, strerror(errno), 0);
  294. return(1);
  295. } /* __PHYSFS_platformFlush */
  296. int __PHYSFS_platformClose(void *opaque)
  297. {
  298. int fd = *((int *) opaque);
  299. BAIL_IF_MACRO(close(fd) == -1, strerror(errno), 0);
  300. allocator.Free(opaque);
  301. return(1);
  302. } /* __PHYSFS_platformClose */
  303. int __PHYSFS_platformDelete(const char *path)
  304. {
  305. BAIL_IF_MACRO(remove(path) == -1, strerror(errno), 0);
  306. return(1);
  307. } /* __PHYSFS_platformDelete */
  308. PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
  309. {
  310. struct stat statbuf;
  311. BAIL_IF_MACRO(stat(fname, &statbuf) < 0, strerror(errno), -1);
  312. return statbuf.st_mtime;
  313. } /* __PHYSFS_platformGetLastModTime */
  314. #endif /* PHYSFS_PLATFORM_POSIX */
  315. /* end of posix.c ... */