Skip to content

Commit 3ba4cfb

Browse files
committed
Invent a random name if the zName to xOpen is null
1 parent 61d0063 commit 3ba4cfb

4 files changed

Lines changed: 29 additions & 21 deletions

File tree

sqlite-wasm-rs/src/shim/vfs/memory.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
//! Memory VFS, used as the default VFS
22
3-
use std::{collections::HashMap, ffi::CStr, sync::Arc};
4-
3+
use crate::shim::libsqlite3::*;
4+
use crate::shim::vfs::utils::get_random_name;
55
use js_sys::{Date, Math};
66
use once_cell::sync::Lazy;
7-
8-
use crate::export::*;
97
use parking_lot::{Mutex, MutexGuard, RwLock};
8+
use std::{collections::HashMap, ffi::CStr, sync::Arc};
109

1110
/// thread::sleep is available when atomics are enabled
1211
#[cfg(target_feature = "atomics")]
@@ -94,23 +93,28 @@ unsafe extern "C" fn xOpen(
9493
flags: ::std::os::raw::c_int,
9594
pOutFlags: *mut ::std::os::raw::c_int,
9695
) -> ::std::os::raw::c_int {
97-
let Ok(s) = CStr::from_ptr(zName).to_str() else {
98-
return SQLITE_ERROR;
96+
let name = if zName.is_null() {
97+
get_random_name()
98+
} else {
99+
let Ok(s) = CStr::from_ptr(zName).to_str() else {
100+
return SQLITE_ERROR;
101+
};
102+
s.into()
99103
};
100104

101105
let mut name2file = name2file();
102-
let mem_file = if let Some(mem_file) = name2file.get(s) {
106+
let mem_file = if let Some(mem_file) = name2file.get(&name) {
103107
Arc::clone(mem_file)
104108
} else {
105109
if flags & SQLITE_OPEN_CREATE == 0 {
106110
return SQLITE_CANTOPEN;
107111
}
108112
let file = Arc::new(RwLock::new(MemFile {
109-
name: s.into(),
113+
name: name.clone(),
110114
flags,
111115
data: Vec::new(),
112116
}));
113-
name2file.insert(s.into(), Arc::clone(&file));
117+
name2file.insert(name, Arc::clone(&file));
114118
file
115119
};
116120

sqlite-wasm-rs/src/shim/vfs/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
pub mod memory;
44
pub mod sahpool;
5+
pub mod utils;

sqlite-wasm-rs/src/shim/vfs/sahpool.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
//!
33
//! <https://github.com/sqlite/sqlite/blob/master/ext/wasm/api/sqlite3-vfs-opfs-sahpool.c-pp.js>
44
5-
use crate::export::*;
6-
75
use crate::fragile::FragileComfirmed;
8-
use js_sys::{
9-
Array, DataView, IteratorNext, Map, Math, Number, Object, Reflect, Set, Uint32Array, Uint8Array,
10-
};
6+
use crate::shim::libsqlite3::*;
7+
use crate::shim::vfs::utils::get_random_name;
8+
use js_sys::{Array, DataView, IteratorNext, Map, Object, Reflect, Set, Uint32Array, Uint8Array};
119
use once_cell::sync::Lazy;
1210
use parking_lot::{Mutex, RwLock};
1311
use std::ffi::CString;
@@ -70,11 +68,6 @@ fn compute_digest(_byte_array: &Uint8Array) -> Uint32Array {
7068
u32_array
7169
}
7270

73-
fn get_random_name() -> String {
74-
let random = Number::from(Math::random()).to_string(36).unwrap();
75-
random.slice(2, random.length()).as_string().unwrap()
76-
}
77-
7871
#[repr(C)]
7972
struct OpfsFile {
8073
io_methods: sqlite3_file,
@@ -841,7 +834,6 @@ unsafe extern "C" fn xFullPathname(
841834
if len > nOut as usize {
842835
return SQLITE_CANTOPEN;
843836
}
844-
845837
zName.copy_to(zOut, len);
846838

847839
SQLITE_OK
@@ -880,7 +872,11 @@ unsafe extern "C" fn xOpen(
880872
let pool = pool(pVfs);
881873

882874
let f = || {
883-
let name = pool.get_path(zName)?;
875+
let name = if zName.is_null() {
876+
get_random_name()
877+
} else {
878+
pool.get_path(zName)?
879+
};
884880
let sah = match pool.get_sah_for_path(&name) {
885881
Some(sah) => sah,
886882
None => {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use js_sys::{Math, Number};
2+
3+
/// get random name if zFileName is null and other cases
4+
pub fn get_random_name() -> String {
5+
let random = Number::from(Math::random()).to_string(36).unwrap();
6+
random.slice(2, random.length()).as_string().unwrap()
7+
}

0 commit comments

Comments
 (0)