Commit Graph
255 Commits
Author SHA1 Message Date
cozis 5c192a1e52 Fix wrong error code returned by file_tree_read 2025-12-03 21:15:54 +01:00
Claude 4bcee9a617 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
2025-12-03 19:49:48 +00:00
Claude 8160c5ac51 Add TOASTY_WRITE_CREATE_IF_MISSING flag for server-side file auto-creation
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
2025-12-03 18:48:42 +00:00
cozis a082f46b78 Update TODO.txt 2025-12-03 19:12:35 +01:00
cozis 6519b0c1bb Fix incorrect call to http_response_builder_body_cap in toastyfs_web 2025-12-03 18:01:39 +01:00
cozis 012b31df3a Fix compilation warnings 2025-12-03 00:03:44 +01:00
cozis b0b2d576cc Refactoring of toastyfs_web 2025-12-03 00:03:36 +01:00
cozis 09ec872591 Merge branch 'main' into synchronization_app 2025-12-02 12:21:39 +01:00
cozis f665dae9ed Update chttp.c/.h 2025-12-02 12:07:10 +01:00
cozis 0d099a1fa6 Return version tags in directory listing 2025-11-25 12:08:33 +01:00
cozis afad13f44f Allow users of libtoastyfs to operate on files/directories with specific version tags 2025-11-24 14:34:57 +01:00
Claude 1281bf0822 Reject write operations with expect_gen=0
Per PROTOCOL.txt specification, WRITE operations must provide a valid
generation counter and cannot use expect_gen=0 (unlike other operations).

This ensures the client has retrieved the file's metadata and knows the
correct chunk size before writing.

Changes:
- Added FILETREE_BADGEN error code (-8)
- Modified file_tree_write() to reject expect_gen=0
- Added error message for FILETREE_BADGEN
2025-11-24 12:46:27 +00:00
Claude acb1336d47 Implement synchronization protocol from PROTOCOL.txt
Updated both client.c and metadata_server.c to match the protocol
specification in misc/PROTOCOL.txt:

Client-to-Server Messages:
- Added expect_gen field to DELETE, LIST, READ, and WRITE messages
- Removed old_hash and chunk_size from WRITE message chunks

Server-to-Client Responses:
- Added generation counter to CREATE_SUCCESS response
- Added generation counter to LIST_SUCCESS response
- Fixed LIST_SUCCESS field order (gen, truncated, item_count)
- Added generation counter to READ_SUCCESS response
- Added generation counter to WRITE_SUCCESS response

All changes compile successfully and pass valgrind memory checks.
2025-11-24 12:16:55 +00:00
cozis 49091aff6d Document communication between metadata server and clients in the misc/PROTOCOL.txt file and add generation counters for files 2025-11-24 13:04:29 +01:00
cozis 1c768bb8f1 Add logging to app.py 2025-11-23 11:33:30 +01:00
cozis 8e37bc674f Draft of app.py, a python script which synchronized local folders to ToastyFS through the web interface 2025-11-23 11:21:34 +01:00
cozis 70a3a91783 Change README.md to refer to the cluster demo script 2025-11-23 09:53:36 +01:00
Claudeandcozis 3b0ef325fa Fix web server hanging issues
Fixed two bugs in the HTTP server that could cause hangs:

1. web/chttp.c:4060 - Fixed incorrect error buffer check
   - Was checking output buffer for errors after a read operation
   - Should check input buffer instead
   - This could cause the server to miss read errors and not close
     bad connections properly

2. web/main.c:334 - Added loop to process multiple queued requests
   - Previously only processed one request per event loop iteration
   - Now processes all queued requests before returning to poll()
   - This prevents requests from being stuck in the queue when the
     socket is in ESTABLISHED_READY state

These fixes improve the server's ability to handle multiple requests
on Keep-Alive connections.
2025-11-23 09:48:07 +01:00
cozis e99c574767 Add options --reuse-addr and --trace-bytes to toastyfs_web 2025-11-23 09:08:45 +01:00
Claudeandcozis cc758a5d3a Add web server to cluster demo and fix port bug
- Add web server integration to cluster_demo.sh script
  - Configure web server port (8090)
  - Update build_if_needed() to check for toastyfs_web binary
  - Add get_web_binary() function
  - Start web server after chunk servers
  - Update status display to show web server
  - Update help text to show HTTP interface

- Fix bug in web/main.c where --local-port incorrectly set
  upstream_port instead of local_port

The cluster demo now starts a full cluster with:
- Metadata server on port 8080 (ToastyFS protocol)
- N chunk servers starting from port 8081
- Web server on port 8090 (HTTP interface)
2025-11-23 09:03:13 +01:00
cozis b661f18214 Fix bug 2025-11-23 00:55:07 +01:00
cozis 0dbb5b1fc5 Add command-line arguments to toastyfs_web 2025-11-23 00:46:27 +01:00
Claude 7e63c211d3 Add cluster demo script for easy cluster management
This script provides a convenient way to spawn and manage a ToastyFS cluster
for demo and testing purposes. Features include:

