archiver_dir.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #define __PHYSICSFS_INTERNAL__
  9. #include "physfs_internal.h"
  10. /* There's no PHYSFS_Io interface here. Use __PHYSFS_createNativeIo(). */
  11. static void *DIR_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
  12. {
  13. PHYSFS_Stat statbuf;
  14. const char *dirsep = PHYSFS_getDirSeparator();
  15. char *retval = NULL;
  16. const size_t namelen = strlen(name);
  17. const size_t seplen = strlen(dirsep);
  18. int exists = 0;
  19. assert(io == NULL); /* shouldn't create an Io for these. */
  20. BAIL_IF_MACRO(!__PHYSFS_platformStat(name, &exists, &statbuf), NULL, NULL);
  21. if ((!exists) || (statbuf.filetype != PHYSFS_FILETYPE_DIRECTORY))
  22. BAIL_MACRO(ERR_NOT_AN_ARCHIVE, NULL);
  23. retval = allocator.Malloc(namelen + seplen + 1);
  24. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  25. /* make sure there's a dir separator at the end of the string */
  26. /* !!! FIXME: is there really any place where (seplen != 1)? */
  27. strcpy(retval, name);
  28. if (strcmp((name + namelen) - seplen, dirsep) != 0)
  29. strcat(retval, dirsep);
  30. return retval;
  31. } /* DIR_openArchive */
  32. /* !!! FIXME: I would like to smallAlloc() all these conversions somehow. */
  33. static void DIR_enumerateFiles(dvoid *opaque, const char *dname,
  34. int omitSymLinks, PHYSFS_EnumFilesCallback cb,
  35. const char *origdir, void *callbackdata)
  36. {
  37. char *d = __PHYSFS_platformCvtToDependent((char *) opaque, dname, NULL);
  38. if (d != NULL)
  39. {
  40. __PHYSFS_platformEnumerateFiles(d, omitSymLinks, cb,
  41. origdir, callbackdata);
  42. allocator.Free(d);
  43. } /* if */
  44. } /* DIR_enumerateFiles */
  45. static PHYSFS_Io *doOpen(dvoid *opaque, const char *name,
  46. const int mode, int *fileExists)
  47. {
  48. char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
  49. PHYSFS_Io *io = NULL;
  50. int existtmp = 0;
  51. if (fileExists == NULL)
  52. fileExists = &existtmp;
  53. *fileExists = 0;
  54. BAIL_IF_MACRO(f == NULL, NULL, NULL);
  55. io = __PHYSFS_createNativeIo(f, mode);
  56. allocator.Free(f);
  57. if (io == NULL)
  58. {
  59. PHYSFS_Stat statbuf;
  60. __PHYSFS_platformStat(f, fileExists, &statbuf);
  61. return NULL;
  62. } /* if */
  63. *fileExists = 1;
  64. return io;
  65. } /* doOpen */
  66. static PHYSFS_Io *DIR_openRead(dvoid *opaque, const char *fnm, int *exist)
  67. {
  68. return doOpen(opaque, fnm, 'r', exist);
  69. } /* DIR_openRead */
  70. static PHYSFS_Io *DIR_openWrite(dvoid *opaque, const char *filename)
  71. {
  72. return doOpen(opaque, filename, 'w', NULL);
  73. } /* DIR_openWrite */
  74. static PHYSFS_Io *DIR_openAppend(dvoid *opaque, const char *filename)
  75. {
  76. return doOpen(opaque, filename, 'a', NULL);
  77. } /* DIR_openAppend */
  78. static int DIR_remove(dvoid *opaque, const char *name)
  79. {
  80. char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
  81. int retval;
  82. BAIL_IF_MACRO(f == NULL, NULL, 0);
  83. retval = __PHYSFS_platformDelete(f);
  84. allocator.Free(f);
  85. return retval;
  86. } /* DIR_remove */
  87. static int DIR_mkdir(dvoid *opaque, const char *name)
  88. {
  89. char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
  90. int retval;
  91. BAIL_IF_MACRO(f == NULL, NULL, 0);
  92. retval = __PHYSFS_platformMkDir(f);
  93. allocator.Free(f);
  94. return retval;
  95. } /* DIR_mkdir */
  96. static void DIR_dirClose(dvoid *opaque)
  97. {
  98. allocator.Free(opaque);
  99. } /* DIR_dirClose */
  100. static int DIR_stat(dvoid *opaque, const char *name, int *exists,
  101. PHYSFS_Stat *stat)
  102. {
  103. char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
  104. int retval = 0;
  105. BAIL_IF_MACRO(d == NULL, NULL, 0);
  106. retval = __PHYSFS_platformStat(d, exists, stat);
  107. allocator.Free(d);
  108. return retval;
  109. } /* DIR_stat */
  110. const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
  111. {
  112. "",
  113. DIR_ARCHIVE_DESCRIPTION,
  114. "Ryan C. Gordon <icculus@icculus.org>",
  115. "http://icculus.org/physfs/",
  116. };
  117. const PHYSFS_Archiver __PHYSFS_Archiver_DIR =
  118. {
  119. &__PHYSFS_ArchiveInfo_DIR,
  120. DIR_openArchive, /* openArchive() method */
  121. DIR_enumerateFiles, /* enumerateFiles() method */
  122. DIR_openRead, /* openRead() method */
  123. DIR_openWrite, /* openWrite() method */
  124. DIR_openAppend, /* openAppend() method */
  125. DIR_remove, /* remove() method */
  126. DIR_mkdir, /* mkdir() method */
  127. DIR_dirClose, /* dirClose() method */
  128. DIR_stat /* stat() method */
  129. };
  130. /* end of dir.c ... */