dir.c 8.6 KB

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