dir.c 11 KB

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