C/C++ quickstart

This is a ~60-second round-trip in C/C++: snapshot a directory, push it to a local file:// store, then pull it back and confirm the ID matches. Snapshot IDs are 64-character lowercase hex and are bit-identical to the CLI and every other binding.

1. Install

Build the snapdir-ffi C ABI from crates.io and compile against snapdir.hpp — see Install snapdir for C/C++ for the full recipe:

cargo build --release                       # -> target/release/libsnapdir_ffi.a
cbindgen --config cbindgen.toml --crate snapdir-ffi --output snapdir.h .
g++ -std=c++20 roundtrip.cpp -I. -L. -lsnapdir_ffi -lpthread -ldl -lm -o roundtrip

2. Snapshot + push

Compute the snapshot ID of a directory, then push it to a local store. The store URI is always an argument; file://$PWD/store needs no setup.

#include <snapdir.hpp>
#include <cstdlib>
#include <iostream>
#include <string>

int main() {
    const std::string dir   = "./my-dir";
    const std::string store = std::string("file://") + std::getenv("PWD") + "/store";

    // Synchronous id — the 64-char lowercase hex snapshot id.
    std::cout << "id:   " << snapdir::id(dir) << '\n';

    // Async push returns std::future<std::string>; .get() resolves or throws.
    const std::string id = snapdir::push(dir, store).get();
    std::cout << "push: " << id << '\n';   // <snapshot-id>
}

3. Pull it back

Pull the snapshot into a fresh directory and recompute its ID — it matches the one you pushed.

snapdir::pull(id, store, "./restored").get();

const std::string back = snapdir::id("./restored");
std::cout << (back == id ? "match" : "MISMATCH") << ": " << back << '\n';

The id you get here is the same snapshot id the CLI prints for snapdir id and snapdir push — every binding agrees byte-for-byte.