Add MISSING_FILE_GENERATION special value for "file does not exist"

Implements a new special generation counter value (UINT64_MAX) that
allows clients to assert that a file should NOT exist when performing
operations.

Primary use case: Atomic "create-if-not-exists" operations
When combined with TOASTY_WRITE_CREATE_IF_MISSING flag:
- File doesn't exist → server creates file and writes (SUCCESS)
- File exists → gen_match fails → BADGEN error (prevents overwrite)

This enables race-condition-free file creation where you want to
create a NEW file or fail if it already exists.

Key changes:
- Added MISSING_FILE_GENERATION constant to file_tree.h
- Updated gen_match() to return false when checking existing entities
  against MISSING_FILE_GENERATION
- Modified file_tree_delete_entity() to succeed (no-op) when file is
  missing and MISSING_FILE_GENERATION is specified (idempotent delete)
- Modified file_tree_write() to validate against the new value
- Added documentation in metadata_server.c explaining how
  MISSING_FILE_GENERATION works WITH CREATE_IF_MISSING
- Updated protocol documentation in PROTOCOL.txt
- Added comprehensive feature documentation in MISSING_FILE_GENERATION.md

Additional use cases:
- Idempotent delete operations (delete only if not already gone)
- Detecting unexpected file creation in validation/testing scenarios

The implementation is backward compatible and type-safe. Files are
never assigned MISSING_FILE_GENERATION as their actual generation value.
This commit is contained in:
Claude
2025-12-05 09:10:17 +01:00
committed by cozis
parent e73bb3a7d8
commit 6746b0d5fb
6 changed files with 85 additions and 11 deletions
+4 -1
View File
@@ -19,6 +19,7 @@ typedef pthread_mutex_t Mutex;
#include "system.h"
#include "config.h"
#include "message.h"
#include "file_tree.h"
#include <ToastyFS.h>
@@ -1494,7 +1495,7 @@ static void process_event_for_write(ToastyFS *toasty,
return;
}
gen = 0; // TODO: is setting the generation to 0 right?
gen = MISSING_FILE_GENERATION; // TODO: is setting the generation to 0 right?
chunk_size = 4096; // The creation flag defaults to a chunk size of 4K
num_hashes = 0;
@@ -1505,6 +1506,7 @@ static void process_event_for_write(ToastyFS *toasty,
toasty->operations[opidx].result = (ToastyResult) { .type=TOASTY_RESULT_WRITE_ERROR, .user=toasty->operations[opidx].user };
return;
}
assert(gen != NO_GENERATION); // TODO: should this be an assertion?
if (!binary_read(&reader, &chunk_size, sizeof(chunk_size))) {
toasty->operations[opidx].result = (ToastyResult) { .type=TOASTY_RESULT_WRITE_ERROR, .user=toasty->operations[opidx].user };
@@ -2007,6 +2009,7 @@ static void process_event_for_write(ToastyFS *toasty,
}
uint16_t path_len = path.len;
uint64_t expect_gen = toasty->operations[opidx].expect_gen;
assert(expect_gen != NO_GENERATION);
uint32_t num_chunks = num_upload_results;