day-sqlite-worker 0.4.1

Typed SQLite persistence and browser storage for Day apps
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
// Copyright © The Daybrite Project
// SPDX-License-Identifier: MPL-2.0

//! The two VFSes behind the engine (docs/persistence.md).
//!
//! **day-mem** is the default: anonymous in-RAM files. `:memory:` databases never open a file
//! at all, but SQLite can still ask the default VFS for spill files in corner cases, and the
//! main-thread app instance must never touch OPFS, so the default answers everything from a
//! crate-private RAM store on every target.
//!
//! **day-opfs** is the worker's VFS: every file operation goes through the ten synchronous
//! primitives in [`fsx`]. On wasm those are `day_sql_fs_*` imports the day-sql worker page
//! implements over pre-opened OPFS sync access handles; on native they are an in-memory fake
//! with the same slot/refcount semantics, which is what lets `cargo test` drive real SQLite
//! through this exact VFS with no browser. Locking is a no-op: one connection per database
//! per worker, and the access handles themselves are origin-exclusive.

use core::ffi::{c_char, c_int, c_void};
use std::cell::RefCell;
use std::collections::HashMap;

use crate::bindings as b;
use crate::os;

/// The synchronous file primitives. One implementation talks to the worker page's OPFS pool,
/// the other to a native fake; the VFS above and the pool verbs in lib.rs see no difference.
pub(crate) mod fsx {
    #[cfg(all(target_family = "wasm", target_os = "unknown"))]
    mod imp {
        // Implemented by web/day-sql-worker.js over FileSystemSyncAccessHandle. The main app
        // instance links these too (same wasm module) but never calls them; web/day-sql.js
        // gives it stubs that refuse.
        #[link(wasm_import_module = "env")]
        unsafe extern "C" {
            fn day_sql_fs_open(name: *const u8, name_len: usize, create: i32) -> i32;
            fn day_sql_fs_read(slot: u32, off: f64, buf: *mut u8, len: usize) -> i32;
            fn day_sql_fs_write(slot: u32, off: f64, buf: *const u8, len: usize) -> i32;
            fn day_sql_fs_truncate(slot: u32, size: f64) -> i32;
            fn day_sql_fs_size(slot: u32) -> f64;
            fn day_sql_fs_flush(slot: u32) -> i32;
            fn day_sql_fs_close(slot: u32, delete: i32);
            fn day_sql_fs_delete(name: *const u8, name_len: usize) -> i32;
            fn day_sql_fs_exists(name: *const u8, name_len: usize) -> i32;
            fn day_sql_fs_list(buf: *mut u8, cap: usize) -> i32;
        }

        pub fn open(name: &str, create: bool) -> Option<u32> {
            let slot = unsafe { day_sql_fs_open(name.as_ptr(), name.len(), i32::from(create)) };
            u32::try_from(slot).ok()
        }
        pub fn read(slot: u32, off: u64, buf: &mut [u8]) -> Option<usize> {
            let n = unsafe { day_sql_fs_read(slot, off as f64, buf.as_mut_ptr(), buf.len()) };
            usize::try_from(n).ok()
        }
        pub fn write(slot: u32, off: u64, buf: &[u8]) -> bool {
            unsafe { day_sql_fs_write(slot, off as f64, buf.as_ptr(), buf.len()) == 0 }
        }
        pub fn truncate(slot: u32, size: u64) -> bool {
            unsafe { day_sql_fs_truncate(slot, size as f64) == 0 }
        }
        pub fn size(slot: u32) -> Option<u64> {
            let s = unsafe { day_sql_fs_size(slot) };
            if s < 0.0 {
                None
            } else {
                Some(s as u64)
            }
        }
        pub fn flush(slot: u32) -> bool {
            unsafe { day_sql_fs_flush(slot) == 0 }
        }
        pub fn close(slot: u32, delete: bool) {
            unsafe { day_sql_fs_close(slot, i32::from(delete)) }
        }
        pub fn delete(name: &str) -> bool {
            unsafe { day_sql_fs_delete(name.as_ptr(), name.len()) == 0 }
        }
        pub fn exists(name: &str) -> bool {
            unsafe { day_sql_fs_exists(name.as_ptr(), name.len()) == 1 }
        }
        pub fn list() -> Vec<String> {
            let needed = unsafe { day_sql_fs_list(core::ptr::null_mut(), 0) };
            let Ok(needed) = usize::try_from(needed) else {
                return Vec::new();
            };
            if needed == 0 {
                return Vec::new();
            }
            let mut buf = vec![0u8; needed];
            let got = unsafe { day_sql_fs_list(buf.as_mut_ptr(), buf.len()) };
            if usize::try_from(got) != Ok(needed) {
                return Vec::new();
            }
            String::from_utf8_lossy(&buf)
                .split('\u{1f}')
                .filter(|s| !s.is_empty())
                .map(str::to_owned)
                .collect()
        }
    }

