Skip to main content

tuwunel_service/storage/provider/
local.rs

1//! Local-filesystem storage-provider construction.
2//!
3//! A configured directory is wrapped by the common provider interface. Empty
4//! base paths disable the provider, while optional directory creation happens
5//! before the backend is initialized.
6
7use std::{fs, sync::Arc};
8
9use object_store::local::LocalFileSystem;
10use tuwunel_core::{
11	Result,
12	config::{StorageProvider, StorageProviderLocal},
13	debug, debug_info, error, trace,
14};
15
16use super::Provider;
17
18/// Builds an enabled local-filesystem provider.
19///
20/// An empty base path returns `None`. When requested, the directory is created
21/// before the backend is configured with its cleanup policy.
22#[tracing::instrument(name = "new", level = "info", skip_all, err)]
23pub(in super::super) fn new(
24	_args: &crate::Args<'_>,
25	name: &str,
26	config: &StorageProviderLocal,
27) -> Result<Option<(String, Arc<Provider>)>> {
28	if !config.is_enabled() {
29		debug!(?name, "'base_path' is not set. This configuration will be skipped");
30		return Ok(None);
31	}
32
33	if config.create_if_missing {
34		trace!(
35			%name,
36			path = ?config.base_path,
37			"Creating directory on local filesystem if missing...",
38		);
39
40		fs::create_dir_all(&config.base_path)?;
41	}
42
43	trace!(?name, ?config, "Initializing LocalFS...");
44
45	let provider = LocalFileSystem::new_with_prefix(config.base_path.clone())
46		.inspect_err(|e| error!("Failed to configure LocalFS storage client: {e}"))?
47		.with_automatic_cleanup(config.delete_empty_directories);
48
49	debug_info!(
50		name = %name,
51		path = ?config.base_path,
52		"Started Local FS storage client.",
53	);
54
55	let provider = Provider {
56		name: name.to_owned(),
57		base_path: None, // LocalFileSystem computes base_path internally
58		config: StorageProvider::local(config.clone()),
59		startup_check: config.startup_check,
60		provider: Box::new(provider),
61		#[cfg(any(feature = "gcs_storage", feature = "s3_storage"))]
62		signer: None,
63	};
64
65	Ok(Some((name.to_owned(), Arc::new(provider))))
66}