- Start a cluster with configurable number of chunk servers
- Automatic building if binary is not present
- Process management with PID tracking
- Status checking for all cluster nodes
- Easy cleanup of all cluster processes
- Separate log files for each server component
- Colorized output for better readability

Usage:
  ./scripts/cluster_demo.sh start [num_servers]  - Start cluster
  ./scripts/cluster_demo.sh stop                 - Stop cluster
  ./scripts/cluster_demo.sh status               - Show status
  ./scripts/cluster_demo.sh clean                - Clean data/logs
2025-11-22 23:16:50 +00:00
cozis d7a2262172 Fix compilation errors of the web interface 2025-11-22 23:59:08 +01:00
Claude 7b0b1acf61 Fix file size tracking to use actual written bytes
Previously, file_size was calculated as num_chunks * chunk_size, which
incorrectly treated the file size as the full capacity of all allocated
chunks. The actual file size should be the offset of the last byte
written plus 1.

Changes:
- Added file_size field to File structure to track actual file extent
- Initialize file_size to 0 when creating new files
- Update file_size in file_tree_write() based on write offset + length
- Modified file_tree_read() to use file_size instead of num_chunks * chunk_size
- Updated serialization/deserialization to handle file_size field

This ensures that actual_bytes calculations correctly reflect the true
file size, not just the allocated chunk capacity.
2025-11-22 22:33:15 +00:00
Claude f856bd0e05 Track actual bytes read in read operations
Add functionality to determine the actual number of bytes read during
read operations, making it possible to detect when a read was truncated
because it went past the end of the file.

Changes:
- Modified file_tree_read() to calculate and return actual_bytes via
  new output parameter
- Updated metadata server to send actual_bytes in READ_SUCCESS messages
- Added bytes_read field to ToastyResult structure
- Modified client to parse, store, and report actual bytes read
- Updated toasty_read() to return the actual number of bytes read
  instead of always returning 0
- Fixed web server to use bytes_read field instead of non-existent
  count field

This allows clients to distinguish between:
- Reading zeros because the file is sparse (has holes)
- Reading past the end of the file (truncated read)
2025-11-22 22:22:43 +00:00
cozis b930ba08e3 Add the ability to associate an opaque user pointer to a ToastyHandle which is then returned in its associated ToastyResult 2025-11-22 23:09:31 +01:00
cozis e1fa3729c6 Drop the MAX_CONNS macro and define TCP_CONNECTION_LIMIT, TCP_POLL_CAPACITY, TCP_EVENT_CAPACITY instead 2025-11-22 22:59:32 +01:00
cozis 582893be70 Add notes on the HTTP's interface semantics 2025-11-22 13:13:10 +01:00
cozis b2b1ed540b Update cHTTP and rewrite web/main.c 2025-11-22 12:30:47 +01:00
cozis 747f851155 Draft of the REST server client using cHTTP 2025-11-20 10:50:55 +01:00
cozis b0313ffcf8 Make toasty client thread-safe and implement toasty_wakeup 2025-11-20 03:51:01 +01:00
cozis 9b40edb56c Minor change to README.md code snippet 2025-11-18 18:57:39 +01:00
cozis 8c98835364 Add entries to TODO.txt 2025-11-18 11:44:53 +01:00
cozis 9d0a8e7de5 Bug fix 2025-11-17 23:30:50 +01:00
Francesco CozzutoandGitHub 2a2537b382 Merge pull request #15 from cozis/metadata-server-WAL
Implement metadata server WAL
2025-11-17 23:19:16 +01:00
cozis c83b56ad90 Bug fix 2025-11-17 23:18:10 +01:00
cozis 690a950e71 Bug fixes 2025-11-17 23:07:50 +01:00
cozis dabd337bb8 Use mock versions of lseek and SetFilePointer in file_system.c 2025-11-17 22:49:41 +01:00
cozis dd74106935 Ignore .wal and .tmp files 2025-11-17 22:49:20 +01:00
Francesco CozzutoandGitHub a1c2e52ec1 Merge pull request #14 from cozis/claude/add-file-seek-mocks-01VBJ2woEagtYiZ4EXZj1ou1
Add mocks for lseek and SetFilePointer
2025-11-17 22:46:04 +01:00
Claude 2ab69e43fc Add mocks for lseek and SetFilePointer
- Added mock_lseek declaration and implementation for Linux
- Added mock_SetFilePointer declaration and implementation for Windows
- Added sys_lseek and sys_SetFilePointer macros for both BUILD_TEST and production modes
- Mocks follow existing pattern: validate descriptors, check type, forward to real functions
- Both mocks properly handle error cases and set errno/SetLastError
2025-11-17 21:44:39 +00:00
cozis 94744ede6f Use mocked version of MoveFileExW in wal.c 2025-11-17 22:34:38 +01:00
Francesco CozzutoandGitHub ba4936f565 Merge pull request #13 from cozis/claude/make-auth-system-018EC3ZfTMZUjJbR5SXo5J7K
Add MoveFileExW mock to system.h/.c
2025-11-17 22:32:09 +01:00
Claude 047203a745 Add MoveFileExW mock to system.h/.c
Add mock implementation for MoveFileExW Windows API function following
the existing pattern in the codebase. The mock forwards calls to the
real Windows API, allowing for future interception in the simulation
framework if needed.
2025-11-17 21:30:24 +00:00
Francesco CozzutoandGitHub 57ec853b4d Merge pull request #12 from cozis/claude/fix-wal-metadata-server-01Dva8AjnwAxq3JKaimzpRiY
Complete implementation of WAL for the metadata server
2025-11-17 22:25:37 +01:00
Claude e195c3b3b2 Fix critical data loss bug in Windows WAL rotation
CRITICAL BUG FIX: The previous Windows implementation had a fatal flaw
where it deleted the old WAL file before renaming, creating a window
where all data could be lost if the rename failed.

