dir.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 PHYSFS_sint64 DIR_read(FileHandle *handle, void *buffer,
  17. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  18. static PHYSFS_sint64 DIR_write(FileHandle *handle, const void *buffer,
  19. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  20. static int DIR_eof(FileHandle *handle);
  21. static PHYSFS_sint64 DIR_tell(FileHandle *handle);
  22. static int DIR_seek(FileHandle *handle, PHYSFS_uint64 offset);
  23. static PHYSFS_sint64 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 PHYSFS_sint64 DIR_read(FileHandle *handle, void *buffer,
  85. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  86. {
  87. PHYSFS_sint64 retval;
  88. retval = __PHYSFS_platformRead(handle->opaque, buffer, objSize, objCount);
  89. return(retval);
  90. } /* DIR_read */
  91. static PHYSFS_sint64 DIR_write(FileHandle *handle, const void *buffer,
  92. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  93. {
  94. PHYSFS_sint64 retval;
  95. retval = __PHYSFS_platformWrite(handle->opaque, buffer, objSize, objCount);
  96. return(retval);
  97. } /* DIR_write */
  98. static int DIR_eof(FileHandle *handle)
  99. {
  100. return(__PHYSFS_platformEOF(handle->opaque));
  101. } /* DIR_eof */
  102. static PHYSFS_sint64 DIR_tell(FileHandle *handle)
  103. {
  104. return(__PHYSFS_platformTell(handle->opaque));
  105. } /* DIR_tell */
  106. static int DIR_seek(FileHandle *handle, PHYSFS_uint64 offset)
  107. {
  108. return(__PHYSFS_platformSeek(handle->opaque, offset));
  109. } /* DIR_seek */
  110. static PHYSFS_sint64 DIR_fileLength(FileHandle *handle)
  111. {
  112. return(__PHYSFS_platformFileLength(handle->opaque));
  113. } /* DIR_fileLength */
  114. static int DIR_fileClose(FileHandle *handle)
  115. {
  116. /*
  117. * we manually flush the buffer, since that's the place a close will
  118. * most likely fail, but that will leave the file handle in an undefined
  119. * state if it fails. Flush failures we can recover from.
  120. */
  121. BAIL_IF_MACRO(!__PHYSFS_platformFlush(handle->opaque), NULL, 0);
  122. BAIL_IF_MACRO(!__PHYSFS_platformClose(handle->opaque), NULL, 0);
  123. free(handle);
  124. return(1);
  125. } /* DIR_fileClose */
  126. static int DIR_isArchive(const char *filename, int forWriting)
  127. {
  128. /* directories ARE archives in this driver... */
  129. return(__PHYSFS_platformIsDirectory(filename));
  130. } /* DIR_isArchive */
  131. static DirHandle *DIR_openArchive(const char *name, int forWriting)
  132. {
  133. const char *dirsep = PHYSFS_getDirSeparator();
  134. DirHandle *retval = NULL;
  135. size_t namelen = strlen(name);
  136. size_t seplen = strlen(dirsep);
  137. BAIL_IF_MACRO(!DIR_isArchive(name, forWriting),
  138. ERR_UNSUPPORTED_ARCHIVE, NULL);
  139. retval = (DirHandle *) malloc(sizeof (DirHandle));
  140. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  141. retval->opaque = malloc(namelen + seplen + 1);
  142. if (retval->opaque == NULL)
  143. {
  144. free(retval);
  145. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  146. } /* if */
  147. /* make sure there's a dir separator at the end of the string */
  148. strcpy((char *) (retval->opaque), name);
  149. if (strcmp((name + namelen) - seplen, dirsep) != 0)
  150. strcat((char *) (retval->opaque), dirsep);
  151. retval->funcs = &__PHYSFS_DirFunctions_DIR;
  152. return(retval);
  153. } /* DIR_openArchive */
  154. static LinkedStringList *DIR_enumerateFiles(DirHandle *h,
  155. const char *dname,
  156. int omitSymLinks)
  157. {
  158. char *d = __PHYSFS_platformCvtToDependent((char *)(h->opaque),dname,NULL);
  159. LinkedStringList *retval;
  160. BAIL_IF_MACRO(d == NULL, NULL, NULL);
  161. retval = __PHYSFS_platformEnumerateFiles(d, omitSymLinks);
  162. free(d);
  163. return(retval);
  164. } /* DIR_enumerateFiles */
  165. static int DIR_exists(DirHandle *h, const char *name)
  166. {
  167. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  168. int retval;
  169. BAIL_IF_MACRO(f == NULL, NULL, 0);
  170. retval = __PHYSFS_platformExists(f);
  171. free(f);
  172. return(retval);
  173. } /* DIR_exists */
  174. static int DIR_isDirectory(DirHandle *h, const char *name)
  175. {
  176. char *d = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  177. int retval;
  178. BAIL_IF_MACRO(d == NULL, NULL, 0);
  179. retval = __PHYSFS_platformIsDirectory(d);
  180. free(d);
  181. return(retval);
  182. } /* DIR_isDirectory */
  183. static int DIR_isSymLink(DirHandle *h, const char *name)
  184. {
  185. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  186. int retval;
  187. BAIL_IF_MACRO(f == NULL, NULL, 0); /* !!! might be a problem. */
  188. retval = __PHYSFS_platformIsSymLink(f);
  189. free(f);
  190. return(retval);
  191. } /* DIR_isSymLink */
  192. static FileHandle *doOpen(DirHandle *h, const char *name,
  193. void *(*openFunc)(const char *filename),
  194. const FileFunctions *fileFuncs)
  195. {
  196. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  197. void *rc;
  198. FileHandle *retval;
  199. BAIL_IF_MACRO(f == NULL, NULL, NULL);
  200. retval = (FileHandle *) malloc(sizeof (FileHandle));
  201. if (!retval)
  202. {
  203. free(f);
  204. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  205. } /* if */
  206. rc = openFunc(f);
  207. free(f);
  208. if (!rc)
  209. {
  210. free(retval);
  211. return(NULL);
  212. } /* if */
  213. retval->opaque = (void *) rc;
  214. retval->dirHandle = h;
  215. retval->funcs = fileFuncs;
  216. return(retval);
  217. } /* doOpen */
  218. static FileHandle *DIR_openRead(DirHandle *h, const char *filename)
  219. {
  220. return(doOpen(h, filename, __PHYSFS_platformOpenRead,
  221. &__PHYSFS_FileFunctions_DIR));
  222. } /* DIR_openRead */
  223. static FileHandle *DIR_openWrite(DirHandle *h, const char *filename)
  224. {
  225. return(doOpen(h, filename, __PHYSFS_platformOpenWrite,
  226. &__PHYSFS_FileFunctions_DIRW));
  227. } /* DIR_openWrite */
  228. static FileHandle *DIR_openAppend(DirHandle *h, const char *filename)
  229. {
  230. return(doOpen(h, filename, __PHYSFS_platformOpenAppend,
  231. &__PHYSFS_FileFunctions_DIRW));
  232. } /* DIR_openAppend */
  233. static int DIR_remove(DirHandle *h, const char *name)
  234. {
  235. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  236. int retval;
  237. BAIL_IF_MACRO(f == NULL, NULL, 0);
  238. retval = __PHYSFS_platformDelete(f);
  239. free(f);
  240. return(retval);
  241. } /* DIR_remove */
  242. static int DIR_mkdir(DirHandle *h, const char *name)
  243. {
  244. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  245. int retval;
  246. BAIL_IF_MACRO(f == NULL, NULL, 0);
  247. retval = __PHYSFS_platformMkDir(f);
  248. free(f);
  249. return(retval);
  250. } /* DIR_mkdir */
  251. static void DIR_dirClose(DirHandle *h)
  252. {
  253. free(h->opaque);
  254. free(h);
  255. } /* DIR_dirClose */
  256. /* end of dir.c ... */