| 1 |
use std::ffi::OsStr; |
| 2 |
use std::fs::{self, File, OpenOptions}; |
| 3 |
use std::io; |
| 4 |
|
| 5 |
use crate::util; |
| 6 |
use std::path::Path; |
| 7 |
|
| 8 |
use { |
| 9 |
rustix::fs::{rename, unlink}, |
| 10 |
std::fs::hard_link, |
| 11 |
}; |
| 12 |
|
| 13 |
pub fn create_named( |
| 14 |
path: &Path, |
| 15 |
open_options: &mut OpenOptions, |
| 16 |
#[cfg_attr(target_os = "wasi", allow(unused))] permissions: Option<&std::fs::Permissions>, |
| 17 |
) -> io::Result<File> { |
| 18 |
open_options.read(true).write(true).create_new(true); |
| 19 |
|
| 20 |
#[cfg(not(target_os = "wasi"))] |
| 21 |
{ |
| 22 |
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; |
| 23 |
open_options.mode(permissions.map(|p| p.mode()).unwrap_or(0o600)); |
| 24 |
} |
| 25 |
|
| 26 |
open_options.open(path) |
| 27 |
} |
| 28 |
|
| 29 |
fn create_unlinked(path: &Path) -> io::Result<File> { |
| 30 |
let tmp; |
| 31 |
// shadow this to decrease the lifetime. It can't live longer than `tmp`. |
| 32 |
let mut path = path; |
| 33 |
if !path.is_absolute() { |
| 34 |
let cur_dir = std::env::current_dir()?; |
| 35 |
tmp = cur_dir.join(path); |
| 36 |
path = &tmp; |
| 37 |
} |
| 38 |
|
| 39 |
let f = create_named(path, &mut OpenOptions::new(), None)?; |
| 40 |
// don't care whether the path has already been unlinked, |
| 41 |
// but perhaps there are some IO error conditions we should send up? |
| 42 |
let _ = fs::remove_file(path); |
| 43 |
Ok(f) |
| 44 |
} |
| 45 |
|
| 46 |
#[cfg(target_os = "linux")] |
| 47 |
pub fn create(dir: &Path) -> io::Result<File> { |
| 48 |
use rustix::{fs::OFlags, io::Errno}; |
| 49 |
use std::os::unix::fs::OpenOptionsExt; |
| 50 |
OpenOptions::new() |
| 51 |
.read(true) |
| 52 |
.write(true) |
| 53 |
.custom_flags(OFlags::TMPFILE.bits() as i32) // do not mix with `create_new(true)` |
| 54 |
.open(dir) |
| 55 |
.or_else(|e| { |
| 56 |
match Errno::from_io_error(&e) { |
| 57 |
// These are the three "not supported" error codes for O_TMPFILE. |
| 58 |
Some(Errno::OPNOTSUPP) | Some(Errno::ISDIR) | Some(Errno::NOENT) => { |
| 59 |
create_unix(dir) |
| 60 |
} |
| 61 |
_ => Err(e), |
| 62 |
} |
| 63 |
}) |
| 64 |
} |
| 65 |
|
| 66 |
#[cfg(not(target_os = "linux"))] |
| 67 |
pub fn create(dir: &Path) -> io::Result<File> { |
| 68 |
create_unix(dir) |
| 69 |
} |
| 70 |
|
| 71 |
fn create_unix(dir: &Path) -> io::Result<File> { |
| 72 |
util::create_helper( |
| 73 |
dir, |
| 74 |
OsStr::new(".tmp"), |
| 75 |
OsStr::new(""), |
| 76 |
crate::NUM_RAND_CHARS, |
| 77 |
|path| create_unlinked(&path), |
| 78 |
) |
| 79 |
} |
| 80 |
|
| 81 |
pub fn reopen(file: &File, path: &Path) -> io::Result<File> { |
| 82 |
let new_file = OpenOptions::new().read(true).write(true).open(path)?; |
| 83 |
let old_meta = rustix::fs::fstat(file)?; |
| 84 |
let new_meta = rustix::fs::fstat(&new_file)?; |
| 85 |
if old_meta.st_dev != new_meta.st_dev || old_meta.st_ino != new_meta.st_ino { |
| 86 |
return Err(io::Error::new( |
| 87 |
io::ErrorKind::NotFound, |
| 88 |
"original tempfile has been replaced", |
| 89 |
)); |
| 90 |
} |
| 91 |
Ok(new_file) |
| 92 |
} |
| 93 |
|
| 94 |
pub fn persist(old_path: &Path, new_path: &Path, overwrite: bool) -> io::Result<()> { |
| 95 |
if overwrite { |
| 96 |
rename(old_path, new_path)?; |
| 97 |
} else { |
| 98 |
// On Linux, apple and redox operating systems, use `renameat_with` to avoid overwriting an |
| 99 |
// existing name, if the kernel and the filesystem support it. |
| 100 |
#[cfg(any( |
| 101 |
target_os = "android", |
| 102 |
target_os = "linux", |
| 103 |
target_os = "macos", |
| 104 |
target_os = "ios", |
| 105 |
target_os = "tvos", |
| 106 |
target_os = "visionos", |
| 107 |
target_os = "watchos", |
| 108 |
target_os = "redox", |
| 109 |
))] |
| 110 |
{ |
| 111 |
use rustix::fs::{renameat_with, RenameFlags, CWD}; |
| 112 |
use rustix::io::Errno; |
| 113 |
use std::sync::atomic::{AtomicBool, Ordering::Relaxed}; |
| 114 |
|
| 115 |
static NOSYS: AtomicBool = AtomicBool::new(false); |
| 116 |
if !NOSYS.load(Relaxed) { |
| 117 |
match renameat_with(CWD, old_path, CWD, new_path, RenameFlags::NOREPLACE) { |
| 118 |
Ok(()) => return Ok(()), |
| 119 |
Err(Errno::NOSYS) => NOSYS.store(true, Relaxed), |
| 120 |
Err(Errno::INVAL) => {} |
| 121 |
Err(e) => return Err(e.into()), |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
// Otherwise use `hard_link` to create the new filesystem name, which |
| 127 |
// will fail if the name already exists, and then `unlink` to remove |
| 128 |
// the old name. |
| 129 |
hard_link(old_path, new_path)?; |
| 130 |
|
| 131 |
// Ignore unlink errors. Can we do better? |
| 132 |
let _ = unlink(old_path); |
| 133 |
} |
| 134 |
Ok(()) |
| 135 |
} |
| 136 |
|
| 137 |
pub fn keep(_: &Path) -> io::Result<()> { |
| 138 |
Ok(()) |
| 139 |
} |