dir.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <errno.h>
  12. #include <sys/stat.h>
  13. #include <sys/types.h>
  14. #include <fcntl.h>
  15. #include <unistd.h>
  16. #include "physfs.h"
  17. #define __PHYSICSFS_INTERNAL__
  18. #include "physfs_internal.h"
  19. extern const DirFunctions __PHYSFS_DirFunctions_DIR;
  20. static const FileFunctions __PHYSFS_FileFunctions_DIR;
  21. static const FileFunctions __PHYSFS_FileFunctions_DIRW;
  22. static int DIR_read(FileHandle *handle, void *buffer,
  23. unsigned int objSize, unsigned int objCount)
  24. {
  25. FILE *h = (FILE *) (handle->opaque);
  26. int retval;
  27. errno = 0;
  28. retval = fread(buffer, objSize, objCount, h);
  29. BAIL_IF_MACRO((retval < objCount) && (ferror(h)),strerror(errno),retval);
  30. return(retval);
  31. } /* DIR_read */
  32. static int DIR_write(FileHandle *handle, void *buffer,
  33. unsigned int objSize, unsigned int objCount)
  34. {
  35. FILE *h = (FILE *) (handle->opaque);
  36. int retval;
  37. errno = 0;
  38. retval = fwrite(buffer, objSize, objCount, h);
  39. if ( (retval < objCount) && (ferror(h)) )
  40. __PHYSFS_setError(strerror(errno));
  41. return(retval);
  42. } /* DIR_write */
  43. static int DIR_eof(FileHandle *handle)
  44. {
  45. return(feof((FILE *) (handle->opaque)));
  46. } /* DIR_eof */
  47. static int DIR_tell(FileHandle *handle)
  48. {
  49. return(ftell((FILE *) (handle->opaque)));
  50. } /* DIR_tell */
  51. static int DIR_seek(FileHandle *handle, int offset)
  52. {
  53. return(fseek((FILE *) (handle->opaque), offset, SEEK_SET) == 0);
  54. } /* DIR_seek */
  55. static int DIR_fileClose(FileHandle *handle)
  56. {
  57. FILE *h = (FILE *) (handle->opaque);
  58. /*
  59. * we manually fflush() the buffer, since that's the place fclose() will
  60. * most likely fail, but that will leave the file handle in an undefined
  61. * state if it fails. fflush() failures we can recover from.
  62. */
  63. /* keep trying until there's success or an unrecoverable error... */
  64. do {
  65. __PHYSFS_platformTimeslice();
  66. errno = 0;
  67. } while ( (fflush(h) == EOF) && ((errno == EAGAIN) || (errno == EINTR)) );
  68. /* EBADF == "Not open for writing". That's fine. */
  69. BAIL_IF_MACRO((errno != 0) && (errno != EBADF), strerror(errno), 0);
  70. /* if fclose fails anyhow, we just have to pray that it's still usable. */
  71. errno = 0;
  72. BAIL_IF_MACRO(fclose(h) == EOF, strerror(errno), 0); /* (*shrug*) */
  73. free(handle);
  74. return(1);
  75. } /* DIR_fileClose */
  76. static int DIR_isArchive(const char *filename, int forWriting)
  77. {
  78. /* directories ARE archives in this driver... */
  79. return(__PHYSFS_platformIsDirectory(filename));
  80. } /* DIR_isArchive */
  81. static DirHandle *DIR_openArchive(const char *name, int forWriting)
  82. {
  83. const char *dirsep = PHYSFS_getDirSeparator();
  84. DirHandle *retval = NULL;
  85. int namelen = strlen(name);
  86. int seplen = strlen(dirsep);
  87. BAIL_IF_MACRO(!DIR_isArchive(name, forWriting),
  88. ERR_UNSUPPORTED_ARCHIVE, NULL);
  89. retval = malloc(sizeof (DirHandle));
  90. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  91. retval->opaque = malloc(namelen + seplen + 1);
  92. if (retval->opaque == NULL)
  93. {
  94. free(retval);
  95. BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
  96. } /* if */
  97. /* make sure there's a dir separator at the end of the string */
  98. strcpy((char *) (retval->opaque), name);
  99. if (strcmp((name + namelen) - seplen, dirsep) != 0)
  100. strcat((char *) (retval->opaque), dirsep);
  101. retval->funcs = &__PHYSFS_DirFunctions_DIR;
  102. return(retval);
  103. } /* DIR_openArchive */
  104. static LinkedStringList *DIR_enumerateFiles(DirHandle *h, const char *dname)
  105. {
  106. char *d = __PHYSFS_platformCvtToDependent((char *)(h->opaque),dname,NULL);
  107. LinkedStringList *retval;
  108. BAIL_IF_MACRO(d == NULL, NULL, NULL);
  109. retval = __PHYSFS_platformEnumerateFiles(d);
  110. free(d);
  111. return(retval);
  112. } /* DIR_enumerateFiles */
  113. static int DIR_exists(DirHandle *h, const char *name)
  114. {
  115. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  116. int retval;
  117. BAIL_IF_MACRO(f == NULL, NULL, 0);
  118. retval = __PHYSFS_platformExists(f);
  119. free(f);
  120. return(retval);
  121. } /* DIR_exists */
  122. static int DIR_isDirectory(DirHandle *h, const char *name)
  123. {
  124. char *d = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  125. int retval;
  126. BAIL_IF_MACRO(d == NULL, NULL, 0);
  127. retval = __PHYSFS_platformIsDirectory(d);
  128. free(d);
  129. return(retval);
  130. } /* DIR_isDirectory */
  131. static int DIR_isSymLink(DirHandle *h, const char *name)
  132. {
  133. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  134. int retval;
  135. BAIL_IF_MACRO(f == NULL, NULL, 0); /* !!! might be a problem. */
  136. retval = __PHYSFS_platformIsSymLink(f);
  137. free(f);
  138. return(retval);
  139. } /* DIR_isSymLink */
  140. static FileHandle *doOpen(DirHandle *h, const char *name, const char *mode)
  141. {
  142. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  143. FILE *rc;
  144. FileHandle *retval;
  145. char *str;
  146. BAIL_IF_MACRO(f == NULL, NULL, NULL);
  147. retval = (FileHandle *) malloc(sizeof (FileHandle));
  148. if (!retval)
  149. {
  150. free(f);
  151. BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
  152. } /* if */
  153. errno = 0;
  154. rc = fopen(f, mode);
  155. str = strerror(errno);
  156. free(f);
  157. if (!rc)
  158. {
  159. free(retval);
  160. BAIL_IF_MACRO(1, str, NULL);
  161. } /* if */
  162. retval->opaque = (void *) rc;
  163. retval->dirHandle = h;
  164. retval->funcs = &__PHYSFS_FileFunctions_DIR;
  165. return(retval);
  166. } /* doOpen */
  167. static FileHandle *DIR_openRead(DirHandle *h, const char *filename)
  168. {
  169. return(doOpen(h, filename, "rb"));
  170. } /* DIR_openRead */
  171. static FileHandle *DIR_openWrite(DirHandle *h, const char *filename)
  172. {
  173. return(doOpen(h, filename, "wb"));
  174. } /* DIR_openWrite */
  175. static FileHandle *DIR_openAppend(DirHandle *h, const char *filename)
  176. {
  177. return(doOpen(h, filename, "ab"));
  178. } /* DIR_openAppend */
  179. static int DIR_remove(DirHandle *h, const char *name)
  180. {
  181. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  182. int retval;
  183. BAIL_IF_MACRO(f == NULL, NULL, 0);
  184. errno = 0;
  185. retval = (remove(f) == 0);
  186. if (!retval)
  187. __PHYSFS_setError(strerror(errno));
  188. free(f);
  189. return(retval);
  190. } /* DIR_remove */
  191. static int DIR_mkdir(DirHandle *h, const char *name)
  192. {
  193. char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
  194. int retval;
  195. BAIL_IF_MACRO(f == NULL, NULL, 0);
  196. errno = 0;
  197. retval = (mkdir(f, S_IRWXU) == 0);
  198. if (!retval)
  199. __PHYSFS_setError(strerror(errno));
  200. free(f);
  201. return(retval);
  202. } /* DIR_mkdir */
  203. static void DIR_dirClose(DirHandle *h)
  204. {
  205. free(h->opaque);
  206. free(h);
  207. } /* DIR_dirClose */
  208. static const FileFunctions __PHYSFS_FileFunctions_DIR =
  209. {
  210. DIR_read, /* read() method */
  211. NULL, /* write() method */
  212. DIR_eof, /* eof() method */
  213. DIR_tell, /* tell() method */
  214. DIR_seek, /* seek() method */
  215. DIR_fileClose, /* fileClose() method */
  216. };
  217. static const FileFunctions __PHYSFS_FileFunctions_DIRW =
  218. {
  219. NULL, /* read() method */
  220. DIR_write, /* write() method */
  221. DIR_eof, /* eof() method */
  222. DIR_tell, /* tell() method */
  223. DIR_seek, /* seek() method */
  224. DIR_fileClose /* fileClose() method */
  225. };
  226. const DirFunctions __PHYSFS_DirFunctions_DIR =
  227. {
  228. DIR_isArchive, /* isArchive() method */
  229. DIR_openArchive, /* openArchive() method */
  230. DIR_enumerateFiles, /* enumerateFiles() method */
  231. DIR_exists, /* exists() method */
  232. DIR_isDirectory, /* isDirectory() method */
  233. DIR_isSymLink, /* isSymLink() method */
  234. DIR_openRead, /* openRead() method */
  235. DIR_openWrite, /* openWrite() method */
  236. DIR_openAppend, /* openAppend() method */
  237. DIR_remove, /* remove() method */
  238. DIR_mkdir, /* mkdir() method */
  239. DIR_dirClose /* dirClose() method */
  240. };
  241. /* This doesn't get listed, since it's technically not an archive... */
  242. #if 0
  243. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
  244. {
  245. "DIR",
  246. "non-archive directory I/O"
  247. };
  248. #endif
  249. /* end of dir.c ... */