    #[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
    mod imp {
        // The native fake: same names, same slot/refcount behavior, one HashMap of byte
        // vectors per thread. Tests drive the real engine through it.
        use std::cell::RefCell;
        use std::collections::HashMap;
        use std::rc::Rc;

        struct Slot {
            name: Option<String>,
            data: Rc<RefCell<Vec<u8>>>,
            refs: u32,
        }

        thread_local! {
            static FILES: RefCell<HashMap<String, Rc<RefCell<Vec<u8>>>>> =
                RefCell::new(HashMap::new());
            static SLOTS: RefCell<Vec<Option<Slot>>> = const { RefCell::new(Vec::new()) };
        }

        fn with_slot<R>(slot: u32, f: impl FnOnce(&mut Slot) -> R) -> Option<R> {
            SLOTS.with(|s| s.borrow_mut().get_mut(slot as usize)?.as_mut().map(f))
        }

        pub fn open(name: &str, create: bool) -> Option<u32> {
            // An already-open name shares its slot (refcounted), matching the exclusive OPFS
            // access handles the worker page holds.
            let existing = SLOTS.with(|s| {
                s.borrow().iter().position(|sl| {
                    sl.as_ref()
                        .is_some_and(|sl| sl.name.as_deref() == Some(name) && !name.is_empty())
                })
            });
            if let Some(i) = existing {
                with_slot(i as u32, |sl| sl.refs += 1);
                return Some(i as u32);
            }
            let data = if name.is_empty() {
                Rc::new(RefCell::new(Vec::new()))
            } else {
                let known = FILES.with(|f| f.borrow().get(name).cloned());
                match known {
                    Some(d) => d,
                    None if create => {
                        let d = Rc::new(RefCell::new(Vec::new()));
                        FILES.with(|f| f.borrow_mut().insert(name.to_string(), d.clone()));
                        d
                    }
                    None => return None,
                }
            };
            let slot = Slot {
                name: (!name.is_empty()).then(|| name.to_string()),
                data,
                refs: 1,
            };
            SLOTS.with(|s| {
                let mut s = s.borrow_mut();
                if let Some(i) = s.iter().position(Option::is_none) {
                    s[i] = Some(slot);
                    Some(i as u32)
                } else {
                    s.push(Some(slot));
                    Some(s.len() as u32 - 1)
                }
            })
        }
        pub fn read(slot: u32, off: u64, buf: &mut [u8]) -> Option<usize> {
            with_slot(slot, |sl| {
                let data = sl.data.borrow();
                let off = off as usize;
                if off >= data.len() {
                    return 0;
                }
                let n = buf.len().min(data.len() - off);
                buf[..n].copy_from_slice(&data[off..off + n]);
                n
            })
        }
        pub fn write(slot: u32, off: u64, buf: &[u8]) -> bool {
            with_slot(slot, |sl| {
                let mut data = sl.data.borrow_mut();
                let end = off as usize + buf.len();
                if data.len() < end {
                    data.resize(end, 0);
                }
                data[off as usize..end].copy_from_slice(buf);
            })
            .is_some()
        }
        pub fn truncate(slot: u32, size: u64) -> bool {
            with_slot(slot, |sl| {
                let mut data = sl.data.borrow_mut();
                let size = size as usize;
                if data.len() > size {
                    data.truncate(size);
                } else {
                    data.resize(size, 0);
                }
            })
            .is_some()
        }
        pub fn size(slot: u32) -> Option<u64> {
            with_slot(slot, |sl| sl.data.borrow().len() as u64)
        }
        pub fn flush(_slot: u32) -> bool {
            true
        }
        pub fn close(slot: u32, delete: bool) {
            let name = SLOTS.with(|s| {
                let mut s = s.borrow_mut();
                let entry = s.get_mut(slot as usize)?.as_mut()?;
                entry.refs -= 1;
                if entry.refs > 0 {
                    return None;
                }
                let name = entry.name.take();
                s[slot as usize] = None;
                Some(name)
            });
            if delete {
                if let Some(Some(name)) = name {
                    FILES.with(|f| f.borrow_mut().remove(&name));
                }
            }
        }
        pub fn delete(name: &str) -> bool {
            FILES.with(|f| f.borrow_mut().remove(name)).is_some()
        }
        pub fn exists(name: &str) -> bool {
            FILES.with(|f| f.borrow().contains_key(name))
        }
        pub fn list() -> Vec<String> {
            FILES.with(|f| {
                let mut names: Vec<String> = f.borrow().keys().cloned().collect();
                names.sort();
                names
            })
        }
    }

