dir.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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(FileHandle *handle, void *buffer,
  18. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  19. static PHYSFS_sint64 DIR_write(FileHandle *handle, const void *buffer,
  20. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  21. static PHYSFS_sint64 DIR_dummyRead(FileHandle *handle, void *buffer,
  22. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  23. static PHYSFS_sint64 DIR_dummyWrite(FileHandle *handle, const void *buffer,
  24. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
  25. static int DIR_eof(FileHandle *handle);
  26. static PHYSFS_sint64 DIR_tell(FileHandle *handle);
  27. static int DIR_seek(FileHandle *handle, PHYSFS_uint64 offset);
  28. static PHYSFS_sint64 DIR_fileLength(FileHandle *handle);
  29. static int DIR_fileClose(FileHandle *handle);
  30. static int DIR_isArchive(const char *filename, int forWriting);
  31. static void *DIR_openArchive(const char *name, int forWriting);
  32. static LinkedStringList *DIR_enumerateFiles(void *opaque,
  33. const char *dname,
  34. int omitSymLinks);
  35. static int DIR_exists(void *opaque, const char *name);
  36. static int DIR_isDirectory(void *opaque, const char *name, int *fileExists);
  37. static int DIR_isSymLink(void *opaque, const char *name, int *fileExists);
  38. static FileHandle *DIR_openRead(void *opaque, const char *fnm, int *exist);
  39. static PHYSFS_sint64 DIR_getLastModTime(void *opaque, const char *f, int *e);
  40. static FileHandle *DIR_openWrite(void *opaque, const char *filename);
  41. static FileHandle *DIR_openAppend(void *opaque, const char *filename);
  42. static int DIR_remove(void *opaque, const char *name);
  43. static int DIR_mkdir(void *opaque, const char *name);
  44. static void DIR_dirClose(void *opaque);
  45. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
  46. {
  47. "",
  48. DIR_ARCHIVE_DESCRIPTION,
  49. "Ryan C. Gordon <icculus@clutteredmind.org>",
  50. "http://icculus.org/physfs/",
  51. };
  52. static const FileFunctions __PHYSFS_FileFunctions_DIR =
  53. {
  54. DIR_read, /* read() method */
  55. DIR_dummyWrite, /* 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. static const FileFunctions __PHYSFS_FileFunctions_DIRW =
  63. {
  64. DIR_dummyRead, /* 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. const DirFunctions __PHYSFS_DirFunctions_DIR =
  73. {
  74. &__PHYSFS_ArchiveInfo_DIR,
  75. DIR_isArchive, /* isArchive() method */
  76. DIR_openArchive, /* openArchive() method */
  77. DIR_enumerateFiles, /* enumerateFiles() method */
  78. DIR_exists, /* exists() method */
  79. DIR_isDirectory, /* isDirectory() method */
  80. DIR_isSymLink, /* isSymLink() method */
  81. DIR_getLastModTime, /* getLastModTime() method */
  82. DIR_openRead, /* openRead() method */
  83. DIR_openWrite, /* openWrite() method */
  84. DIR_openAppend, /* openAppend() method */
  85. DIR_remove, /* remove() method */
  86. DIR_mkdir, /* mkdir() method */
  87. DIR_dirClose /* dirClose() method */
  88. };
  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 PHYSFS_sint64 DIR_dummyRead(FileHandle *handle, void *buffer,
  104. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  105. {
  106. BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
  107. } /* DIR_dummyRead */
  108. static PHYSFS_sint64 DIR_dummyWrite(FileHandle *handle, const void *buffer,
  109. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  110. {
  111. BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
  112. } /* DIR_dummyWrite */
  113. static int DIR_eof(FileHandle *handle)
  114. {
  115. return(__PHYSFS_platformEOF(handle->opaque));
  116. } /* DIR_eof */
  117. static PHYSFS_sint64 DIR_tell(FileHandle *handle)
  118. {
  119. return(__PHYSFS_platformTell(handle->opaque));
  120. } /* DIR_tell */
  121. static int DIR_seek(FileHandle *handle, PHYSFS_uint64 offset)
  122. {
  123. return(__PHYSFS_platformSeek(handle->opaque, offset));
  124. } /* DIR_seek */
  125. static PHYSFS_sint64 DIR_fileLength(FileHandle *handle)
  126. {
  127. return(__PHYSFS_platformFileLength(handle->opaque));
  128. } /* DIR_fileLength */
  129. static int DIR_fileClose(FileHandle *handle)
  130. {
  131. /*
  132. * we manually flush the buffer, since that's the place a close will
  133. * most likely fail, but that will leave the file handle in an undefined
  134. * state if it fails. Flush failures we can recover from.
  135. */
  136. BAIL_IF_MACRO(!__PHYSFS_platformFlush(handle->opaque), NULL, 0);
  137. BAIL_IF_MACRO(!__PHYSFS_platformClose(handle->opaque), NULL, 0);
  138. free(handle);
  139. return(1);
  140. } /* DIR_fileClose */
  141. static int DIR_isArchive(const char *filename, int forWriting)
  142. {
  143. /* directories ARE archives in this driver... */
  144. return(__PHYSFS_platformIsDirectory(filename));
  145. } /* DIR_isArchive */
  146. static void *DIR_openArchive(const char *name, int forWriting)
  147. {
  148. const char *dirsep = PHYSFS_getDirSeparator();
  149. char *retval = NULL;
  150. size_t namelen = strlen(name);
  151. size_t seplen = strlen(dirsep);
  152. /* !!! FIXME: when is this not called right before openArchive? */
  153. BAIL_IF_MACRO(!DIR_isArchive(name, forWriting),
  154. ERR_UNSUPPORTED_ARCHIVE, 0);
  155. retval = malloc(namelen + seplen + 1);
  156. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  157. /* make sure there's a dir separator at the end of the string */
  158. strcpy(retval, name);
  159. if (strcmp((name + namelen) - seplen, dirsep) != 0)
  160. strcat(retval, dirsep);
  161. return(retval);
  162. } /* DIR_openArchive */
  163. static LinkedStringList *DIR_enumerateFiles(void *opaque,
  164. const char *dname,
  165. int omitSymLinks)
  166. {
  167. char *d = __PHYSFS_platformCvtToDependent((char *)opaque, dname, NULL);
  168. LinkedStringList *retval;
  169. BAIL_IF_MACRO(d == NULL, NULL, NULL);
  170. retval = __PHYSFS_platformEnumerateFiles(d, omitSymLinks);
  171. free(d);
  172. return(retval);
  173. } /* DIR_enumerateFiles */
  174. static int DIR_exists(void *opaque, const char *name)
  175. {
  176. char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
  177. int retval;
  178. BAIL_IF_MACRO(f == NULL, NULL, 0);
  179. retval = __PHYSFS_platformExists(f);
  180. free(f);
  181. return(retval);
  182. } /* DIR_exists */
  183. static int DIR_isDirectory(void *opaque, const char *name, int *fileExists)
  184. {
  185. char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
  186. int retval = 0;
  187. BAIL_IF_MACRO(d == NULL, NULL, 0);
  188. *fileExists = __PHYSFS_platformExists(d);
  189. if (*fileExists)
  190. retval = __PHYSFS_platformIsDirectory(d);
  191. free(d);
  192. return(retval);
  193. } /* DIR_isDirectory */
  194. static int DIR_isSymLink(void *opaque, const char *name, int *fileExists)
  195. {
  196. char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
  197. int retval = 0;
  198. BAIL_IF_MACRO(f == NULL, NULL, 0);
  199. *fileExists = __PHYSFS_platformExists(f);
  200. if (*fileExists)
  201. retval = __PHYSFS_platformIsSymLink(f);
  202. free(f);
  203. return(retval);
  204. } /* DIR_isSymLink */
  205. static PHYSFS_sint64 DIR_getLastModTime(void *opaque,
  206. const char *name,
  207. int *fileExists)
  208. {
  209. char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
  210. PHYSFS_sint64 retval = -1;
  211. BAIL_IF_MACRO(d == NULL, NULL, 0);
  212. *fileExists = __PHYSFS_platformExists(d);
  213. if (*fileExists)
  214. retval = __PHYSFS_platformGetLastModTime(d);
  215. free(d);
  216. return(retval);
  217. } /* DIR_getLastModTime */
  218. static FileHandle *doOpen(void *opaque, const char *name,
  219. void *(*openFunc)(const char *filename),
  220. int *fileExists, const FileFunctions *fileFuncs)
  221. {
  222. char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
  223. void *rc;
  224. FileHandle *retval;
  225. BAIL_IF_MACRO(f == NULL, NULL, NULL);
  226. if (fileExists != NULL)
  227. {
  228. *fileExists = __PHYSFS_platformExists(f);
  229. if (!(*fileExists))
  230. {
  231. free(f);
  232. return(NULL);
  233. } /* if */
  234. } /* if */
  235. retval = (FileHandle *) malloc(sizeof (FileHandle));
  236. if (!retval)
  237. {
  238. free(f);
  239. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  240. } /* if */
  241. rc = openFunc(f);
  242. free(f);
  243. if (!rc)
  244. {
  245. free(retval);
  246. return(NULL);
  247. } /* if */
  248. retval->opaque = (void *) rc;
  249. retval->funcs = fileFuncs;
  250. return(retval);
  251. } /* doOpen */
  252. static FileHandle *DIR_openRead(void *opaque, const char *fnm, int *exist)
  253. {
  254. return(doOpen(opaque, fnm, __PHYSFS_platformOpenRead, exist,
  255. &__PHYSFS_FileFunctions_DIR));
  256. } /* DIR_openRead */
  257. static FileHandle *DIR_openWrite(void *opaque, const char *filename)
  258. {
  259. return(doOpen(opaque, filename, __PHYSFS_platformOpenWrite, NULL,
  260. &__PHYSFS_FileFunctions_DIRW));
  261. } /* DIR_openWrite */
  262. static FileHandle *DIR_openAppend(void *opaque, const char *filename)
  263. {
  264. return(doOpen(opaque, filename, __PHYSFS_platformOpenAppend, NULL,
  265. &__PHYSFS_FileFunctions_DIRW));
  266. } /* DIR_openAppend */
  267. static int DIR_remove(void *opaque, const char *name)
  268. {
  269. char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
  270. int retval;
  271. BAIL_IF_MACRO(f == NULL, NULL, 0);
  272. retval = __PHYSFS_platformDelete(f);
  273. free(f);
  274. return(retval);
  275. } /* DIR_remove */
  276. static int DIR_mkdir(void *opaque, const char *name)
  277. {
  278. char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
  279. int retval;
  280. BAIL_IF_MACRO(f == NULL, NULL, 0);
  281. retval = __PHYSFS_platformMkDir(f);
  282. free(f);
  283. return(retval);
  284. } /* DIR_mkdir */
  285. static void DIR_dirClose(void *opaque)
  286. {
  287. free(opaque);
  288. } /* DIR_dirClose */
  289. /* end of dir.c ... */