ソースを参照

Set history file permissions on UNIX based systems

Ferenc Nagy 6 年 前
コミット
50f27189de
1 ファイル変更19 行追加1 行削除
  1. 19 1
      src/history.rs

+ 19 - 1
src/history.rs

@@ -104,11 +104,25 @@ impl History {
             return Ok(());
             return Ok(());
         }
         }
 
 
-        // Ensure the file parnet directories are available
+        // Ensure the file parent directories are available
         if let Some(parent) = path.parent() {
         if let Some(parent) = path.parent() {
             fs::create_dir_all(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
         // Build the data and write to a file
         let data = toml::to_string(self)?;
         let data = toml::to_string(self)?;
         fs::write(&path, data)?;
         fs::write(&path, data)?;
@@ -305,6 +319,10 @@ pub enum SaveError {
     #[fail(display = "failed to write to the history file")]
     #[fail(display = "failed to write to the history file")]
     Write(#[cause] IoError),
     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
     /// Failed to delete the history file, which was tried because there
     /// are no history items to save.
     /// are no history items to save.
     #[fail(display = "failed to delete history file, because history is empty")]
     #[fail(display = "failed to delete history file, because history is empty")]