    pub(crate) use imp::*;

    /// Whole-file read, the Export verb. Shares the open handle if the database is open.
    pub(crate) fn read_all(name: &str) -> Option<Vec<u8>> {
        let slot = open(name, false)?;
        let result = size(slot).and_then(|len| {
            let mut buf = vec![0u8; len as usize];
            (read(slot, 0, &mut buf) == Some(len as usize)).then_some(buf)
        });
        close(slot, false);
        result
    }

    /// Whole-file replace, the Import verb. A connection still open on `name` is invalid
    /// afterwards; the driver closes before importing.
    pub(crate) fn write_all(name: &str, bytes: &[u8]) -> bool {
        let Some(slot) = open(name, true) else {
            return false;
        };
        let ok = truncate(slot, 0) && write(slot, 0, bytes) && flush(slot);
        close(slot, false);
        ok
    }
}

// -------------------------------------------------------------------------------------------
// The RAM store behind day-mem
// -------------------------------------------------------------------------------------------

thread_local! {
    /// day-mem's named files (anonymous files live only in their DayFile). Nothing persistent
    /// routes here (the proxy driver sends every real file to the worker), so this is spill
    /// space, not storage.
    static RAM: RefCell<HashMap<String, Vec<u8>>> = RefCell::new(HashMap::new());
}

// -------------------------------------------------------------------------------------------
// File objects
// -------------------------------------------------------------------------------------------

/// What day-opfs file handles carry after `base`.
#[repr(C)]
struct DayFile {
    base: b::sqlite3_file,
    slot: u32,
    delete_on_close: bool,
}

/// What day-mem file handles carry: the whole file inline, plus its RAM key if named.
#[repr(C)]
struct MemFile {
    base: b::sqlite3_file,
    data: *mut Vec<u8>,
    name: *mut Option<String>,
    delete_on_close: bool,
}

const SHORT_READ_FILL: u8 = 0;

unsafe fn cstr<'a>(p: *const c_char) -> &'a str {
    if p.is_null() {
        ""
    } else {
        core::ffi::CStr::from_ptr(p).to_str().unwrap_or("")
    }
}

// --- day-opfs io methods -------------------------------------------------------------------

unsafe extern "C" fn opfs_close(f: *mut b::sqlite3_file) -> c_int {
    let f = f.cast::<DayFile>();
    fsx::close((*f).slot, (*f).delete_on_close);
    b::SQLITE_OK
}

unsafe extern "C" fn opfs_read(
    f: *mut b::sqlite3_file,
    buf: *mut c_void,
    amt: c_int,
    ofst: b::sqlite3_int64,
) -> c_int {
    let f = f.cast::<DayFile>();
    let out = core::slice::from_raw_parts_mut(buf.cast::<u8>(), amt as usize);
    match fsx::read((*f).slot, ofst as u64, out) {
        Some(n) if n == amt as usize => b::SQLITE_OK,
        Some(n) => {
            out[n..].fill(SHORT_READ_FILL);
            b::SQLITE_IOERR_SHORT_READ
        }
        None => b::SQLITE_IOERR_READ,
    }
}

unsafe extern "C" fn opfs_write(
    f: *mut b::sqlite3_file,
    buf: *const c_void,
    amt: c_int,
    ofst: b::sqlite3_int64,
) -> c_int {
    let f = f.cast::<DayFile>();
    let data = core::slice::from_raw_parts(buf.cast::<u8>(), amt as usize);
    if fsx::write((*f).slot, ofst as u64, data) {
        b::SQLITE_OK
    } else {
        b::SQLITE_IOERR_WRITE
    }
}

unsafe extern "C" fn opfs_truncate(f: *mut b::sqlite3_file, size: b::sqlite3_int64) -> c_int {
    let f = f.cast::<DayFile>();
    if fsx::truncate((*f).slot, size as u64) {
        b::SQLITE_OK
    } else {
        b::SQLITE_IOERR_TRUNCATE
    }
}

