Skip to content

Commit a9b4e82

Browse files
committed
Allow filesystem constructor with data dir.
1 parent 4d81699 commit a9b4e82

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

lib/base-fs/src/filesys.rs

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,9 @@ impl ScopedDirFileSystemInterface for mem_fs::FileSystem {}
272272
#[derive(Debug, Hiarc)]
273273
pub struct ScopedDirFileSystem {
274274
#[hiarc_skip_unsafe]
275-
fs: Box<dyn ScopedDirFileSystemInterface>,
276-
host_path: PathBuf,
277-
mount_path: PathBuf,
275+
pub fs: Box<dyn ScopedDirFileSystemInterface>,
276+
pub host_path: PathBuf,
277+
pub mount_path: PathBuf,
278278
}
279279

280280
impl ScopedDirFileSystem {
@@ -311,12 +311,11 @@ pub struct FileSystem {
311311

312312
impl FileSystem {
313313
#[cfg(not(feature = "bundled_data_dir"))]
314-
fn add_data_dir(scoped_file_systems: &mut Vec<ScopedDirFileSystem>) -> anyhow::Result<usize> {
315-
scoped_file_systems.push(ScopedDirFileSystem::new("data/")?);
316-
Ok(scoped_file_systems.len() - 1)
314+
fn get_data_dir_fs() -> anyhow::Result<ScopedDirFileSystem> {
315+
ScopedDirFileSystem::new("data/")
317316
}
318317
#[cfg(feature = "bundled_data_dir")]
319-
fn add_data_dir(scoped_file_systems: &mut Vec<ScopedDirFileSystem>) -> anyhow::Result<usize> {
318+
fn get_data_dir_fs() -> anyhow::Result<ScopedDirFileSystem> {
320319
use virtual_fs::AsyncWriteExt;
321320
const DATA_DIR: include_dir::Dir =
322321
include_dir::include_dir!("$CARGO_MANIFEST_DIR/../../data");
@@ -356,20 +355,20 @@ impl FileSystem {
356355

357356
add_dirs(fs.as_ref(), &DATA_DIR)?;
358357

359-
scoped_file_systems.push(ScopedDirFileSystem {
358+
Ok(ScopedDirFileSystem {
360359
fs,
361360
host_path: "data/".into(),
362361
mount_path: "/".into(),
363-
});
364-
Ok(scoped_file_systems.len() - 1)
362+
})
365363
}
366364

367-
pub fn new(
365+
pub fn new_with_data_dir(
368366
rt: &tokio::runtime::Runtime,
369367
qualifier: &str,
370368
organization: &str,
371369
application: &str,
372370
secure_appl: &str,
371+
data_dir: ScopedDirFileSystem,
373372
) -> anyhow::Result<Self> {
374373
let config_dir: PathBuf =
375374
if let Some(proj_dirs) = ProjectDirs::from(qualifier, organization, application) {
@@ -406,7 +405,10 @@ impl FileSystem {
406405
log::info!(target: "fs", "Found config dir in {config_dir:?}");
407406
scoped_file_systems.push(ScopedDirFileSystem::new(config_dir)?);
408407
let config_dir_index = scoped_file_systems.len() - 1;
409-
let data_dir_index = Self::add_data_dir(&mut scoped_file_systems)?;
408+
409+
scoped_file_systems.push(data_dir);
410+
let data_dir_index = scoped_file_systems.len() - 1;
411+
410412
if let Ok(exec_path) = std::env::current_dir() {
411413
scoped_file_systems.push(ScopedDirFileSystem::new(exec_path)?);
412414
}
@@ -428,6 +430,27 @@ impl FileSystem {
428430
})
429431
}
430432

433+
pub fn new(
434+
rt: &tokio::runtime::Runtime,
435+
qualifier: &str,
436+
organization: &str,
437+
application: &str,
438+
secure_appl: &str,
439+
) -> anyhow::Result<Self> {
440+
let g = rt.enter();
441+
let data_dir = Self::get_data_dir_fs()?;
442+
drop(g);
443+
444+
Self::new_with_data_dir(
445+
rt,
446+
qualifier,
447+
organization,
448+
application,
449+
secure_appl,
450+
data_dir,
451+
)
452+
}
453+
431454
fn get_scoped_fs(&self, fs_path: FileSystemPath) -> &ScopedDirFileSystem {
432455
let index: usize;
433456
match fs_path {

0 commit comments

Comments
 (0)