physfs_internal.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*
  2. * Internal function/structure declaration. Do NOT include in your
  3. * application.
  4. *
  5. * Please see the file LICENSE.txt 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. /* Turn off MSVC warnings that are aggressively anti-portability. */
  15. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
  16. #define _CRT_SECURE_NO_WARNINGS 1
  17. #endif
  18. #include "physfs.h"
  19. /* The holy trinity. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "physfs_platforms.h"
  24. #include <assert.h>
  25. #define __PHYSFS_COMPILE_TIME_ASSERT(name, x) \
  26. typedef int __PHYSFS_compile_time_assert_##name[(x) * 2 - 1]
  27. /* !!! FIXME: remove this when revamping stack allocation code... */
  28. #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__WATCOMC__)
  29. #include <malloc.h>
  30. #endif
  31. #if defined(PHYSFS_PLATFORM_SOLARIS) || defined(PHYSFS_PLATFORM_LINUX)
  32. #include <alloca.h>
  33. #endif
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. #ifdef __GNUC__
  38. #define PHYSFS_MINIMUM_GCC_VERSION(major, minor) \
  39. ( ((__GNUC__ << 16) + __GNUC_MINOR__) >= (((major) << 16) + (minor)) )
  40. #else
  41. #define PHYSFS_MINIMUM_GCC_VERSION(major, minor) (0)
  42. #endif
  43. #ifdef __cplusplus
  44. /* C++ always has a real inline keyword. */
  45. #elif (defined macintosh) && !(defined __MWERKS__)
  46. # define inline
  47. #elif (defined _MSC_VER)
  48. # define inline __inline
  49. #endif
  50. #if defined(PHYSFS_PLATFORM_LINUX) && !defined(_FILE_OFFSET_BITS)
  51. #define _FILE_OFFSET_BITS 64
  52. #endif
  53. /* All public APIs need to be in physfs.h with a PHYSFS_DECL.
  54. All file-private symbols need to be marked "static".
  55. Everything shared between PhysicsFS sources needs to be in this
  56. file between the visibility pragma blocks. */
  57. #if !defined(_WIN32) && (PHYSFS_MINIMUM_GCC_VERSION(4,0) || defined(__clang__))
  58. #define PHYSFS_HAVE_PRAGMA_VISIBILITY 1
  59. #endif
  60. #if PHYSFS_HAVE_PRAGMA_VISIBILITY
  61. #pragma GCC visibility push(hidden)
  62. #endif
  63. /* These are the build-in archivers. We list them all as "extern" here without
  64. #ifdefs to keep it tidy, but obviously you need to make sure these are
  65. wrapped in PHYSFS_SUPPORTS_* checks before actually referencing them. */
  66. extern const PHYSFS_Archiver __PHYSFS_Archiver_DIR;
  67. extern const PHYSFS_Archiver __PHYSFS_Archiver_ZIP;
  68. extern const PHYSFS_Archiver __PHYSFS_Archiver_7Z;
  69. extern const PHYSFS_Archiver __PHYSFS_Archiver_GRP;
  70. extern const PHYSFS_Archiver __PHYSFS_Archiver_QPAK;
  71. extern const PHYSFS_Archiver __PHYSFS_Archiver_HOG;
  72. extern const PHYSFS_Archiver __PHYSFS_Archiver_MVL;
  73. extern const PHYSFS_Archiver __PHYSFS_Archiver_WAD;
  74. extern const PHYSFS_Archiver __PHYSFS_Archiver_SLB;
  75. extern const PHYSFS_Archiver __PHYSFS_Archiver_ISO9660;
  76. extern const PHYSFS_Archiver __PHYSFS_Archiver_VDF;
  77. /* a real C99-compliant snprintf() is in Visual Studio 2015,
  78. but just use this everywhere for binary compatibility. */
  79. #if defined(_MSC_VER)
  80. #include <stdarg.h>
  81. int __PHYSFS_msvc_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap);
  82. int __PHYSFS_msvc_snprintf(char *outBuf, size_t size, const char *format, ...);
  83. #define vsnprintf __PHYSFS_msvc_vsnprintf
  84. #define snprintf __PHYSFS_msvc_snprintf
  85. #endif
  86. /* Some simple wrappers around WinRT C++ interfaces we can call from C. */
  87. #ifdef PHYSFS_PLATFORM_WINRT
  88. const void *__PHYSFS_winrtCalcBaseDir(void);
  89. const void *__PHYSFS_winrtCalcPrefDir(void);
  90. #endif
  91. /* atomic operations. */
  92. #if defined(_MSC_VER) && (_MSC_VER >= 1500)
  93. #include <intrin.h>
  94. __PHYSFS_COMPILE_TIME_ASSERT(LongEqualsInt, sizeof (int) == sizeof (long));
  95. #define __PHYSFS_ATOMIC_INCR(ptrval) _InterlockedIncrement((long*)(ptrval))
  96. #define __PHYSFS_ATOMIC_DECR(ptrval) _InterlockedDecrement((long*)(ptrval))
  97. #elif defined(__clang__) || (defined(__GNUC__) && (((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100)) >= 40100))
  98. #define __PHYSFS_ATOMIC_INCR(ptrval) __sync_fetch_and_add(ptrval, 1)
  99. #define __PHYSFS_ATOMIC_DECR(ptrval) __sync_fetch_and_add(ptrval, -1)
  100. #else
  101. #define PHYSFS_NEED_ATOMIC_OP_FALLBACK 1
  102. int __PHYSFS_ATOMIC_INCR(int *ptrval);
  103. int __PHYSFS_ATOMIC_DECR(int *ptrval);
  104. #endif
  105. /*
  106. * Interface for small allocations. If you need a little scratch space for
  107. * a throwaway buffer or string, use this. It will make small allocations
  108. * on the stack if possible, and use allocator.Malloc() if they are too
  109. * large. This helps reduce malloc pressure.
  110. * There are some rules, though:
  111. * NEVER return a pointer from this, as stack-allocated buffers go away
  112. * when your function returns.
  113. * NEVER allocate in a loop, as stack-allocated pointers will pile up. Call
  114. * a function that uses smallAlloc from your loop, so the allocation can
  115. * free each time.
  116. * NEVER call smallAlloc with any complex expression (it's a macro that WILL
  117. * have side effects...it references the argument multiple times). Use a
  118. * variable or a literal.
  119. * NEVER free a pointer from this with anything but smallFree. It will not
  120. * be a valid pointer to the allocator, regardless of where the memory came
  121. * from.
  122. * NEVER realloc a pointer from this.
  123. * NEVER forget to use smallFree: it may not be a pointer from the stack.
  124. * NEVER forget to check for NULL...allocation can fail here, of course!
  125. */
  126. #define __PHYSFS_SMALLALLOCTHRESHOLD 256
  127. void *__PHYSFS_initSmallAlloc(void *ptr, const size_t len);
  128. #define __PHYSFS_smallAlloc(bytes) ( \
  129. __PHYSFS_initSmallAlloc( \
  130. (((bytes) < __PHYSFS_SMALLALLOCTHRESHOLD) ? \
  131. alloca((size_t)((bytes)+sizeof(void*))) : NULL), (bytes)) \
  132. )
  133. void __PHYSFS_smallFree(void *ptr);
  134. /* Use the allocation hooks. */
  135. #define malloc(x) Do not use malloc() directly.
  136. #define realloc(x, y) Do not use realloc() directly.
  137. #define free(x) Do not use free() directly.
  138. /* !!! FIXME: add alloca check here. */
  139. /* by default, enable things, so builds can opt out of a few things they
  140. want to avoid. But you can build with this #defined to 0 if you would
  141. like to turn off everything except a handful of things you opt into. */
  142. #ifndef PHYSFS_SUPPORTS_DEFAULT
  143. #define PHYSFS_SUPPORTS_DEFAULT 1
  144. #endif
  145. #ifndef PHYSFS_SUPPORTS_ZIP
  146. #define PHYSFS_SUPPORTS_ZIP PHYSFS_SUPPORTS_DEFAULT
  147. #endif
  148. #ifndef PHYSFS_SUPPORTS_7Z
  149. #define PHYSFS_SUPPORTS_7Z PHYSFS_SUPPORTS_DEFAULT
  150. #endif
  151. #ifndef PHYSFS_SUPPORTS_GRP
  152. #define PHYSFS_SUPPORTS_GRP PHYSFS_SUPPORTS_DEFAULT
  153. #endif
  154. #ifndef PHYSFS_SUPPORTS_HOG
  155. #define PHYSFS_SUPPORTS_HOG PHYSFS_SUPPORTS_DEFAULT
  156. #endif
  157. #ifndef PHYSFS_SUPPORTS_MVL
  158. #define PHYSFS_SUPPORTS_MVL PHYSFS_SUPPORTS_DEFAULT
  159. #endif
  160. #ifndef PHYSFS_SUPPORTS_WAD
  161. #define PHYSFS_SUPPORTS_WAD PHYSFS_SUPPORTS_DEFAULT
  162. #endif
  163. #ifndef PHYSFS_SUPPORTS_QPAK
  164. #define PHYSFS_SUPPORTS_QPAK PHYSFS_SUPPORTS_DEFAULT
  165. #endif
  166. #ifndef PHYSFS_SUPPORTS_SLB
  167. #define PHYSFS_SUPPORTS_SLB PHYSFS_SUPPORTS_DEFAULT
  168. #endif
  169. #ifndef PHYSFS_SUPPORTS_ISO9660
  170. #define PHYSFS_SUPPORTS_ISO9660 PHYSFS_SUPPORTS_DEFAULT
  171. #endif
  172. #ifndef PHYSFS_SUPPORTS_VDF
  173. #define PHYSFS_SUPPORTS_VDF PHYSFS_SUPPORTS_DEFAULT
  174. #endif
  175. #if PHYSFS_SUPPORTS_7Z
  176. /* 7zip support needs a global init function called at startup (no deinit). */
  177. extern void SZIP_global_init(void);
  178. #endif
  179. /* The latest supported PHYSFS_Io::version value. */
  180. #define CURRENT_PHYSFS_IO_API_VERSION 0
  181. /* The latest supported PHYSFS_Archiver::version value. */
  182. #define CURRENT_PHYSFS_ARCHIVER_API_VERSION 0
  183. /* This byteorder stuff was lifted from SDL. https://www.libsdl.org/ */
  184. #define PHYSFS_LIL_ENDIAN 1234
  185. #define PHYSFS_BIG_ENDIAN 4321
  186. #ifdef __linux__
  187. #include <endian.h>
  188. #define PHYSFS_BYTEORDER __BYTE_ORDER
  189. #else /* __linux__ */
  190. #if defined(__hppa__) || \
  191. defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
  192. (defined(__MIPS__) && defined(__MISPEB__)) || \
  193. defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
  194. defined(__sparc__)
  195. #define PHYSFS_BYTEORDER PHYSFS_BIG_ENDIAN
  196. #else
  197. #define PHYSFS_BYTEORDER PHYSFS_LIL_ENDIAN
  198. #endif
  199. #endif /* __linux__ */
  200. /*
  201. * When sorting the entries in an archive, we use a modified QuickSort.
  202. * When there are less then PHYSFS_QUICKSORT_THRESHOLD entries left to sort,
  203. * we switch over to a BubbleSort for the remainder. Tweak to taste.
  204. *
  205. * You can override this setting by defining PHYSFS_QUICKSORT_THRESHOLD
  206. * before #including "physfs_internal.h".
  207. */
  208. #ifndef PHYSFS_QUICKSORT_THRESHOLD
  209. #define PHYSFS_QUICKSORT_THRESHOLD 4
  210. #endif
  211. /*
  212. * Sort an array (or whatever) of (max) elements. This uses a mixture of
  213. * a QuickSort and BubbleSort internally.
  214. * (cmpfn) is used to determine ordering, and (swapfn) does the actual
  215. * swapping of elements in the list.
  216. */
  217. void __PHYSFS_sort(void *entries, size_t max,
  218. int (*cmpfn)(void *, size_t, size_t),
  219. void (*swapfn)(void *, size_t, size_t));
  220. /* These get used all over for lessening code clutter. */
  221. /* "ERRPASS" means "something else just set the error state for us" and is
  222. just to make it clear where the responsibility for the error state lays. */
  223. #define BAIL(e, r) do { if (e) PHYSFS_setErrorCode(e); return r; } while (0)
  224. #define BAIL_ERRPASS(r) do { return r; } while (0)
  225. #define BAIL_IF(c, e, r) do { if (c) { if (e) PHYSFS_setErrorCode(e); return r; } } while (0)
  226. #define BAIL_IF_ERRPASS(c, r) do { if (c) { return r; } } while (0)
  227. #define BAIL_MUTEX(e, m, r) do { if (e) PHYSFS_setErrorCode(e); __PHYSFS_platformReleaseMutex(m); return r; } while (0)
  228. #define BAIL_MUTEX_ERRPASS(m, r) do { __PHYSFS_platformReleaseMutex(m); return r; } while (0)
  229. #define BAIL_IF_MUTEX(c, e, m, r) do { if (c) { if (e) PHYSFS_setErrorCode(e); __PHYSFS_platformReleaseMutex(m); return r; } } while (0)
  230. #define BAIL_IF_MUTEX_ERRPASS(c, m, r) do { if (c) { __PHYSFS_platformReleaseMutex(m); return r; } } while (0)
  231. #define GOTO(e, g) do { if (e) PHYSFS_setErrorCode(e); goto g; } while (0)
  232. #define GOTO_ERRPASS(g) do { goto g; } while (0)
  233. #define GOTO_IF(c, e, g) do { if (c) { if (e) PHYSFS_setErrorCode(e); goto g; } } while (0)
  234. #define GOTO_IF_ERRPASS(c, g) do { if (c) { goto g; } } while (0)
  235. #define GOTO_MUTEX(e, m, g) do { if (e) PHYSFS_setErrorCode(e); __PHYSFS_platformReleaseMutex(m); goto g; } while (0)
  236. #define GOTO_MUTEX_ERRPASS(m, g) do { __PHYSFS_platformReleaseMutex(m); goto g; } while (0)
  237. #define GOTO_IF_MUTEX(c, e, m, g) do { if (c) { if (e) PHYSFS_setErrorCode(e); __PHYSFS_platformReleaseMutex(m); goto g; } } while (0)
  238. #define GOTO_IF_MUTEX_ERRPASS(c, m, g) do { if (c) { __PHYSFS_platformReleaseMutex(m); goto g; } } while (0)
  239. #define __PHYSFS_ARRAYLEN(x) ( (sizeof (x)) / (sizeof (x[0])) )
  240. #ifdef PHYSFS_NO_64BIT_SUPPORT
  241. #define __PHYSFS_SI64(x) ((PHYSFS_sint64) (x))
  242. #define __PHYSFS_UI64(x) ((PHYSFS_uint64) (x))
  243. #elif (defined __GNUC__)
  244. #define __PHYSFS_SI64(x) x##LL
  245. #define __PHYSFS_UI64(x) x##ULL
  246. #elif (defined _MSC_VER)
  247. #define __PHYSFS_SI64(x) x##i64
  248. #define __PHYSFS_UI64(x) x##ui64
  249. #else
  250. #define __PHYSFS_SI64(x) ((PHYSFS_sint64) (x))
  251. #define __PHYSFS_UI64(x) ((PHYSFS_uint64) (x))
  252. #endif
  253. /*
  254. * Check if a ui64 will fit in the platform's address space.
  255. * The initial sizeof check will optimize this macro out entirely on
  256. * 64-bit (and larger?!) platforms, and the other condition will
  257. * return zero or non-zero if the variable will fit in the platform's
  258. * size_t, suitable to pass to malloc. This is kinda messy, but effective.
  259. */
  260. #define __PHYSFS_ui64FitsAddressSpace(s) ( \
  261. (sizeof (PHYSFS_uint64) <= sizeof (size_t)) || \
  262. ((s) < (__PHYSFS_UI64(0xFFFFFFFFFFFFFFFF) >> (64-(sizeof(size_t)*8)))) \
  263. )
  264. /*
  265. * Like strdup(), but uses the current PhysicsFS allocator.
  266. */
  267. char *__PHYSFS_strdup(const char *str);
  268. /*
  269. * Give a hash value for a C string (uses djb's xor hashing algorithm).
  270. */
  271. PHYSFS_uint32 __PHYSFS_hashString(const char *str, size_t len);
  272. /*
  273. * The current allocator. Not valid before PHYSFS_init is called!
  274. */
  275. extern PHYSFS_Allocator __PHYSFS_AllocatorHooks;
  276. /* convenience macro to make this less cumbersome internally... */
  277. #define allocator __PHYSFS_AllocatorHooks
  278. /*
  279. * Create a PHYSFS_Io for a file in the physical filesystem.
  280. * This path is in platform-dependent notation. (mode) must be 'r', 'w', or
  281. * 'a' for Read, Write, or Append.
  282. */
  283. PHYSFS_Io *__PHYSFS_createNativeIo(const char *path, const int mode);
  284. /*
  285. * Create a PHYSFS_Io for a buffer of memory (READ-ONLY). If you already
  286. * have one of these, just use its duplicate() method, and it'll increment
  287. * its refcount without allocating a copy of the buffer.
  288. */
  289. PHYSFS_Io *__PHYSFS_createMemoryIo(const void *buf, PHYSFS_uint64 len,
  290. void (*destruct)(void *));
  291. /*
  292. * Read (len) bytes from (io) into (buf). Returns non-zero on success,
  293. * zero on i/o error. Literally: "return (io->read(io, buf, len) == len);"
  294. */
  295. int __PHYSFS_readAll(PHYSFS_Io *io, void *buf, const size_t len);
  296. /* These are shared between some archivers. */
  297. void UNPK_abandonArchive(void *opaque);
  298. void UNPK_closeArchive(void *opaque);
  299. void *UNPK_openArchive(PHYSFS_Io *io);
  300. void *UNPK_addEntry(void *opaque, char *name, const int isdir,
  301. const PHYSFS_sint64 ctime, const PHYSFS_sint64 mtime,
  302. const PHYSFS_uint64 pos, const PHYSFS_uint64 len);
  303. PHYSFS_Io *UNPK_openRead(void *opaque, const char *name);
  304. PHYSFS_Io *UNPK_openWrite(void *opaque, const char *name);
  305. PHYSFS_Io *UNPK_openAppend(void *opaque, const char *name);
  306. int UNPK_remove(void *opaque, const char *name);
  307. int UNPK_mkdir(void *opaque, const char *name);
  308. int UNPK_stat(void *opaque, const char *fn, PHYSFS_Stat *st);
  309. #define UNPK_enumerate __PHYSFS_DirTreeEnumerate
  310. /* Optional API many archivers use this to manage their directory tree. */
  311. /* !!! FIXME: document this better. */
  312. typedef struct __PHYSFS_DirTreeEntry
  313. {
  314. char *name; /* Full path in archive. */
  315. struct __PHYSFS_DirTreeEntry *hashnext; /* next item in hash bucket. */
  316. struct __PHYSFS_DirTreeEntry *children; /* linked list of kids, if dir. */
  317. struct __PHYSFS_DirTreeEntry *sibling; /* next item in same dir. */
  318. int isdir;
  319. } __PHYSFS_DirTreeEntry;
  320. typedef struct __PHYSFS_DirTree
  321. {
  322. __PHYSFS_DirTreeEntry *root; /* root of directory tree. */
  323. __PHYSFS_DirTreeEntry **hash; /* all entries hashed for fast lookup. */
  324. size_t hashBuckets; /* number of buckets in hash. */
  325. size_t entrylen; /* size in bytes of entries (including subclass). */
  326. } __PHYSFS_DirTree;
  327. int __PHYSFS_DirTreeInit(__PHYSFS_DirTree *dt, const size_t entrylen);
  328. void *__PHYSFS_DirTreeAdd(__PHYSFS_DirTree *dt, char *name, const int isdir);
  329. void *__PHYSFS_DirTreeFind(__PHYSFS_DirTree *dt, const char *path);
  330. PHYSFS_EnumerateCallbackResult __PHYSFS_DirTreeEnumerate(void *opaque,
  331. const char *dname, PHYSFS_EnumerateCallback cb,
  332. const char *origdir, void *callbackdata);
  333. void __PHYSFS_DirTreeDeinit(__PHYSFS_DirTree *dt);
  334. /*--------------------------------------------------------------------------*/
  335. /*--------------------------------------------------------------------------*/
  336. /*------------ ----------------*/
  337. /*------------ You MUST implement the following functions ----------------*/
  338. /*------------ if porting to a new platform. ----------------*/
  339. /*------------ (see platform/unix.c for an example) ----------------*/
  340. /*------------ ----------------*/
  341. /*--------------------------------------------------------------------------*/
  342. /*--------------------------------------------------------------------------*/
  343. /*
  344. * The dir separator; '/' on unix, '\\' on win32, ":" on MacOS, etc...
  345. * Obviously, this isn't a function. If you need more than one char for this,
  346. * you'll need to pull some old pieces of PhysicsFS out of revision control.
  347. */
  348. #if defined(PHYSFS_PLATFORM_WINDOWS) || defined(PHYSFS_PLATFORM_OS2)
  349. #define __PHYSFS_platformDirSeparator '\\'
  350. #else
  351. #define __PHYSFS_STANDARD_DIRSEP 1
  352. #define __PHYSFS_platformDirSeparator '/'
  353. #endif
  354. /*
  355. * Initialize the platform. This is called when PHYSFS_init() is called from
  356. * the application.
  357. *
  358. * Return zero if there was a catastrophic failure (which prevents you from
  359. * functioning at all), and non-zero otherwise.
  360. */
  361. int __PHYSFS_platformInit(void);
  362. /*
  363. * Deinitialize the platform. This is called when PHYSFS_deinit() is called
  364. * from the application. You can use this to clean up anything you've
  365. * allocated in your platform driver.
  366. */
  367. void __PHYSFS_platformDeinit(void);
  368. /*
  369. * Open a file for reading. (filename) is in platform-dependent notation. The
  370. * file pointer should be positioned on the first byte of the file.
  371. *
  372. * The return value will be some platform-specific datatype that is opaque to
  373. * the caller; it could be a (FILE *) under Unix, or a (HANDLE *) under win32.
  374. *
  375. * The same file can be opened for read multiple times, and each should have
  376. * a unique file handle; this is frequently employed to prevent race
  377. * conditions in the archivers.
  378. *
  379. * Call PHYSFS_setErrorCode() and return (NULL) if the file can't be opened.
  380. */
  381. void *__PHYSFS_platformOpenRead(const char *filename);
  382. /*
  383. * Open a file for writing. (filename) is in platform-dependent notation. If
  384. * the file exists, it should be truncated to zero bytes, and if it doesn't
  385. * exist, it should be created as a zero-byte file. The file pointer should
  386. * be positioned on the first byte of the file.
  387. *
  388. * The return value will be some platform-specific datatype that is opaque to
  389. * the caller; it could be a (FILE *) under Unix, or a (HANDLE *) under win32,
  390. * etc.
  391. *
  392. * Opening a file for write multiple times has undefined results.
  393. *
  394. * Call PHYSFS_setErrorCode() and return (NULL) if the file can't be opened.
  395. */
  396. void *__PHYSFS_platformOpenWrite(const char *filename);
  397. /*
  398. * Open a file for appending. (filename) is in platform-dependent notation. If
  399. * the file exists, the file pointer should be place just past the end of the
  400. * file, so that the first write will be one byte after the current end of
  401. * the file. If the file doesn't exist, it should be created as a zero-byte
  402. * file. The file pointer should be positioned on the first byte of the file.
  403. *
  404. * The return value will be some platform-specific datatype that is opaque to
  405. * the caller; it could be a (FILE *) under Unix, or a (HANDLE *) under win32,
  406. * etc.
  407. *
  408. * Opening a file for append multiple times has undefined results.
  409. *
  410. * Call PHYSFS_setErrorCode() and return (NULL) if the file can't be opened.
  411. */
  412. void *__PHYSFS_platformOpenAppend(const char *filename);
  413. /*
  414. * Read more data from a platform-specific file handle. (opaque) should be
  415. * cast to whatever data type your platform uses. Read a maximum of (len)
  416. * 8-bit bytes to the area pointed to by (buf). If there isn't enough data
  417. * available, return the number of bytes read, and position the file pointer
  418. * immediately after those bytes.
  419. * On success, return (len) and position the file pointer immediately past
  420. * the end of the last read byte. Return (-1) if there is a catastrophic
  421. * error, and call PHYSFS_setErrorCode() to describe the problem; the file
  422. * pointer should not move in such a case. A partial read is success; only
  423. * return (-1) on total failure; presumably, the next read call after a
  424. * partial read will fail as such.
  425. */
  426. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buf, PHYSFS_uint64 len);
  427. /*
  428. * Write more data to a platform-specific file handle. (opaque) should be
  429. * cast to whatever data type your platform uses. Write a maximum of (len)
  430. * 8-bit bytes from the area pointed to by (buffer). If there is a problem,
  431. * return the number of bytes written, and position the file pointer
  432. * immediately after those bytes. Return (-1) if there is a catastrophic
  433. * error, and call PHYSFS_setErrorCode() to describe the problem; the file
  434. * pointer should not move in such a case. A partial write is success; only
  435. * return (-1) on total failure; presumably, the next write call after a
  436. * partial write will fail as such.
  437. */
  438. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  439. PHYSFS_uint64 len);
  440. /*
  441. * Set the file pointer to a new position. (opaque) should be cast to
  442. * whatever data type your platform uses. (pos) specifies the number
  443. * of 8-bit bytes to seek to from the start of the file. Seeking past the
  444. * end of the file is an error condition, and you should check for it.
  445. *
  446. * Not all file types can seek; this is to be expected by the caller.
  447. *
  448. * On error, call PHYSFS_setErrorCode() and return zero. On success, return
  449. * a non-zero value.
  450. */
  451. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos);
  452. /*
  453. * Get the file pointer's position, in an 8-bit byte offset from the start of
  454. * the file. (opaque) should be cast to whatever data type your platform
  455. * uses.
  456. *
  457. * Not all file types can "tell"; this is to be expected by the caller.
  458. *
  459. * On error, call PHYSFS_setErrorCode() and return -1. On success, return >= 0.
  460. */
  461. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque);
  462. /*
  463. * Determine the current size of a file, in 8-bit bytes, from an open file.
  464. *
  465. * The caller expects that this information may not be available for all
  466. * file types on all platforms.
  467. *
  468. * Return -1 if you can't do it, and call PHYSFS_setErrorCode(). Otherwise,
  469. * return the file length in 8-bit bytes.
  470. */
  471. PHYSFS_sint64 __PHYSFS_platformFileLength(void *handle);
  472. /*
  473. * Read filesystem metadata for a specific path.
  474. *
  475. * This needs to fill in all the fields of (stat). For fields that might not
  476. * mean anything on a platform (access time, perhaps), choose a reasonable
  477. * default. if (follow), we want to follow symlinks and stat what they
  478. * link to and not the link itself.
  479. *
  480. * Return zero on failure, non-zero on success.
  481. */
  482. int __PHYSFS_platformStat(const char *fn, PHYSFS_Stat *stat, const int follow);
  483. /*
  484. * Flush any pending writes to disk. (opaque) should be cast to whatever data
  485. * type your platform uses. Be sure to check for errors; the caller expects
  486. * that this function can fail if there was a flushing error, etc.
  487. *
  488. * Return zero on failure, non-zero on success.
  489. */
  490. int __PHYSFS_platformFlush(void *opaque);
  491. /*
  492. * Close file and deallocate resources. (opaque) should be cast to whatever
  493. * data type your platform uses. This should close the file in any scenario:
  494. * flushing is a separate function call, and this function should never fail.
  495. *
  496. * You should clean up all resources associated with (opaque); the pointer
  497. * will be considered invalid after this call.
  498. */
  499. void __PHYSFS_platformClose(void *opaque);
  500. /*
  501. * Platform implementation of PHYSFS_getCdRomDirsCallback()...
  502. * CD directories are discovered and reported to the callback one at a time.
  503. * Pointers passed to the callback are assumed to be invalid to the
  504. * application after the callback returns, so you can free them or whatever.
  505. * Callback does not assume results will be sorted in any meaningful way.
  506. */
  507. void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data);
  508. /*
  509. * Calculate the base dir, if your platform needs special consideration.
  510. * Just return NULL if the standard routines will suffice. (see
  511. * calculateBaseDir() in physfs.c ...)
  512. * Your string must end with a dir separator if you don't return NULL.
  513. * Caller will allocator.Free() the retval if it's not NULL.
  514. */
  515. char *__PHYSFS_platformCalcBaseDir(const char *argv0);
  516. /*
  517. * Get the platform-specific user dir.
  518. * As of PhysicsFS 2.1, returning NULL means fatal error.
  519. * Your string must end with a dir separator if you don't return NULL.
  520. * Caller will allocator.Free() the retval if it's not NULL.
  521. */
  522. char *__PHYSFS_platformCalcUserDir(void);
  523. /* This is the cached version from PHYSFS_init(). This is a fast call. */
  524. const char *__PHYSFS_getUserDir(void); /* not deprecated internal version. */
  525. /*
  526. * Get the platform-specific pref dir.
  527. * Returning NULL means fatal error.
  528. * Your string must end with a dir separator if you don't return NULL.
  529. * Caller will allocator.Free() the retval if it's not NULL.
  530. * Caller will make missing directories if necessary; this just reports
  531. * the final path.
  532. */
  533. char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app);
  534. /*
  535. * Return a pointer that uniquely identifies the current thread.
  536. * On a platform without threading, (0x1) will suffice. These numbers are
  537. * arbitrary; the only requirement is that no two threads have the same
  538. * pointer.
  539. */
  540. void *__PHYSFS_platformGetThreadID(void);
  541. /*
  542. * Enumerate a directory of files. This follows the rules for the
  543. * PHYSFS_Archiver::enumerate() method, except that the (dirName) that is
  544. * passed to this function is converted to platform-DEPENDENT notation by
  545. * the caller. The PHYSFS_Archiver version uses platform-independent
  546. * notation. Note that ".", "..", and other meta-entries should always
  547. * be ignored.
  548. */
  549. PHYSFS_EnumerateCallbackResult __PHYSFS_platformEnumerate(const char *dirname,
  550. PHYSFS_EnumerateCallback callback,
  551. const char *origdir, void *callbackdata);
  552. /*
  553. * Make a directory in the actual filesystem. (path) is specified in
  554. * platform-dependent notation. On error, return zero and set the error
  555. * message. Return non-zero on success.
  556. */
  557. int __PHYSFS_platformMkDir(const char *path);
  558. /*
  559. * Remove a file or directory entry in the actual filesystem. (path) is
  560. * specified in platform-dependent notation. Note that this deletes files
  561. * _and_ directories, so you might need to do some determination.
  562. * Non-empty directories should report an error and not delete themselves
  563. * or their contents.
  564. *
  565. * Deleting a symlink should remove the link, not what it points to.
  566. *
  567. * On error, return zero and set the error message. Return non-zero on success.
  568. */
  569. int __PHYSFS_platformDelete(const char *path);
  570. /*
  571. * Create a platform-specific mutex. This can be whatever datatype your
  572. * platform uses for mutexes, but it is cast to a (void *) for abstractness.
  573. *
  574. * Return (NULL) if you couldn't create one. Systems without threads can
  575. * return any arbitrary non-NULL value.
  576. */
  577. void *__PHYSFS_platformCreateMutex(void);
  578. /*
  579. * Destroy a platform-specific mutex, and clean up any resources associated
  580. * with it. (mutex) is a value previously returned by
  581. * __PHYSFS_platformCreateMutex(). This can be a no-op on single-threaded
  582. * platforms.
  583. */
  584. void __PHYSFS_platformDestroyMutex(void *mutex);
  585. /*
  586. * Grab possession of a platform-specific mutex. Mutexes should be recursive;
  587. * that is, the same thread should be able to call this function multiple
  588. * times in a row without causing a deadlock. This function should block
  589. * until a thread can gain possession of the mutex.
  590. *
  591. * Return non-zero if the mutex was grabbed, zero if there was an
  592. * unrecoverable problem grabbing it (this should not be a matter of
  593. * timing out! We're talking major system errors; block until the mutex
  594. * is available otherwise.)
  595. *
  596. * _DO NOT_ call PHYSFS_setErrorCode() in here! Since setErrorCode calls this
  597. * function, you'll cause an infinite recursion. This means you can't
  598. * use the BAIL_*MACRO* macros, either.
  599. */
  600. int __PHYSFS_platformGrabMutex(void *mutex);
  601. /*
  602. * Relinquish possession of the mutex when this method has been called
  603. * once for each time that platformGrabMutex was called. Once possession has
  604. * been released, the next thread in line to grab the mutex (if any) may
  605. * proceed.
  606. *
  607. * _DO NOT_ call PHYSFS_setErrorCode() in here! Since setErrorCode calls this
  608. * function, you'll cause an infinite recursion. This means you can't
  609. * use the BAIL_*MACRO* macros, either.
  610. */
  611. void __PHYSFS_platformReleaseMutex(void *mutex);
  612. #if PHYSFS_HAVE_PRAGMA_VISIBILITY
  613. #pragma GCC visibility pop
  614. #endif
  615. #ifdef __cplusplus
  616. }
  617. #endif
  618. #endif
  619. /* end of physfs_internal.h ... */