Skip to main content

libdoltlite_sys/
lib.rs

1#![expect(non_snake_case, non_camel_case_types)]
2#![cfg_attr(not(test), no_std)]
3pub use self::error::*;
4
5use core::mem;
6#[cfg(not(feature = "loadable_extension"))]
7use core::sync::atomic::{AtomicI32, Ordering};
8
9mod error;
10
11#[must_use]
12pub fn SQLITE_STATIC() -> sqlite3_destructor_type {
13    None
14}
15
16#[must_use]
17pub fn SQLITE_TRANSIENT() -> sqlite3_destructor_type {
18    Some(unsafe { mem::transmute::<isize, unsafe extern "C" fn(*mut core::ffi::c_void)>(-1_isize) })
19}
20
21#[allow(dead_code, clippy::all)]
22mod bindings {
23    include!(concat!(env!("OUT_DIR"), "/bindgen.rs"));
24}
25pub use bindings::*;
26
27/// Raw Cloud Backed SQLite block-cache VFS interface.
28#[cfg(feature = "blockcachevfs")]
29pub mod blockcachevfs {
30    use core::ffi::{c_char, c_int, c_void};
31
32    use super::{sqlite3, sqlite3_int64};
33
34    /// Opaque Cloud Backed SQLite VFS handle.
35    #[repr(C)]
36    pub struct sqlite3_bcvfs {
37        _private: [u8; 0],
38    }
39
40    /// Authentication callback used by the CBS VFS.
41    pub type sqlite3_bcvfs_auth_callback = unsafe extern "C" fn(
42        p_ctx: *mut c_void,
43        z_storage: *const c_char,
44        z_account: *const c_char,
45        z_container: *const c_char,
46        pz_auth_token: *mut *mut c_char,
47    ) -> c_int;
48
49    /// Busy callback used by an upload checkpoint.
50    pub type sqlite3_bcvfs_busy_callback = unsafe extern "C" fn(*mut c_void, c_int) -> c_int;
51
52    pub const SQLITE_BCV_CACHESIZE: c_int = 1;
53    pub const SQLITE_BCV_NREQUEST: c_int = 2;
54    pub const SQLITE_BCV_HTTPTIMEOUT: c_int = 3;
55    pub const SQLITE_BCV_CURLVERBOSE: c_int = 4;
56    pub const SQLITE_BCV_HTTPLOG_TIMEOUT: c_int = 5;
57    pub const SQLITE_BCV_HTTPLOG_NENTRY: c_int = 6;
58
59    pub const SQLITE_BCV_ATTACH_SECURE: c_int = 0x0001;
60    pub const SQLITE_BCV_ATTACH_IFNOT: c_int = 0x0002;
61
62    unsafe extern "C" {
63        pub fn sqlite3_bcvfs_create(
64            z_dir: *const c_char,
65            z_name: *const c_char,
66            pp_fs: *mut *mut sqlite3_bcvfs,
67            pz_err: *mut *mut c_char,
68        ) -> c_int;
69        pub fn sqlite3_bcvfs_destroy(fs: *mut sqlite3_bcvfs) -> c_int;
70        pub fn sqlite3_bcvfs_isdaemon(fs: *mut sqlite3_bcvfs) -> c_int;
71        pub fn sqlite3_bcvfs_register_vtab(db: *mut sqlite3) -> c_int;
72        pub fn sqlite3_bcvfs_config(
73            fs: *mut sqlite3_bcvfs,
74            op: c_int,
75            value: sqlite3_int64,
76        ) -> c_int;
77        pub fn sqlite3_bcvfs_auth_callback(
78            fs: *mut sqlite3_bcvfs,
79            auth_ctx: *mut c_void,
80            auth: Option<sqlite3_bcvfs_auth_callback>,
81        ) -> c_int;
82        pub fn sqlite3_bcvfs_attach(
83            fs: *mut sqlite3_bcvfs,
84            z_storage: *const c_char,
85            z_account: *const c_char,
86            z_container: *const c_char,
87            z_alias: *const c_char,
88            flags: c_int,
89            pz_err: *mut *mut c_char,
90        ) -> c_int;
91        pub fn sqlite3_bcvfs_detach(
92            fs: *mut sqlite3_bcvfs,
93            z_alias: *const c_char,
94            pz_err: *mut *mut c_char,
95        ) -> c_int;
96        pub fn sqlite3_bcvfs_poll(
97            fs: *mut sqlite3_bcvfs,
98            z_container: *const c_char,
99            pz_err: *mut *mut c_char,
100        ) -> c_int;
101        pub fn sqlite3_bcvfs_upload(
102            fs: *mut sqlite3_bcvfs,
103            z_container: *const c_char,
104            busy: Option<sqlite3_bcvfs_busy_callback>,
105            busy_ctx: *mut c_void,
106            pz_err: *mut *mut c_char,
107        ) -> c_int;
108    }
109}
110
111/// Raw Cloud Backed SQLite database-management interface.
112#[cfg(feature = "blockcachevfs")]
113pub mod bcvutil {
114    use core::ffi::{c_char, c_int};
115
116    /// Opaque Cloud Backed SQLite database-management handle.
117    #[repr(C)]
118    pub struct sqlite3_bcv {
119        _private: [u8; 0],
120    }
121
122    unsafe extern "C" {
123        pub fn sqlite3_bcv_open(
124            z_module: *const c_char,
125            z_user: *const c_char,
126            z_auth: *const c_char,
127            z_container: *const c_char,
128            pp_out: *mut *mut sqlite3_bcv,
129        ) -> c_int;
130        pub fn sqlite3_bcv_close(handle: *mut sqlite3_bcv);
131        pub fn sqlite3_bcv_errmsg(handle: *mut sqlite3_bcv) -> *const c_char;
132        pub fn sqlite3_bcv_upload(
133            handle: *mut sqlite3_bcv,
134            z_local: *const c_char,
135            z_remote: *const c_char,
136        ) -> c_int;
137        pub fn sqlite3_bcv_create_if_not_exists(
138            handle: *mut sqlite3_bcv,
139            sz_name: c_int,
140            sz_block: c_int,
141        ) -> c_int;
142    }
143}
144
145#[cfg(all(feature = "remote", not(target_arch = "wasm32")))]
146mod remote {
147    use core::ffi::{c_char, c_int, c_long};
148
149    #[repr(C)]
150    pub struct DoltliteServer {
151        _private: [u8; 0],
152    }
153
154    #[repr(C)]
155    pub struct DoltliteServeOpts {
156        pub zDir: *const c_char,
157        pub port: c_int,
158        pub zBindAddr: *const c_char,
159        pub certFile: *const c_char,
160        pub keyFile: *const c_char,
161        pub authKeysDir: *const c_char,
162        pub audience: *const c_char,
163        pub timeoutMs: c_int,
164    }
165
166    unsafe extern "C" {
167        pub fn doltliteServe(
168            directory: *const c_char,
169            port: c_int,
170            bind_address: *const c_char,
171        ) -> c_int;
172
173        pub fn doltliteServeAsync(
174            directory: *const c_char,
175            port: c_int,
176            bind_address: *const c_char,
177        ) -> *mut DoltliteServer;
178
179        pub fn doltliteServeOpts(options: *const DoltliteServeOpts) -> c_int;
180
181        pub fn doltliteServeAsyncOpts(options: *const DoltliteServeOpts) -> *mut DoltliteServer;
182
183        pub fn doltliteServerStop(server: *mut DoltliteServer);
184
185        pub fn doltliteServerPort(server: *mut DoltliteServer) -> c_int;
186
187        pub fn doltliteCredsVerifyBearer(
188            authorization: *const c_char,
189            expected_audience: *const c_char,
190            authorized_keys_directory: *const c_char,
191            now: c_long,
192            key_id: *mut *mut c_char,
193        ) -> c_int;
194    }
195}
196
197#[cfg(all(feature = "remote", not(target_arch = "wasm32")))]
198pub use remote::*;
199
200#[cfg(not(feature = "loadable_extension"))]
201unsafe extern "C" {
202    fn doltliteInstallAutoExt() -> core::ffi::c_int;
203}
204
205#[cfg(not(feature = "loadable_extension"))]
206static DOLTLITE_INIT_RESULT: AtomicI32 = AtomicI32::new(i32::MIN);
207
208#[cfg(feature = "loadable_extension")]
209pub fn initialize_doltlite() -> core::ffi::c_int {
210    // A loadable extension runs inside an already-initialized host process;
211    // do not load DoltLite's process-wide auto-extension a second time.
212    SQLITE_OK
213}
214
215#[cfg(not(feature = "loadable_extension"))]
216pub fn initialize_doltlite() -> core::ffi::c_int {
217    let existing = DOLTLITE_INIT_RESULT.load(Ordering::Acquire);
218    if existing != i32::MIN {
219        return existing;
220    }
221
222    let result = unsafe { doltliteInstallAutoExt() };
223    match DOLTLITE_INIT_RESULT.compare_exchange(
224        i32::MIN,
225        result,
226        Ordering::AcqRel,
227        Ordering::Acquire,
228    ) {
229        Ok(_) => result,
230        Err(previous) => previous,
231    }
232}
233
234impl Default for sqlite3_vtab {
235    fn default() -> Self {
236        unsafe { mem::zeroed() }
237    }
238}
239
240impl Default for sqlite3_vtab_cursor {
241    fn default() -> Self {
242        unsafe { mem::zeroed() }
243    }
244}