Skip to content

Commit

Permalink
Implementing backup of broken config files
Browse files Browse the repository at this point in the history
This addresses issue fxbox#390.
  • Loading branch information
Christiane Ruetten committed May 3, 2016
1 parent 478c34a commit 7641536
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/config_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,20 @@ impl ConfigStore {
Err(error) => {
error!("Unable to generate JSON from config file {}: {}",
file_name, error.to_string());
empty_config
let mut backup_name = file_name.to_owned();
backup_name.push_str("_BROKEN");
match fs::rename(file_name, &backup_name) {
Ok(_) => {
warn!("Moved broken config file to {}", backup_name);
},
Err(error) => {
error!("Error while writing config backup to {}: {}",
backup_name, error.to_string());
panic!("Unrecoverable error: unable to backup broken config");
}
}
warn!("Continuing with empty config file");
empty_config
}
};

Expand Down Expand Up @@ -270,5 +283,28 @@ describe! config {
assert_eq!(foo_bar, "baz");
};
}

it "ConfigService should back-up broken config files" {
use std::io::{ Read, Write };
use std::path::Path;
// Create broken config file
let broken_conf = "{\"foo\":\"bar\",}".to_owned();
let mut file = fs::File::create(&Path::new(&config_file_name)).ok().unwrap();
let _ = file.write_all(&broken_conf.as_bytes());
// Block to make `config` go out of scope
{
let config = ConfigService::new(&config_file_name);
config.set("foo", "bar", "baz");
}
// `config` should now be out of scope and dropped
{
let mut backup_name = config_file_name.clone();
backup_name.push_str("_BROKEN");
let mut file = fs::File::open(&Path::new(&backup_name)).ok().unwrap();
let mut moved_conf = String::new();
let _ = file.read_to_string(&mut moved_conf);
assert_eq!(moved_conf, broken_conf);
};
}
}
}

0 comments on commit 7641536

Please sign in to comment.