Treat an unreadable session file as corruption
get_current_step swallowed a bad STEP= value into None, which callers read as "tour not started". It now returns Result<Option<u32>>: Ok(None) means no session, a malformed STEP= means CorruptedTour. Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
+1
-1
@@ -8,7 +8,7 @@ use std::path::Path;
|
||||
pub fn list() -> Result<(), TourError> {
|
||||
require_tour()?;
|
||||
let total = get_tour_step()?;
|
||||
let current = get_current_step();
|
||||
let current = get_current_step()?;
|
||||
|
||||
if total == 0 {
|
||||
println!("No steps yet.");
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ pub fn status() -> Result<(), TourError> {
|
||||
|
||||
let total = get_tour_step()?;
|
||||
let ended = Path::new(TOUR_DIR).join("ended").exists();
|
||||
let current = get_current_step();
|
||||
let current = get_current_step()?;
|
||||
|
||||
println!("Tour: {} steps{}", total, if ended { " (ended)" } else { "" });
|
||||
|
||||
|
||||
+3
-3
@@ -31,7 +31,7 @@ pub fn next(n: Option<u32>) -> Result<(), TourError> {
|
||||
}
|
||||
let delta = n.unwrap_or(1);
|
||||
let last = total - 1;
|
||||
match get_current_step() {
|
||||
match get_current_step()? {
|
||||
Some(current) if current >= last => {
|
||||
println!("Already at the last step ({total}/{total}).");
|
||||
Ok(())
|
||||
@@ -48,7 +48,7 @@ pub fn prev(n: Option<u32>) -> Result<(), TourError> {
|
||||
return Err(TourError::NoStepsToNavigate);
|
||||
}
|
||||
let delta = n.unwrap_or(1);
|
||||
let current = get_current_step().ok_or(TourError::NotStarted)?;
|
||||
let current = get_current_step()?.ok_or(TourError::NotStarted)?;
|
||||
if current == 0 {
|
||||
println!("Already at the first step (1/{total}).");
|
||||
return Ok(());
|
||||
@@ -72,7 +72,7 @@ fn go_to_step(target: u32, total: u32) -> Result<(), TourError> {
|
||||
|
||||
// Files the tour owns in the working directory: the current step's set if a
|
||||
// session exists, otherwise every file any step tracks (conservative).
|
||||
let current_files = match get_current_step() {
|
||||
let current_files = match get_current_step()? {
|
||||
Some(current) if steps_dir.join(current.to_string()).is_dir() => {
|
||||
let dir = steps_dir.join(current.to_string());
|
||||
let mut set = BTreeSet::new();
|
||||
|
||||
+36
-8
@@ -13,14 +13,42 @@ pub fn require_tour() -> Result<(), TourError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_current_step() -> Option<u32> {
|
||||
fs::read_to_string(SESSION_PATH)
|
||||
.ok()
|
||||
.and_then(|s| {
|
||||
s.lines()
|
||||
.find_map(|l| l.strip_prefix("STEP="))
|
||||
.and_then(|v| v.trim().parse::<u32>().ok())
|
||||
})
|
||||
/// Fails the command if the tour is missing or already ended. Use for any
|
||||
/// command that mutates authoring state.
|
||||
pub fn require_active() -> Result<(), TourError> {
|
||||
require_tour()?;
|
||||
if Path::new(TOUR_DIR).join("ended").exists() {
|
||||
return Err(TourError::TourEnded);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Collapses `./a/./b` to `a/b` so the same file always has one spelling in
|
||||
/// the staged/removed lists and in step snapshots.
|
||||
pub fn normalize_path(p: &Path) -> PathBuf {
|
||||
// components() drops interior "." but keeps a leading one, so filter it out.
|
||||
p.components()
|
||||
.filter(|c| !matches!(c, std::path::Component::CurDir))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// `Ok(None)` means no session yet; a session file with an unreadable STEP is
|
||||
/// corruption, not "not started".
|
||||
pub fn get_current_step() -> Result<Option<u32>, TourError> {
|
||||
let content = match fs::read_to_string(SESSION_PATH) {
|
||||
Ok(c) => c,
|
||||
Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(None),
|
||||
Err(e) => return Err(e.into()),
|
||||
};
|
||||
match content.lines().find_map(|l| l.strip_prefix("STEP=")) {
|
||||
None if content.trim().is_empty() => Ok(None),
|
||||
None => Err(TourError::CorruptedTour(
|
||||
"session file has no STEP= entry".into(),
|
||||
)),
|
||||
Some(v) => v.trim().parse::<u32>().map(Some).map_err(|_| {
|
||||
TourError::CorruptedTour(format!("session step {:?} is not a number", v.trim()))
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_tour_step() -> Result<u32, TourError> {
|
||||
|
||||
Reference in New Issue
Block a user