physfs_internal.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Internal function/structure declaration. Do NOT include in your
  3. * application.
  4. *
  5. * Please see the file LICENSE in the source's root directory.
  6. *
  7. * This file written by Ryan C. Gordon.
  8. */
  9. #ifndef _INCLUDE_PHYSFS_INTERNAL_H_
  10. #define _INCLUDE_PHYSFS_INTERNAL_H_
  11. #ifndef __PHYSICSFS_INTERNAL__
  12. #error Do not include this header from your applications.
  13. #endif
  14. struct __PHYSFS_DIRHANDLE__;
  15. struct __PHYSFS_FILEFUNCTIONS__;
  16. typedef struct __PHYSFS_FILEHANDLE__
  17. {
  18. /*
  19. * This is reserved for the driver to store information.
  20. */
  21. void *opaque;
  22. /*
  23. * This should be the DirHandle that created this FileHandle.
  24. */
  25. const struct __PHYSFS_DIRHANDLE__ *dirHandle;
  26. /*
  27. * Pointer to the file i/o functions for this filehandle.
  28. */
  29. const struct __PHYSFS_FILEFUNCTIONS__ *funcs;
  30. } FileHandle;
  31. typedef struct __PHYSFS_FILEFUNCTIONS__
  32. {
  33. /*
  34. * Read more from the file.
  35. * Returns number of objects of (objSize) bytes read from file, -1
  36. * if complete failure.
  37. * On failure, call __PHYSFS_setError().
  38. */
  39. int (*read)(FileHandle *handle, void *buffer,
  40. unsigned int objSize, unsigned int objCount);
  41. /*
  42. * Write more to the file. Archives don't have to implement this.
  43. * (Set it to NULL if not implemented).
  44. * Returns number of objects of (objSize) bytes written to file, -1
  45. * if complete failure.
  46. * On failure, call __PHYSFS_setError().
  47. */
  48. int (*write)(FileHandle *handle, void *buffer,
  49. unsigned int objSize, unsigned int objCount);
  50. /*
  51. * Returns non-zero if at end of file.
  52. */
  53. int (*eof)(FileHandle *handle);
  54. /*
  55. * Returns byte offset from start of file.
  56. */
  57. int (*tell)(FileHandle *handle);
  58. /*
  59. * Move read/write pointer to byte offset from start of file.
  60. * Returns non-zero on success, zero on error.
  61. * On failure, call __PHYSFS_setError().
  62. */
  63. int (*seek)(FileHandle *handle, int offset);
  64. /*
  65. * Close the file, and free the FileHandle structure (including "opaque").
  66. * returns non-zero on success, zero if can't close file.
  67. * On failure, call __PHYSFS_setError().
  68. */
  69. int (*close)(FileHandle *handle);
  70. } FileFunctions;
  71. typedef struct __PHYSFS_DIRHANDLE__
  72. {
  73. /*
  74. * This is reserved for the driver to store information.
  75. */
  76. void *opaque;
  77. /*
  78. * Pointer to the directory i/o functions for this handle.
  79. */
  80. const struct __PHYSFS_DIRFUNCTIONS__ *funcs;
  81. } DirHandle;
  82. typedef struct __PHYSFS_LINKEDSTRINGLIST__
  83. {
  84. char *str;
  85. struct __PHYSFS_LINKEDSTRINGLIST__ *next;
  86. } LinkedStringList;
  87. /*
  88. * Symlinks should always be followed; PhysicsFS will use
  89. * DirFunctions->isSymLink() and make a judgement on whether to
  90. * continue to call other methods based on that.
  91. */
  92. typedef struct __PHYSFS_DIRFUNCTIONS__
  93. {
  94. /*
  95. * Returns non-zero if (filename) is a valid archive that this
  96. * driver can handle. This filename is in platform-dependent
  97. * notation. forWriting is non-zero if this is to be used for
  98. * the write directory, and zero if this is to be used for an
  99. * element of the search path.
  100. */
  101. int (*isArchive)(const char *filename, int forWriting);
  102. /*
  103. * Return a DirHandle for dir/archive (name).
  104. * This filename is in platform-dependent notation.
  105. * forWriting is non-zero if this is to be used for
  106. * the write directory, and zero if this is to be used for an
  107. * element of the search path.
  108. * Returns NULL on failure, and calls __PHYSFS_setError().
  109. */
  110. DirHandle *(*openArchive)(const char *name, int forWriting);
  111. /*
  112. * Returns a list of all files in dirname. Each element of this list
  113. * (and its "str" field) will be deallocated with the system's free()
  114. * function by the caller, so be sure to explicitly malloc() each
  115. * chunk.
  116. * If you have a memory failure, return as much as you can.
  117. * This dirname is in platform-independent notation.
  118. */
  119. LinkedStringList *(*enumerateFiles)(DirHandle *r, const char *dirname);
  120. /*
  121. * Returns non-zero if filename can be opened for reading.
  122. * This filename is in platform-independent notation.
  123. */
  124. int (*exists)(DirHandle *r, const char *name);
  125. /*
  126. * Returns non-zero if filename is really a directory.
  127. * This filename is in platform-independent notation.
  128. */
  129. int (*isDirectory)(DirHandle *r, const char *name);
  130. /*
  131. * Returns non-zero if filename is really a symlink.
  132. * This filename is in platform-independent notation.
  133. */
  134. int (*isSymLink)(DirHandle *r, const char *name);
  135. /*
  136. * Open file for reading, and return a FileHandle.
  137. * This filename is in platform-independent notation.
  138. * If you can't handle multiple opens of the same file,
  139. * you can opt to fail for the second call.
  140. * Returns NULL on failure, and calls __PHYSFS_setError().
  141. */
  142. FileHandle *(*openRead)(DirHandle *r, const char *filename);
  143. /*
  144. * Open file for writing, and return a FileHandle.
  145. * This filename is in platform-independent notation.
  146. * This method may be NULL.
  147. * If you can't handle multiple opens of the same file,
  148. * you can opt to fail for the second call.
  149. * Returns NULL on failure, and calls __PHYSFS_setError().
  150. */
  151. FileHandle *(*openWrite)(DirHandle *r, const char *filename);
  152. /*
  153. * Open file for appending, and return a FileHandle.
  154. * This filename is in platform-independent notation.
  155. * This method may be NULL.
  156. * If you can't handle multiple opens of the same file,
  157. * you can opt to fail for the second call.
  158. * Returns NULL on failure, and calls __PHYSFS_setError().
  159. */
  160. FileHandle *(*openAppend)(DirHandle *r, const char *filename);
  161. /*
  162. * Delete a file in the archive/directory.
  163. * Return non-zero on success, zero on failure.
  164. * This filename is in platform-independent notation.
  165. * This method may be NULL.
  166. * On failure, call __PHYSFS_setError().
  167. */
  168. int (*remove)(DirHandle *r, const char *filename);
  169. /*
  170. * Create a directory in the archive/directory.
  171. * If the application is trying to make multiple dirs, PhysicsFS
  172. * will split them up into multiple calls before passing them to
  173. * your driver.
  174. * Return non-zero on success, zero on failure.
  175. * This filename is in platform-independent notation.
  176. * This method may be NULL.
  177. * On failure, call __PHYSFS_setError().
  178. */
  179. int (*mkdir)(DirHandle *r, const char *filename);
  180. /*
  181. * Close directories/archives, and free the handle, including
  182. * the "opaque" entry. This should assume that it won't be called if
  183. * there are still files open from this DirHandle.
  184. */
  185. void (*close)(DirHandle *r);
  186. } DirFunctions;
  187. /* error messages... */
  188. #define ERR_IS_INITIALIZED "Already initialized"
  189. #define ERR_NOT_INITIALIZED "Not initialized"
  190. #define ERR_INVALID_ARGUMENT "Invalid argument"
  191. #define ERR_FILES_STILL_OPEN "Files still open"
  192. #define ERR_NO_DIR_CREATE "Failed to create directories"
  193. #define ERR_OUT_OF_MEMORY "Out of memory"
  194. #define ERR_NOT_IN_SEARCH_PATH "No such entry in search path"
  195. #define ERR_NOT_SUPPORTED "Operation not supported"
  196. #define ERR_UNSUPPORTED_ARCHIVE "Archive type unsupported"
  197. #define ERR_NOT_A_HANDLE "Not a file handle"
  198. #define ERR_INSECURE_FNAME "Insecure filename"
  199. #define ERR_SYMLINK_DISALLOWED "Symbolic links are disabled"
  200. #define ERR_NO_WRITE_DIR "Write directory is not set"
  201. /*
  202. * Call this to set the message returned by PHYSFS_getLastError().
  203. * Please only use the ERR_* constants above, or add new constants to the
  204. * above group, but I want these all in one place.
  205. *
  206. * Calling this with a NULL argument is a safe no-op.
  207. */
  208. void __PHYSFS_setError(const char *err);
  209. /*
  210. * Convert (dirName) to platform-dependent notation, then prepend (prepend)
  211. * and append (append) to the converted string.
  212. *
  213. * So, on Win32, calling:
  214. * __PHYSFS_convertToDependentNotation("C:\", "my/files", NULL);
  215. * ...will return the string "C:\my\files".
  216. *
  217. * This is a convenience function; you might want to hack something out that
  218. * is less generic (and therefore more efficient).
  219. *
  220. * Be sure to free() the return value when done with it.
  221. */
  222. char *__PHYSFS_convertToDependentNotation(const char *prepend,
  223. const char *dirName,
  224. const char *append);
  225. /*
  226. * Verify that (fname) (in platform-independent notation), in relation
  227. * to (h) is secure. That means that each element of fname is checked
  228. * for symlinks (if they aren't permitted). Also, elements such as
  229. * ".", "..", or ":" are flagged.
  230. *
  231. * Returns non-zero if string is safe, zero if there's a security issue.
  232. * PHYSFS_getLastError() will specify what was wrong.
  233. */
  234. int __PHYSFS_verifySecurity(DirHandle *h, const char *fname);
  235. /* This gets used all over for lessening code clutter. */
  236. #define BAIL_IF_MACRO(c, e, r) if (c) { __PHYSFS_setError(e); return r; }
  237. /*--------------------------------------------------------------------------*/
  238. /*--------------------------------------------------------------------------*/
  239. /*------------ ----------------*/
  240. /*------------ You MUST implement the following functions ----------------*/
  241. /*------------ if porting to a new platform. ----------------*/
  242. /*------------ (see unix.c for an example) ----------------*/
  243. /*------------ ----------------*/
  244. /*--------------------------------------------------------------------------*/
  245. /*--------------------------------------------------------------------------*/
  246. /*
  247. * The dir separator; "/" on unix, "\\" on win32, ":" on MacOS, etc...
  248. * Obviously, this isn't a function, but it IS a null-terminated string.
  249. */
  250. extern const char *__PHYSFS_platformDirSeparator;
  251. /*
  252. * Platform implementation of PHYSFS_getCdRomDirs()...
  253. * See physfs.h. The retval should be freeable via PHYSFS_freeList().
  254. */
  255. char **__PHYSFS_platformDetectAvailableCDs(void);
  256. /*
  257. * Calculate the base dir, if your platform needs special consideration.
  258. * Just return NULL if the standard routines will suffice. (see
  259. * calculateBaseDir() in physfs.c ...)
  260. * Caller will free() the retval if it's not NULL.
  261. */
  262. char *__PHYSFS_platformCalcBaseDir(char *argv0);
  263. /*
  264. * Get the platform-specific user name.
  265. * Caller will free() the retval if it's not NULL. If it's NULL, the username
  266. * will default to "default".
  267. */
  268. char *__PHYSFS_platformGetUserName(void);
  269. /*
  270. * Get the platform-specific user dir.
  271. * Caller will free() the retval if it's not NULL. If it's NULL, the userdir
  272. * will default to basedir/username.
  273. */
  274. char *__PHYSFS_platformGetUserDir(void);
  275. /*
  276. * Return a number that uniquely identifies the current thread.
  277. * On a platform without threading, (1) will suffice. These numbers are
  278. * arbitrary; the only requirement is that no two threads have the same
  279. * number.
  280. */
  281. int __PHYSFS_platformGetThreadID(void);
  282. /*
  283. * This is a pass-through to whatever stricmp() is called on your platform.
  284. */
  285. int __PHYSFS_platformStricmp(const char *str1, const char *str2);
  286. /*
  287. * Return non-zero if filename (in platform-dependent notation) is a symlink.
  288. */
  289. int __PHYSFS_platformIsSymlink(const char *fname);
  290. /*
  291. * Return non-zero if filename (in platform-dependent notation) is a symlink.
  292. */
  293. int __PHYSFS_platformIsDirectory(const char *fname);
  294. #ifdef __cplusplus
  295. extern "C" {
  296. #endif
  297. #endif
  298. /* end of physfs_internal.h ... */