48 lines
1.3 KiB
Rust
48 lines
1.3 KiB
Rust
//! Per-user profile management
|
|
//!
|
|
//! Port of `profile.c` / `profile.h` from the C project.
|
|
|
|
use std::path::PathBuf;
|
|
|
|
/// Get the profiles directory.
|
|
pub fn profile_get_dir() -> PathBuf {
|
|
let home = std::env::var("HOME").unwrap_or_else(|_| "/tmp".to_string());
|
|
PathBuf::from(home).join(".sovereign_browser").join("profiles")
|
|
}
|
|
|
|
/// Get the profile directory for a given pubkey.
|
|
pub fn profile_get_profile_dir(pubkey_hex: &str) -> PathBuf {
|
|
profile_get_dir().join(pubkey_hex)
|
|
}
|
|
|
|
/// List all profiles.
|
|
pub fn profile_list() -> Vec<String> {
|
|
let dir = profile_get_dir();
|
|
if !dir.exists() {
|
|
return Vec::new();
|
|
}
|
|
|
|
let mut profiles = Vec::new();
|
|
if let Ok(entries) = std::fs::read_dir(dir) {
|
|
for entry in entries.flatten() {
|
|
if entry.file_type().map(|t| t.is_dir()).unwrap_or(false) {
|
|
if let Some(name) = entry.file_name().to_str() {
|
|
profiles.push(name.to_string());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
profiles
|
|
}
|
|
|
|
/// Check if a profile exists.
|
|
pub fn profile_exists(pubkey_hex: &str) -> bool {
|
|
profile_get_profile_dir(pubkey_hex).exists()
|
|
}
|
|
|
|
/// Create a profile directory.
|
|
pub fn profile_create(pubkey_hex: &str) -> std::io::Result<()> {
|
|
let dir = profile_get_profile_dir(pubkey_hex);
|
|
std::fs::create_dir_all(dir)
|
|
}
|