Previous (BROKEN) Windows code:
    remove_file_or_dir(wal->file_path);  // Delete old file
    rename(...);  // <-- If this fails, we've lost all data!

New implementation uses MoveFileExW with MOVEFILE_REPLACE_EXISTING:
- Windows: MoveFileExW(..., MOVEFILE_REPLACE_EXISTING) atomically
  replaces the destination file, matching Unix semantics
- Unix/Linux: rename() continues to atomically replace as before
- Both platforms now have atomic file replacement with no data loss window

This ensures durability on both platforms - if the operation fails at
any point, we still have either the old file or the new file, never
losing all data.
2025-11-17 21:17:11 +00:00
Claude e8d1e059c3 Improve WAL robustness: fix lifetime issues and use dynamic allocation
This commit addresses several important robustness issues in the WAL
implementation:

1. Fixed file_path lifetime issue:
   - The file_path argument to wal_open() may not have the same lifetime
     as the WAL structure
   - Now allocate and copy the path string to ensure WAL owns its data
   - Properly free the allocated path in wal_close()
   - Added error cleanup path in wal_open() to prevent memory leaks

2. Replaced static arrays with dynamic allocation in next_entry():
   - Static arrays were not thread-safe and had limited lifetimes
   - Now dynamically allocate path buffer and hash buffers using sys_malloc
   - Caller must free allocated fields after using the entry
   - Added proper error cleanup to free allocations on failure
   - Updated wal_open() to free entry fields after processing each entry

3. Improved swap_file() for cross-platform compatibility:
   - On Unix/Linux: rename() atomically replaces the destination file
   - On Windows: rename() doesn't overwrite, so delete old file first
   - Added platform-specific handling with #ifdef _WIN32
   - Ensures WAL rotation works correctly on both platforms

4. Added system.h include for sys_malloc/sys_free definitions

These changes ensure proper memory management, prevent leaks, and make
the WAL implementation more robust across different platforms.
2025-11-17 21:12:33 +00:00
Claude 52bc1ff450 Complete WAL implementation with file rotation
This commit completes the Write-Ahead Log (WAL) implementation for the
metadata server, including all missing functionality and error fixes:

File System Changes (src/file_system.c):
- Implemented file_set_offset() using lseek (Linux) and SetFilePointer (Windows)
- Implemented file_get_offset() to get current file position

WAL Header Changes (src/wal.h):
- Added file_tree pointer to WAL structure for snapshot serialization
- Added file_path to WAL structure for rotation operations

WAL Implementation (src/wal.c):
- Fixed handle assignment bug in wal_open (was using wal->handle before assignment)
- Fixed missing return statement in append_begin()
- Implemented serialize_callback for writing file tree snapshots to WAL
- Implemented deserialize_callback for reading file tree snapshots from WAL
- Implemented next_entry() to read WAL entries from file during recovery
- Implemented wal_append_write() to write file modifications to WAL
- Implemented swap_file() for complete WAL file rotation:
  * Creates temporary file with new snapshot
  * Writes WAL header and serialized file tree
  * Atomically replaces old WAL file
  * Resets entry counter for new rotation cycle
- Added WAL file initialization for newly created files
- Added helper functions: read_exact, read_u8, read_u16, read_u32, read_u64, write_u32
- Fixed typo in comment (Not -> Now)

The WAL now supports:
- Full crash recovery by replaying logged operations
- Automatic file rotation when entry limit is reached
- Atomic file replacement to ensure durability
- Proper file locking throughout rotation process
2025-11-17 21:02:19 +00:00
cozis fb28e94a83 Continued work on the WAL 2025-11-17 21:52:44 +01:00