use std::fs::{self, File, OpenOptions};
use std::path::{Path, PathBuf};
use std::time::Duration;
use fs2::FileExt;
use crate::{Error, ErrorCode, Result};
pub(crate) fn reject_symlink_components(path: &Path, label: &str) -> Result<()> {
let absolute = if path.is_absolute() {
path.to_path_buf()
} else {
std::env::current_dir()
.map_err(|error| Error::new(ErrorCode::Io, error.to_string()))?
.join(path)
};
let mut current = PathBuf::new();
for component in absolute.components() {
if matches!(component, std::path::Component::ParentDir) {
return Err(Error::new(
ErrorCode::ConfigInvalid,
format!("{label} paths may not contain parent-directory components"),
)
.with_path(path));
}
current.push(component.as_os_str());
match fs::symlink_metadata(¤t) {
Ok(metadata) if metadata.file_type().is_symlink() => {
return Err(Error::new(
ErrorCode::ConfigInvalid,
format!("{label} path contains a symbolic-link component"),
)
.with_path(current));
}
Ok(metadata) if current != absolute && !metadata.is_dir() => {
return Err(Error::new(
ErrorCode::ConfigInvalid,
format!("{label} path contains a non-directory component"),
)
.with_path(current));
}
Ok(_) => {}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
Err(error) => {
return Err(Error::new(ErrorCode::Io, error.to_string()).with_path(current));
}
}
}
Ok(())
}
pub(crate) fn create_private_dir_all(path: &Path) -> Result<()> {
#[cfg(unix)]
{
use std::os::unix::fs::{DirBuilderExt, PermissionsExt};
let mut current = PathBuf::new();
for component in path.components() {
current.push(component.as_os_str());
let mut builder = fs::DirBuilder::new();
builder.mode(0o700);
match builder.create(¤t) {
Ok(()) => fs::set_permissions(¤t, fs::Permissions::from_mode(0o700))
.map_err(|error| {
Error::new(ErrorCode::Io, error.to_string()).with_path(¤t)
})?,
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {}
Err(error) => {
return Err(Error::new(ErrorCode::Io, error.to_string()).with_path(¤t));
}
}
}
fs::create_dir_all(path)
.map_err(|error| Error::new(ErrorCode::Io, error.to_string()).with_path(path))?;
}
#[cfg(not(unix))]
fs::create_dir_all(path)
.map_err(|error| Error::new(ErrorCode::Io, error.to_string()).with_path(path))?;
Ok(())
}
pub(crate) fn ensure_private_directory(
path: &Path,
label: &str,
not_directory: &str,
) -> Result<()> {
reject_symlink_components(path, label)?;
if let Ok(metadata) = fs::symlink_metadata(path) {
if metadata.file_type().is_symlink() || !metadata.is_dir() {
return Err(Error::new(ErrorCode::ConfigInvalid, not_directory).with_path(path));
}
}
create_private_dir_all(path)?;
reject_symlink_components(path, label)?;
let metadata = fs::symlink_metadata(path)
.map_err(|error| Error::new(ErrorCode::Io, error.to_string()).with_path(path))?;
if metadata.file_type().is_symlink() || !metadata.is_dir() {
return Err(Error::new(ErrorCode::ConfigInvalid, not_directory).with_path(path));
}
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
fs::set_permissions(path, fs::Permissions::from_mode(0o700))
.map_err(|error| Error::new(ErrorCode::Io, error.to_string()).with_path(path))?;
}
Ok(())
}
pub(crate) fn set_private_file(file: &File) -> Result<()> {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
file.set_permissions(fs::Permissions::from_mode(0o600))
.map_err(|error| Error::new(ErrorCode::Io, error.to_string()))?;
}
Ok(())
}
pub(crate) fn sync_directory(path: &Path) -> Result<()> {
File::open(path)
.and_then(|file| file.sync_all())
.map_err(|error| Error::new(ErrorCode::Io, error.to_string()).with_path(path))
}
pub(crate) fn open_private_lock(path: &Path) -> Result<File> {
let file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(path)
.map_err(|error| Error::new(ErrorCode::Io, error.to_string()).with_path(path))?;
set_private_file(&file)?;
Ok(file)
}
pub(crate) fn lock_exclusive_bounded(
file: &File,
timeout: Duration,
lock_name: &str,
) -> Result<()> {
let started = std::time::Instant::now();
loop {
match FileExt::try_lock_exclusive(file) {
Ok(()) => return Ok(()),
Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
if started.elapsed() >= timeout {
return Err(Error::new(
ErrorCode::CacheBusy,
format!("{lock_name} lock exceeded {timeout:?}"),
));
}
std::thread::sleep(Duration::from_millis(10));
}
Err(error) => return Err(Error::new(ErrorCode::CacheBusy, error.to_string())),
}
}
}
pub(crate) fn unique_temporary(
directory: &Path,
name: &str,
suffix: &str,
busy_message: &'static str,
) -> Result<PathBuf> {
for nonce in 0..1024u32 {
let path = directory.join(format!(".{name}.{}.{}.{suffix}", std::process::id(), nonce));
if !path.exists() {
return Ok(path);
}
}
Err(Error::new(ErrorCode::CacheBusy, busy_message))
}
pub(crate) fn monotonic_nonce() -> u128 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_nanos()
}
pub(crate) fn hex(bytes: &[u8]) -> String {
bytes.iter().map(|byte| format!("{byte:02x}")).collect()
}
const APPLICATION: &str = "toktier";
fn present(name: &str) -> Option<PathBuf> {
std::env::var_os(name)
.filter(|value| !value.is_empty())
.map(PathBuf::from)
}
fn cache_root_from(lookup: impl Fn(&str) -> Option<PathBuf>) -> Option<PathBuf> {
lookup("TOKTIER_HOME")
.map(|home| home.join("cache"))
.or_else(|| lookup("XDG_CACHE_HOME").map(|base| base.join(APPLICATION)))
.or_else(|| lookup("HOME").map(|home| home.join(".cache").join(APPLICATION)))
}
fn state_root_from(lookup: impl Fn(&str) -> Option<PathBuf>) -> Option<PathBuf> {
lookup("TOKTIER_HOME")
.map(|home| home.join("state"))
.or_else(|| lookup("XDG_STATE_HOME").map(|base| base.join(APPLICATION)))
.or_else(|| lookup("HOME").map(|home| home.join(".local").join("state").join(APPLICATION)))
}
pub(crate) fn default_state_directory() -> Option<PathBuf> {
state_root_from(present)
}
pub(crate) fn default_cache_directory(env_var: &str, subdirectory: &str) -> PathBuf {
cache_directory_from(present, env_var, subdirectory)
}
fn cache_directory_from(
lookup: impl Fn(&str) -> Option<PathBuf> + Copy,
env_var: &str,
subdirectory: &str,
) -> PathBuf {
lookup(env_var)
.or_else(|| cache_root_from(lookup).map(|root| root.join(subdirectory)))
.unwrap_or_else(|| PathBuf::from(".toktier").join(subdirectory))
}
#[cfg(test)]
mod tests {
use super::{cache_directory_from, cache_root_from, ensure_private_directory, state_root_from};
use std::path::PathBuf;
#[cfg(unix)]
#[test]
fn intermediate_directories_are_created_owner_only() {
use std::os::unix::fs::PermissionsExt;
let root = std::env::temp_dir().join(format!(
"toktier-private-dirs-{}-{:?}",
std::process::id(),
std::thread::current().id()
));
let _ = std::fs::remove_dir_all(&root);
let leaf = root.join("cache").join("artifacts").join(".locks");
ensure_private_directory(&leaf, "cache", "cache path is not a directory")
.expect("private directory");
let mut current = leaf.as_path();
loop {
let mode = std::fs::metadata(current)
.expect("created directory")
.permissions()
.mode()
& 0o777;
assert_eq!(mode, 0o700, "{}", current.display());
if current == root {
break;
}
current = current.parent().expect("parent inside the root");
}
std::fs::remove_dir_all(&root).expect("clean up");
}
fn environment(pairs: &[(&'static str, &'static str)]) -> impl Fn(&str) -> Option<PathBuf> {
let pairs = pairs.to_vec();
move |name: &str| {
pairs
.iter()
.find(|(key, _)| *key == name)
.map(|(_, value)| *value)
.filter(|value| !value.is_empty())
.map(PathBuf::from)
}
}
type Case = (
&'static [(&'static str, &'static str)],
Option<&'static str>,
Option<&'static str>,
);
#[test]
fn the_surface_variable_counts_as_unset_when_empty() {
let unset = environment(&[("HOME", "/tmp/user")]);
let empty = environment(&[("TOKTIER_ARTIFACT_CACHE", ""), ("HOME", "/tmp/user")]);
let named = environment(&[
("TOKTIER_ARTIFACT_CACHE", "/tmp/explicit"),
("HOME", "/tmp/user"),
]);
let resolve = |lookup: &dyn Fn(&str) -> Option<PathBuf>| {
cache_directory_from(lookup, "TOKTIER_ARTIFACT_CACHE", "artifacts")
};
assert_eq!(
resolve(&unset),
PathBuf::from("/tmp/user/.cache/toktier/artifacts")
);
assert_eq!(resolve(&empty), resolve(&unset));
assert_eq!(resolve(&named), PathBuf::from("/tmp/explicit"));
let nothing = environment(&[("TOKTIER_JIT_CACHE", "")]);
assert_eq!(
cache_directory_from(¬hing, "TOKTIER_JIT_CACHE", "jit-rust"),
PathBuf::from(".toktier/jit-rust")
);
}
#[test]
fn the_roots_follow_the_python_precedence() {
let cases: [Case; 5] = [
(
&[
("TOKTIER_HOME", "/tmp/home"),
("XDG_CACHE_HOME", "/tmp/xdg"),
("XDG_STATE_HOME", "/tmp/xdg-state"),
("HOME", "/tmp/user"),
],
Some("/tmp/home/cache"),
Some("/tmp/home/state"),
),
(
&[
("XDG_CACHE_HOME", "/tmp/xdg"),
("XDG_STATE_HOME", "/tmp/xdg-state"),
("HOME", "/tmp/user"),
],
Some("/tmp/xdg/toktier"),
Some("/tmp/xdg-state/toktier"),
),
(
&[
("TOKTIER_HOME", ""),
("XDG_CACHE_HOME", ""),
("XDG_STATE_HOME", ""),
("HOME", "/tmp/user"),
],
Some("/tmp/user/.cache/toktier"),
Some("/tmp/user/.local/state/toktier"),
),
(
&[("HOME", "/tmp/user")],
Some("/tmp/user/.cache/toktier"),
Some("/tmp/user/.local/state/toktier"),
),
(&[], None, None),
];
for (pairs, cache, state) in cases {
let lookup = environment(pairs);
assert_eq!(cache_root_from(&lookup), cache.map(PathBuf::from));
assert_eq!(state_root_from(&lookup), state.map(PathBuf::from));
}
}
}