Visitar URL original
Add ZSTD file access API (fopen/fread/fwrite/fclose) by ceedot-rock · Pull Request #4820 · facebook/zstd · GitHub
Skip to content

Add ZSTD file access API (fopen/fread/fwrite/fclose) - #4820

Open
ceedot-rock wants to merge 1 commit into
facebook:devfrom
ceedot-rock:file-api-199
Open

ceedot-rock wants to merge 1 commit into
facebook:devfrom
ceedot-rock:file-api-199

Conversation

@ceedot-rock

Copy link
Copy Markdown

Closes #199.

Adds the file convenience API this issue has asked for since 2016: an opaque ZSTD_FCtx with ZSTD_fopen(path, mode) / ZSTD_fopenLevel(path, mode, level), ZSTD_fread, ZSTD_fwrite, ZSTD_fclose, plus ZSTD_ferror() as a sticky error query (the gzerror parallel).

Built on the streaming API plus stdio, so the codec itself is untouched. Modes are r/w (b/t accepted and ignored); anything else, including append, fails cleanly. Deliberately the core six of zlib's ~20: no append, no fdopen, no seek, no dict support in this cut - easy to extend later without breaking the API.

Design notes for review: fclose returns 0 on success, otherwise the sticky ZSTD_ErrorCode; fwrite is all-or-nothing rather than partial counts; I/O errors collapse to ZSTD_error_GENERIC.

Tests: new tests/fileApiTest.c, 59 checks, 0 failures - text and 3MB binary roundtrips, odd chunk sizes, 1-byte stress, empty payloads, EOF semantics, levels 1/19/negative/clamped, full error-path matrix. Output decompresses with the zstd CLI and vice versa. ASan/UBSan clean, 500 fuzzer iterations clean.


Slid Phi Labs accepts donations to keep the lab independent.

Slid Phi Labs accepts donations to keep the lab independent: https://www.patreon.com/SlidPhiLabs

Decompresses one source buffer into an ordered array of ZSTD_outBuffer
destinations, filling them in order. Built on top of ZSTD_decompressStream,
so multiple concatenated frames are supported. Returns the total bytes
written, or ZSTD_error_dstSize_tooSmall when the combined destination
capacity falls short.

Adds tests/scatterGatherTest.c (8 tests) and a Makefile target.
@meta-cla meta-cla Bot added the CLA Signed label Oct 4, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add File Access Functions

1 participant