dir.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 DirHandle *DIR_openArchive(const char *name, int forWriting);
  32. static LinkedStringList *DIR_enumerateFiles(DirHandle *h,
  33. const char *dname,
  34. int omitSymLinks);
  35. static int DIR_exists(DirHandle *h, const char *name);
  36. static int DIR_isDirectory(DirHandle *h, const char *name, int *fileExists);
  37. static int DIR_isSymLink(DirHandle *h, const char *name, int *fileExists);
  38. static FileHandle *DIR_openRead(DirHandle *h, const char *fnm, int *exist);
  39. static PHYSFS_sint64 DIR_getLastModTime(DirHandle *h, const char *f, int *e);
  40. static FileHandle *DIR_openWrite(DirHandle *h, const char *filename);
  41. static FileHandle *DIR_openAppend(DirHandle *h, const char *filename);
  42. static int DIR_remove(DirHandle *h, const char *name);
  43. static int DIR_mkdir(DirHandle *h, const char *name);
  44. static void DIR_dirClose(DirHandle *h);
  45. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
  46. {
  47. "",
  48. DIR_ARCHIVE_DESCRIPTION,
  49. "Ryan C. Gordon <icculus@icculus.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 DirHandle *DIR_openArchive(const char *name, int forWriting)
  147. {
  148. const char *dirsep = PHYSFS_getDirSeparator();
  149. DirHandle *retval = NULL;
  150. size_t namelen = strlen(name);
  151. size_t seplen = strlen(dirsep);
  152. BAIL_IF_MACRO(!DIR_isArchive(name, forWriting),
  153. ERR_UNSUPPORTED_ARCHIVE, NULL);
  154. retval = (DirHandle *) malloc(sizeof (DirHandle));
  155. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  156. retval->opaque = malloc(namelen + seplen + 1);
  157. if (retval->opaque == NULL)
  158. {
  159. free(retval);
  160. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  161. } /* if */
  162. /* make sure there's a dir separator at the end of the string */
  163. strcpy((char *) (retval->opaque), name);
  164. if (strcmp((name + namelen) - seplen, dirsep) != 0)
  165. strcat((char *) (retval->opaque), dirsep);
  166. retval->funcs = &__PHYSFS_DirFunctions_DIR;
  167. return(retval);
  168. } /* DIR_openArchive */
  169. static LinkedStringList *DIR_enumerateFiles(DirHandle *h,
  170. const char *dname,
  171. int omitSymLinks)
  172. {
  173. char *d = __PHYSFS_platformCvtToDependent((char *)(h->opaque),dname,NULL);
  174. LinkedStringList *retval;
  175. BAIL_IF_MACRO(d == NULL, NULL, NULL);
  176. retval = __PHYSFS_platformEnumerateFiles(d, omitSymLinks);
  177. free(d);
  178. return(retval);
  179. } /* DIR_enumerateFiles */
  180. static int DIR_exists(DirHandle *h, const char *name)
  181. {
  182. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  183. int retval;
  184. BAIL_IF_MACRO(f == NULL, NULL, 0);
  185. retval = __PHYSFS_platformExists(f);
  186. free(f);
  187. return(retval);
  188. } /* DIR_exists */
  189. static int DIR_isDirectory(DirHandle *h, const char *name, int *fileExists)
  190. {
  191. char *d = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  192. int retval = 0;
  193. BAIL_IF_MACRO(d == NULL, NULL, 0);
  194. *fileExists = __PHYSFS_platformExists(d);
  195. if (*fileExists)
  196. retval = __PHYSFS_platformIsDirectory(d);
  197. free(d);
  198. return(retval);
  199. } /* DIR_isDirectory */
  200. static int DIR_isSymLink(DirHandle *h, const char *name, int *fileExists)
  201. {
  202. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  203. int retval = 0;
  204. BAIL_IF_MACRO(f == NULL, NULL, 0);
  205. *fileExists = __PHYSFS_platformExists(f);
  206. if (*fileExists)
  207. retval = __PHYSFS_platformIsSymLink(f);
  208. free(f);
  209. return(retval);
  210. } /* DIR_isSymLink */
  211. static PHYSFS_sint64 DIR_getLastModTime(DirHandle *h,
  212. const char *name,
  213. int *fileExists)
  214. {
  215. char *d = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  216. PHYSFS_sint64 retval = -1;
  217. BAIL_IF_MACRO(d == NULL, NULL, 0);
  218. *fileExists = __PHYSFS_platformExists(d);
  219. if (*fileExists)
  220. retval = __PHYSFS_platformGetLastModTime(d);
  221. free(d);
  222. return(retval);
  223. } /* DIR_getLastModTime */
  224. static FileHandle *doOpen(DirHandle *h, const char *name,
  225. void *(*openFunc)(const char *filename),
  226. int *fileExists, const FileFunctions *fileFuncs)
  227. {
  228. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  229. void *rc;
  230. FileHandle *retval;
  231. BAIL_IF_MACRO(f == NULL, NULL, NULL);
  232. if (fileExists != NULL)
  233. {
  234. *fileExists = __PHYSFS_platformExists(f);
  235. if (!(*fileExists))
  236. {
  237. free(f);
  238. return(NULL);
  239. } /* if */
  240. } /* if */
  241. retval = (FileHandle *) malloc(sizeof (FileHandle));
  242. if (!retval)
  243. {
  244. free(f);
  245. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  246. } /* if */
  247. rc = openFunc(f);
  248. free(f);
  249. if (!rc)
  250. {
  251. free(retval);
  252. return(NULL);
  253. } /* if */
  254. retval->opaque = (void *) rc;
  255. retval->dirHandle = h;
  256. retval->funcs = fileFuncs;
  257. return(retval);
  258. } /* doOpen */
  259. static FileHandle *DIR_openRead(DirHandle *h, const char *fnm, int *exist)
  260. {
  261. return(doOpen(h, fnm, __PHYSFS_platformOpenRead, exist,
  262. &__PHYSFS_FileFunctions_DIR));
  263. } /* DIR_openRead */
  264. static FileHandle *DIR_openWrite(DirHandle *h, const char *filename)
  265. {
  266. return(doOpen(h, filename, __PHYSFS_platformOpenWrite, NULL,
  267. &__PHYSFS_FileFunctions_DIRW));
  268. } /* DIR_openWrite */
  269. static FileHandle *DIR_openAppend(DirHandle *h, const char *filename)
  270. {
  271. return(doOpen(h, filename, __PHYSFS_platformOpenAppend, NULL,
  272. &__PHYSFS_FileFunctions_DIRW));
  273. } /* DIR_openAppend */
  274. static int DIR_remove(DirHandle *h, const char *name)
  275. {
  276. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  277. int retval;
  278. BAIL_IF_MACRO(f == NULL, NULL, 0);
  279. retval = __PHYSFS_platformDelete(f);
  280. free(f);
  281. return(retval);
  282. } /* DIR_remove */
  283. static int DIR_mkdir(DirHandle *h, const char *name)
  284. {
  285. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  286. int retval;
  287. BAIL_IF_MACRO(f == NULL, NULL, 0);
  288. retval = __PHYSFS_platformMkDir(f);
  289. free(f);
  290. return(retval);
  291. } /* DIR_mkdir */
  292. static void DIR_dirClose(DirHandle *h)
  293. {
  294. free(h->opaque);
  295. free(h);
  296. } /* DIR_dirClose */
  297. /* end of dir.c ... */