mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-03 16:25:35 +00:00
add fs_util module with a few useful functions for file/path management
This commit is contained in:
parent
46e42b88da
commit
72bec02e21
22
src/fs_util.rs
Normal file
22
src/fs_util.rs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
/// Ensures the given path exists by creating it if it doesn't.
|
||||||
|
pub fn ensure_path_exists(path: &PathBuf) -> anyhow::Result<()> {
|
||||||
|
if !path.exists() {
|
||||||
|
debug!("Path {:?} doesn't exist, creating it", path);
|
||||||
|
std::fs::create_dir_all(path)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Joins a parent folder and a sub-path, resolving the subpath beforehand to ensure that
|
||||||
|
/// the resulting path is still within the parent folder, regardless of ".." in the sub-path.
|
||||||
|
pub fn join_path_secure(parent: &Path, sub: &Path) -> anyhow::Result<PathBuf> {
|
||||||
|
Ok(parent.join(sub.canonicalize()?.as_path()))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Converts a PathBuf into a String in a lossy way. This is generally the way we want to do it
|
||||||
|
/// in the server.
|
||||||
|
pub fn path_to_string(path: PathBuf) -> String {
|
||||||
|
path.into_os_string().to_string_lossy().to_string()
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user