Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 01eb20b95d | |||
| 78263b5819 | |||
| e6c55356c8 | |||
| 95568ae34e | |||
| 12ca9e736b |
@@ -1,3 +1,137 @@
|
|||||||
# FastSync
|
# FastFileTransfer
|
||||||
|
|
||||||
A high-performance file synchronization tool for Nextcloud.
|
A high-performance file synchronization system that implements a custom client-server protocol for efficient file transfer with compression and multithreading support.
|
||||||
|
|
||||||
|
## Technical Overview
|
||||||
|
|
||||||
|
FastFileTransfer is a C implementation of a file synchronization system that:
|
||||||
|
|
||||||
|
1. Uses a custom TCP-based protocol for client-server communication
|
||||||
|
2. Implements chunked file transfer (10MB chunks by default)
|
||||||
|
3. Supports zstd compression with configurable levels (1-22)
|
||||||
|
4. Utilizes multithreading for parallel file processing
|
||||||
|
5. Implements producer-consumer patterns with thread-safe queues
|
||||||
|
6. Provides both in-memory and disk-based storage options
|
||||||
|
|
||||||
|
## System Architecture
|
||||||
|
|
||||||
|
The system consists of two main components:
|
||||||
|
|
||||||
|
### Client
|
||||||
|
- Scans source directories recursively
|
||||||
|
- Creates file chunks with configurable size (10MB default)
|
||||||
|
- Compresses data using zstd algorithm
|
||||||
|
- Sends files to server using custom protocol
|
||||||
|
- Supports both single-threaded and multi-threaded operation
|
||||||
|
|
||||||
|
### Server
|
||||||
|
- Listens for client connections on port 8080
|
||||||
|
- Receives files using the custom protocol
|
||||||
|
- Decompresses received data
|
||||||
|
- Stores files either in memory or on disk
|
||||||
|
- Implements thread pool for parallel processing
|
||||||
|
|
||||||
|
## Protocol Details
|
||||||
|
|
||||||
|
The client-server communication uses the following status codes:
|
||||||
|
- `STATUS_OK`: Operation successful
|
||||||
|
- `STATUS_ERROR`: Error occurred
|
||||||
|
- `STATUS_FINISHED`: Transfer complete
|
||||||
|
- `STATUS_NEXT`: Ready for next chunk
|
||||||
|
|
||||||
|
## Configuration Options
|
||||||
|
|
||||||
|
### Command Line Arguments
|
||||||
|
| Argument | Description |
|
||||||
|
|----------|-------------|
|
||||||
|
| `-m` | Enable multithreading mode |
|
||||||
|
| `-c [level]` | Enable compression with optional level (1-22, default: 5) |
|
||||||
|
| `-s` | Enable chunk serialization |
|
||||||
|
|
||||||
|
### Environment Variables
|
||||||
|
| Variable | Description | Default |
|
||||||
|
|----------|-------------|---------|
|
||||||
|
| `FASTSYNC_SOURCE_DIR` | Source directory for files | Current user's documents directory |
|
||||||
|
| `FASTSYNC_DEST_DIR` | Destination directory | `./data_copied` |
|
||||||
|
| `FASTSYNC_SERVER_IP` | Server IP address | `127.0.0.1` |
|
||||||
|
| `FASTSYNC_SERVER_PORT` | Server port | `8080` |
|
||||||
|
| `FASTSYNC_SAVE_TO_DISK` | Save to disk (true/false) | `false` |
|
||||||
|
|
||||||
|
## Implementation Details
|
||||||
|
|
||||||
|
### Data Structures
|
||||||
|
|
||||||
|
1. **Chunk**: Collection of files (default 10MB total size)
|
||||||
|
2. **File**: File metadata with path and content
|
||||||
|
3. **FileReceive**: Received file data structure
|
||||||
|
4. **Config**: Configuration parameters structure
|
||||||
|
5. **Queue**: Thread-safe queue implementation using condition variables
|
||||||
|
|
||||||
|
### Key Algorithms
|
||||||
|
|
||||||
|
1. **File Scanning**: Recursive directory traversal with BFS
|
||||||
|
2. **Chunking**: Files grouped into chunks with size limit
|
||||||
|
3. **Compression**: zstd compression with configurable levels
|
||||||
|
4. **Network Protocol**: Custom TCP-based protocol with status codes
|
||||||
|
5. **Thread Synchronization**: Condition variables and mutexes for thread coordination
|
||||||
|
|
||||||
|
## Build Requirements
|
||||||
|
|
||||||
|
- C11 compatible compiler
|
||||||
|
- CMake 4.1 or later
|
||||||
|
- zstd library
|
||||||
|
- pthread support
|
||||||
|
|
||||||
|
## Building
|
||||||
|
|
||||||
|
```bash
|
||||||
|
mkdir -p build && cd build
|
||||||
|
cmake ..
|
||||||
|
make
|
||||||
|
```
|
||||||
|
|
||||||
|
## Running
|
||||||
|
|
||||||
|
### Server
|
||||||
|
```bash
|
||||||
|
./build/server
|
||||||
|
```
|
||||||
|
|
||||||
|
### Client
|
||||||
|
```bash
|
||||||
|
# Basic usage
|
||||||
|
./build/client -m -c 10
|
||||||
|
```
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
|
||||||
|
The project includes comprehensive unit tests for core functionality:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./build/tests
|
||||||
|
```
|
||||||
|
|
||||||
|
## Code Organization
|
||||||
|
|
||||||
|
```
|
||||||
|
src/
|
||||||
|
client/ # Client implementation
|
||||||
|
server/ # Server implementation
|
||||||
|
shared/ # Shared data structures and utilities
|
||||||
|
tests/ # Unit tests
|
||||||
|
```
|
||||||
|
|
||||||
|
## Performance Considerations
|
||||||
|
|
||||||
|
1. Chunk size (10MB default) affects memory usage and transfer efficiency
|
||||||
|
2. Compression level (1-22) trades CPU usage for space savings
|
||||||
|
3. Multithreading improves performance on multi-core systems
|
||||||
|
4. Thread-safe queues minimize contention between producer/consumer threads
|
||||||
|
|
||||||
|
## Extensibility
|
||||||
|
|
||||||
|
The system is designed with clear interfaces that allow for:
|
||||||
|
1. Additional compression algorithms
|
||||||
|
2. Different transport protocols
|
||||||
|
3. Custom storage backends
|
||||||
|
4. Extended metadata support
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
# jetstream - A High-Performance File Transfer Utility
|
|
||||||
|
|
||||||
## Features
|
|
||||||
|
|
||||||
## Architecture ideas
|
|
||||||
|
|
||||||
### Pipeline Stages Client
|
|
||||||
|
|
||||||
- Directory Scanning -> Files
|
|
||||||
- File Reading -> SendableBuffer
|
|
||||||
- optional: Compression -> Sendable Buffer
|
|
||||||
- Send Data
|
|
||||||
|
|
||||||
### Pipeline Stages Server
|
|
||||||
|
|
||||||
- Receive Data -> Sendable Buffer
|
|
||||||
- optional: Decompress -> Files
|
|
||||||
- File Writing
|
|
||||||
|
|
||||||
### Passing Data between Steps
|
|
||||||
|
|
||||||
- use Queue
|
|
||||||
|
|
||||||
##
|
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://opencode.ai/config.json",
|
||||||
|
"permission": {
|
||||||
|
"bash": {
|
||||||
|
"*": "allow",
|
||||||
|
"git push origin main": "deny",
|
||||||
|
"git push origin master": "deny",
|
||||||
|
"git push *": "ask",
|
||||||
|
"git commit *": "ask"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,23 +1,8 @@
|
|||||||
{
|
{ pkgs }: {
|
||||||
pkgs ? import <nixpkgs> { },
|
|
||||||
}:
|
|
||||||
|
|
||||||
pkgs.mkShell {
|
|
||||||
nativeBuildInputs = with pkgs; [
|
|
||||||
gcc
|
|
||||||
cmake
|
|
||||||
gnumake
|
|
||||||
pkg-config
|
|
||||||
];
|
|
||||||
|
|
||||||
buildInputs = with pkgs; [
|
|
||||||
zstd
|
|
||||||
];
|
|
||||||
|
|
||||||
NIX_ENFORCE_PURITY = 0;
|
|
||||||
|
|
||||||
shellHook = ''
|
shellHook = ''
|
||||||
export NIX_ENFORCE_PURITY=0
|
echo "Setting up environment with gh CLI tool"
|
||||||
cmake -B build
|
|
||||||
'';
|
'';
|
||||||
|
buildInputs = [
|
||||||
|
pkgs.gh
|
||||||
|
];
|
||||||
}
|
}
|
||||||
+84
-9
@@ -134,20 +134,95 @@ Data *chunk_compress(Chunk *chunk, int compression_level) {
|
|||||||
Chunk *chunk_decompress(Data *compressed_data) {
|
Chunk *chunk_decompress(Data *compressed_data) {
|
||||||
log_message(LOG_LEVEL_DEBUG, "Starting to decompress chunk");
|
log_message(LOG_LEVEL_DEBUG, "Starting to decompress chunk");
|
||||||
Data *uncompressed_data = data_decompress(compressed_data);
|
Data *uncompressed_data = data_decompress(compressed_data);
|
||||||
ArrayList *files = array_list_create(file_destroy);
|
if (uncompressed_data == NULL) {
|
||||||
size_t *data_pointer = uncompressed_data->data;
|
log_message(LOG_LEVEL_ERROR, "Failed to decompress chunk data");
|
||||||
while (data_pointer <
|
return NULL;
|
||||||
(size_t *)uncompressed_data->data + uncompressed_data->size) {
|
|
||||||
size_t path_len = data_pointer[0];
|
|
||||||
|
|
||||||
printf("%zu, testing", path_len);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log_message(LOG_LEVEL_DEBUG, "Chunk succesfully decompressed");
|
ArrayList *files = array_list_create(file_destroy);
|
||||||
|
char *data_pointer = uncompressed_data->data;
|
||||||
|
size_t remaining_size = uncompressed_data->size;
|
||||||
|
|
||||||
|
while (remaining_size > 0) {
|
||||||
|
if (remaining_size < sizeof(size_t)) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for path length");
|
||||||
|
array_list_destroy(files);
|
||||||
|
data_delete(uncompressed_data);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t path_len = *(size_t *)data_pointer;
|
||||||
|
data_pointer += sizeof(size_t);
|
||||||
|
remaining_size -= sizeof(size_t);
|
||||||
|
|
||||||
|
if (remaining_size < path_len) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for path");
|
||||||
|
array_list_destroy(files);
|
||||||
|
data_delete(uncompressed_data);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *path = malloc(path_len + 1);
|
||||||
|
if (path == NULL) {
|
||||||
|
perror("Could not allocate memory for file path");
|
||||||
|
array_list_destroy(files);
|
||||||
|
data_delete(uncompressed_data);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
memcpy(path, data_pointer, path_len);
|
||||||
|
path[path_len] = '\0';
|
||||||
|
data_pointer += path_len;
|
||||||
|
remaining_size -= path_len;
|
||||||
|
|
||||||
|
if (remaining_size < sizeof(size_t)) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for data size");
|
||||||
|
free(path);
|
||||||
|
array_list_destroy(files);
|
||||||
|
data_delete(uncompressed_data);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t data_size = *(size_t *)data_pointer;
|
||||||
|
data_pointer += sizeof(size_t);
|
||||||
|
remaining_size -= sizeof(size_t);
|
||||||
|
|
||||||
|
if (remaining_size < data_size) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for file content");
|
||||||
|
free(path);
|
||||||
|
array_list_destroy(files);
|
||||||
|
data_delete(uncompressed_data);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
File *file = file_create(path, data_size);
|
||||||
|
if (file == NULL) {
|
||||||
|
free(path);
|
||||||
|
array_list_destroy(files);
|
||||||
|
data_delete(uncompressed_data);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(file->data, data_pointer, data_size);
|
||||||
|
data_pointer += data_size;
|
||||||
|
remaining_size -= data_size;
|
||||||
|
|
||||||
|
array_list_add(files, file);
|
||||||
|
free(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the chunk from the files
|
||||||
|
File **file_array = (File **)array_list_to_array(files);
|
||||||
|
Chunk *chunk = chunk_create(file_array, array_list_size(files));
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
free(file_array);
|
||||||
|
array_list_destroy(files);
|
||||||
|
data_delete(uncompressed_data);
|
||||||
|
|
||||||
|
log_message(LOG_LEVEL_DEBUG, "Chunk successfully decompressed");
|
||||||
|
return chunk;
|
||||||
|
}
|
||||||
|
|
||||||
Data *chunk_data_create(void *data, unsigned long long data_size) {
|
Data *chunk_data_create(void *data, unsigned long long data_size) {
|
||||||
Data *chunk_formated = malloc(sizeof(Data));
|
Data *chunk_formated = malloc(sizeof(Data));
|
||||||
if (chunk_formated == NULL) {
|
if (chunk_formated == NULL) {
|
||||||
|
|||||||
+1
-1
@@ -61,7 +61,7 @@ void server_listen(Server *server, void (*handler)(int file_descriptor)) {
|
|||||||
int file_descriptor =
|
int file_descriptor =
|
||||||
accept(server->file_descriptor, (struct sockaddr *)&server->address,
|
accept(server->file_descriptor, (struct sockaddr *)&server->address,
|
||||||
&server->address_length);
|
&server->address_length);
|
||||||
if (server->file_descriptor < 0) {
|
if (file_descriptor < 0) {
|
||||||
perror("Could not accept the connection");
|
perror("Could not accept the connection");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user