use std::env; use std::ffi::OsStr; use std::fs::{self, File, OpenOptions}; use std::io;
cfg_if::cfg_if! { if#[cfg(not(target_os = "wasi"))] { use std::os::unix::fs::MetadataExt;
} else { #[cfg(feature = "nightly")] use std::os::wasi::fs::MetadataExt;
}
} usecrate::util; use std::path::Path;
#[cfg(not(target_os = "redox"))] use {
rustix::fs::{rename, unlink},
std::fs::hard_link,
};
#[cfg(not(target_os = "wasi"))]
{ use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
open_options.mode(permissions.map(|p| p.mode()).unwrap_or(0o600));
}
open_options.open(path)
}
fn create_unlinked(path: &Path) -> io::Result<File> { let tmp; // shadow this to decrease the lifetime. It can't live longer than `tmp`. letmut path = path; if !path.is_absolute() { let cur_dir = env::current_dir()?;
tmp = cur_dir.join(path);
path = &tmp;
}
let f = create_named(path, &mut OpenOptions::new(), None)?; // don't care whether the path has already been unlinked, // but perhaps there are some IO error conditions we should send up? let _ = fs::remove_file(path);
Ok(f)
}
#[cfg(target_os = "linux")] pubfn create(dir: &Path) -> io::Result<File> { use rustix::{fs::OFlags, io::Errno}; use std::os::unix::fs::OpenOptionsExt;
OpenOptions::new()
.read(true)
.write(true)
.custom_flags(OFlags::TMPFILE.bits() as i32) // do not mix with `create_new(true)`
.open(dir)
.or_else(|e| { match Errno::from_io_error(&e) { // These are the three "not supported" error codes for O_TMPFILE.
Some(Errno::OPNOTSUPP) | Some(Errno::ISDIR) | Some(Errno::NOENT) => {
create_unix(dir)
}
_ => Err(e),
}
})
}
#[cfg(any(not(target_os = "wasi"), feature = "nightly"))] pubfn reopen(file: &File, path: &Path) -> io::Result<File> { let new_file = OpenOptions::new().read(true).write(true).open(path)?; let old_meta = file.metadata()?; let new_meta = new_file.metadata()?; if old_meta.dev() != new_meta.dev() || old_meta.ino() != new_meta.ino() { return Err(io::Error::new(
io::ErrorKind::NotFound, "original tempfile has been replaced",
));
}
Ok(new_file)
}
#[cfg(all(target_os = "wasi", not(feature = "nightly")))] pubfn reopen(_file: &File, _path: &Path) -> io::Result<File> { return Err(io::Error::new(
io::ErrorKind::Other, "this operation is supported on WASI only on nightly Rust (with `nightly` feature enabled)",
));
}
#[cfg(not(target_os = "redox"))] pubfn persist(old_path: &Path, new_path: &Path, overwrite: bool) -> io::Result<()> { if overwrite {
rename(old_path, new_path)?;
} else { // On Linux, use `renameat_with` to avoid overwriting an existing name, // if the kernel and the filesystem support it. #[cfg(any(target_os = "android", target_os = "linux"))]
{ use rustix::fs::{renameat_with, RenameFlags, CWD}; use rustix::io::Errno; use std::sync::atomic::{AtomicBool, Ordering::Relaxed};
// Otherwise use `hard_link` to create the new filesystem name, which // will fail if the name already exists, and then `unlink` to remove // the old name.
hard_link(old_path, new_path)?;
// Ignore unlink errors. Can we do better? let _ = unlink(old_path);
}
Ok(())
}
#[cfg(target_os = "redox")] pubfn persist(_old_path: &Path, _new_path: &Path, _overwrite: bool) -> io::Result<()> { // XXX implement when possible use rustix::io::Errno;
Err(Errno::NOSYS.into())
}
pubfn keep(_: &Path) -> io::Result<()> {
Ok(())
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.17 Sekunden
(vorverarbeitet am 2026-06-19)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.