physfs_internal.h 27 KB

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