dir.c 9.9 KB

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