fix(api): trailing-slash self-conflict and stray relocation dir
A root path differing only by a trailing separator now normalises to the same value on create and update, so PUT no longer treats a no-op edit as a relocation whose destinations conflict with their own sources. A failed root-path move now removes the new root directory it created for that move, but only when it created it — a directory that already existed at the destination is left alone, matching the retry guarantee relocate.rs documents. Refs #243
This commit is contained in:
@@ -49,6 +49,11 @@ struct PlannedRename {
|
||||
pub(crate) struct Relocation {
|
||||
performed: Vec<PlannedRename>,
|
||||
rewrites: Vec<(i64, String)>,
|
||||
/// The new root directory, set only when this request created it (it did
|
||||
/// not already exist). [`Self::undo`] removes it, so it is cleaned up
|
||||
/// exactly when the move it was created for does not complete; a move
|
||||
/// that commits never calls `undo` and the directory stays.
|
||||
created_root: Option<PathBuf>,
|
||||
}
|
||||
|
||||
/// Whether the destination root is a directory that must already be there.
|
||||
@@ -173,12 +178,20 @@ async fn relocate_files(
|
||||
}
|
||||
}
|
||||
|
||||
// Tracked only when this call is the one that created the directory, so
|
||||
// a failed move can remove it again without ever touching a root path
|
||||
// that already existed on disk.
|
||||
let mut created_root: Option<PathBuf> = None;
|
||||
if destination == Destination::Create && !renames.is_empty() {
|
||||
let already_there = tokio::fs::symlink_metadata(new_root).await.is_ok();
|
||||
if let Err(error) = tokio::fs::create_dir_all(new_root).await {
|
||||
return Err(ApiError::Filesystem(format!(
|
||||
"could not create '{new_root}': {error}"
|
||||
)));
|
||||
}
|
||||
if !already_there {
|
||||
created_root = Some(PathBuf::from(new_root));
|
||||
}
|
||||
}
|
||||
|
||||
let mut performed: Vec<PlannedRename> = Vec::new();
|
||||
@@ -195,7 +208,7 @@ async fn relocate_files(
|
||||
);
|
||||
continue;
|
||||
}
|
||||
Err(error) => return Err(failed(&rename, &error, performed).await),
|
||||
Err(error) => return Err(failed(&rename, &error, performed, created_root).await),
|
||||
}
|
||||
match tokio::fs::rename(&rename.source, &rename.destination).await {
|
||||
Ok(()) => {
|
||||
@@ -206,27 +219,31 @@ async fn relocate_files(
|
||||
);
|
||||
performed.push(rename);
|
||||
}
|
||||
Err(error) => return Err(failed(&rename, &error, performed).await),
|
||||
Err(error) => return Err(failed(&rename, &error, performed, created_root).await),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Relocation {
|
||||
performed,
|
||||
rewrites,
|
||||
created_root,
|
||||
})
|
||||
}
|
||||
|
||||
/// One rename failed: move back everything that had already moved and name
|
||||
/// the folder that stopped the move, so the operator knows which title to
|
||||
/// look at before retrying.
|
||||
/// One rename failed: move back everything that had already moved, remove
|
||||
/// the new root if this request is the one that created it, and name the
|
||||
/// folder that stopped the move so the operator knows which title to look at
|
||||
/// before retrying.
|
||||
async fn failed(
|
||||
rename: &PlannedRename,
|
||||
error: &std::io::Error,
|
||||
performed: Vec<PlannedRename>,
|
||||
created_root: Option<PathBuf>,
|
||||
) -> ApiError {
|
||||
Relocation {
|
||||
performed,
|
||||
rewrites: Vec::new(),
|
||||
created_root,
|
||||
}
|
||||
.undo()
|
||||
.await;
|
||||
@@ -276,6 +293,18 @@ impl Relocation {
|
||||
);
|
||||
}
|
||||
}
|
||||
// `remove_dir` rather than `remove_dir_all`: it only succeeds on an
|
||||
// empty directory, so anything unexpected left inside it — this
|
||||
// request's own undo failing, say — is a reason to leave it alone.
|
||||
if let Some(root) = &self.created_root {
|
||||
if let Err(error) = tokio::fs::remove_dir(root).await {
|
||||
tracing::warn!(
|
||||
path = %root.display(),
|
||||
%error,
|
||||
"could not remove the directory created for a move that did not complete"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+121
-8
@@ -67,6 +67,20 @@ impl RootInput {
|
||||
}
|
||||
}
|
||||
|
||||
/// Strips a trailing separator so `/mnt/media` and `/mnt/media/` name the
|
||||
/// same root — otherwise a `PUT` that only adds or drops a trailing slash
|
||||
/// takes the relocation branch and every planned destination conflicts with
|
||||
/// its own source. Leaves a bare `/` alone.
|
||||
fn normalize_path(path: &str) -> String {
|
||||
let trimmed = path.trim();
|
||||
let stripped = trimmed.trim_end_matches('/');
|
||||
if stripped.is_empty() {
|
||||
"/".to_owned()
|
||||
} else {
|
||||
stripped.to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
async fn load_root(state: &AppState, id: i64) -> Result<Root, ApiError> {
|
||||
sqlx::query_as!(
|
||||
Root,
|
||||
@@ -137,7 +151,7 @@ pub async fn create(
|
||||
let input = parsed(body)?;
|
||||
input.validate().map_err(ApiError::Invalid)?;
|
||||
input.policy_exists(&state).await?;
|
||||
let path = input.path.trim().to_owned();
|
||||
let path = normalize_path(&input.path);
|
||||
let result = sqlx::query!(
|
||||
"INSERT INTO roots (kind, audience, path, policy_id) VALUES (?, ?, ?, ?)",
|
||||
input.kind,
|
||||
@@ -173,7 +187,7 @@ pub async fn update(
|
||||
input.validate().map_err(ApiError::Invalid)?;
|
||||
input.policy_exists(&state).await?;
|
||||
let current = load_root(&state, id).await?;
|
||||
let path = input.path.trim().to_owned();
|
||||
let path = normalize_path(&input.path);
|
||||
// A path change moves every §7.4 title folder under this root with the
|
||||
// row (issue #236), the same way changing a title's root moves one
|
||||
// (#228). Disk first, row second: a failed rename leaves the root row
|
||||
@@ -857,12 +871,8 @@ mod tests {
|
||||
);
|
||||
}
|
||||
assert!(
|
||||
new.exists(),
|
||||
"the move reached the disk: the new path was created for it"
|
||||
);
|
||||
assert!(
|
||||
!new.join("Dune").exists(),
|
||||
"the folder that had already moved was moved back"
|
||||
!new.exists(),
|
||||
"the directory created for the failed move was removed, since it never held anything"
|
||||
);
|
||||
assert_eq!(file_paths(&state).await, before, "the rows are untouched");
|
||||
assert_eq!(
|
||||
@@ -884,6 +894,109 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// Issue #243: a directory a failed move did not create is not this
|
||||
/// request's to remove, even though it sits at the same new root path.
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn a_new_root_that_already_existed_survives_a_failed_move() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
let (_dir, state, base) = application().await;
|
||||
let old = tempfile::tempdir().expect("old root");
|
||||
let new = tempfile::tempdir().expect("new root, already there");
|
||||
point_root_at(&state, 1, old.path()).await;
|
||||
|
||||
let id = add_movie(&base, 100, "Dune", 1).await;
|
||||
let stuck = library_folder(&state, id, old.path(), "Dune").await;
|
||||
tokio::fs::set_permissions(&stuck, std::fs::Permissions::from_mode(0o555))
|
||||
.await
|
||||
.expect("freeze the title folder");
|
||||
|
||||
let response = reqwest::Client::new()
|
||||
.put(format!("{base}/api/roots/1"))
|
||||
.json(&root_payload(&base, 1, new.path().to_str().expect("utf-8")).await)
|
||||
.send()
|
||||
.await
|
||||
.expect("move the root");
|
||||
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||
|
||||
tokio::fs::set_permissions(&stuck, std::fs::Permissions::from_mode(0o755))
|
||||
.await
|
||||
.expect("thaw the title folder");
|
||||
assert!(
|
||||
new.path().exists(),
|
||||
"a directory this request did not create is left alone"
|
||||
);
|
||||
}
|
||||
|
||||
/// Issue #243: a path differing only by a trailing separator is the same
|
||||
/// root, not a self-conflict — `PUT` with it is a no-op.
|
||||
#[tokio::test]
|
||||
async fn a_trailing_separator_is_not_a_path_change() {
|
||||
let (_dir, state, base) = application().await;
|
||||
let old = tempfile::tempdir().expect("old root");
|
||||
point_root_at(&state, 1, old.path()).await;
|
||||
let id = add_movie(&base, 100, "Dune", 1).await;
|
||||
let folder = library_folder(&state, id, old.path(), "Dune").await;
|
||||
|
||||
let mut trailing = old.path().to_str().expect("utf-8").to_owned();
|
||||
trailing.push('/');
|
||||
let response = reqwest::Client::new()
|
||||
.put(format!("{base}/api/roots/1"))
|
||||
.json(&root_payload(&base, 1, &trailing).await)
|
||||
.send()
|
||||
.await
|
||||
.expect("update root with a trailing separator");
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
|
||||
assert!(folder.exists(), "nothing on disk moved");
|
||||
assert_eq!(
|
||||
stored_path(&base, 1).await,
|
||||
old.path().to_str().expect("utf-8"),
|
||||
"the stored path keeps no trailing separator"
|
||||
);
|
||||
}
|
||||
|
||||
/// Issue #243: `create` normalises the same way, so a trailing separator
|
||||
/// cannot dodge the duplicate-path check.
|
||||
#[tokio::test]
|
||||
async fn create_normalises_a_trailing_separator_before_checking_for_conflicts() {
|
||||
let (_dir, _state, base) = application().await;
|
||||
let policy_ids = first_policy_ids(&base).await;
|
||||
let roots: Vec<serde_json::Value> = reqwest::get(format!("{base}/api/roots"))
|
||||
.await
|
||||
.expect("roots")
|
||||
.json()
|
||||
.await
|
||||
.expect("roots json");
|
||||
let existing_path = roots[0]["path"].as_str().expect("path").to_owned();
|
||||
|
||||
// Free the (movie, kids) pair so the conflict below is the path
|
||||
// check alone, not the (kind, audience) uniqueness check.
|
||||
let kids_root = roots[1]["id"].as_i64().expect("movie kids root id");
|
||||
let freed = reqwest::Client::new()
|
||||
.delete(format!("{base}/api/roots/{kids_root}"))
|
||||
.send()
|
||||
.await
|
||||
.expect("free the seeded movie kids root");
|
||||
assert_eq!(freed.status(), StatusCode::NO_CONTENT);
|
||||
|
||||
let mut payload = root_input(policy_ids[0]);
|
||||
payload["audience"] = serde_json::json!("kids");
|
||||
payload["path"] = serde_json::json!(format!("{existing_path}/"));
|
||||
let response = reqwest::Client::new()
|
||||
.post(format!("{base}/api/roots"))
|
||||
.json(&payload)
|
||||
.send()
|
||||
.await
|
||||
.expect("create root with a trailing separator");
|
||||
assert_eq!(
|
||||
response.status(),
|
||||
StatusCode::CONFLICT,
|
||||
"a trailing separator does not dodge the duplicate-path check"
|
||||
);
|
||||
}
|
||||
|
||||
/// Issue #236: a TV root carries series folders, whose files hang off
|
||||
/// episodes rather than off the title row. They move with the root too.
|
||||
#[tokio::test]
|
||||
|
||||
Reference in New Issue
Block a user