Deleting files before overwriting them

pull/46/head
Dennis Schwerdel 2019-12-19 16:08:51 +01:00
parent ea9e3cd5e1
commit 0d29293309
3 changed files with 15 additions and 2 deletions

View File

@ -10,6 +10,7 @@ This project follows [semantic versioning](http://semver.org).
- [changed] Set keepalive to 120 secs when NAT is detected
- [fixed] Added parameter keepalive to manpage
- [fixed] Fixed problems on stats file when dropping permissions
- [fixed] Deleting files before overwriting them
### v1.1.0 (2019-12-04)

View File

@ -213,9 +213,13 @@ impl<TS: TimeSource> BeaconSerializer<TS> {
pub fn write_to_file<P: AsRef<Path>>(&self, peers: &[SocketAddr], path: P) -> Result<(), io::Error> {
let beacon = self.encode(peers);
debug!("Beacon: {}", beacon);
let mut f = File::create(&path)?;
let path = path.as_ref();
if path.exists() {
fs::remove_file(path)?
}
let mut f = File::create(path)?;
writeln!(&mut f, "{}", beacon)?;
fs::set_permissions(&path, Permissions::from_mode(0o644))?;
fs::set_permissions(path, Permissions::from_mode(0o444))?;
Ok(())
}

View File

@ -103,6 +103,10 @@ struct DualLogger {
impl DualLogger {
pub fn new<P: AsRef<Path>>(path: Option<P>) -> Result<Self, io::Error> {
if let Some(path) = path {
let path = path.as_ref();
if path.exists() {
fs::remove_file(path)?
}
let file = File::create(path)?;
Ok(DualLogger { file: Mutex::new(Some(file)) })
} else {
@ -269,6 +273,10 @@ fn run<P: Protocol>(config: Config) {
let stats_file = match config.stats_file {
None => None,
Some(ref name) => {
let path = Path::new(name);
if path.exists() {
try_fail!(fs::remove_file(path), "Failed to remove file {}: {}", name);
}
let file = try_fail!(File::create(name), "Failed to create stats file: {}");
try_fail!(
fs::set_permissions(name, Permissions::from_mode(0o644)),