Adds support for automatically creating files when write operations target non-existent files. This is essential for REST PUT operations which should be able to create new resources. Implementation: - Added TOASTY_WRITE_CREATE_IF_MISSING flag to ToastyFS API header - Updated toasty_write() and toasty_begin_write() to accept flags parameter - Client sends write flags in MESSAGE_TYPE_WRITE message to metadata server - Metadata server parses flags and handles file auto-creation atomically: * When write fails with FILETREE_NOENT and flag is set * Logs creation to WAL for crash consistency * Creates file with default 4096-byte chunk size * Retries write with newly created file's generation tag - Updated web proxy PUT handler to use TOASTY_WRITE_CREATE_IF_MISSING - Updated examples and simulation client for new API signature Benefits: - Works for both sync and async APIs - Single round trip (atomic server-side operation) - Prevents race conditions - Proper WAL logging ensures crash consistency
35 lines
837 B
C
35 lines
837 B
C
#include <stdio.h>
|
|
#include <ToastyFS.h>
|
|
|
|
int main(void)
|
|
{
|
|
ToastyString remote_addr = TOASTY_STR("127.0.0.1");
|
|
uint16_t remote_port = 8080;
|
|
|
|
ToastyFS *toasty = toasty_connect(remote_addr, remote_port);
|
|
if (toasty == NULL) {
|
|
printf("Couldn't connect to metadata server");
|
|
return -1;
|
|
}
|
|
|
|
ToastyString path = TOASTY_STR("/first_file");
|
|
|
|
int ret = toasty_create_file(toasty, path, 1024, NULL);
|
|
if (ret < 0) {
|
|
printf("Couldn't create file\n");
|
|
toasty_disconnect(toasty);
|
|
return -1;
|
|
}
|
|
|
|
char data[] = "Hello, world!";
|
|
ret = toasty_write(toasty, path, 0, data, sizeof(data)-1, NULL, 0);
|
|
if (ret < 0) {
|
|
printf("Couldn't write to file\n");
|
|
toasty_disconnect(toasty);
|
|
return -1;
|
|
}
|
|
|
|
toasty_disconnect(toasty);
|
|
return 0;
|
|
}
|