unsafe extern "C" fn opfs_sync(f: *mut b::sqlite3_file, _flags: c_int) -> c_int {
    let f = f.cast::<DayFile>();
    if fsx::flush((*f).slot) {
        b::SQLITE_OK
    } else {
        b::SQLITE_IOERR_FSYNC
    }
}

unsafe extern "C" fn opfs_file_size(f: *mut b::sqlite3_file, out: *mut b::sqlite3_int64) -> c_int {
    let f = f.cast::<DayFile>();
    match fsx::size((*f).slot) {
        Some(s) => {
            *out = s as b::sqlite3_int64;
            b::SQLITE_OK
        }
        None => b::SQLITE_IOERR_FSTAT,
    }
}

// --- shared trivial io methods -------------------------------------------------------------

unsafe extern "C" fn io_lock(_f: *mut b::sqlite3_file, _level: c_int) -> c_int {
    // One connection per database per context; the OPFS access handle is the real lock.
    b::SQLITE_OK
}
unsafe extern "C" fn io_unlock(_f: *mut b::sqlite3_file, _level: c_int) -> c_int {
    b::SQLITE_OK
}
unsafe extern "C" fn io_check_reserved(_f: *mut b::sqlite3_file, out: *mut c_int) -> c_int {
    *out = 0;
    b::SQLITE_OK
}
unsafe extern "C" fn io_file_control(
    _f: *mut b::sqlite3_file,
    _op: c_int,
    _arg: *mut c_void,
) -> c_int {
    b::SQLITE_NOTFOUND
}
unsafe extern "C" fn io_sector_size(_f: *mut b::sqlite3_file) -> c_int {
    4096
}
unsafe extern "C" fn io_device_characteristics(_f: *mut b::sqlite3_file) -> c_int {
    0
}

// --- day-mem io methods --------------------------------------------------------------------

unsafe extern "C" fn mem_close(f: *mut b::sqlite3_file) -> c_int {
    let f = f.cast::<MemFile>();
    let data = Box::from_raw((*f).data);
    let name = Box::from_raw((*f).name);
    match (*name, (*f).delete_on_close) {
        (Some(n), true) => {
            RAM.with(|r| r.borrow_mut().remove(&n));
        }
        (Some(n), false) => {
            RAM.with(|r| r.borrow_mut().insert(n, *data));
        }
        (None, _) => {}
    }
    b::SQLITE_OK
}

unsafe extern "C" fn mem_read(
    f: *mut b::sqlite3_file,
    buf: *mut c_void,
    amt: c_int,
    ofst: b::sqlite3_int64,
) -> c_int {
    let data = &*(*f.cast::<MemFile>()).data;
    let out = core::slice::from_raw_parts_mut(buf.cast::<u8>(), amt as usize);
    let off = ofst as usize;
    let n = if off >= data.len() {
        0
    } else {
        out.len().min(data.len() - off)
    };
    out[..n].copy_from_slice(&data[off..off + n]);
    if n == out.len() {
        b::SQLITE_OK
    } else {
        out[n..].fill(SHORT_READ_FILL);
        b::SQLITE_IOERR_SHORT_READ
    }
}

unsafe extern "C" fn mem_write(
    f: *mut b::sqlite3_file,
    buf: *const c_void,
    amt: c_int,
    ofst: b::sqlite3_int64,
) -> c_int {
    let data = &mut *(*f.cast::<MemFile>()).data;
    let src = core::slice::from_raw_parts(buf.cast::<u8>(), amt as usize);
    let end = ofst as usize + src.len();
    if data.len() < end {
        data.resize(end, 0);
    }
    data[ofst as usize..end].copy_from_slice(src);
    b::SQLITE_OK
}

unsafe extern "C" fn mem_truncate(f: *mut b::sqlite3_file, size: b::sqlite3_int64) -> c_int {
    let data = &mut *(*f.cast::<MemFile>()).data;
    data.truncate(size as usize);
    b::SQLITE_OK
}

unsafe extern "C" fn mem_sync(_f: *mut b::sqlite3_file, _flags: c_int) -> c_int {
    b::SQLITE_OK
}

unsafe extern "C" fn mem_file_size(f: *mut b::sqlite3_file, out: *mut b::sqlite3_int64) -> c_int {
    *out = (*(*f.cast::<MemFile>()).data).len() as b::sqlite3_int64;
    b::SQLITE_OK
}

