Skip to content

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

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

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

Conversation

@ceedot-rock

@ceedot-rock ceedot-rock commented Sep 28, 2026 •

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.

@meta-cla

meta-cla Bot commented Sep 28, 2026

Copy link
Copy Markdown

Hi @ceedot-rock!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla

meta-cla Bot commented Sep 28, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed label Sep 28, 2026
…close)

Implements meta#199: gzopen-style wrappers that compress,
buffer, and talk to the filesystem. Purely additive on top of
ZSTD_compressStream2 / ZSTD_decompressStream plus stdio; files are
plain zstd frames, so the zstd CLI reads what the API writes and
vice versa.

- New lib/compress/zstd_file.c, picked up by the existing wildcard
  builds (make, cmake, buck); symbols export from the shared lib.
- API + error semantics documented in lib/zstd.h.
- New tests/fileApiTest.c wired into tests/Makefile (test-fileapi,
  also part of 'make test'): roundtrips, CLI interop both ways,
  error paths, fclose flush, levels. 59 checks, all passing.
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