/* * spffa.h — scorpiox-scorpioplayer-folder-file-accessor * * SCORPIO PLAYER 2027 storage contract: the vtable every storage backend fills in, * the item model every consumer walks, a registry, a fake backend and a * conformance suite. No network, no protocol, no parsing of any wire format. * * See SPEC.md for every decision and its C# citation, and README.md for the * ABI stability rules (which fields are frozen, which may be appended). * * Threading: the registry is single-threaded (global state, fleet style). * The ONLY export that may be called from another thread is spffa_cancel(). * * Strings: every const char * returned by an spffa_* export is owned by the * library and valid until the next call that changes the same state. Copy it. */ #ifndef SPFFA_H #define SPFFA_H #include #ifdef __cplusplus extern "C" { #endif #if defined(SPFFA_STATIC) # define SPFFA_API #elif defined(_WIN32) # if defined(SPFFA_BUILD) # define SPFFA_API __declspec(dllexport) # else # define SPFFA_API __declspec(dllimport) # endif #else # define SPFFA_API __attribute__((visibility("default"))) #endif #define SPFFA_ABI_VERSION 1 /* ---- error taxonomy (SPEC §9) — values are frozen ---- */ #define SPFFA_OK 0 #define SPFFA_ERR_NO_BACKEND 1 #define SPFFA_ERR_NOT_OPEN 2 #define SPFFA_ERR_NOT_FOUND 3 #define SPFFA_ERR_ACCESS 4 #define SPFFA_ERR_TRANSPORT 5 #define SPFFA_ERR_UNSUPPORTED 6 #define SPFFA_ERR_CANCELLED 7 #define SPFFA_ERR_BAD_CONFIG 8 #define SPFFA_ERR_INVALID_ARG 9 #define SPFFA_ERR_OOM 10 /* ---- capability bits (SPEC §10) — values are frozen, new bits append ---- */ #define SPFFA_CAP_PAGING (1u << 0) /* native cursor; no re-listing to resume */ #define SPFFA_CAP_STAT (1u << 1) /* stat(path) without listing the parent */ #define SPFFA_CAP_KNOWN_META (1u << 2) /* backend supplies title/year (Jellyfin) */ #define SPFFA_CAP_STABLE_ID (1u << 3) /* backend supplies its own item id */ #define SPFFA_CAP_RANGE (1u << 4) /* locator is range-readable by sprs */ #define SPFFA_CAP_ALL (0x1Fu) /* ---- item presence bits — values are frozen, new bits append ---- */ #define SPFFA_F_SIZE (1u << 0) #define SPFFA_F_MTIME (1u << 1) #define SPFFA_F_TITLE (1u << 2) #define SPFFA_F_YEAR (1u << 3) #define SPFFA_F_ID (1u << 4) #define SPFFA_F_LOCATOR (1u << 5) #define SPFFA_F_ALL (0x3Fu) /* ---- the item model: ONE struct, presence bits, no interface hierarchy ---- * ABI: fields are never reordered or removed; new fields are appended only. * struct_size is set by whoever allocates the struct. */ typedef struct spffa_item { unsigned struct_size; /* sizeof(spffa_item) as the allocator compiled it */ unsigned fields; /* SPFFA_F_* */ const char *name; /* required: one path segment, UTF-8, no '/' */ const char *path; /* required: backend-defined; pass back verbatim */ int is_folder; /* 0 or 1 — never derived from size */ int year; /* valid iff F_YEAR (1800..9999) */ long long size; /* bytes; -1 = unknown, NEVER 0 for unknown */ long long mtime_ms; /* Unix epoch ms UTC; -1 = unknown */ const char *title; /* valid iff F_TITLE */ const char *backend_id; /* valid iff F_ID — the backend's own stable id */ const char *locator; /* valid iff F_LOCATOR — opaque, for remote-seek-stream */ } spffa_item; /* ---- one page of a listing ---- */ typedef struct spffa_page { unsigned struct_size; const char *cursor; /* in: NULL = first page */ spffa_item *items; /* in: caller-owned, zeroed, struct_size set */ size_t cap; /* in: >= 1 — return AT MOST this many */ size_t count; /* out */ int more; /* out: 1 = another page exists */ const char *next_cursor; /* out: non-empty iff more */ const volatile int *cancel; /* in: poll between entries; !=0 => ERR_CANCELLED */ } spffa_page; /* ---- the vtable a backend fills in ---- * Strings a backend writes into items/pages must stay valid until the next * call on the same ctx; the registry deep-copies immediately. */ typedef struct spffa_backend { unsigned struct_size; /* sizeof(spffa_backend) */ const char *id; /* "smb2", "webdav", "onedrive" — NOT an enum */ unsigned capabilities; /* SPFFA_CAP_* */ void *ctx; int (*open) (void *ctx, const char *config_json); int (*list) (void *ctx, const char *path, spffa_page *inout); /* paged */ int (*stat) (void *ctx, const char *path, spffa_item *out); /* NULL iff !CAP_STAT */ int (*locator)(void *ctx, const char *path, char *out, size_t n); /* for #20 */ void (*close) (void *ctx); } spffa_backend; /* ---- item accessors (safe on NULL: "" / 0 / -1) ---- */ SPFFA_API const char *spffa_item_name(const spffa_item *it); SPFFA_API const char *spffa_item_path(const spffa_item *it); SPFFA_API int spffa_item_is_folder(const spffa_item *it); SPFFA_API long long spffa_item_size(const spffa_item *it); /* -1 = unknown */ SPFFA_API long long spffa_item_mtime_ms(const spffa_item *it); /* -1 = unknown */ SPFFA_API int spffa_item_has(const spffa_item *it, unsigned field); SPFFA_API unsigned spffa_item_fields(const spffa_item *it); SPFFA_API const char *spffa_item_title(const spffa_item *it); /* "" if absent */ SPFFA_API int spffa_item_year(const spffa_item *it); /* 0 if absent */ SPFFA_API const char *spffa_item_backend_id(const spffa_item *it);/* "" if absent */ SPFFA_API const char *spffa_item_locator(const spffa_item *it); /* "" if absent */ /* ---- registry ---- */ SPFFA_API int spffa_register(const spffa_backend *b); /* copies the vtable */ SPFFA_API void spffa_reset(void); /* close + forget all */ SPFFA_API int spffa_backend_count(void); SPFFA_API const char *spffa_backend_id(int i); SPFFA_API int spffa_backend_caps(int i); /* -1 if out of range */ /* ---- session + walk ---- */ SPFFA_API int spffa_open(const char *backend_id, const char *config_json); SPFFA_API int spffa_set_page_size(int n); /* 1..65536, default 256 */ SPFFA_API int spffa_list(const char *path); /* first page */ SPFFA_API int spffa_list_next(void); /* 1 = more pages, 0 = done, <0 = -SPFFA_ERR_* */ SPFFA_API int spffa_list_all(const char *path); /* list + list_next until done */ SPFFA_API int spffa_more(void); SPFFA_API int spffa_count(void); SPFFA_API const spffa_item *spffa_item_at(int i); /* valid until next list/close */ SPFFA_API int spffa_rejected(void); /* entries dropped by validator */ SPFFA_API int spffa_get(const char *path, spffa_item *out); /* the missing GetFileAsync */ SPFFA_API const spffa_item *spffa_got(void); /* last successful get */ SPFFA_API const char *spffa_locator(const char *path); /* NULL on error */ SPFFA_API void spffa_cancel(void); /* any thread */ SPFFA_API void spffa_close(void); /* ---- helpers ---- */ SPFFA_API const char *spffa_path_join(const char *parent, const char *name); /* NULL on error */ SPFFA_API int spffa_validate_item(const spffa_item *it, const char *listed_path); SPFFA_API int spffa_decode(const char *json); /* items JSON -> current listing */ /* ---- the fake backend: the test harness AND the safety mechanism ---- */ #define SPFFA_OP_OPEN 1 #define SPFFA_OP_LIST 2 #define SPFFA_OP_LIST_NEXT 3 #define SPFFA_OP_STAT 4 #define SPFFA_OP_LOCATOR 5 #define SPFFA_OP_CANCEL_AT 6 /* err = n: fake calls spffa_cancel() after n entries */ SPFFA_API int spffa_fake_register(void); SPFFA_API int spffa_fake_load_tree(const char *json); SPFFA_API int spffa_fake_set_fault(int op, int err); /* err 0 clears */ SPFFA_API int spffa_fake_set_caps(unsigned caps); SPFFA_API int spffa_fake_calls(int op); /* call counter per op */ /* ---- conformance suite: a backend implements the vtable iff this returns 0 ---- */ SPFFA_API int spffa_conformance(const char *backend_id, const char *config_json, const char *root_path); SPFFA_API int spffa_conformance_assertions(void); /* how many were run */ /* ---- errors + JSON ---- */ SPFFA_API const char *spffa_last_error(void); SPFFA_API int spffa_last_code(void); SPFFA_API const char *spffa_strerror(int code); SPFFA_API const char *spffa_result_json(void); /* listing / got / conformance report */ SPFFA_API const char *spffa_version(void); #ifdef __cplusplus } #endif #endif /* SPFFA_H */