use crate::service::*;
use adk_core::{AdkError, ErrorCategory, ErrorComponent, Part, Result};
use async_trait::async_trait;
use std::collections::BTreeSet;
use std::fmt::Write as _;
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::fs;
use tokio::io::AsyncWriteExt;
const USER_SCOPE_DIR: &str = USER_SCOPE_SESSION_ID;
const LEGACY_USER_SCOPE_DIR: &str = "_user_scoped_";
const TEMP_PREFIX: &str = ".save-";
const MAX_SAVE_ATTEMPTS: usize = 1024;
static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0);
fn path_escapes() -> AdkError {
AdkError::new(
ErrorComponent::Artifact,
ErrorCategory::InvalidInput,
"artifact.path_escapes",
"the artifact path resolves outside the configured base directory",
)
}
fn io_error(operation: &str, error: impl std::fmt::Display) -> AdkError {
AdkError::internal(
ErrorComponent::Artifact,
"artifact.file.io",
format!("{operation}: {error}"),
)
}
fn encode_name(name: &str) -> String {
let mut encoded = String::with_capacity(name.len());
let mut after_underscore = false;
for c in name.chars() {
let escape = match c {
'%' | '<' | '>' | ':' | '"' | '|' | '?' | '*' => true,
'_' => after_underscore,
other => other.is_ascii_control(),
};
if escape {
let _ = write!(encoded, "%{:02X}", u32::from(c));
after_underscore = false;
} else {
encoded.push(c);
after_underscore = c == '_';
}
}
encoded
}
fn legacy_encode_name(name: &str) -> String {
name.replace(':', "__")
}
fn decode_name(dir_name: &str) -> String {
if dir_name.contains("__") {
return dir_name.replace("__", ":");
}
percent_decode(dir_name).unwrap_or_else(|| dir_name.to_string())
}
fn percent_decode(encoded: &str) -> Option<String> {
let bytes = encoded.as_bytes();
let mut decoded = Vec::with_capacity(bytes.len());
let mut index = 0;
while index < bytes.len() {
if bytes[index] == b'%' {
let hex = bytes.get(index + 1..index + 3)?;
if !hex.iter().all(u8::is_ascii_hexdigit) {
return None;
}
decoded.push(u8::from_str_radix(std::str::from_utf8(hex).ok()?, 16).ok()?);
index += 3;
} else {
decoded.push(bytes[index]);
index += 1;
}
}
String::from_utf8(decoded).ok()
}
fn version_file_name(version: i64) -> String {
format!("v{version}.json")
}
struct ArtifactDirs {
primary: PathBuf,
legacy: Option<PathBuf>,
}
impl ArtifactDirs {
fn all(&self) -> impl Iterator<Item = &PathBuf> {
std::iter::once(&self.primary).chain(self.legacy.as_ref())
}
}
pub struct FileArtifactService {
base_dir: PathBuf,
}
impl FileArtifactService {
pub fn new(base_dir: impl Into<PathBuf>) -> Result<Self> {
let raw = base_dir.into();
std::fs::create_dir_all(&raw).map_err(|e| io_error("create base dir failed", e))?;
let canonical =
raw.canonicalize().map_err(|e| io_error("canonicalize base dir failed", e))?;
Ok(Self { base_dir: canonical })
}
fn validate_file_name(file_name: &str) -> Result<()> {
if file_name.is_empty() {
return Err(invalid_file_name(file_name, "the name is empty"));
}
if file_name.contains('/')
|| file_name.contains('\\')
|| file_name == "."
|| file_name == ".."
|| file_name.contains("..")
{
return Err(invalid_file_name(
file_name,
"path separators and traversal patterns are not allowed",
));
}
Ok(())
}
fn validate_path_component(component: &str, field: &str) -> Result<()> {
if component.is_empty() {
return Err(invalid_identifier(field, component, "the value is empty"));
}
if component.contains('/')
|| component.contains('\\')
|| component == "."
|| component == ".."
|| component.contains("..")
{
return Err(invalid_identifier(
field,
component,
"path separators and traversal patterns are not allowed",
));
}
Ok(())
}
fn user_root(&self, app_name: &str, user_id: &str, session_id: &str) -> Result<PathBuf> {
Self::validate_path_component(app_name, "app name")?;
Self::validate_path_component(user_id, "user id")?;
Self::validate_path_component(session_id, "session id")?;
check_session_id(session_id)?;
Ok(self.base_dir.join(app_name).join(user_id))
}
fn ensure_within_base_dir(&self, path: &Path) -> Result<()> {
let canonical_base = self
.base_dir
.canonicalize()
.map_err(|e| io_error("canonicalize base dir failed", e))?;
let canonical_path = match path.canonicalize() {
Ok(canonical) => canonical,
Err(_) => {
let relative = path.strip_prefix(&self.base_dir).unwrap_or(path);
canonical_base.join(relative)
}
};
if !canonical_path.starts_with(&canonical_base) {
return Err(path_escapes());
}
Ok(())
}
fn is_user_scoped(file_name: &str) -> bool {
file_name.starts_with("user:")
}
fn artifact_dirs(
&self,
app_name: &str,
user_id: &str,
session_id: &str,
file_name: &str,
) -> Result<ArtifactDirs> {
let user_root = self.user_root(app_name, user_id, session_id)?;
Self::validate_file_name(file_name)?;
let (scope, legacy_scope) = if Self::is_user_scoped(file_name) {
(USER_SCOPE_DIR, LEGACY_USER_SCOPE_DIR)
} else {
(session_id, session_id)
};
let primary = user_root.join(scope).join(encode_name(file_name));
let legacy = user_root.join(legacy_scope).join(legacy_encode_name(file_name));
let dirs = ArtifactDirs { legacy: (legacy != primary).then_some(legacy), primary };
for dir in dirs.all() {
self.ensure_within_base_dir(dir)?;
}
Ok(dirs)
}
async fn versions_in(dir: &Path) -> Result<Vec<i64>> {
let mut entries = match fs::read_dir(dir).await {
Ok(entries) => entries,
Err(error) if error.kind() == ErrorKind::NotFound => return Ok(Vec::new()),
Err(error) => {
return Err(io_error("read dir failed", error));
}
};
let mut versions = Vec::new();
while let Some(entry) =
entries.next_entry().await.map_err(|e| io_error("read dir entry failed", e))?
{
let Some(file_name) = entry.file_name().to_str().map(ToString::to_string) else {
continue;
};
let Some(raw) =
file_name.strip_prefix('v').and_then(|value| value.strip_suffix(".json"))
else {
continue;
};
if let Ok(version) = raw.parse::<i64>() {
versions.push(version);
}
}
Ok(versions)
}
async fn stored_versions(dirs: &ArtifactDirs) -> Result<BTreeSet<i64>> {
let mut versions = BTreeSet::new();
for dir in dirs.all() {
versions.extend(Self::versions_in(dir).await?);
}
Ok(versions)
}
async fn version_path(dirs: &ArtifactDirs, version: i64) -> Option<PathBuf> {
for dir in dirs.all() {
let path = dir.join(version_file_name(version));
if fs::try_exists(&path).await.unwrap_or(false) {
return Some(path);
}
}
None
}
async fn list_scope_dir(path: &Path, keep: fn(&str) -> bool) -> Result<BTreeSet<String>> {
let mut names = BTreeSet::new();
let mut entries = match fs::read_dir(path).await {
Ok(entries) => entries,
Err(error) if error.kind() == ErrorKind::NotFound => return Ok(names),
Err(error) => {
return Err(io_error("read dir failed", error));
}
};
while let Some(entry) =
entries.next_entry().await.map_err(|e| io_error("read dir entry failed", e))?
{
if entry.file_type().await.map_err(|e| io_error("file type check failed", e))?.is_dir()
&& let Some(dir_name) = entry.file_name().to_str()
{
let name = decode_name(dir_name);
if keep(&name) {
names.insert(name);
}
}
}
Ok(names)
}
async fn user_version_files(
&self,
app_name: &str,
user_id: &str,
) -> Result<Vec<(Option<String>, String, i64, PathBuf)>> {
Self::validate_path_component(app_name, "app name")?;
Self::validate_path_component(user_id, "user id")?;
let user_root = self.base_dir.join(app_name).join(user_id);
self.ensure_within_base_dir(&user_root)?;
let mut found = Vec::new();
for scope in Self::subdirectories(&user_root).await? {
let scope_dir = user_root.join(&scope);
for dir_name in Self::subdirectories(&scope_dir).await? {
let name = decode_name(&dir_name);
let session = match scope.as_str() {
USER_SCOPE_DIR => None,
LEGACY_USER_SCOPE_DIR if Self::is_user_scoped(&name) => None,
_ => Some(scope.clone()),
};
let artifact_dir = scope_dir.join(&dir_name);
let mut versions = Self::versions_in(&artifact_dir).await?;
versions.sort_unstable();
for version in versions {
let path = artifact_dir.join(version_file_name(version));
found.push((session.clone(), name.clone(), version, path));
}
}
}
found.sort_by(|a, b| (&a.0, &a.1, a.2).cmp(&(&b.0, &b.1, b.2)));
Ok(found)
}
async fn subdirectories(dir: &Path) -> Result<Vec<String>> {
let mut entries = match fs::read_dir(dir).await {
Ok(entries) => entries,
Err(error) if error.kind() == ErrorKind::NotFound => return Ok(Vec::new()),
Err(error) => return Err(io_error("read dir failed", error)),
};
let mut names = Vec::new();
while let Some(entry) =
entries.next_entry().await.map_err(|e| io_error("read dir entry failed", e))?
{
if entry.file_type().await.map_err(|e| io_error("file type check failed", e))?.is_dir()
&& let Some(name) = entry.file_name().to_str()
{
names.push(name.to_string());
}
}
Ok(names)
}
async fn write_synced(path: &Path, payload: &[u8]) -> std::io::Result<()> {
let mut file = fs::OpenOptions::new().write(true).create_new(true).open(path).await?;
file.write_all(payload).await?;
file.sync_all().await
}
async fn publish(temp: &Path, target: &Path, payload: &[u8]) -> std::io::Result<bool> {
match fs::hard_link(temp, target).await {
Ok(()) => Ok(true),
Err(error) if error.kind() == ErrorKind::AlreadyExists => Ok(false),
Err(_) => match Self::write_synced(target, payload).await {
Ok(()) => Ok(true),
Err(error) if error.kind() == ErrorKind::AlreadyExists => Ok(false),
Err(error) => Err(error),
},
}
}
async fn claim_version(
dirs: &ArtifactDirs,
dir: &Path,
temp: &Path,
payload: &[u8],
req: &SaveRequest,
) -> Result<i64> {
let mut version = match req.version {
Some(version) => {
if let Some(legacy) = &dirs.legacy
&& fs::try_exists(legacy.join(version_file_name(version)))
.await
.unwrap_or(false)
{
return Err(version_exists(&req.file_name, version));
}
version
}
None => Self::stored_versions(dirs).await?.last().map_or(1, |latest| latest + 1),
};
for _ in 0..MAX_SAVE_ATTEMPTS {
let target = dir.join(version_file_name(version));
let published = Self::publish(temp, &target, payload)
.await
.map_err(|e| io_error("write failed", e))?;
if published {
return Ok(version);
}
if req.version.is_some() {
return Err(version_exists(&req.file_name, version));
}
version += 1;
}
Err(AdkError::new(
ErrorComponent::Artifact,
ErrorCategory::Conflict,
"artifact.save_contended",
format!(
"artifact '{}' gained {MAX_SAVE_ATTEMPTS} versions while this save ran; retry the save",
req.file_name
),
))
}
}
async fn sync_dir(dir: &Path) {
#[cfg(unix)]
if let Ok(handle) = fs::File::open(dir).await {
let _ = handle.sync_all().await;
}
#[cfg(not(unix))]
let _ = dir;
}
#[async_trait]
impl ArtifactService for FileArtifactService {
async fn list_user(&self, app_name: &str, user_id: &str) -> Result<Vec<UserArtifact>> {
let mut artifacts = Vec::new();
for (session_id, file_name, version, path) in
self.user_version_files(app_name, user_id).await?
{
let bytes = fs::read(&path).await.map_err(|e| io_error("read failed", e))?;
let part: Part = serde_json::from_slice(&bytes)
.map_err(|error| io_error("decode artifact failed", error))?;
let (mime_type, size) = part_summary(&part);
artifacts.push(UserArtifact { session_id, file_name, version, mime_type, size });
}
Ok(artifacts)
}
async fn delete_user(&self, app_name: &str, user_id: &str) -> Result<u64> {
let versions = self.user_version_files(app_name, user_id).await?.len() as u64;
match fs::remove_dir_all(self.base_dir.join(app_name).join(user_id)).await {
Ok(()) => Ok(versions),
Err(error) if error.kind() == ErrorKind::NotFound => Ok(0),
Err(error) => Err(io_error("delete user dir failed", error)),
}
}
async fn save(&self, req: SaveRequest) -> Result<SaveResponse> {
let dirs =
self.artifact_dirs(&req.app_name, &req.user_id, &req.session_id, &req.file_name)?;
let payload = serde_json::to_vec(&req.part)
.map_err(|error| io_error("encode artifact failed", error))?;
fs::create_dir_all(&dirs.primary).await.map_err(|e| io_error("create dir failed", e))?;
let verified_dir = dirs
.primary
.canonicalize()
.map_err(|e| io_error("canonicalize artifact dir failed", e))?;
if !verified_dir.starts_with(&self.base_dir) {
return Err(path_escapes());
}
let nonce = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_nanos();
let temp = verified_dir.join(format!(
"{TEMP_PREFIX}{}-{nonce}-{}.tmp",
std::process::id(),
TEMP_COUNTER.fetch_add(1, Ordering::Relaxed)
));
if let Err(error) = Self::write_synced(&temp, &payload).await {
let _ = fs::remove_file(&temp).await;
return Err(io_error("write failed", error));
}
let claimed = Self::claim_version(&dirs, &verified_dir, &temp, &payload, &req).await;
let _ = fs::remove_file(&temp).await;
let version = claimed?;
sync_dir(&verified_dir).await;
Ok(SaveResponse { version })
}
async fn load(&self, req: LoadRequest) -> Result<LoadResponse> {
let dirs =
self.artifact_dirs(&req.app_name, &req.user_id, &req.session_id, &req.file_name)?;
let version = match req.version {
Some(version) => version,
None => *Self::stored_versions(&dirs)
.await?
.last()
.ok_or_else(|| artifact_not_found(&req.file_name, None))?,
};
let path = Self::version_path(&dirs, version)
.await
.ok_or_else(|| artifact_not_found(&req.file_name, req.version))?;
let payload = fs::read(&path).await.map_err(|error| {
if error.kind() == ErrorKind::NotFound {
artifact_not_found(&req.file_name, req.version)
} else {
io_error("read failed", error)
}
})?;
let part = serde_json::from_slice::<Part>(&payload).map_err(|error| {
AdkError::internal(
ErrorComponent::Artifact,
"artifact.file.corrupt",
format!(
"artifact '{}' version {version} is not a stored artifact: {error}",
req.file_name
),
)
})?;
Ok(LoadResponse { part })
}
async fn delete(&self, req: DeleteRequest) -> Result<()> {
let dirs =
self.artifact_dirs(&req.app_name, &req.user_id, &req.session_id, &req.file_name)?;
for dir in dirs.all() {
let removed = match req.version {
Some(version) => fs::remove_file(dir.join(version_file_name(version))).await,
None => fs::remove_dir_all(dir).await,
};
match removed {
Ok(()) => {}
Err(error) if error.kind() == ErrorKind::NotFound => {}
Err(error) => {
return Err(io_error("delete failed", error));
}
}
}
Ok(())
}
async fn list(&self, req: ListRequest) -> Result<ListResponse> {
let user_root = self.user_root(&req.app_name, &req.user_id, &req.session_id)?;
let session_dir = user_root.join(&req.session_id);
self.ensure_within_base_dir(&session_dir)?;
let mut names =
Self::list_scope_dir(&session_dir, |name| !Self::is_user_scoped(name)).await?;
for scope in [USER_SCOPE_DIR, LEGACY_USER_SCOPE_DIR] {
let user_dir = user_root.join(scope);
self.ensure_within_base_dir(&user_dir)?;
names.extend(Self::list_scope_dir(&user_dir, Self::is_user_scoped).await?);
}
Ok(ListResponse { file_names: names.into_iter().collect() })
}
async fn versions(&self, req: VersionsRequest) -> Result<VersionsResponse> {
let dirs =
self.artifact_dirs(&req.app_name, &req.user_id, &req.session_id, &req.file_name)?;
let versions: Vec<i64> = Self::stored_versions(&dirs).await?.into_iter().rev().collect();
if versions.is_empty() {
return Err(artifact_not_found(&req.file_name, None));
}
Ok(VersionsResponse { versions })
}
async fn health_check(&self) -> Result<()> {
fs::create_dir_all(&self.base_dir).await.map_err(|e| io_error("health check failed", e))?;
let nonce = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_nanos();
let path = self.base_dir.join(format!(".healthcheck-{nonce}"));
fs::write(&path, b"ok").await.map_err(|e| io_error("health check failed", e))?;
fs::remove_file(path).await.map_err(|e| io_error("health check failed", e))?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn user_scoped_artifacts_are_visible_across_sessions() {
let tempdir = tempfile::tempdir().unwrap();
let service = FileArtifactService::new(tempdir.path()).unwrap();
service
.save(SaveRequest {
app_name: "app".into(),
user_id: "user".into(),
session_id: "s1".into(),
file_name: "user:shared.txt".into(),
part: Part::Text { text: "hello".into() },
version: None,
})
.await
.unwrap();
let list = service
.list(ListRequest {
app_name: "app".into(),
user_id: "user".into(),
session_id: "s2".into(),
})
.await
.unwrap();
assert_eq!(list.file_names, vec!["user:shared.txt".to_string()]);
}
#[test]
fn name_encoding_is_injective_and_round_trips() {
let names = [
"a:b",
"a__b",
"a_b",
"a%3Ab",
"a___b",
"_",
"__",
"___",
"a_:b",
"50%.txt",
"what?.txt",
"q<1>|\"*\"",
"tab\tname",
"user:prefs",
"plain.txt",
"日本:語",
];
let encoded: BTreeSet<String> = names.iter().map(|name| encode_name(name)).collect();
assert_eq!(encoded.len(), names.len(), "{encoded:?}");
for name in names {
let dir = encode_name(name);
assert!(!dir.contains("__"), "{name} encoded as {dir}");
assert!(!dir.contains(['<', '>', ':', '"', '|', '?', '*', '\t']), "{dir}");
assert_eq!(decode_name(&dir), name);
}
}
#[test]
fn legacy_directory_names_decode_as_before() {
assert_eq!(decode_name("user__prefs"), "user:prefs");
assert_eq!(decode_name("report__q1"), "report:q1");
assert_eq!(decode_name("plain.txt"), "plain.txt");
assert_eq!(decode_name("50%off"), "50%off");
}
}