physfs_internal.h 16 KB

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