// -------------------------------------------------------------------------------------------
// VFS methods
// -------------------------------------------------------------------------------------------

static OPFS_IO: b::sqlite3_io_methods = b::sqlite3_io_methods {
    iVersion: 1,
    xClose: Some(opfs_close),
    xRead: Some(opfs_read),
    xWrite: Some(opfs_write),
    xTruncate: Some(opfs_truncate),
    xSync: Some(opfs_sync),
    xFileSize: Some(opfs_file_size),
    xLock: Some(io_lock),
    xUnlock: Some(io_unlock),
    xCheckReservedLock: Some(io_check_reserved),
    xFileControl: Some(io_file_control),
    xSectorSize: Some(io_sector_size),
    xDeviceCharacteristics: Some(io_device_characteristics),
    xShmMap: None,
    xShmLock: None,
    xShmBarrier: None,
    xShmUnmap: None,
    xFetch: None,
    xUnfetch: None,
};

static MEM_IO: b::sqlite3_io_methods = b::sqlite3_io_methods {
    iVersion: 1,
    xClose: Some(mem_close),
    xRead: Some(mem_read),
    xWrite: Some(mem_write),
    xTruncate: Some(mem_truncate),
    xSync: Some(mem_sync),
    xFileSize: Some(mem_file_size),
    xLock: Some(io_lock),
    xUnlock: Some(io_unlock),
    xCheckReservedLock: Some(io_check_reserved),
    xFileControl: Some(io_file_control),
    xSectorSize: Some(io_sector_size),
    xDeviceCharacteristics: Some(io_device_characteristics),
    xShmMap: None,
    xShmLock: None,
    xShmBarrier: None,
    xShmUnmap: None,
    xFetch: None,
    xUnfetch: None,
};

unsafe extern "C" fn opfs_open(
    _vfs: *mut b::sqlite3_vfs,
    name: b::sqlite3_filename,
    file: *mut b::sqlite3_file,
    flags: c_int,
    out_flags: *mut c_int,
) -> c_int {
    let f = file.cast::<DayFile>();
    (*f).base.pMethods = core::ptr::null();
    let create = flags & b::SQLITE_OPEN_CREATE != 0;
    let Some(slot) = fsx::open(cstr(name), create) else {
        return b::SQLITE_CANTOPEN;
    };
    (*f).slot = slot;
    (*f).delete_on_close = flags & b::SQLITE_OPEN_DELETEONCLOSE != 0;
    (*f).base.pMethods = &raw const OPFS_IO;
    if !out_flags.is_null() {
        *out_flags = flags;
    }
    b::SQLITE_OK
}

unsafe extern "C" fn mem_open(
    _vfs: *mut b::sqlite3_vfs,
    name: b::sqlite3_filename,
    file: *mut b::sqlite3_file,
    flags: c_int,
    out_flags: *mut c_int,
) -> c_int {
    let f = file.cast::<MemFile>();
    (*f).base.pMethods = core::ptr::null();
    let name = cstr(name);
    let (data, key) = if name.is_empty() {
        (Vec::new(), None)
    } else {
        let known = RAM.with(|r| r.borrow().get(name).cloned());
        match known {
            Some(d) => (d, Some(name.to_string())),
            None if flags & b::SQLITE_OPEN_CREATE != 0 => (Vec::new(), Some(name.to_string())),
            None => return b::SQLITE_CANTOPEN,
        }
    };
    (*f).data = Box::into_raw(Box::new(data));
    (*f).name = Box::into_raw(Box::new(key));
    (*f).delete_on_close = flags & b::SQLITE_OPEN_DELETEONCLOSE != 0;
    (*f).base.pMethods = &raw const MEM_IO;
    if !out_flags.is_null() {
        *out_flags = flags;
    }
    b::SQLITE_OK
}

unsafe extern "C" fn opfs_delete(
    _vfs: *mut b::sqlite3_vfs,
    name: *const c_char,
    _sync_dir: c_int,
) -> c_int {
    fsx::delete(cstr(name));
    b::SQLITE_OK
}

unsafe extern "C" fn mem_delete(
    _vfs: *mut b::sqlite3_vfs,
    name: *const c_char,
    _sync_dir: c_int,
) -> c_int {
    RAM.with(|r| r.borrow_mut().remove(cstr(name)));
    b::SQLITE_OK
}

