dir.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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_StringCallback cb,
  82. void *callbackdata)
  83. {
  84. char *d = __PHYSFS_platformCvtToDependent((char *)opaque, dname, NULL);
  85. if (d != NULL)
  86. {
  87. __PHYSFS_platformEnumerateFiles(d, omitSymLinks, cb, callbackdata);
  88. allocator.Free(d);
  89. } /* if */
  90. } /* DIR_enumerateFiles */
  91. static int DIR_exists(dvoid *opaque, const char *name)
  92. {
  93. char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
  94. int retval;
  95. BAIL_IF_MACRO(f == NULL, NULL, 0);
  96. retval = __PHYSFS_platformExists(f);
  97. allocator.Free(f);
  98. return(retval);
  99. } /* DIR_exists */
  100. static int DIR_isDirectory(dvoid *opaque, const char *name, int *fileExists)
  101. {
  102. char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
  103. int retval = 0;
  104. BAIL_IF_MACRO(d == NULL, NULL, 0);
  105. *fileExists = __PHYSFS_platformExists(d);
  106. if (*fileExists)
  107. retval = __PHYSFS_platformIsDirectory(d);
  108. allocator.Free(d);
  109. return(retval);
  110. } /* DIR_isDirectory */
  111. static int DIR_isSymLink(dvoid *opaque, const char *name, int *fileExists)
  112. {
  113. char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
  114. int retval = 0;
  115. BAIL_IF_MACRO(f == NULL, NULL, 0);
  116. *fileExists = __PHYSFS_platformExists(f);
  117. if (*fileExists)
  118. retval = __PHYSFS_platformIsSymLink(f);
  119. allocator.Free(f);
  120. return(retval);
  121. } /* DIR_isSymLink */
  122. static PHYSFS_sint64 DIR_getLastModTime(dvoid *opaque,
  123. const char *name,
  124. int *fileExists)
  125. {
  126. char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
  127. PHYSFS_sint64 retval = -1;
  128. BAIL_IF_MACRO(d == NULL, NULL, 0);
  129. *fileExists = __PHYSFS_platformExists(d);
  130. if (*fileExists)
  131. retval = __PHYSFS_platformGetLastModTime(d);
  132. allocator.Free(d);
  133. return(retval);
  134. } /* DIR_getLastModTime */
  135. static fvoid *doOpen(dvoid *opaque, const char *name,
  136. void *(*openFunc)(const char *filename),
  137. int *fileExists)
  138. {
  139. char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
  140. void *rc = NULL;
  141. BAIL_IF_MACRO(f == NULL, NULL, NULL);
  142. if (fileExists != NULL)
  143. {
  144. *fileExists = __PHYSFS_platformExists(f);
  145. if (!(*fileExists))
  146. {
  147. allocator.Free(f);
  148. return(NULL);
  149. } /* if */
  150. } /* if */
  151. rc = openFunc(f);
  152. allocator.Free(f);
  153. return((fvoid *) rc);
  154. } /* doOpen */
  155. static fvoid *DIR_openRead(dvoid *opaque, const char *fnm, int *exist)
  156. {
  157. return(doOpen(opaque, fnm, __PHYSFS_platformOpenRead, exist));
  158. } /* DIR_openRead */
  159. static fvoid *DIR_openWrite(dvoid *opaque, const char *filename)
  160. {
  161. return(doOpen(opaque, filename, __PHYSFS_platformOpenWrite, NULL));
  162. } /* DIR_openWrite */
  163. static fvoid *DIR_openAppend(dvoid *opaque, const char *filename)
  164. {
  165. return(doOpen(opaque, filename, __PHYSFS_platformOpenAppend, NULL));
  166. } /* DIR_openAppend */
  167. static int DIR_remove(dvoid *opaque, const char *name)
  168. {
  169. char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
  170. int retval;
  171. BAIL_IF_MACRO(f == NULL, NULL, 0);
  172. retval = __PHYSFS_platformDelete(f);
  173. allocator.Free(f);
  174. return(retval);
  175. } /* DIR_remove */
  176. static int DIR_mkdir(dvoid *opaque, const char *name)
  177. {
  178. char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
  179. int retval;
  180. BAIL_IF_MACRO(f == NULL, NULL, 0);
  181. retval = __PHYSFS_platformMkDir(f);
  182. allocator.Free(f);
  183. return(retval);
  184. } /* DIR_mkdir */
  185. static void DIR_dirClose(dvoid *opaque)
  186. {
  187. allocator.Free(opaque);
  188. } /* DIR_dirClose */
  189. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
  190. {
  191. "",
  192. DIR_ARCHIVE_DESCRIPTION,
  193. "Ryan C. Gordon <icculus@clutteredmind.org>",
  194. "http://icculus.org/physfs/",
  195. };
  196. const PHYSFS_Archiver __PHYSFS_Archiver_DIR =
  197. {
  198. &__PHYSFS_ArchiveInfo_DIR,
  199. DIR_isArchive, /* isArchive() method */
  200. DIR_openArchive, /* openArchive() method */
  201. DIR_enumerateFiles, /* enumerateFiles() method */
  202. DIR_exists, /* exists() method */
  203. DIR_isDirectory, /* isDirectory() method */
  204. DIR_isSymLink, /* isSymLink() method */
  205. DIR_getLastModTime, /* getLastModTime() method */
  206. DIR_openRead, /* openRead() method */
  207. DIR_openWrite, /* openWrite() method */
  208. DIR_openAppend, /* openAppend() method */
  209. DIR_remove, /* remove() method */
  210. DIR_mkdir, /* mkdir() method */
  211. DIR_dirClose, /* dirClose() method */
  212. DIR_read, /* read() method */
  213. DIR_write, /* write() method */
  214. DIR_eof, /* eof() method */
  215. DIR_tell, /* tell() method */
  216. DIR_seek, /* seek() method */
  217. DIR_fileLength, /* fileLength() method */
  218. DIR_fileClose /* fileClose() method */
  219. };
  220. /* end of dir.c ... */