physfs_internal.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. #define ERR_ARC_IS_READ_ONLY "Archive is read-only"
  210. /*
  211. * Call this to set the message returned by PHYSFS_getLastError().
  212. * Please only use the ERR_* constants above, or add new constants to the
  213. * above group, but I want these all in one place.
  214. *
  215. * Calling this with a NULL argument is a safe no-op.
  216. */
  217. void __PHYSFS_setError(const char *err);
  218. /*
  219. * Convert (dirName) to platform-dependent notation, then prepend (prepend)
  220. * and append (append) to the converted string.
  221. *
  222. * So, on Win32, calling:
  223. * __PHYSFS_convertToDependent("C:\", "my/files", NULL);
  224. * ...will return the string "C:\my\files".
  225. *
  226. * This is a convenience function; you might want to hack something out that
  227. * is less generic (and therefore more efficient).
  228. *
  229. * Be sure to free() the return value when done with it.
  230. */
  231. char *__PHYSFS_convertToDependent(const char *prepend,
  232. const char *dirName,
  233. const char *append);
  234. /*
  235. * Verify that (fname) (in platform-independent notation), in relation
  236. * to (h) is secure. That means that each element of fname is checked
  237. * for symlinks (if they aren't permitted). Also, elements such as
  238. * ".", "..", or ":" are flagged.
  239. *
  240. * Returns non-zero if string is safe, zero if there's a security issue.
  241. * PHYSFS_getLastError() will specify what was wrong.
  242. */
  243. int __PHYSFS_verifySecurity(DirHandle *h, const char *fname);
  244. /* This gets used all over for lessening code clutter. */
  245. #define BAIL_IF_MACRO(c, e, r) if (c) { __PHYSFS_setError(e); return r; }
  246. /*--------------------------------------------------------------------------*/
  247. /*--------------------------------------------------------------------------*/
  248. /*------------ ----------------*/
  249. /*------------ You MUST implement the following functions ----------------*/
  250. /*------------ if porting to a new platform. ----------------*/
  251. /*------------ (see unix.c for an example) ----------------*/
  252. /*------------ ----------------*/
  253. /*--------------------------------------------------------------------------*/
  254. /*--------------------------------------------------------------------------*/
  255. /*
  256. * The dir separator; "/" on unix, "\\" on win32, ":" on MacOS, etc...
  257. * Obviously, this isn't a function, but it IS a null-terminated string.
  258. */
  259. extern const char *__PHYSFS_platformDirSeparator;
  260. /*
  261. * Platform implementation of PHYSFS_getCdRomDirs()...
  262. * See physfs.h. The retval should be freeable via PHYSFS_freeList().
  263. */
  264. char **__PHYSFS_platformDetectAvailableCDs(void);
  265. /*
  266. * Calculate the base dir, if your platform needs special consideration.
  267. * Just return NULL if the standard routines will suffice. (see
  268. * calculateBaseDir() in physfs.c ...)
  269. * Caller will free() the retval if it's not NULL.
  270. */
  271. char *__PHYSFS_platformCalcBaseDir(const char *argv0);
  272. /*
  273. * Get the platform-specific user name.
  274. * Caller will free() the retval if it's not NULL. If it's NULL, the username
  275. * will default to "default".
  276. */
  277. char *__PHYSFS_platformGetUserName(void);
  278. /*
  279. * Get the platform-specific user dir.
  280. * Caller will free() the retval if it's not NULL. If it's NULL, the userdir
  281. * will default to basedir/username.
  282. */
  283. char *__PHYSFS_platformGetUserDir(void);
  284. /*
  285. * Return a number that uniquely identifies the current thread.
  286. * On a platform without threading, (1) will suffice. These numbers are
  287. * arbitrary; the only requirement is that no two threads have the same
  288. * number.
  289. */
  290. int __PHYSFS_platformGetThreadID(void);
  291. /*
  292. * This is a pass-through to whatever stricmp() is called on your platform.
  293. */
  294. int __PHYSFS_platformStricmp(const char *str1, const char *str2);
  295. /*
  296. * Return non-zero if filename (in platform-dependent notation) exists.
  297. * Symlinks should be followed; if what the symlink points to is missing,
  298. * then the retval is false.
  299. */
  300. int __PHYSFS_platformExists(const char *fname);
  301. /*
  302. * Return non-zero if filename (in platform-dependent notation) is a symlink.
  303. */
  304. int __PHYSFS_platformIsSymLink(const char *fname);
  305. /*
  306. * Return non-zero if filename (in platform-dependent notation) is a symlink.
  307. * Symlinks should be followed; if what the symlink points to is missing,
  308. * or isn't a directory, then the retval is false.
  309. */
  310. int __PHYSFS_platformIsDirectory(const char *fname);
  311. /*
  312. * Convert (dirName) to platform-dependent notation, then prepend (prepend)
  313. * and append (append) to the converted string.
  314. *
  315. * So, on Win32, calling:
  316. * __PHYSFS_platformCvtToDependent("C:\", "my/files", NULL);
  317. * ...will return the string "C:\my\files".
  318. *
  319. * This can be implemented in a platform-specific manner, so you can get
  320. * get a speed boost that the default implementation can't, since
  321. * you can make assumptions about the size of strings, etc..
  322. *
  323. * Platforms that choose not to implement this may just call
  324. * __PHYSFS_convertToDependent() as a passthrough.
  325. *
  326. * Be sure to free() the return value when done with it.
  327. */
  328. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  329. const char *dirName,
  330. const char *append);
  331. /*
  332. * Make the current thread give up a timeslice. This is called in a loop
  333. * while waiting for various external forces to get back to us.
  334. */
  335. void __PHYSFS_platformTimeslice(void);
  336. /*
  337. * Enumerate a directory of files. This follows the rules for the
  338. * DirFunctions->enumerateFiles() method (see above), except that the
  339. * (dirName) that is passed to this function is converted to
  340. * platform-DEPENDENT notation by the caller. The DirFunctions version
  341. * uses platform-independent notation.
  342. */
  343. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname);
  344. #ifdef __cplusplus
  345. extern "C" {
  346. #endif
  347. #endif
  348. /* end of physfs_internal.h ... */