physfs_internal.h 15 KB

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