dir.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
  44. {
  45. "",
  46. "non-archive directory I/O",
  47. "Ryan C. Gordon <icculus@clutteredmind.org>",
  48. "http://icculus.org/physfs/",
  49. };
  50. static const FileFunctions __PHYSFS_FileFunctions_DIR =
  51. {
  52. DIR_read, /* read() method */
  53. NULL, /* write() method */
  54. DIR_eof, /* eof() method */
  55. DIR_tell, /* tell() method */
  56. DIR_seek, /* seek() method */
  57. DIR_fileLength, /* fileLength() method */
  58. DIR_fileClose /* fileClose() method */
  59. };
  60. static const FileFunctions __PHYSFS_FileFunctions_DIRW =
  61. {
  62. NULL, /* read() method */
  63. DIR_write, /* write() method */
  64. DIR_eof, /* eof() method */
  65. DIR_tell, /* tell() method */
  66. DIR_seek, /* seek() method */
  67. DIR_fileLength, /* fileLength() method */
  68. DIR_fileClose /* fileClose() method */
  69. };
  70. const DirFunctions __PHYSFS_DirFunctions_DIR =
  71. {
  72. &__PHYSFS_ArchiveInfo_DIR,
  73. DIR_isArchive, /* isArchive() method */
  74. DIR_openArchive, /* openArchive() method */
  75. DIR_enumerateFiles, /* enumerateFiles() method */
  76. DIR_exists, /* exists() method */
  77. DIR_isDirectory, /* isDirectory() method */
  78. DIR_isSymLink, /* isSymLink() method */
  79. DIR_getLastModTime, /* getLastModTime() method */
  80. DIR_openRead, /* openRead() method */
  81. DIR_openWrite, /* openWrite() method */
  82. DIR_openAppend, /* openAppend() method */
  83. DIR_remove, /* remove() method */
  84. DIR_mkdir, /* mkdir() method */
  85. DIR_dirClose /* dirClose() method */
  86. };
  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);
  191. retval = __PHYSFS_platformIsSymLink(f);
  192. free(f);
  193. return(retval);
  194. } /* DIR_isSymLink */
  195. static PHYSFS_sint64 DIR_getLastModTime(DirHandle *h, const char *name)
  196. {
  197. char *d = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  198. PHYSFS_sint64 retval;
  199. BAIL_IF_MACRO(d == NULL, NULL, 0);
  200. retval = __PHYSFS_platformGetLastModTime(d);
  201. free(d);
  202. return(retval);
  203. } /* DIR_getLastModTime */
  204. static FileHandle *doOpen(DirHandle *h, const char *name,
  205. void *(*openFunc)(const char *filename),
  206. const FileFunctions *fileFuncs)
  207. {
  208. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  209. void *rc;
  210. FileHandle *retval;
  211. BAIL_IF_MACRO(f == NULL, NULL, NULL);
  212. retval = (FileHandle *) malloc(sizeof (FileHandle));
  213. if (!retval)
  214. {
  215. free(f);
  216. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  217. } /* if */
  218. rc = openFunc(f);
  219. free(f);
  220. if (!rc)
  221. {
  222. free(retval);
  223. return(NULL);
  224. } /* if */
  225. retval->opaque = (void *) rc;
  226. retval->dirHandle = h;
  227. retval->funcs = fileFuncs;
  228. return(retval);
  229. } /* doOpen */
  230. static FileHandle *DIR_openRead(DirHandle *h, const char *filename)
  231. {
  232. return(doOpen(h, filename, __PHYSFS_platformOpenRead,
  233. &__PHYSFS_FileFunctions_DIR));
  234. } /* DIR_openRead */
  235. static FileHandle *DIR_openWrite(DirHandle *h, const char *filename)
  236. {
  237. return(doOpen(h, filename, __PHYSFS_platformOpenWrite,
  238. &__PHYSFS_FileFunctions_DIRW));
  239. } /* DIR_openWrite */
  240. static FileHandle *DIR_openAppend(DirHandle *h, const char *filename)
  241. {
  242. return(doOpen(h, filename, __PHYSFS_platformOpenAppend,
  243. &__PHYSFS_FileFunctions_DIRW));
  244. } /* DIR_openAppend */
  245. static int DIR_remove(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_platformDelete(f);
  251. free(f);
  252. return(retval);
  253. } /* DIR_remove */
  254. static int DIR_mkdir(DirHandle *h, const char *name)
  255. {
  256. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  257. int retval;
  258. BAIL_IF_MACRO(f == NULL, NULL, 0);
  259. retval = __PHYSFS_platformMkDir(f);
  260. free(f);
  261. return(retval);
  262. } /* DIR_mkdir */
  263. static void DIR_dirClose(DirHandle *h)
  264. {
  265. free(h->opaque);
  266. free(h);
  267. } /* DIR_dirClose */
  268. /* end of dir.c ... */