dir.c 7.8 KB

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