Skip to content

Commit

Permalink
feat!: store header in record
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Apr 22, 2024
1 parent cf632f1 commit 452c476
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
35 changes: 26 additions & 9 deletions src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,21 @@ pub struct Record {

impl Record {
pub fn new(graveyard: &Path) -> Record {
Record {
path: graveyard.join(RECORD),
let path = graveyard.join(RECORD);
// Create the record file if it doesn't exist
if !path.exists() {
// Write a header to the record file
let mut record_file = fs::OpenOptions::new()
.truncate(true)
.create(true)
.write(true)
.open(&path)
.expect("Failed to open record file");
record_file
.write_all(b"Time\tOriginal\tDestination\n")
.expect("Failed to write header to record file");
}
Record { path }
}

pub fn open(&self) -> Result<fs::File, Error> {
Expand All @@ -60,7 +72,9 @@ impl Record {
// This will be None if there is nothing, or Some
// if there is items in the vector
let mut graves_to_exhume = Vec::new();
for entry in contents.lines().rev().map(RecordItem::new) {
let mut lines = contents.lines();
lines.next();
for entry in lines.rev().map(RecordItem::new) {
// Check that the file is still in the graveyard.
// If it is, return the corresponding line.
if util::symlink_exists(entry.dest) {
Expand All @@ -86,8 +100,9 @@ impl Record {
// Get the lines to write back to the record, which is every line except
// the ones matching the exhumed graves. Store them in a vector
// since we'll be overwriting the record in-place.
let lines_to_write: Vec<String> = BufReader::new(record_file)
.lines()
let mut reader = BufReader::new(record_file).lines();
reader.next();
let lines_to_write: Vec<String> = reader
.map_while(Result::ok)
.filter(|line| !graves.iter().any(|y| y == RecordItem::new(line).dest))
.collect();
Expand Down Expand Up @@ -116,8 +131,9 @@ impl Record {
graves: &'a [PathBuf],
) -> impl Iterator<Item = String> + 'a {
let record_file = self.open().unwrap();
BufReader::new(record_file)
.lines()
let mut reader = BufReader::new(record_file).lines();
reader.next();
reader
.map_while(Result::ok)
.filter(move |line| graves.iter().any(|y| y == RecordItem::new(line).dest))
}
Expand All @@ -128,8 +144,9 @@ impl Record {
gravepath: &'a PathBuf,
) -> io::Result<impl Iterator<Item = PathBuf> + 'a> {
let record_file = self.open()?;
Ok(BufReader::new(record_file)
.lines()
let mut reader = BufReader::new(record_file).lines();
reader.next();
Ok(reader
.map_while(Result::ok)
.map(|line| PathBuf::from(RecordItem::new(&line).dest))
.filter(move |d| d.starts_with(gravepath)))
Expand Down
9 changes: 2 additions & 7 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,16 +707,11 @@ fn read_empty_record() {
let _env_lock = aquire_lock();
let test_env = TestEnv::new();
let cwd = env::current_dir().unwrap();
fs::create_dir(&test_env.graveyard).unwrap();
let record = record::Record::new(&test_env.graveyard);
let gravepath = &util::join_absolute(&test_env.graveyard, dunce::canonicalize(cwd).unwrap());
let result = record.seance(gravepath);
assert!(result.is_err());
if let Err(e) = result {
assert_eq!(e.kind(), ErrorKind::NotFound);
assert_eq!(e.to_string(), "Failed to read record!");
} else {
panic!("Expected an error");
}
assert!(result.is_ok());
}

/// Hash the directory and all contents
Expand Down

0 comments on commit 452c476

Please sign in to comment.