Set history file permissions on UNIX based systems

This commit is contained in:
Ferenc Nagy 2019-03-18 22:29:39 +01:00
parent 9b1cf9dab4
commit 50f27189de

View file

@ -104,11 +104,25 @@ impl History {
return Ok(());
}
// Ensure the file parnet directories are available
// Ensure the file parent directories are available
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
// Set file permissions on unix based systems
#[cfg(unix)] {
use std::fs::Permissions;
use std::os::unix::fs::PermissionsExt;
if !path.exists() {
let file = fs::File::create(path).map_err(SaveError::Write)?;
// Set Read/Write permissions for the user
file.set_permissions(Permissions::from_mode(0o600))
.map_err(SaveError::SetPermissions)?;
}
}
// Build the data and write to a file
let data = toml::to_string(self)?;
fs::write(&path, data)?;
@ -305,6 +319,10 @@ pub enum SaveError {
#[fail(display = "failed to write to the history file")]
Write(#[cause] IoError),
/// Failed to set file permissions to the history file.
#[fail(display = "failed to set permissions to the history file")]
SetPermissions(#[cause] IoError),
/// Failed to delete the history file, which was tried because there
/// are no history items to save.
#[fail(display = "failed to delete history file, because history is empty")]