dir.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Standard directory I/O support routines for PhysicsFS.
  3. *
  4. * Please see the file LICENSE in the source's root directory.
  5. *
  6. * This file written by Ryan C. Gordon.
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <errno.h>
  12. #include <sys/stat.h>
  13. #include <sys/types.h>
  14. #include <fcntl.h>
  15. #include <unistd.h>
  16. #include "physfs.h"
  17. #define __PHYSICSFS_INTERNAL__
  18. #include "physfs_internal.h"
  19. extern const DirFunctions __PHYSFS_DirFunctions_DIR;
  20. static const FileFunctions __PHYSFS_FileFunctions_DIR;
  21. static const FileFunctions __PHYSFS_FileFunctions_DIRW;
  22. static int DIR_read(FileHandle *handle, void *buffer,
  23. unsigned int objSize, unsigned int objCount)
  24. {
  25. FILE *h = (FILE *) (handle->opaque);
  26. int retval;
  27. errno = 0;
  28. retval = fread(buffer, objSize, objCount, h);
  29. BAIL_IF_MACRO((retval < objCount) && (ferror(h)),strerror(errno),retval);
  30. return(retval);
  31. } /* DIR_read */
  32. static int DIR_write(FileHandle *handle, void *buffer,
  33. unsigned int objSize, unsigned int objCount)
  34. {
  35. FILE *h = (FILE *) (handle->opaque);
  36. int retval;
  37. errno = 0;
  38. retval = fwrite(buffer, objSize, objCount, h);
  39. if ( (retval < objCount) && (ferror(h)) )
  40. __PHYSFS_setError(strerror(errno));
  41. return(retval);
  42. } /* DIR_write */
  43. static int DIR_eof(FileHandle *handle)
  44. {
  45. return(feof((FILE *) (handle->opaque)));
  46. } /* DIR_eof */
  47. static int DIR_tell(FileHandle *handle)
  48. {
  49. return(ftell((FILE *) (handle->opaque)));
  50. } /* DIR_tell */
  51. static int DIR_seek(FileHandle *handle, int offset)
  52. {
  53. return(fseek((FILE *) (handle->opaque), offset, SEEK_SET) == 0);
  54. } /* DIR_seek */
  55. static int DIR_fileLength(FileHandle *handle)
  56. {
  57. return(__PHYSFS_platformFileLength((FILE *) (handle->opaque)));
  58. } /* DIR_fileLength */
  59. static int DIR_fileClose(FileHandle *handle)
  60. {
  61. FILE *h = (FILE *) (handle->opaque);
  62. /*
  63. * we manually fflush() the buffer, since that's the place fclose() will
  64. * most likely fail, but that will leave the file handle in an undefined
  65. * state if it fails. fflush() failures we can recover from.
  66. */
  67. /* keep trying until there's success or an unrecoverable error... */
  68. do {
  69. __PHYSFS_platformTimeslice();
  70. errno = 0;
  71. } while ( (fflush(h) == EOF) && ((errno == EAGAIN) || (errno == EINTR)) );
  72. /* EBADF == "Not open for writing". That's fine. */
  73. BAIL_IF_MACRO((errno != 0) && (errno != EBADF), strerror(errno), 0);
  74. /* if fclose fails anyhow, we just have to pray that it's still usable. */
  75. errno = 0;
  76. BAIL_IF_MACRO(fclose(h) == EOF, strerror(errno), 0); /* (*shrug*) */
  77. free(handle);
  78. return(1);
  79. } /* DIR_fileClose */
  80. static int DIR_isArchive(const char *filename, int forWriting)
  81. {
  82. /* directories ARE archives in this driver... */
  83. return(__PHYSFS_platformIsDirectory(filename));
  84. } /* DIR_isArchive */
  85. static DirHandle *DIR_openArchive(const char *name, int forWriting)
  86. {
  87. const char *dirsep = PHYSFS_getDirSeparator();
  88. DirHandle *retval = NULL;
  89. int namelen = strlen(name);
  90. int seplen = strlen(dirsep);
  91. BAIL_IF_MACRO(!DIR_isArchive(name, forWriting),
  92. ERR_UNSUPPORTED_ARCHIVE, NULL);
  93. retval = malloc(sizeof (DirHandle));
  94. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  95. retval->opaque = malloc(namelen + seplen + 1);
  96. if (retval->opaque == NULL)
  97. {
  98. free(retval);
  99. BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
  100. } /* if */
  101. /* make sure there's a dir separator at the end of the string */
  102. strcpy((char *) (retval->opaque), name);
  103. if (strcmp((name + namelen) - seplen, dirsep) != 0)
  104. strcat((char *) (retval->opaque), dirsep);
  105. retval->funcs = &__PHYSFS_DirFunctions_DIR;
  106. return(retval);
  107. } /* DIR_openArchive */
  108. static LinkedStringList *DIR_enumerateFiles(DirHandle *h,
  109. const char *dname,
  110. int omitSymLinks)
  111. {
  112. char *d = __PHYSFS_platformCvtToDependent((char *)(h->opaque),dname,NULL);
  113. LinkedStringList *retval;
  114. BAIL_IF_MACRO(d == NULL, NULL, NULL);
  115. retval = __PHYSFS_platformEnumerateFiles(d, omitSymLinks);
  116. free(d);
  117. return(retval);
  118. } /* DIR_enumerateFiles */
  119. static int DIR_exists(DirHandle *h, const char *name)
  120. {
  121. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  122. int retval;
  123. BAIL_IF_MACRO(f == NULL, NULL, 0);
  124. retval = __PHYSFS_platformExists(f);
  125. free(f);
  126. return(retval);
  127. } /* DIR_exists */
  128. static int DIR_isDirectory(DirHandle *h, const char *name)
  129. {
  130. char *d = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  131. int retval;
  132. BAIL_IF_MACRO(d == NULL, NULL, 0);
  133. retval = __PHYSFS_platformIsDirectory(d);
  134. free(d);
  135. return(retval);
  136. } /* DIR_isDirectory */
  137. static int DIR_isSymLink(DirHandle *h, const char *name)
  138. {
  139. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  140. int retval;
  141. BAIL_IF_MACRO(f == NULL, NULL, 0); /* !!! might be a problem. */
  142. retval = __PHYSFS_platformIsSymLink(f);
  143. free(f);
  144. return(retval);
  145. } /* DIR_isSymLink */
  146. static FileHandle *doOpen(DirHandle *h, const char *name, const char *mode)
  147. {
  148. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  149. FILE *rc;
  150. FileHandle *retval;
  151. char *str;
  152. BAIL_IF_MACRO(f == NULL, NULL, NULL);
  153. retval = (FileHandle *) malloc(sizeof (FileHandle));
  154. if (!retval)
  155. {
  156. free(f);
  157. BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
  158. } /* if */
  159. errno = 0;
  160. rc = fopen(f, mode);
  161. str = strerror(errno);
  162. free(f);
  163. if (!rc)
  164. {
  165. free(retval);
  166. BAIL_IF_MACRO(1, str, NULL);
  167. } /* if */
  168. retval->opaque = (void *) rc;
  169. retval->dirHandle = h;
  170. retval->funcs = &__PHYSFS_FileFunctions_DIR;
  171. return(retval);
  172. } /* doOpen */
  173. static FileHandle *DIR_openRead(DirHandle *h, const char *filename)
  174. {
  175. return(doOpen(h, filename, "rb"));
  176. } /* DIR_openRead */
  177. static FileHandle *DIR_openWrite(DirHandle *h, const char *filename)
  178. {
  179. return(doOpen(h, filename, "wb"));
  180. } /* DIR_openWrite */
  181. static FileHandle *DIR_openAppend(DirHandle *h, const char *filename)
  182. {
  183. return(doOpen(h, filename, "ab"));
  184. } /* DIR_openAppend */
  185. static int DIR_remove(DirHandle *h, const char *name)
  186. {
  187. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  188. int retval;
  189. BAIL_IF_MACRO(f == NULL, NULL, 0);
  190. errno = 0;
  191. retval = (remove(f) == 0);
  192. if (!retval)
  193. __PHYSFS_setError(strerror(errno));
  194. free(f);
  195. return(retval);
  196. } /* DIR_remove */
  197. static int DIR_mkdir(DirHandle *h, const char *name)
  198. {
  199. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  200. int retval;
  201. BAIL_IF_MACRO(f == NULL, NULL, 0);
  202. errno = 0;
  203. retval = (mkdir(f, S_IRWXU) == 0);
  204. if (!retval)
  205. __PHYSFS_setError(strerror(errno));
  206. free(f);
  207. return(retval);
  208. } /* DIR_mkdir */
  209. static void DIR_dirClose(DirHandle *h)
  210. {
  211. free(h->opaque);
  212. free(h);
  213. } /* DIR_dirClose */
  214. static const FileFunctions __PHYSFS_FileFunctions_DIR =
  215. {
  216. DIR_read, /* read() method */
  217. NULL, /* write() method */
  218. DIR_eof, /* eof() method */
  219. DIR_tell, /* tell() method */
  220. DIR_seek, /* seek() method */
  221. DIR_fileLength, /* fileLength() method */
  222. DIR_fileClose /* fileClose() method */
  223. };
  224. static const FileFunctions __PHYSFS_FileFunctions_DIRW =
  225. {
  226. NULL, /* read() method */
  227. DIR_write, /* write() method */
  228. DIR_eof, /* eof() method */
  229. DIR_tell, /* tell() method */
  230. DIR_seek, /* seek() method */
  231. DIR_fileLength, /* fileLength() method */
  232. DIR_fileClose /* fileClose() method */
  233. };
  234. const DirFunctions __PHYSFS_DirFunctions_DIR =
  235. {
  236. DIR_isArchive, /* isArchive() method */
  237. DIR_openArchive, /* openArchive() method */
  238. DIR_enumerateFiles, /* enumerateFiles() method */
  239. DIR_exists, /* exists() method */
  240. DIR_isDirectory, /* isDirectory() method */
  241. DIR_isSymLink, /* isSymLink() method */
  242. DIR_openRead, /* openRead() method */
  243. DIR_openWrite, /* openWrite() method */
  244. DIR_openAppend, /* openAppend() method */
  245. DIR_remove, /* remove() method */
  246. DIR_mkdir, /* mkdir() method */
  247. DIR_dirClose /* dirClose() method */
  248. };
  249. /* This doesn't get listed, since it's technically not an archive... */
  250. #if 0
  251. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
  252. {
  253. "DIR",
  254. "non-archive directory I/O",
  255. "Ryan C. Gordon",
  256. "http://www.icculus.org/",
  257. };
  258. #endif
  259. /* end of dir.c ... */