dir.c 9.5 KB

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