Add read-only memfd backed temporary files
parent
fbf2b7dc7b
commit
413be3f334
|
@ -42,8 +42,8 @@ enum Cursor {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum EmbedStatus {
|
enum EmbedStatus {
|
||||||
Stopped(Arc<Mutex<EmbedGrid>>, File),
|
Stopped(Arc<Mutex<EmbedGrid>>, MeliFile),
|
||||||
Running(Arc<Mutex<EmbedGrid>>, File),
|
Running(Arc<Mutex<EmbedGrid>>, MeliFile),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::ops::Deref for EmbedStatus {
|
impl std::ops::Deref for EmbedStatus {
|
||||||
|
@ -1132,10 +1132,11 @@ impl Component for Composer {
|
||||||
};
|
};
|
||||||
/* update Draft's headers based on form values */
|
/* update Draft's headers based on form values */
|
||||||
self.update_draft();
|
self.update_draft();
|
||||||
let f = create_temp_file(
|
let f = MeliFile::create_temp_file(
|
||||||
self.draft.to_string().unwrap().as_str().as_bytes(),
|
self.draft.to_string().unwrap().as_str().as_bytes(),
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
|
false,
|
||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -1230,11 +1231,11 @@ impl Component for Composer {
|
||||||
));
|
));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
let f = create_temp_file(&[], None, None, true);
|
let f = MeliFile::create_temp_file(&[], None, None, false, true);
|
||||||
match std::process::Command::new("sh")
|
match std::process::Command::new("sh")
|
||||||
.args(&["-c", command])
|
.args(&["-c", command])
|
||||||
.stdin(std::process::Stdio::null())
|
.stdin(std::process::Stdio::null())
|
||||||
.stdout(std::process::Stdio::from(f.file()))
|
.stdout(std::process::Stdio::from(f.get_file()))
|
||||||
.spawn()
|
.spawn()
|
||||||
{
|
{
|
||||||
Ok(child) => {
|
Ok(child) => {
|
||||||
|
|
|
@ -26,8 +26,8 @@ use std::process::{Command, Stdio};
|
||||||
pub fn verify_signature(a: &Attachment, context: &mut Context) -> Vec<u8> {
|
pub fn verify_signature(a: &Attachment, context: &mut Context) -> Vec<u8> {
|
||||||
match melib::signatures::verify_signature(a) {
|
match melib::signatures::verify_signature(a) {
|
||||||
Ok((bytes, sig)) => {
|
Ok((bytes, sig)) => {
|
||||||
let bytes_file = create_temp_file(&bytes, None, None, true);
|
let bytes_file = MeliFile::create_temp_file(&bytes, None, None, true, true);
|
||||||
let signature_file = create_temp_file(sig, None, None, true);
|
let signature_file = MeliFile::create_temp_file(sig, None, None, true, true);
|
||||||
match Command::new(
|
match Command::new(
|
||||||
context
|
context
|
||||||
.settings
|
.settings
|
||||||
|
|
|
@ -541,11 +541,12 @@ impl MailView {
|
||||||
name_opt = name.as_ref().map(|n| n.clone());
|
name_opt = name.as_ref().map(|n| n.clone());
|
||||||
}
|
}
|
||||||
if let Ok(binary) = binary {
|
if let Ok(binary) = binary {
|
||||||
let p = create_temp_file(
|
let p = MeliFile::create_temp_file(
|
||||||
&decode(u, None),
|
&decode(u, None),
|
||||||
name_opt.as_ref().map(String::as_str),
|
name_opt.as_ref().map(String::as_str),
|
||||||
None,
|
None,
|
||||||
true,
|
true,
|
||||||
|
true,
|
||||||
);
|
);
|
||||||
match debug!(context.plugin_manager.activate_hook(
|
match debug!(context.plugin_manager.activate_hook(
|
||||||
"attachment-view",
|
"attachment-view",
|
||||||
|
|
|
@ -439,11 +439,12 @@ impl Component for EnvelopeView {
|
||||||
let attachment_type = u.mime_type();
|
let attachment_type = u.mime_type();
|
||||||
let binary = query_default_app(&attachment_type);
|
let binary = query_default_app(&attachment_type);
|
||||||
if let Ok(binary) = binary {
|
if let Ok(binary) = binary {
|
||||||
let p = create_temp_file(
|
let p = MeliFile::create_temp_file(
|
||||||
&decode(u, None),
|
&decode(u, None),
|
||||||
name.as_ref().map(String::as_str),
|
name.as_ref().map(String::as_str),
|
||||||
None,
|
None,
|
||||||
true,
|
true,
|
||||||
|
true,
|
||||||
);
|
);
|
||||||
match Command::new(&binary)
|
match Command::new(&binary)
|
||||||
.arg(p.path())
|
.arg(p.path())
|
||||||
|
|
|
@ -133,7 +133,7 @@ impl Component for HtmlView {
|
||||||
if let UIEvent::Input(Key::Char('v')) = event {
|
if let UIEvent::Input(Key::Char('v')) = event {
|
||||||
let binary = query_default_app("text/html");
|
let binary = query_default_app("text/html");
|
||||||
if let Ok(binary) = binary {
|
if let Ok(binary) = binary {
|
||||||
let p = create_temp_file(&self.bytes, None, None, true);
|
let p = MeliFile::create_temp_file(&self.bytes, None, None, true, true);
|
||||||
match Command::new(&binary)
|
match Command::new(&binary)
|
||||||
.arg(p.path())
|
.arg(p.path())
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
|
|
|
@ -1133,7 +1133,7 @@ impl Account {
|
||||||
if let Some(mailbox_hash) = saved_at {
|
if let Some(mailbox_hash) = saved_at {
|
||||||
Ok(mailbox_hash)
|
Ok(mailbox_hash)
|
||||||
} else {
|
} else {
|
||||||
let file = crate::types::create_temp_file(bytes, None, None, false);
|
let file = crate::types::MeliFile::create_temp_file(bytes, None, None, false, false);
|
||||||
debug!("message saved in {}", file.path.display());
|
debug!("message saved in {}", file.path.display());
|
||||||
melib::log(
|
melib::log(
|
||||||
format!(
|
format!(
|
||||||
|
@ -1684,7 +1684,9 @@ impl Account {
|
||||||
let r = channel.try_recv().unwrap();
|
let r = channel.try_recv().unwrap();
|
||||||
if let Some(Err(err)) = r {
|
if let Some(Err(err)) = r {
|
||||||
melib::log(format!("Could not save message: {}", err), melib::ERROR);
|
melib::log(format!("Could not save message: {}", err), melib::ERROR);
|
||||||
let file = crate::types::create_temp_file(bytes, None, None, false);
|
let file = crate::types::MeliFile::create_temp_file(
|
||||||
|
bytes, None, None, false, false,
|
||||||
|
);
|
||||||
debug!("message saved in {}", file.path.display());
|
debug!("message saved in {}", file.path.display());
|
||||||
melib::log(
|
melib::log(
|
||||||
format!(
|
format!(
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
/*! Find mailcap entries to execute attachments.
|
/*! Find mailcap entries to execute attachments.
|
||||||
*/
|
*/
|
||||||
use crate::state::Context;
|
use crate::state::Context;
|
||||||
use crate::types::{create_temp_file, ForkType, UIEvent};
|
use crate::types::{ForkType, MeliFile, UIEvent};
|
||||||
use melib::attachments::decode;
|
use melib::attachments::decode;
|
||||||
use melib::text_processing::GlobMatch;
|
use melib::text_processing::GlobMatch;
|
||||||
use melib::{email::Attachment, MeliError, Result};
|
use melib::{email::Attachment, MeliError, Result};
|
||||||
|
@ -157,7 +157,13 @@ impl MailcapEntry {
|
||||||
.map(|arg| match *arg {
|
.map(|arg| match *arg {
|
||||||
"%s" => {
|
"%s" => {
|
||||||
needs_stdin = false;
|
needs_stdin = false;
|
||||||
let _f = create_temp_file(&decode(a, None), None, None, true);
|
let _f = MeliFile::create_temp_file(
|
||||||
|
&decode(a, None),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
);
|
||||||
let p = _f.path().display().to_string();
|
let p = _f.path().display().to_string();
|
||||||
f = Some(_f);
|
f = Some(_f);
|
||||||
p
|
p
|
||||||
|
|
|
@ -117,7 +117,7 @@ pub struct Context {
|
||||||
pub children: Vec<std::process::Child>,
|
pub children: Vec<std::process::Child>,
|
||||||
pub plugin_manager: PluginManager,
|
pub plugin_manager: PluginManager,
|
||||||
|
|
||||||
pub temp_files: Vec<File>,
|
pub temp_files: Vec<MeliFile>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Context {
|
impl Context {
|
||||||
|
|
|
@ -32,8 +32,8 @@
|
||||||
*/
|
*/
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod helpers;
|
mod files;
|
||||||
pub use self::helpers::*;
|
pub use self::files::*;
|
||||||
|
|
||||||
use super::command::Action;
|
use super::command::Action;
|
||||||
use super::jobs::JobId;
|
use super::jobs::JobId;
|
||||||
|
@ -86,7 +86,7 @@ pub enum ForkType {
|
||||||
/// Embed pty
|
/// Embed pty
|
||||||
Embed(Pid),
|
Embed(Pid),
|
||||||
Generic(std::process::Child),
|
Generic(std::process::Child),
|
||||||
NewDraft(File, std::process::Child),
|
NewDraft(MeliFile, std::process::Child),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Copy, Clone)]
|
#[derive(Debug, PartialEq, Copy, Clone)]
|
||||||
|
|
|
@ -0,0 +1,198 @@
|
||||||
|
/*
|
||||||
|
* meli
|
||||||
|
*
|
||||||
|
* Copyright 2017-2018 Manos Pitsidianakis
|
||||||
|
*
|
||||||
|
* This file is part of meli.
|
||||||
|
*
|
||||||
|
* meli is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* meli is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with meli. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use std::fs;
|
||||||
|
use std::fs::OpenOptions;
|
||||||
|
use std::io::{Read, Write};
|
||||||
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
enum FileType {
|
||||||
|
Real,
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
Memory {
|
||||||
|
fd: std::os::unix::io::RawFd,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
impl core::fmt::Debug for FileType {
|
||||||
|
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
|
||||||
|
match self {
|
||||||
|
FileType::Real => fmt.debug_struct("FileType::Real").finish(),
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
FileType::Memory { fd } => fmt
|
||||||
|
.debug_struct(&format!("FileType::Memory({})", fd))
|
||||||
|
.finish(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct MeliFile {
|
||||||
|
backing: FileType,
|
||||||
|
pub path: PathBuf,
|
||||||
|
delete_on_drop: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for MeliFile {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
if self.delete_on_drop {
|
||||||
|
std::fs::remove_file(self.path()).unwrap_or_else(|_| {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MeliFile {
|
||||||
|
pub fn get_file(&self) -> std::fs::File {
|
||||||
|
OpenOptions::new()
|
||||||
|
.read(true)
|
||||||
|
.write(true)
|
||||||
|
.create(true)
|
||||||
|
.open(&self.path)
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn path(&self) -> &PathBuf {
|
||||||
|
&self.path
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_to_string(&self) -> String {
|
||||||
|
let mut buf = Vec::new();
|
||||||
|
let mut f = fs::File::open(&self.path)
|
||||||
|
.unwrap_or_else(|_| panic!("Can't open {}", &self.path.display()));
|
||||||
|
f.read_to_end(&mut buf)
|
||||||
|
.unwrap_or_else(|_| panic!("Can't read {}", &self.path.display()));
|
||||||
|
String::from_utf8(buf).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returned [`MeliFile`] will be deleted when dropped if delete_on_drop is set, so make sure to
|
||||||
|
/// add it on [`Context'] `temp_files` to reap it later.
|
||||||
|
pub fn create_temp_file(
|
||||||
|
bytes: &[u8],
|
||||||
|
filename: Option<&str>,
|
||||||
|
path: Option<&PathBuf>,
|
||||||
|
read_only: bool,
|
||||||
|
delete_on_drop: bool,
|
||||||
|
) -> MeliFile {
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
if delete_on_drop && read_only && filename.is_none() && path.is_none() {
|
||||||
|
debug!("creating memfd");
|
||||||
|
match MeliFile::create_mem_file(bytes) {
|
||||||
|
Ok(f) => return f,
|
||||||
|
Err(err) => {
|
||||||
|
debug!("creating memfd failed {:?}", &err);
|
||||||
|
melib::log(
|
||||||
|
format!(
|
||||||
|
"Could not memfd_create file of len {}: {}",
|
||||||
|
bytes.len(),
|
||||||
|
err
|
||||||
|
),
|
||||||
|
melib::LoggingLevel::DEBUG,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut dir = std::env::temp_dir();
|
||||||
|
|
||||||
|
let path = path.unwrap_or_else(|| {
|
||||||
|
dir.push("meli");
|
||||||
|
std::fs::DirBuilder::new()
|
||||||
|
.recursive(true)
|
||||||
|
.create(&dir)
|
||||||
|
.unwrap();
|
||||||
|
if let Some(filename) = filename {
|
||||||
|
dir.push(filename)
|
||||||
|
} else {
|
||||||
|
let u = Uuid::new_v4();
|
||||||
|
dir.push(u.to_hyphenated().to_string());
|
||||||
|
}
|
||||||
|
&dir
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut f = std::fs::File::create(path).unwrap();
|
||||||
|
let metadata = f.metadata().unwrap();
|
||||||
|
let mut permissions = metadata.permissions();
|
||||||
|
|
||||||
|
permissions.set_mode(0o600); // Read/write for owner only.
|
||||||
|
f.set_permissions(permissions).unwrap();
|
||||||
|
|
||||||
|
f.write_all(bytes).unwrap();
|
||||||
|
f.flush().unwrap();
|
||||||
|
MeliFile {
|
||||||
|
backing: FileType::Real,
|
||||||
|
path: path.clone(),
|
||||||
|
delete_on_drop,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
pub fn create_mem_file(bytes: &[u8]) -> melib::Result<MeliFile> {
|
||||||
|
use std::convert::TryInto;
|
||||||
|
|
||||||
|
use nix::fcntl::SealFlag;
|
||||||
|
use nix::sys::memfd::{memfd_create, MemFdCreateFlag};
|
||||||
|
use std::ffi::CStr;
|
||||||
|
let name: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(&b"meli\0"[..]) };
|
||||||
|
let len = bytes
|
||||||
|
.len()
|
||||||
|
.try_into()
|
||||||
|
.map_err(|err| Box::new(err) as Box<dyn std::error::Error + Sync + Send + 'static>)?;
|
||||||
|
|
||||||
|
let fd = debug!(memfd_create(
|
||||||
|
name,
|
||||||
|
MemFdCreateFlag::MFD_ALLOW_SEALING //| MemFdCreateFlag::MFD_CLOEXEC,
|
||||||
|
))?;
|
||||||
|
debug!(nix::unistd::ftruncate(fd, len))?;
|
||||||
|
let addr = unsafe {
|
||||||
|
debug!(nix::sys::mman::mmap(
|
||||||
|
std::ptr::null_mut(),
|
||||||
|
bytes.len(),
|
||||||
|
nix::sys::mman::ProtFlags::PROT_WRITE,
|
||||||
|
nix::sys::mman::MapFlags::MAP_SHARED,
|
||||||
|
fd,
|
||||||
|
0,
|
||||||
|
))?
|
||||||
|
};
|
||||||
|
unsafe { std::ptr::copy_nonoverlapping(bytes.as_ptr(), addr as *mut u8, bytes.len()) };
|
||||||
|
debug!(unsafe { nix::sys::mman::munmap(addr, bytes.len()) })?;
|
||||||
|
debug!(nix::fcntl::fcntl(
|
||||||
|
fd,
|
||||||
|
nix::fcntl::FcntlArg::F_ADD_SEALS(
|
||||||
|
SealFlag::F_SEAL_SHRINK
|
||||||
|
| SealFlag::F_SEAL_GROW
|
||||||
|
| SealFlag::F_SEAL_WRITE
|
||||||
|
| SealFlag::F_SEAL_SEAL,
|
||||||
|
),
|
||||||
|
))?;
|
||||||
|
Ok(MeliFile {
|
||||||
|
backing: FileType::Memory { fd },
|
||||||
|
path: PathBuf::from(format!(
|
||||||
|
"/proc/{pid}/fd/{fd}",
|
||||||
|
pid = std::process::id(),
|
||||||
|
fd = fd
|
||||||
|
)),
|
||||||
|
delete_on_drop: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,105 +0,0 @@
|
||||||
/*
|
|
||||||
* meli
|
|
||||||
*
|
|
||||||
* Copyright 2017-2018 Manos Pitsidianakis
|
|
||||||
*
|
|
||||||
* This file is part of meli.
|
|
||||||
*
|
|
||||||
* meli is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* meli is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with meli. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
use std::fs;
|
|
||||||
use std::fs::OpenOptions;
|
|
||||||
use std::io::{Read, Write};
|
|
||||||
use std::os::unix::fs::PermissionsExt;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct File {
|
|
||||||
pub path: PathBuf,
|
|
||||||
delete_on_drop: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drop for File {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
if self.delete_on_drop {
|
|
||||||
std::fs::remove_file(self.path()).unwrap_or_else(|_| {});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl File {
|
|
||||||
pub fn file(&self) -> std::fs::File {
|
|
||||||
OpenOptions::new()
|
|
||||||
.read(true)
|
|
||||||
.write(true)
|
|
||||||
.create(true)
|
|
||||||
.open(&self.path)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn path(&self) -> &PathBuf {
|
|
||||||
&self.path
|
|
||||||
}
|
|
||||||
pub fn read_to_string(&self) -> String {
|
|
||||||
let mut buf = Vec::new();
|
|
||||||
let mut f = fs::File::open(&self.path)
|
|
||||||
.unwrap_or_else(|_| panic!("Can't open {}", &self.path.display()));
|
|
||||||
f.read_to_end(&mut buf)
|
|
||||||
.unwrap_or_else(|_| panic!("Can't read {}", &self.path.display()));
|
|
||||||
String::from_utf8(buf).unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returned `File` will be deleted when dropped if delete_on_drop is set, so make sure to add it on `context.temp_files`
|
|
||||||
/// to reap it later.
|
|
||||||
pub fn create_temp_file(
|
|
||||||
bytes: &[u8],
|
|
||||||
filename: Option<&str>,
|
|
||||||
path: Option<&PathBuf>,
|
|
||||||
delete_on_drop: bool,
|
|
||||||
) -> File {
|
|
||||||
let mut dir = std::env::temp_dir();
|
|
||||||
|
|
||||||
let path = path.unwrap_or_else(|| {
|
|
||||||
dir.push("meli");
|
|
||||||
std::fs::DirBuilder::new()
|
|
||||||
.recursive(true)
|
|
||||||
.create(&dir)
|
|
||||||
.unwrap();
|
|
||||||
if let Some(filename) = filename {
|
|
||||||
dir.push(filename)
|
|
||||||
} else {
|
|
||||||
let u = Uuid::new_v4();
|
|
||||||
dir.push(u.to_hyphenated().to_string());
|
|
||||||
}
|
|
||||||
&dir
|
|
||||||
});
|
|
||||||
|
|
||||||
let mut f = std::fs::File::create(path).unwrap();
|
|
||||||
let metadata = f.metadata().unwrap();
|
|
||||||
let mut permissions = metadata.permissions();
|
|
||||||
|
|
||||||
permissions.set_mode(0o600); // Read/write for owner only.
|
|
||||||
f.set_permissions(permissions).unwrap();
|
|
||||||
|
|
||||||
f.write_all(bytes).unwrap();
|
|
||||||
f.flush().unwrap();
|
|
||||||
File {
|
|
||||||
path: path.clone(),
|
|
||||||
delete_on_drop,
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue