Parcourir la source

Set history file permissions on UNIX based systems

Ferenc Nagy il y a 6 ans
Parent
commit
50f27189de
1 fichiers modifiés avec 19 ajouts et 1 suppressions
  1. 19 1
      src/history.rs

+ 19 - 1
src/history.rs

@@ -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")]