(322150c) Make mv crash-safe by writing before renaming, and bump to 0.3.3
cmd_mv renamed the ticket into its new directory and only then wrote the updated content, so a write that failed left the ticket in the target directory still claiming the old status. Reproduced with a read-only ticket file: the move reported a bare "Permission denied" and left the file in committed/ carrying "Status: backlog". move_ticket_file now stages the content in a temp file, renames it over the source, and only then renames the file into the target directory. Two atomic steps, so a partial write is discarded and the original is untouched. The other obvious order, writing to the target and deleting the source afterwards, was rejected because it has the ticket in two directories at once. The worst case left is a ticket still in its old directory with a Status one move ahead, which the next mv corrects since the directory is what decides the current status. Tested, including the retry. The temp file is staged under .worklane/ rather than beside the ticket, so a leftover from a hard kill is invisible to ticket lookup and to the listing instead of appearing as a stray untitled ticket. IO errors now name the ticket and the path rather than propagating bare. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
f5204f6d8b
commit
e20396ce6b
+77
-7
@@ -311,6 +311,67 @@ pub fn cmd_show(search: &str, worklane_dir: &Path) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Put `content` in `filename` and move it from `from` to `to`, without ever
|
||||
/// leaving the ticket in two status directories or with half a file in it.
|
||||
///
|
||||
/// Two atomic steps, in this order. The content is staged in a temp file and
|
||||
/// renamed over the source, so a partial write is thrown away and the original is
|
||||
/// untouched. Only then is the file renamed into the target directory.
|
||||
///
|
||||
/// The old order renamed first and wrote second, so a failure in between left the
|
||||
/// ticket sitting in its new directory still claiming the old status. Writing
|
||||
/// straight to the target and deleting the source afterwards would instead have it
|
||||
/// in both directories at once. Neither happens here. The worst case is a ticket
|
||||
/// still in its old directory carrying a Status field one move ahead, which the
|
||||
/// next `mv` corrects, since the directory is what decides the current status.
|
||||
fn move_ticket_file(
|
||||
worklane_dir: &Path,
|
||||
from: &Path,
|
||||
to: &Path,
|
||||
filename: &str,
|
||||
content: &str,
|
||||
title: &str,
|
||||
) -> Result<()> {
|
||||
let source = from.join(filename);
|
||||
let dest = to.join(filename);
|
||||
|
||||
// Staged outside the status directories on purpose: a temp file left behind by a
|
||||
// hard kill is then invisible to ticket lookup and to the listing, rather than
|
||||
// showing up as a stray untitled ticket.
|
||||
let temp = worklane_dir.join(format!(".mv-{}.tmp", filename));
|
||||
|
||||
if let Err(e) = fs::write(&temp, content) {
|
||||
let _ = fs::remove_file(&temp);
|
||||
return Err(anyhow!(
|
||||
"Could not stage the update for ticket '{}' at {}: {}",
|
||||
title,
|
||||
temp.display(),
|
||||
e
|
||||
));
|
||||
}
|
||||
|
||||
if let Err(e) = fs::rename(&temp, &source) {
|
||||
let _ = fs::remove_file(&temp);
|
||||
return Err(anyhow!(
|
||||
"Could not update the status of ticket '{}' at {}: {}",
|
||||
title,
|
||||
source.display(),
|
||||
e
|
||||
));
|
||||
}
|
||||
|
||||
// The content is correct and in one place by now, so a failure here leaves the
|
||||
// ticket where it started rather than half moved.
|
||||
fs::rename(&source, &dest).map_err(|e| {
|
||||
anyhow!(
|
||||
"Could not move ticket '{}' to {}: {}",
|
||||
title,
|
||||
dest.display(),
|
||||
e
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn cmd_mv(ticket_id: &str, target_status: &str, force: bool, start: &Path) -> Result<()> {
|
||||
let valid_statuses = ["backlog", "committed", "reviewing", "done"];
|
||||
let forward_order = ["backlog", "committed", "reviewing", "done"];
|
||||
@@ -390,15 +451,24 @@ pub fn cmd_mv(ticket_id: &str, target_status: &str, force: bool, start: &Path) -
|
||||
// Read and update the ticket content before touching the filesystem, so a ticket
|
||||
// without a Status field is never half-moved.
|
||||
let source_path = current_path.join(&found_filename);
|
||||
let content = fs::read_to_string(&source_path)?;
|
||||
let content = fs::read_to_string(&source_path).map_err(|e| {
|
||||
anyhow!(
|
||||
"Could not read ticket '{}' at {}: {}",
|
||||
title,
|
||||
source_path.display(),
|
||||
e
|
||||
)
|
||||
})?;
|
||||
let content = set_status_field(&content, target_status, &title)?;
|
||||
|
||||
// Move the file
|
||||
let dest_path = target_path.join(&found_filename);
|
||||
fs::rename(&source_path, &dest_path)?;
|
||||
|
||||
// Write back updated content
|
||||
fs::write(&dest_path, content)?;
|
||||
move_ticket_file(
|
||||
&worklane_dir,
|
||||
¤t_path,
|
||||
&target_path,
|
||||
&found_filename,
|
||||
&content,
|
||||
&title,
|
||||
)?;
|
||||
|
||||
println!(
|
||||
"Moved ticket '{}' from {} to {}",
|
||||
|
||||
Reference in New Issue
Block a user