unsafe extern "C" fn opfs_access(
    _vfs: *mut b::sqlite3_vfs,
    name: *const c_char,
    _flags: c_int,
    out: *mut c_int,
) -> c_int {
    *out = c_int::from(fsx::exists(cstr(name)));
    b::SQLITE_OK
}

unsafe extern "C" fn mem_access(
    _vfs: *mut b::sqlite3_vfs,
    name: *const c_char,
    _flags: c_int,
    out: *mut c_int,
) -> c_int {
    *out = c_int::from(RAM.with(|r| r.borrow().contains_key(cstr(name))));
    b::SQLITE_OK
}

unsafe extern "C" fn vfs_full_pathname(
    _vfs: *mut b::sqlite3_vfs,
    name: *const c_char,
    n_out: c_int,
    out: *mut c_char,
) -> c_int {
    let src = cstr(name).as_bytes();
    if src.len() + 1 > n_out as usize {
        return b::SQLITE_CANTOPEN;
    }
    core::ptr::copy_nonoverlapping(src.as_ptr().cast::<c_char>(), out, src.len());
    *out.add(src.len()) = 0;
    b::SQLITE_OK
}

unsafe extern "C" fn vfs_randomness(
    _vfs: *mut b::sqlite3_vfs,
    n: c_int,
    out: *mut c_char,
) -> c_int {
    let buf = core::slice::from_raw_parts_mut(out.cast::<u8>(), n as usize);
    os::entropy(buf);
    n
}

unsafe extern "C" fn vfs_sleep(_vfs: *mut b::sqlite3_vfs, _micros: c_int) -> c_int {
    // Single-threaded: nothing to wait for, nothing to yield to.
    0
}

unsafe extern "C" fn vfs_current_time(_vfs: *mut b::sqlite3_vfs, out: *mut f64) -> c_int {
    *out = 2_440_587.5 + os::now_ms() / 86_400_000.0;
    b::SQLITE_OK
}

unsafe extern "C" fn vfs_current_time_i64(
    _vfs: *mut b::sqlite3_vfs,
    out: *mut b::sqlite3_int64,
) -> c_int {
    // Julian-day epoch in milliseconds + Unix milliseconds, SQLite's convention.
    *out = 210_866_760_000_000 + os::now_ms() as b::sqlite3_int64;
    b::SQLITE_OK
}

unsafe extern "C" fn vfs_get_last_error(
    _vfs: *mut b::sqlite3_vfs,
    _n: c_int,
    _out: *mut c_char,
) -> c_int {
    b::SQLITE_OK
}

fn make_vfs(name: &'static core::ffi::CStr, sz_os_file: c_int, mem: bool) -> b::sqlite3_vfs {
    b::sqlite3_vfs {
        iVersion: 2,
        szOsFile: sz_os_file,
        mxPathname: 512,
        pNext: core::ptr::null_mut(),
        zName: name.as_ptr(),
        pAppData: core::ptr::null_mut(),
        xOpen: Some(if mem { mem_open } else { opfs_open }),
        xDelete: Some(if mem { mem_delete } else { opfs_delete }),
        xAccess: Some(if mem { mem_access } else { opfs_access }),
        xFullPathname: Some(vfs_full_pathname),
        xDlOpen: None,
        xDlError: None,
        xDlSym: None,
        xDlClose: None,
        xRandomness: Some(vfs_randomness),
        xSleep: Some(vfs_sleep),
        xCurrentTime: Some(vfs_current_time),
        xGetLastError: Some(vfs_get_last_error),
        xCurrentTimeInt64: Some(vfs_current_time_i64),
        xSetSystemCall: None,
        xGetSystemCall: None,
        xNextSystemCall: None,
    }
}

/// The name day-opfs registers under; connections opt in via `sqlite3_open_v2`'s zVfs.
pub(crate) const OPFS_VFS: &core::ffi::CStr = c"day-opfs";

/// Register day-mem (default) and day-opfs; this is `sqlite3_os_init`'s body.
pub(crate) fn register_all() -> c_int {
    let mem = Box::leak(Box::new(make_vfs(
        c"day-mem",
        core::mem::size_of::<MemFile>() as c_int,
        true,
    )));
    let opfs = Box::leak(Box::new(make_vfs(
        OPFS_VFS,
        core::mem::size_of::<DayFile>() as c_int,
        false,
    )));
    let rc = unsafe { b::sqlite3_vfs_register(mem, 1) };
    if rc != b::SQLITE_OK {
        return rc;
    }
    unsafe { b::sqlite3_vfs_register(opfs, 0) }
}