diff options
Diffstat (limited to 'src/session.rs')
| -rw-r--r-- | src/session.rs | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/src/session.rs b/src/session.rs index 9f2b699..f27a188 100644 --- a/src/session.rs +++ b/src/session.rs | |||
| @@ -1,6 +1,8 @@ | |||
| 1 | use std::fs::{File, OpenOptions}; | 1 | use std::fs::{File, OpenOptions}; |
| 2 | use std::hash::{Hash, Hasher}; | 2 | use std::hash::{Hash, Hasher}; |
| 3 | use std::io::{BufRead, BufReader, Read, Write}; | 3 | use std::io::{BufRead, BufReader, Read, Write}; |
| 4 | use std::os::unix::fs::PermissionsExt; | ||
| 5 | use std::path::Path; | ||
| 4 | use std::path::PathBuf; | 6 | use std::path::PathBuf; |
| 5 | use std::time::{SystemTime, UNIX_EPOCH}; | 7 | use std::time::{SystemTime, UNIX_EPOCH}; |
| 6 | 8 | ||
| @@ -41,6 +43,11 @@ pub fn sessions_dir() -> PathBuf { | |||
| 41 | data_root().join("sessions") | 43 | data_root().join("sessions") |
| 42 | } | 44 | } |
| 43 | 45 | ||
| 46 | /// Sessions contain the whole conversation, so they are for this user only. | ||
| 47 | fn restrict(path: &Path, mode: u32) { | ||
| 48 | let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode)); | ||
| 49 | } | ||
| 50 | |||
| 44 | pub struct Session { | 51 | pub struct Session { |
| 45 | pub id: String, | 52 | pub id: String, |
| 46 | pub model: String, | 53 | pub model: String, |
| @@ -66,12 +73,15 @@ impl Session { | |||
| 66 | pub fn create_as(id: String, model: &str, system: &Value) -> Result<Session, String> { | 73 | pub fn create_as(id: String, model: &str, system: &Value) -> Result<Session, String> { |
| 67 | let dir = sessions_dir(); | 74 | let dir = sessions_dir(); |
| 68 | std::fs::create_dir_all(&dir).map_err(|e| format!("{}: {e}", dir.display()))?; | 75 | std::fs::create_dir_all(&dir).map_err(|e| format!("{}: {e}", dir.display()))?; |
| 76 | restrict(&data_root(), 0o700); | ||
| 77 | restrict(&dir, 0o700); | ||
| 69 | let path = dir.join(format!("{id}.jsonl")); | 78 | let path = dir.join(format!("{id}.jsonl")); |
| 70 | let mut file = OpenOptions::new() | 79 | let mut file = OpenOptions::new() |
| 71 | .create_new(true) | 80 | .create_new(true) |
| 72 | .append(true) | 81 | .append(true) |
| 73 | .open(&path) | 82 | .open(&path) |
| 74 | .map_err(|e| format!("{}: {e}", path.display()))?; | 83 | .map_err(|e| format!("{}: {e}", path.display()))?; |
| 84 | restrict(&path, 0o600); | ||
| 75 | 85 | ||
| 76 | let header = json!({ | 86 | let header = json!({ |
| 77 | "t": "header", | 87 | "t": "header", |
| @@ -141,6 +151,8 @@ impl Session { | |||
| 141 | .append(true) | 151 | .append(true) |
| 142 | .open(&path) | 152 | .open(&path) |
| 143 | .map_err(|e| format!("{}: {e}", path.display()))?; | 153 | .map_err(|e| format!("{}: {e}", path.display()))?; |
| 154 | // Older builds created these 0644. | ||
| 155 | restrict(&path, 0o600); | ||
| 144 | 156 | ||
| 145 | Ok(( | 157 | Ok(( |
| 146 | Session { | 158 | Session { |
