Skip to content

Commit bd29680

Browse files
authored
Update registered_vfs function (#114)
* Update `registered_vfs` function * Add document about vfs has been registered
1 parent b83796c commit bd29680

3 files changed

Lines changed: 33 additions & 50 deletions

File tree

src/vfs/relaxed_idb.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! relaxed-idb vfs implementation
2+
//!
23
//! **The `relaxed-idb` feature is required, and it is not recommended to use in a production environment.**
34
//!
45
//! ```rust
@@ -900,26 +901,27 @@ impl RelaxedIdbUtil {
900901

901902
/// Register `relaxed-idb` vfs and return a utility object which can be used
902903
/// to perform basic administration of the file pool
904+
///
905+
/// If the vfs corresponding to `options.vfs_name` has been registered,
906+
/// only return a utility object.
903907
pub async fn install(options: &RelaxedIdbCfg, default_vfs: bool) -> Result<RelaxedIdbUtil> {
904908
static REGISTER_GUARD: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
905909
let _guard = REGISTER_GUARD.lock().await;
906910

907-
let pool = match registered_vfs(&options.vfs_name) {
908-
Ok(vfs) => unsafe { RelaxedIdbStore::app_data(vfs) },
909-
Err(RegisterVfsError::VfsNotRegistered) => {
910-
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
911-
let pool = RelaxedIdb::new(options, tx).await?;
912-
let vfs = register_vfs::<RelaxedIdbIoMethods, RelaxedIdbVfs>(
913-
&options.vfs_name,
914-
pool,
915-
default_vfs,
916-
)?;
917-
918-
let app_data = unsafe { RelaxedIdbStore::app_data(vfs) };
919-
wasm_bindgen_futures::spawn_local(app_data.commit_loop(rx));
920-
app_data
921-
}
922-
Err(vfs_error) => return Err(RelaxedIdbError::Vfs(vfs_error)),
911+
let pool = if let Some(vfs) = registered_vfs(&options.vfs_name)? {
912+
unsafe { RelaxedIdbStore::app_data(vfs) }
913+
} else {
914+
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
915+
let pool = RelaxedIdb::new(options, tx).await?;
916+
let vfs = register_vfs::<RelaxedIdbIoMethods, RelaxedIdbVfs>(
917+
&options.vfs_name,
918+
pool,
919+
default_vfs,
920+
)?;
921+
922+
let app_data = unsafe { RelaxedIdbStore::app_data(vfs) };
923+
wasm_bindgen_futures::spawn_local(app_data.commit_loop(rx));
924+
app_data
923925
};
924926

925927
Ok(RelaxedIdbUtil { pool })

src/vfs/sahpool.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -790,21 +790,23 @@ impl OpfsSAHPoolUtil {
790790

791791
/// Register `opfs-sahpool` vfs and return a utility object which can be used
792792
/// to perform basic administration of the file pool
793+
///
794+
/// If the vfs corresponding to `options.vfs_name` has been registered,
795+
/// only return a utility object.
793796
pub async fn install(options: &OpfsSAHPoolCfg, default_vfs: bool) -> Result<OpfsSAHPoolUtil> {
794797
static REGISTER_GUARD: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
795798
let _guard = REGISTER_GUARD.lock().await;
796799

797-
let vfs = match registered_vfs(&options.vfs_name) {
798-
Ok(vfs) => vfs,
799-
Err(RegisterVfsError::VfsNotRegistered) => {
800-
register_vfs::<SyncAccessHandleIoMethods, SyncAccessHandleVfs>(
801-
&options.vfs_name,
802-
OpfsSAHPool::new(options).await?,
803-
default_vfs,
804-
)?
805-
}
806-
Err(vfs_error) => return Err(OpfsSAHError::Vfs(vfs_error)),
800+
let vfs = if let Some(vfs) = registered_vfs(&options.vfs_name)? {
801+
vfs
802+
} else {
803+
register_vfs::<SyncAccessHandleIoMethods, SyncAccessHandleVfs>(
804+
&options.vfs_name,
805+
OpfsSAHPool::new(options).await?,
806+
default_vfs,
807+
)?
807808
};
809+
808810
let pool = unsafe { SyncAccessHandleStore::app_data(vfs) };
809811

810812
Ok(OpfsSAHPoolUtil { pool })

src/vfs/utils.rs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -323,28 +323,15 @@ impl SQLiteVfsFile {
323323
pub enum RegisterVfsError {
324324
#[error("An error occurred converting the given vfs name to a CStr")]
325325
ToCStr,
326-
#[error("Vfs has already been registered")]
327-
VfsAlreadyRegistered,
328-
#[error("Vfs is not registered")]
329-
VfsNotRegistered,
330326
#[error("An error occurred while registering vfs with sqlite")]
331327
RegisterVfs,
332328
}
333329

334330
/// Check whether vfs is registered and get vfs pointer
335-
pub fn registered_vfs(vfs_name: &str) -> Result<*mut sqlite3_vfs, RegisterVfsError> {
331+
pub fn registered_vfs(vfs_name: &str) -> Result<Option<*mut sqlite3_vfs>, RegisterVfsError> {
336332
let name = CString::new(vfs_name).map_err(|_| RegisterVfsError::ToCStr)?;
337-
let name_ptr = name.into_raw();
338-
let vfs = unsafe {
339-
let vfs = sqlite3_vfs_find(name_ptr);
340-
drop(CString::from_raw(name_ptr));
341-
vfs
342-
};
343-
if vfs.is_null() {
344-
Err(RegisterVfsError::VfsNotRegistered)
345-
} else {
346-
Ok(vfs)
347-
}
333+
let vfs = unsafe { sqlite3_vfs_find(name.as_ptr()) };
334+
Ok((!vfs.is_null()).then_some(vfs))
348335
}
349336

350337
/// Register vfs general method
@@ -356,15 +343,7 @@ pub fn register_vfs<IO: SQLiteIoMethods, V: SQLiteVfs<IO>>(
356343
let name = CString::new(vfs_name).map_err(|_| RegisterVfsError::ToCStr)?;
357344
let name_ptr = name.into_raw();
358345

359-
unsafe {
360-
if !sqlite3_vfs_find(name_ptr).is_null() {
361-
drop(CString::from_raw(name_ptr));
362-
return Err(RegisterVfsError::VfsAlreadyRegistered);
363-
}
364-
}
365-
366346
let app_data = VfsAppData::new(app_data).leak();
367-
368347
let vfs = Box::leak(Box::new(V::vfs(name_ptr, app_data.cast())));
369348
let ret = unsafe { sqlite3_vfs_register(vfs, i32::from(default_vfs)) };
370349

0 commit comments

Comments
 (0)