updating key files permissions on save (#1010)

* chmod when saving keyfile content

* to func

* returning error upstream instead of panic
This commit is contained in:
Nikolay Volf 2016-04-28 17:59:40 +03:00 committed by Gav Wood
parent a86c39f7fa
commit d238b5e578
1 changed files with 13 additions and 0 deletions

View File

@ -465,6 +465,14 @@ pub struct KeyDirectory {
cache_usage: RwLock<VecDeque<Uuid>>,
}
fn restrict_permissions_owner(file_path: &Path) -> Result<(), i32> {
let cstr = ::std::ffi::CString::new(file_path.to_str().unwrap()).unwrap();
match unsafe { ::libc::chmod(cstr.as_ptr(), ::libc::S_IWUSR | ::libc::S_IRUSR) } {
0 => Ok(()),
x => Err(x),
}
}
impl KeyDirectory {
/// Initializes new cache directory context with a given `path`
pub fn new(path: &Path) -> KeyDirectory {
@ -484,6 +492,11 @@ impl KeyDirectory {
let json_bytes = json_text.into_bytes();
try!(file.write(&json_bytes));
}
if let Err(error_code) = restrict_permissions_owner(self.key_path(&key_file.id).as_path()) {
fs::remove_file(self.key_path(&key_file.id)).unwrap();
warn!(target: "sstore", "fatal: failed to modify permissions of the file (chmod: {})", error_code);
return Err(::std::io::Error::last_os_error());
}
let mut cache = self.cache.write().unwrap();
let id = key_file.id.clone();
cache.insert(id.clone(), key_file);