dir.c 7.9 KB

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