1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
// INSERT_README_VIA_MAKE
#[cfg(unix)]
extern crate rustix;
extern crate tempfile;
use std::convert::AsRef;
use std::error::Error as ErrorTrait;
use std::fmt;
use std::fs;
use std::io;
use std::path;
pub use OverwriteBehavior::{AllowOverwrite, DisallowOverwrite};
/// Whether to allow overwriting if the target file exists.
#[derive(Clone, Copy)]
pub enum OverwriteBehavior {
/// Overwrite files silently.
AllowOverwrite,
/// Don't overwrite files. `AtomicFile.write` will raise errors for such conditions only after
/// you've already written your data.
DisallowOverwrite,
}
/// Represents an error raised by `AtomicFile.write`.
#[derive(Debug)]
pub enum Error<E> {
/// The error originated in the library itself, while it was either creating a temporary file
/// or moving the file into place.
Internal(io::Error),
/// The error originated in the user-supplied callback.
User(E),
}
/// If your callback returns a `std::io::Error`, you can unwrap this type to `std::io::Error`.
impl From<Error<io::Error>> for io::Error {
fn from(e: Error<io::Error>) -> Self {
match e {
Error::Internal(x) => x,
Error::User(x) => x,
}
}
}
impl<E: fmt::Display> fmt::Display for Error<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Internal(ref e) => e.fmt(f),
Error::User(ref e) => e.fmt(f),
}
}
}
impl<E: ErrorTrait> ErrorTrait for Error<E> {
fn cause(&self) -> Option<&dyn ErrorTrait> {
match *self {
Error::Internal(ref e) => Some(e),
Error::User(ref e) => Some(e),
}
}
}
fn safe_parent(p: &path::Path) -> Option<&path::Path> {
match p.parent() {
None => None,
Some(x) if x.as_os_str().is_empty() => Some(path::Path::new(".")),
x => x,
}
}
/// Create a file and write to it atomically, in a callback.
pub struct AtomicFile {
/// Path to the final file that is atomically written.
path: path::PathBuf,
overwrite: OverwriteBehavior,
/// Directory to which to write the temporary subdirectories.
tmpdir: path::PathBuf,
}
impl AtomicFile {
/// Helper for writing to the file at `path` atomically, in write-only mode.
///
/// If `OverwriteBehaviour::DisallowOverwrite` is given,
/// an `Error::Internal` containing an `std::io::ErrorKind::AlreadyExists`
/// will be returned from `self.write(...)` if the file exists.
///
/// The temporary file is written to a temporary subdirectory in `.`, to ensure
/// it’s on the same filesystem (so that the move is atomic).
pub fn new<P>(path: P, overwrite: OverwriteBehavior) -> Self
where
P: AsRef<path::Path>,
{
let p = path.as_ref();
AtomicFile::new_with_tmpdir(
p,
overwrite,
safe_parent(p).unwrap_or_else(|| path::Path::new(".")),
)
}
/// Like `AtomicFile::new`, but the temporary file is written to a temporary subdirectory in `tmpdir`.
///
/// TODO: does `tmpdir` have to exist?
pub fn new_with_tmpdir<P, Q>(path: P, overwrite: OverwriteBehavior, tmpdir: Q) -> Self
where
P: AsRef<path::Path>,
Q: AsRef<path::Path>,
{
AtomicFile {
path: path.as_ref().to_path_buf(),
overwrite,
tmpdir: tmpdir.as_ref().to_path_buf(),
}
}
/// Move the file to `self.path()`. Only call once! Not exposed!
fn commit(&self, tmppath: &path::Path) -> io::Result<()> {
match self.overwrite {
AllowOverwrite => replace_atomic(tmppath, self.path()),
DisallowOverwrite => move_atomic(tmppath, self.path()),
}
}
/// Get the target filepath.
pub fn path(&self) -> &path::Path {
&self.path
}
/// Open a temporary file, call `f` on it (which is supposed to write to it), then move the
/// file atomically to `self.path`.
///
/// The temporary file is written to a randomized temporary subdirectory with prefix `.atomicwrite`.
pub fn write<T, E, F>(&self, f: F) -> Result<T, Error<E>>
where
F: FnOnce(&mut fs::File) -> Result<T, E>,
{
let mut options = fs::OpenOptions::new();
// These are the same options as `File::create`.
options.write(true).create(true).truncate(true);
self.write_with_options(f, options)
}
/// Open a temporary file with custom [`OpenOptions`], call `f` on it (which is supposed to
/// write to it), then move the file atomically to `self.path`.
///
/// The temporary file is written to a randomized temporary subdirectory with prefix
/// `.atomicwrite`.
///
/// [`OpenOptions`]: fs::OpenOptions
pub fn write_with_options<T, E, F>(&self, f: F, options: fs::OpenOptions) -> Result<T, Error<E>>
where
F: FnOnce(&mut fs::File) -> Result<T, E>,
{
let tmpdir = tempfile::Builder::new()
.prefix(".atomicwrite")
.tempdir_in(&self.tmpdir)
.map_err(Error::Internal)?;
let tmppath = tmpdir.path().join("tmpfile.tmp");
let rv = {
let mut tmpfile = options.open(&tmppath).map_err(Error::Internal)?;
let r = f(&mut tmpfile).map_err(Error::User)?;
tmpfile.sync_all().map_err(Error::Internal)?;
r
};
self.commit(&tmppath).map_err(Error::Internal)?;
Ok(rv)
}
}
#[cfg(all(unix, not(target_os = "redox")))]
mod imp {
use super::safe_parent;
use rustix::fs::AtFlags;
use std::{fs, io, path};
pub fn replace_atomic(src: &path::Path, dst: &path::Path) -> io::Result<()> {
let src_parent_path = safe_parent(src).unwrap();
let dst_parent_path = safe_parent(dst).unwrap();
let src_child_path = src.file_name().unwrap();
let dst_child_path = dst.file_name().unwrap();
// Open the parent directories. If src and dst have the same parent
// path, open it once and reuse it.
let src_parent = fs::File::open(src_parent_path)?;
let dst_parent;
let dst_parent = if src_parent_path == dst_parent_path {
&src_parent
} else {
dst_parent = fs::File::open(dst_parent_path)?;
&dst_parent
};
// Do the `renameat`.
rustix::fs::renameat(&src_parent, src_child_path, dst_parent, dst_child_path)?;
// Fsync the parent directory (or directories, if they're different).
src_parent.sync_all()?;
if src_parent_path != dst_parent_path {
dst_parent.sync_all()?;
}
Ok(())
}
pub fn move_atomic(src: &path::Path, dst: &path::Path) -> io::Result<()> {
let src_parent_path = safe_parent(src).unwrap();
let dst_parent_path = safe_parent(dst).unwrap();
let src_child_path = src.file_name().unwrap();
let dst_child_path = dst.file_name().unwrap();
// Open the parent directories. If src and dst have the same parent
// path, open it once and reuse it.
let src_parent = fs::File::open(src_parent_path)?;
let dst_parent;
let dst_parent = if src_parent_path == dst_parent_path {
&src_parent
} else {
dst_parent = fs::File::open(dst_parent_path)?;
&dst_parent
};
// On Linux, use `renameat2` with `RENAME_NOREPLACE` if we have it, as
// that does an atomic rename.
#[cfg(any(target_os = "android", target_os = "linux"))]
{
use rustix::fs::RenameFlags;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::Relaxed;
static NO_RENAMEAT2: AtomicBool = AtomicBool::new(false);
if !NO_RENAMEAT2.load(Relaxed) {
match rustix::fs::renameat_with(
&src_parent,
src_child_path,
dst_parent,
dst_child_path,
RenameFlags::NOREPLACE,
) {
Ok(()) => {
// Fsync the parent directory (or directories, if
// they're different).
src_parent.sync_all()?;
if src_parent_path != dst_parent_path {
dst_parent.sync_all()?;
}
return Ok(());
}
Err(rustix::io::Errno::NOSYS) => {
// The OS doesn't support `renameat2`; remember this so
// that we don't bother calling it again.
NO_RENAMEAT2.store(true, Relaxed);
}
Err(e) => return Err(e.into()),
}
}
}
// Otherwise, hard-link the src to the dst, and then delete the dst.
rustix::fs::linkat(
&src_parent,
src_child_path,
dst_parent,
dst_child_path,
AtFlags::empty(),
)?;
rustix::fs::unlinkat(&src_parent, src_child_path, AtFlags::empty())?;
// Fsync the parent directory (or directories, if they're different).
src_parent.sync_all()?;
if src_parent_path != dst_parent_path {
dst_parent.sync_all()?;
}
Ok(())
}
}
#[cfg(target_os = "redox")]
mod imp {
use std::{fs, io, path};
pub fn replace_atomic(src: &path::Path, dst: &path::Path) -> io::Result<()> {
fs::rename(src, dst)
}
pub fn move_atomic(src: &path::Path, dst: &path::Path) -> io::Result<()> {
fs::hard_link(src, dst)?;
fs::remove_file(src)
}
}
#[cfg(windows)]
mod imp {
extern crate windows_sys;
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
use std::{io, path};
macro_rules! call {
($e: expr) => {
if $e != 0 {
Ok(())
} else {
Err(io::Error::last_os_error())
}
};
}
fn path_to_windows_str<T: AsRef<OsStr>>(x: T) -> Vec<u16> {
x.as_ref().encode_wide().chain(Some(0)).collect()
}
pub fn replace_atomic(src: &path::Path, dst: &path::Path) -> io::Result<()> {
call!(unsafe {
windows_sys::Win32::Storage::FileSystem::MoveFileExW(
path_to_windows_str(src).as_ptr(),
path_to_windows_str(dst).as_ptr(),
windows_sys::Win32::Storage::FileSystem::MOVEFILE_WRITE_THROUGH
| windows_sys::Win32::Storage::FileSystem::MOVEFILE_REPLACE_EXISTING,
)
})
}
pub fn move_atomic(src: &path::Path, dst: &path::Path) -> io::Result<()> {
call!(unsafe {
windows_sys::Win32::Storage::FileSystem::MoveFileExW(
path_to_windows_str(src).as_ptr(),
path_to_windows_str(dst).as_ptr(),
windows_sys::Win32::Storage::FileSystem::MOVEFILE_WRITE_THROUGH,
)
})
}
}
/// Move `src` to `dst`. If `dst` exists, it will be silently overwritten.
///
/// Both paths must reside on the same filesystem for the operation to be atomic.
pub fn replace_atomic(src: &path::Path, dst: &path::Path) -> io::Result<()> {
imp::replace_atomic(src, dst)
}
/// Move `src` to `dst`. An error will be returned if `dst` exists.
///
/// Both paths must reside on the same filesystem for the operation to be atomic.
pub fn move_atomic(src: &path::Path, dst: &path::Path) -> io::Result<()> {
imp::move_atomic(src, dst)
}