Add TOASTY_WRITE_TRUNCATE_AFTER flag for HTTP PUT semantics

Adds support for truncating files after write operations, enabling proper
HTTP PUT semantics where the entire file content should be replaced.

Implementation:
- Added TOASTY_WRITE_TRUNCATE_AFTER flag (0x02) to ToastyFS API
- Updated file_tree_write() to accept truncate_after parameter:
  * When true, sets file size to exactly offset+length
  * Removes chunks beyond the new file size
  * Marks removed chunks for garbage collection
- Metadata server extracts and passes truncate flag to file_tree_write()
- WAL replay passes false for truncate_after (already handled in original write)
- Web proxy PUT now uses both CREATE_IF_MISSING and TRUNCATE_AFTER flags
- Updated documentation in PROTOCOL.txt, DESIGN.txt, and web/DESIGN.txt

Benefits:
- Proper HTTP PUT semantics (replace entire file content)
- Efficient: single write operation truncates and updates file
- Works with both sync and async APIs
- Garbage collection automatically removes orphaned chunks
This commit is contained in:
Claude
2025-12-03 19:49:48 +00:00
parent 8160c5ac51
commit 4bcee9a617
9 changed files with 61 additions and 20 deletions
+7 -4
View File
@@ -35,10 +35,13 @@ PUT
operation fails with code 400.
If the path refers to a file, the file is automatically created if it
doesn't exist, then the specified contents are written to it. This is
implemented using the TOASTY_WRITE_CREATE_IF_MISSING flag which performs
file creation and write atomically on the metadata server. Files are
created with a default chunk size of 4096 bytes.
doesn't exist, then the specified contents are written to it, replacing
the entire file content (HTTP PUT semantics). This is implemented using
two flags:
- TOASTY_WRITE_CREATE_IF_MISSING: Creates file if it doesn't exist
- TOASTY_WRITE_TRUNCATE_AFTER: Truncates file after the write, discarding
any data beyond the uploaded content
Files are created with a default chunk size of 4096 bytes.
If a file or directory already existed at that path, it is overwritten
unless the header "X-ToastyFS-Overwrite: no" is sent.
+4 -3
View File
@@ -7,11 +7,12 @@ bool process_request_put(ProxyState *state, ProxyOperation *operation,
request->url.path.ptr,
request->url.path.len,
};
// Use TOASTY_WRITE_CREATE_IF_MISSING to automatically create files
// when they don't exist, enabling PUT operations to create new files
// Use both flags for proper HTTP PUT semantics:
// - TOASTY_WRITE_CREATE_IF_MISSING: Create file if it doesn't exist
// - TOASTY_WRITE_TRUNCATE_AFTER: Replace entire file content (truncate after write)
ToastyHandle handle = toasty_begin_write(state->backend, path, 0,
request->body.ptr, request->body.len, TOASTY_VERSION_TAG_EMPTY,
TOASTY_WRITE_CREATE_IF_MISSING);
TOASTY_WRITE_CREATE_IF_MISSING | TOASTY_WRITE_TRUNCATE_AFTER);
if (handle == TOASTY_INVALID) {
http_response_builder_status(builder, 500); // Internal Server Error
http_response_builder_send(builder);