physfs_internal.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. * Close the file, and free the FileHandle structure (including "opaque").
  71. * returns non-zero on success, zero if can't close file.
  72. * On failure, call __PHYSFS_setError().
  73. */
  74. int (*fileClose)(FileHandle *handle);
  75. } FileFunctions;
  76. typedef struct __PHYSFS_DIRHANDLE__
  77. {
  78. /*
  79. * This is reserved for the driver to store information.
  80. */
  81. void *opaque;
  82. /*
  83. * Pointer to the directory i/o functions for this handle.
  84. */
  85. const struct __PHYSFS_DIRFUNCTIONS__ *funcs;
  86. } DirHandle;
  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. * Fail if the file does not exist.
  141. * Returns NULL on failure, and calls __PHYSFS_setError().
  142. */
  143. FileHandle *(*openRead)(DirHandle *r, const char *filename);
  144. /*
  145. * Open file for writing, and return a FileHandle.
  146. * If the file does not exist, it should be created. If it exists,
  147. * it should be truncated to zero bytes. The writing
  148. * offset should be the start of the file.
  149. * This filename is in platform-independent notation.
  150. * This method may be NULL.
  151. * If you can't handle multiple opens of the same file,
  152. * you can opt to fail for the second call.
  153. * Returns NULL on failure, and calls __PHYSFS_setError().
  154. */
  155. FileHandle *(*openWrite)(DirHandle *r, const char *filename);
  156. /*
  157. * Open file for appending, and return a FileHandle.
  158. * If the file does not exist, it should be created. The writing
  159. * offset should be the end of the file.
  160. * This filename is in platform-independent notation.
  161. * This method may be NULL.
  162. * If you can't handle multiple opens of the same file,
  163. * you can opt to fail for the second call.
  164. * Returns NULL on failure, and calls __PHYSFS_setError().
  165. */
  166. FileHandle *(*openAppend)(DirHandle *r, const char *filename);
  167. /*
  168. * Delete a file in the archive/directory.
  169. * Return non-zero on success, zero on failure.
  170. * This filename is in platform-independent notation.
  171. * This method may be NULL.
  172. * On failure, call __PHYSFS_setError().
  173. */
  174. int (*remove)(DirHandle *r, const char *filename);
  175. /*
  176. * Create a directory in the archive/directory.
  177. * If the application is trying to make multiple dirs, PhysicsFS
  178. * will split them up into multiple calls before passing them to
  179. * your driver.
  180. * Return non-zero on success, zero on failure.
  181. * This filename is in platform-independent notation.
  182. * This method may be NULL.
  183. * On failure, call __PHYSFS_setError().
  184. */
  185. int (*mkdir)(DirHandle *r, const char *filename);
  186. /*
  187. * Close directories/archives, and free the handle, including
  188. * the "opaque" entry. This should assume that it won't be called if
  189. * there are still files open from this DirHandle.
  190. */
  191. void (*dirClose)(DirHandle *r);
  192. } DirFunctions;
  193. /* error messages... */
  194. #define ERR_IS_INITIALIZED "Already initialized"
  195. #define ERR_NOT_INITIALIZED "Not initialized"
  196. #define ERR_INVALID_ARGUMENT "Invalid argument"
  197. #define ERR_FILES_STILL_OPEN "Files still open"
  198. #define ERR_NO_DIR_CREATE "Failed to create directories"
  199. #define ERR_OUT_OF_MEMORY "Out of memory"
  200. #define ERR_NOT_IN_SEARCH_PATH "No such entry in search path"
  201. #define ERR_NOT_SUPPORTED "Operation not supported"
  202. #define ERR_UNSUPPORTED_ARCHIVE "Archive type unsupported"
  203. #define ERR_NOT_A_HANDLE "Not a file handle"
  204. #define ERR_INSECURE_FNAME "Insecure filename"
  205. #define ERR_SYMLINK_DISALLOWED "Symbolic links are disabled"
  206. #define ERR_NO_WRITE_DIR "Write directory is not set"
  207. #define ERR_NO_SUCH_FILE "No such file"
  208. #define ERR_PAST_EOF "Past end of file"
  209. /*
  210. * Call this to set the message returned by PHYSFS_getLastError().
  211. * Please only use the ERR_* constants above, or add new constants to the
  212. * above group, but I want these all in one place.
  213. *
  214. * Calling this with a NULL argument is a safe no-op.
  215. */
  216. void __PHYSFS_setError(const char *err);
  217. /*
  218. * Convert (dirName) to platform-dependent notation, then prepend (prepend)
  219. * and append (append) to the converted string.
  220. *
  221. * So, on Win32, calling:
  222. * __PHYSFS_convertToDependent("C:\", "my/files", NULL);
  223. * ...will return the string "C:\my\files".
  224. *
  225. * This is a convenience function; you might want to hack something out that
  226. * is less generic (and therefore more efficient).
  227. *
  228. * Be sure to free() the return value when done with it.
  229. */
  230. char *__PHYSFS_convertToDependent(const char *prepend,
  231. const char *dirName,
  232. const char *append);
  233. /*
  234. * Verify that (fname) (in platform-independent notation), in relation
  235. * to (h) is secure. That means that each element of fname is checked
  236. * for symlinks (if they aren't permitted). Also, elements such as
  237. * ".", "..", or ":" are flagged.
  238. *
  239. * Returns non-zero if string is safe, zero if there's a security issue.
  240. * PHYSFS_getLastError() will specify what was wrong.
  241. */
  242. int __PHYSFS_verifySecurity(DirHandle *h, const char *fname);
  243. /* This gets used all over for lessening code clutter. */
  244. #define BAIL_IF_MACRO(c, e, r) if (c) { __PHYSFS_setError(e); return r; }
  245. /*--------------------------------------------------------------------------*/
  246. /*--------------------------------------------------------------------------*/
  247. /*------------ ----------------*/
  248. /*------------ You MUST implement the following functions ----------------*/
  249. /*------------ if porting to a new platform. ----------------*/
  250. /*------------ (see unix.c for an example) ----------------*/
  251. /*------------ ----------------*/
  252. /*--------------------------------------------------------------------------*/
  253. /*--------------------------------------------------------------------------*/
  254. /*
  255. * The dir separator; "/" on unix, "\\" on win32, ":" on MacOS, etc...
  256. * Obviously, this isn't a function, but it IS a null-terminated string.
  257. */
  258. extern const char *__PHYSFS_platformDirSeparator;
  259. /*
  260. * Platform implementation of PHYSFS_getCdRomDirs()...
  261. * See physfs.h. The retval should be freeable via PHYSFS_freeList().
  262. */
  263. char **__PHYSFS_platformDetectAvailableCDs(void);
  264. /*
  265. * Calculate the base dir, if your platform needs special consideration.
  266. * Just return NULL if the standard routines will suffice. (see
  267. * calculateBaseDir() in physfs.c ...)
  268. * Caller will free() the retval if it's not NULL.
  269. */
  270. char *__PHYSFS_platformCalcBaseDir(char *argv0);
  271. /*
  272. * Get the platform-specific user name.
  273. * Caller will free() the retval if it's not NULL. If it's NULL, the username
  274. * will default to "default".
  275. */
  276. char *__PHYSFS_platformGetUserName(void);
  277. /*
  278. * Get the platform-specific user dir.
  279. * Caller will free() the retval if it's not NULL. If it's NULL, the userdir
  280. * will default to basedir/username.
  281. */
  282. char *__PHYSFS_platformGetUserDir(void);
  283. /*
  284. * Return a number that uniquely identifies the current thread.
  285. * On a platform without threading, (1) will suffice. These numbers are
  286. * arbitrary; the only requirement is that no two threads have the same
  287. * number.
  288. */
  289. int __PHYSFS_platformGetThreadID(void);
  290. /*
  291. * This is a pass-through to whatever stricmp() is called on your platform.
  292. */
  293. int __PHYSFS_platformStricmp(const char *str1, const char *str2);
  294. /*
  295. * Return non-zero if filename (in platform-dependent notation) exists.
  296. * Symlinks should be followed; if what the symlink points to is missing,
  297. * then the retval is false.
  298. */
  299. int __PHYSFS_platformExists(const char *fname);
  300. /*
  301. * Return non-zero if filename (in platform-dependent notation) is a symlink.
  302. */
  303. int __PHYSFS_platformIsSymLink(const char *fname);
  304. /*
  305. * Return non-zero if filename (in platform-dependent notation) is a symlink.
  306. * Symlinks should be followed; if what the symlink points to is missing,
  307. * or isn't a directory, then the retval is false.
  308. */
  309. int __PHYSFS_platformIsDirectory(const char *fname);
  310. /*
  311. * Convert (dirName) to platform-dependent notation, then prepend (prepend)
  312. * and append (append) to the converted string.
  313. *
  314. * So, on Win32, calling:
  315. * __PHYSFS_platformCvtToDependent("C:\", "my/files", NULL);
  316. * ...will return the string "C:\my\files".
  317. *
  318. * This can be implemented in a platform-specific manner, so you can get
  319. * get a speed boost that the default implementation can't, since
  320. * you can make assumptions about the size of strings, etc..
  321. *
  322. * Platforms that choose not to implement this may just call
  323. * __PHYSFS_convertToDependent() as a passthrough.
  324. *
  325. * Be sure to free() the return value when done with it.
  326. */
  327. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  328. const char *dirName,
  329. const char *append);
  330. /*
  331. * Make the current thread give up a timeslice. This is called in a loop
  332. * while waiting for various external forces to get back to us.
  333. */
  334. void __PHYSFS_platformTimeslice(void);
  335. /*
  336. * Enumerate a directory of files. This follows the rules for the
  337. * DirFunctions->enumerateFiles() method (see above), except that the
  338. * (dirName) that is passed to this function is converted to
  339. * platform-DEPENDENT notation by the caller. The DirFunctions version
  340. * uses platform-independent notation.
  341. */
  342. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname);
  343. #ifdef __cplusplus
  344. extern "C" {
  345. #endif
  346. #endif
  347. /* end of physfs_internal.h ... */