Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/agy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ pub(crate) fn build(
// print mode takes text only: an image goes in as a file path the agent can open
let mut p = x.to_string();
for (k, (m, b)) in imgs.iter().enumerate() {
let f = std::env::temp_dir().join(format!(
let n = format!(
"noname-{}-{t}-{k}.{}",
std::process::id(),
crate::img::ext(m)
));
if std::fs::write(&f, b).is_ok() {
);
if let Some(f) = crate::sys::tmp_write(&n, b) {
p.push_str(&format!("\n\n(attached image: {})", f.display()));
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/codex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,12 @@ impl App {
}
cmd.args(&c.args);
for (k, (m, b)) in imgs.iter().enumerate() {
let f = std::env::temp_dir().join(format!(
let n = format!(
"noname-{}-{t}-{k}.{}",
std::process::id(),
crate::img::ext(m)
));
if std::fs::write(&f, b).is_ok() {
);
if let Some(f) = crate::sys::tmp_write(&n, b) {
cmd.arg("-i").arg(f);
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4680,8 +4680,10 @@ fn ttl(a: &App) -> String {
}

fn editor(s: &str) -> Option<String> {
let f = std::env::temp_dir().join(format!("noname-prompt-{}.md", std::process::id()));
std::fs::write(&f, s).ok()?;
let f = sys::tmp_write(
&format!("noname-prompt-{}.md", std::process::id()),
s.as_bytes(),
)?;
let ed = std::env::var("VISUAL")
.or_else(|_| std::env::var("EDITOR"))
.unwrap_or_else(|_| {
Expand Down
6 changes: 3 additions & 3 deletions src/opencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ pub(crate) fn build(
cmd.arg(x);
}
for (k, (m, b)) in imgs.iter().enumerate() {
let f = std::env::temp_dir().join(format!(
let n = format!(
"noname-{}-{t}-{k}.{}",
std::process::id(),
crate::img::ext(m)
));
if std::fs::write(&f, b).is_ok() {
);
if let Some(f) = crate::sys::tmp_write(&n, b) {
cmd.arg("-f").arg(f);
}
}
Expand Down
30 changes: 30 additions & 0 deletions src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@
use std::path::PathBuf;
use std::process::{Child, Command};

/// Writes `b` to a new file `name` in the temp folder that only this user can read (the
/// shared /tmp would make it 0644). A file of that name that is in the way is removed first;
/// when that fails (someone else's file or link), nothing is written.
pub fn tmp_write(name: &str, b: &[u8]) -> Option<PathBuf> {
use std::io::Write;
let f = std::env::temp_dir().join(name);
let _ = std::fs::remove_file(&f);
let mut o = std::fs::OpenOptions::new();
o.write(true).create_new(true);
#[cfg(unix)]
std::os::unix::fs::OpenOptionsExt::mode(&mut o, 0o600);
o.open(&f).and_then(|mut x| x.write_all(b)).ok()?;
Some(f)
}

/// The home folder from `HOME` or `USERPROFILE`, or an empty string when neither is set.
pub fn home() -> String {
std::env::var("HOME")
Expand Down Expand Up @@ -827,6 +842,21 @@ pub fn opacity_back() {
mod tests {
use super::*;

#[test]
fn temp_files_are_private_and_fresh() {
let n = format!("noname-tmptest-{}", std::process::id());
let f = tmp_write(&n, b"old").unwrap();
let f2 = tmp_write(&n, b"new").unwrap();
assert_eq!(std::fs::read(&f2).unwrap(), b"new");
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let m = std::fs::metadata(&f).unwrap().permissions().mode();
assert_eq!(m & 0o777, 0o600);
}
let _ = std::fs::remove_file(f);
}

#[test]
fn panic_report_is_appended_not_overwritten() {
let d = std::env::temp_dir().join(format!("noname-panic-{}", std::process::id()));
Expand Down
Loading