dir.c 9.9 KB

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