diff --git a/CMakeLists.txt b/CMakeLists.txt index b7e055c..d609140 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,19 +1,12 @@ cmake_minimum_required(VERSION 3.14) -project(storageconsole C) +project(easylibstorage C) set(CMAKE_C_STANDARD 11) -add_executable(storageconsole - main.c - easylibstorage.c - easylibstorage.h -) - if(NOT DEFINED LOGOS_STORAGE_NIM_ROOT) message(FATAL_ERROR "Need to set LOGOS_STORAGE_NIM_ROOT") endif() -target_include_directories(storageconsole PRIVATE "${LOGOS_STORAGE_NIM_ROOT}/library") - +# --- Find libstorage --- if(APPLE) set(LIBSTORAGE_NAMES libstorage.dylib) elseif(WIN32) @@ -24,10 +17,34 @@ endif() find_library(LIBSTORAGE_PATH NAMES ${LIBSTORAGE_NAMES} PATHS ${LOGOS_STORAGE_NIM_ROOT}/build NO_DEFAULT_PATH) +if(NOT LIBSTORAGE_PATH) + message(WARNING "libstorage not found. Build or provide it before linking.") +endif() + +# --- Shared library: easystorage --- +add_library(easystorage SHARED + easystorage.c + easystorage.h +) + +target_include_directories(easystorage PUBLIC + "${CMAKE_SOURCE_DIR}" + "${LOGOS_STORAGE_NIM_ROOT}/library" +) + +if(LIBSTORAGE_PATH) + target_link_libraries(easystorage PRIVATE ${LIBSTORAGE_PATH}) +endif() + +# --- Example: storageconsole --- +add_executable(storageconsole + examples/storageconsole.c +) + +target_link_libraries(storageconsole PRIVATE easystorage) + if(LIBSTORAGE_PATH) target_link_libraries(storageconsole PRIVATE ${LIBSTORAGE_PATH}) -else() - message(WARNING "libstorage not found. Build or provide it before linking.") endif() # --- Tests --- @@ -35,7 +52,7 @@ enable_testing() add_executable(test_runner tests/test_runner.c - easylibstorage.c + easystorage.c tests/mock_libstorage.c ) @@ -44,4 +61,4 @@ target_include_directories(test_runner PRIVATE "${LOGOS_STORAGE_NIM_ROOT}/library" ) -add_test(NAME easylibstorage_tests COMMAND test_runner) +add_test(NAME easystorage_tests COMMAND test_runner) diff --git a/PROMPT.md b/PROMPT.md index f14ccdc..1b81d00 100644 --- a/PROMPT.md +++ b/PROMPT.md @@ -1,5 +1,5 @@ # Context -I want you to implement the API described in `easylibstorage.h` in a file named `easylibstorage.c`. This is a simplified wrapper on top of `libstorage`. +I want you to implement the API described in `easystorage.h` in a file named `easystorage.c`. This is a simplified wrapper on top of `libstorage`. # Dependencies The headers for `libstorage` are located at `/home/giuliano/Work/Status/logos-storage-nim/library/libstorage.h`. @@ -14,9 +14,9 @@ You MUST follow a Test Driven Development approach. Since no test environment ex 1. Create a simple test runner (e.g., `tests/test_runner.c`) using basic `assert()` statements. 2. Update `CMakeLists.txt` to build this test runner. -Then, for *each* function in `easylibstorage.h`: +Then, for *each* function in `easystorage.h`: 1. **Red**: Write a failing test case in `tests/test_runner.c`. -2. **Green**: Implement the function in `easylibstorage.c` until the test passes. +2. **Green**: Implement the function in `easystorage.c` until the test passes. 3. **Refactor**: Stop, think of ways to refactor and simplify the code, and do it. **CRITICAL**: This refactoring step is VERY IMPORTANT. You MUST look for ways to simplify, deduplicate, and coalesce code here, WITHOUT OVERCOMPLICATING. **SIMPLICITY IS KEY AND YOUR GUIDING PRINCIPLE.** diff --git a/README.md b/README.md index 6d7246e..35dae3d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,12 @@ -# Storage Console +# easystorage -A simple wrapper and a command-line interface for interacting with libstorage, providing simple file upload and download operations via a distributed storage network. +A simplified C wrapper around [libstorage](https://github.com/status-im/logos-storage-nim), providing an easy-to-use API for distributed file storage operations. + +## Features + +- Simple node lifecycle management (create, start, stop, destroy) +- File upload with progress callback +- File download by CID with progress callback ## Prerequisites @@ -15,44 +21,47 @@ cmake -B build -DLOGOS_STORAGE_NIM_ROOT=/path/to/logos-storage-nim cmake --build build ``` -## Running +This produces: +- `libeasystorage.so` — the shared library +- `storageconsole` — an example CLI application + +## API + +```c +#include "easystorage.h" + +// Create and start a node +node_config cfg = { + .api_port = 8080, + .disc_port = 9090, + .data_dir = "./data", + .log_level = "INFO", + .bootstrap_node = "" +}; +STORAGE_NODE node = e_storage_new(cfg); +e_storage_start(node); + +// Upload a file +char *cid = e_storage_upload(node, "/path/to/file.txt", progress_cb); +free(cid); + +// Download a file +e_storage_download(node, cid, "/path/to/output.txt", progress_cb); + +// Cleanup +e_storage_stop(node); +e_storage_destroy(node); +``` + +## Example: storageconsole + +An interactive CLI is included in `examples/storageconsole.c`: ```bash ./build/storageconsole ``` -## Commands - -| Command | Description | -|---------|-------------| -| `help` | Prints available commands | -| `start [API_PORT] [DISC_PORT] [DATA_DIR] [BOOTSTRAP_NODE]` | Creates and starts a storage node | -| `stop` | Stops and destroys the node | -| `upload [PATH]` | Uploads a local file; displays progress and prints CID | -| `download [CID] [PATH]` | Downloads content by CID to a local path | -| `quit` | Exits the program | - -### Example Session - -``` -> start 8080 9090 ./data -Creating node... -Starting node... -Node started on API port 8080, discovery port 9090. - -> upload /path/to/file.txt -Uploading /path/to/file.txt... - 1024 / 1024 bytes -CID: QmXyz... - -> download QmXyz... /path/to/output.txt -Downloading QmXyz... to /path/to/output.txt... - 1024 / 1024 bytes -Download complete. - -> quit -Quitting... -``` +Commands: `help`, `start`, `stop`, `upload`, `download`, `quit`. ## Testing @@ -64,12 +73,13 @@ cmake --build build ## Project Structure ``` -storageconsole/ -├── main.c # Console application -├── easylibstorage.h # Simplified libstorage API -├── easylibstorage.c # API implementation +easystorage/ +├── easystorage.h # Public API +├── easystorage.c # Implementation +├── examples/ +│ └── storageconsole.c # CLI example ├── tests/ -│ ├── test_runner.c # Unit tests +│ ├── test_runner.c │ └── mock_libstorage.c └── CMakeLists.txt ``` diff --git a/easylibstorage.c b/easystorage.c similarity index 99% rename from easylibstorage.c rename to easystorage.c index 06f6c54..edab05e 100644 --- a/easylibstorage.c +++ b/easystorage.c @@ -1,4 +1,4 @@ -#include "easylibstorage.h" +#include "easystorage.h" #include "libstorage.h" #include diff --git a/easylibstorage.h b/easystorage.h similarity index 87% rename from easylibstorage.h rename to easystorage.h index f10df98..d84a840 100644 --- a/easylibstorage.h +++ b/easystorage.h @@ -1,5 +1,5 @@ -#ifndef STORAGECONSOLE_EASYLIBSTORAGE_H -#define STORAGECONSOLE_EASYLIBSTORAGE_H +#ifndef EASYSTORAGE_H +#define EASYSTORAGE_H #define STORAGE_NODE void * @@ -26,4 +26,4 @@ char *e_storage_upload(STORAGE_NODE node, const char *filepath, progress_callbac // Downloads content identified by cid to filepath. Returns 0 on success. int e_storage_download(STORAGE_NODE node, const char *cid, const char *filepath, progress_callback cb); -#endif // STORAGECONSOLE_EASYLIBSTORAGE_H +#endif // EASYSTORAGE_H diff --git a/main.c b/examples/storageconsole.c similarity index 99% rename from main.c rename to examples/storageconsole.c index 1dd1962..02293ea 100644 --- a/main.c +++ b/examples/storageconsole.c @@ -1,4 +1,4 @@ -#include "easylibstorage.h" +#include "easystorage.h" #include #include diff --git a/tests/test_runner.c b/tests/test_runner.c index 50f40f8..3750a93 100644 --- a/tests/test_runner.c +++ b/tests/test_runner.c @@ -1,4 +1,4 @@ -#include "easylibstorage.h" +#include "easystorage.h" #include #include