dir.c 9.2 KB

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