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 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
//! This crate offers functionality to write and overwrite files *atomically*, that is: without
//! leaving the file in an intermediate state. Either the new contents of the files are written to
//! the filesystem, or the old contents (if any) are preserved.
//!
//! This crate implements two main structs: [`AtomicWriteFile`] and [`OpenOptions`], which mimic
//! the standard [`std::fs::File`] and [`std::fs::OpenOptions`] as much as possible.
//!
//! This crate supports all major platforms, including: Unix systems, Windows, and WASI.
//!
//! # Motivation and Example
//!
//! Consider the following snippet of code to write a configuration file in JSON format:
//!
//! ```
//! # fn main() -> std::io::Result<()> {
//! # let test_dir = option_env!("TEST_DIR").unwrap_or("target/test-files");
//! # std::fs::create_dir_all(&test_dir).expect("failed to create test dir");
//! # std::env::set_current_dir(test_dir).expect("failed to move to test dir");
//! use std::io::Write;
//! use std::fs::File;
//!
//! let mut file = File::options()
//! .write(true)
//! .create(true)
//! .open("config.json")?;
//!
//! writeln!(file, "{{")?;
//! writeln!(file, " \"key1\": \"value1\",")?;
//! writeln!(file, " \"key2\": \"value2\"")?;
//! writeln!(file, "}}")?;
//! # Ok(())
//! # }
//! ```
//!
//! This code opens a file named `config.json`, truncates its contents (if the file already
//! existed), and writes the JSON content line-by-line.
//!
//! If the code is interrupted before all of the `writeln!` calls are completed (because of a
//! panic, or a signal is received, or the process is killed, or a filesystem error occurs), then
//! the file will be left in a broken state: it will not contain valid JSON data, and the original
//! contents (if any) will be lost.
//!
//! [`AtomicWriteFile`] solves this problem by placing the new contents into the destination file
//! only after it has been completely written to the filesystem. The snippet above can be rewritten
//! using [`AtomicWriteFile`] instead of [`File`] as follows:
//!
//! ```
//! # fn main() -> std::io::Result<()> {
//! # let test_dir = option_env!("TEST_DIR").unwrap_or("target/test-files");
//! # std::fs::create_dir_all(&test_dir).expect("failed to create test dir");
//! # std::env::set_current_dir(test_dir).expect("failed to move to test dir");
//! use std::io::Write;
//! use atomic_write_file::AtomicWriteFile;
//!
//! let mut file = AtomicWriteFile::options()
//! .open("config.json")?;
//!
//! writeln!(file, "{{")?;
//! writeln!(file, " \"key1\": \"value1\",")?;
//! writeln!(file, " \"key2\": \"value2\"")?;
//! writeln!(file, "}}")?;
//!
//! file.commit()?;
//! # Ok(())
//! # }
//! ```
//!
//! Note that this code is almost the same as the original, except that it now uses
//! `AtomicWriteFile` instead of `File` and there's an additional call to `commit()`.
//!
//! If the code is interrupted early, before the call to `commit()`, the original file
//! `config.json` will be left untouched. Only if the new contents are fully written to the
//! filesystem, `config.json` will get them.
//!
//! # How it works
//!
//! This crate works by creating a temporary file in the same directory as the destination file,
//! and then replacing the destination file with the temporary file once the new contents are fully
//! written to the filesystem.
//!
//! On **Unix**, the implementation is roughly equivalent to this pseudocode:
//!
//! ```text
//! fd = open("/path/to/directory/.filename.XXXXXX", O_WRONLY | O_CLOEXEC);
//! /* ... write contents ... */
//! fsync(fd);
//! rename("/path/to/directory/.filename.XXXXXX", "/path/to/directory/filename");
//! ```
//!
//! Where `XXXXXX` represents a random suffix. On **non-Unix** platforms, the implementation is
//! similar and uses the equivalent platform-specific system calls.
//!
//! On **Unix**, the actual implementation is more robust and makes use of directory file
//! descriptors (and the system calls `openat`, `linkat`, `renameat`) to make sure that, if the
//! directory is renamed or remounted during the operations, the file still ends up in the original
//! destination directory, and no cross-device writes happen.
//!
//! # Notes and Limitations
//!
//! * If the path of an [`AtomicWriteFile`] is a directory or a file that cannot be removed (due to
//! permissions or special attributes), an error will be produced when the [`AtomicWriteFile`] is
//! committed. This is in contrast with the standard `File`, which would instead produce an error
//! at `open()` time.
//!
//! * [`AtomicWriteFile`] is designed so that the temporary files it creates are automatically
//! removed if an error (such as a panic) occurs. However, if the process is interrupted abruptly
//! (without unwinding or running destructors), temporary files may be left on the filesystem.
//!
//! * If the path of an [`AtomicWriteFile`] is a symlink to another file, the symlink is replaced,
//! and the target of the original symlink is left untouched. If you intend to modify the file
//! pointed by a symlink at open time, call [`Path::canonicalize()`] prior to calling
//! [`AtomicWriteFile::open()`] or [`OpenOptions::open()`]. In the future, handling of symlinks
//! will be better customizable.
//!
//! * Because [`AtomicWriteFile`] works by creating a temporary file, and then replacing the
//! original file (see ["how it works"](#how-it-works) above), some metadata of the original file
//! may be lost:
//!
//! * On Unix, it is possible to preserve permissions and ownership of the original file.
//! However, it is not generally possible to preserve the same owner user/group of the original
//! file unless the process runs as root (or with the `CAP_CHOWN` capability on Linux). See
//! [`OpenOptionsExt::try_preserve_owner()`](crate::unix::OpenOptionsExt::try_preserve_owner)
//! for more details on the behavior of [`open()`](OpenOptions::open) when ownership cannot be
//! preserved.
//!
//! * On non-Unix platform, there is no support for preserving file permissions or ownership.
//! Support may be added in the future.
//!
//! * On all platforms, there is no support for preserving timestamps, ACLs (POSIX Access Control
//! Lists), Linux extended attributes (xattrs), or SELinux contexts. Support may be added in
//! the future.
//!
//! # Cargo features
//!
//! ## `unnamed-tmpfile` (Linux only)
//!
//! As explained in [how it works](#how-it-works), this crate works by creating a temporary file,
//! which is then renamed at commit time. By default, the temporary file has a path on the
//! filesystem, and as such if a crash occurs, there is a chance that the temporary file may be
//! left on the filesystem.
//!
//! On **Linux**, the implementation of this crate can make use of anonymous temporary files (files
//! opened with [`O_TMPFILE`](https://www.man7.org/linux/man-pages/man2/open.2.html)) if supported,
//! and the implementation is roughly equivalent to this pseudocode:
//!
//! ```text
//! fd = open("/path/to/directory", O_TMPFILE | O_WRONLY | O_CLOEXEC);
//! /* ... write contents ... */
//! fsync(fd);
//! link("/proc/self/fd/$fd", "/path/to/directory/.filename.XXXXXX");
//! rename("/path/to/directory/.filename.XXXXXX", "/path/to/directory/filename");
//! ```
//!
//! This feature has the following limitations:
//!
//! * The use of anonymous temporary files ensures that, if the process is interrupted abruptly
//! *before* a commit, the temporary file is automatically cleaned up by the operating system.
//! However, if the process is interrupted *during* a commit, it's still possible (although
//! unlikely) that a named temporary file will be left inside the destination directory.
//!
//! * This feature requires the `/proc` filesystem to be mounted. This makes [`AtomicWriteFile`]
//! with `unnamed-tmpfile` unsuitable for use in processes that run early at boot.
//!
//! This feature has no effect on platforms other than Linux.
#![warn(clippy::dbg_macro)]
#![warn(clippy::print_stderr)]
#![warn(clippy::print_stdout)]
#![warn(missing_debug_implementations)]
#![warn(unnameable_types)]
#![warn(unused_macro_rules)]
#![warn(missing_docs)]
#![warn(unreachable_pub)]
#![warn(unused_crate_dependencies)]
#![warn(unused_qualifications)]
#![doc(test(attr(deny(warnings))))]
#![cfg_attr(feature = "unstable-can_vector", feature(can_vector))]
#![cfg_attr(feature = "unstable-read_buf", feature(core_io_borrowed_buf))]
#![cfg_attr(feature = "unstable-read_buf", feature(read_buf))]
#![cfg_attr(feature = "unstable-seek_stream_len", feature(seek_stream_len))]
#![cfg_attr(
feature = "unstable-unix_file_vectored_at",
feature(unix_file_vectored_at)
)]
#![cfg_attr(feature = "unstable-write_all_vectored", feature(write_all_vectored))]
use std::fmt::Arguments;
use std::fs::File;
use std::io::IoSlice;
use std::io::IoSliceMut;
use std::io::Read;
use std::io::Result;
use std::io::Seek;
use std::io::SeekFrom;
use std::io::Write;
use std::ops::Deref;
use std::ops::DerefMut;
use std::path::Path;
#[cfg(feature = "unstable-read_buf")]
use std::io::BorrowedCursor;
mod imp;
mod dir;
pub use dir::Directory;
#[cfg(any(unix, target_os = "wasi"))]
mod fd;
#[cfg(unix)]
pub mod unix;
#[cfg(test)]
mod tests;
/// Options to configure how an [`AtomicWriteFile`] is opened.
///
/// This struct mimics the standard struct [`std::fs::OpenOptions`], and offers a subset of its
/// features that are applicable to [`AtomicWriteFile`].
///
/// Options can be set using methods like [`read()`](OpenOptions::read). Once the desired options
/// are set, the file can be opened using [`open()`](OpenOptions::open).
///
/// This crate offers some platform-specific extensions for `OpenOptions` in the form of traits:
///
/// * [`unix::OpenOptionsExt`]
///
/// # Notable differences between `std::fs::OpenOptions` and `atomic_write_file::OpenOptions`
///
/// The `OpenOptions` provided in this crate opens all files for writing by default, and the opened
/// file is always initially empty ("truncated"). As such, the following methods are not provided:
/// `write()`, `truncate()`, `append()`.
///
/// `create()` is not provided because a new file is always created if an original file does not
/// exist.
///
/// `create_new()` is also not provided because there is no way to ensure that a file never exists
/// from the time an [`AtomicWriteFile`] is opened to the time it is committed.
///
/// # Behavior when opening a file that already exists
///
/// When passing a path to [`open()`](OpenOptions::open) that points to a file that already exists,
/// [`AtomicWriteFile`] may preserve some of the metadata of the existing file (permissions,
/// ownership, and more). This behavior is platform-specific and can be controlled using the
/// platform-specific `OpenOptionsExt` traits. See also the ["notes and limitations" section on the
/// module-level documentations](crate#notes-and-limitations) for more information about what
/// metadata is preserved, what is not preserved, and in what circumstances.
///
/// # Examples
///
/// Opening a file for writing with default options (equivalent to a call to
/// [`AtomicWriteFile::open()`]):
///
/// ```
/// # let test_dir = option_env!("TEST_DIR").unwrap_or("target/test-files");
/// # std::fs::create_dir_all(&test_dir).expect("failed to create test dir");
/// # std::env::set_current_dir(test_dir).expect("failed to move to test dir");
/// use atomic_write_file::OpenOptions;
/// let file = OpenOptions::new().open("foo.txt");
/// # std::mem::drop(file);
/// ```
///
/// Opening a file for both reading and writing:
///
/// ```
/// # let test_dir = option_env!("TEST_DIR").unwrap_or("target/test-files");
/// # std::fs::create_dir_all(&test_dir).expect("failed to create test dir");
/// # std::env::set_current_dir(test_dir).expect("failed to move to test dir");
/// use atomic_write_file::OpenOptions;
/// let file = OpenOptions::new().read(true).open("foo.txt");
/// # std::mem::drop(file)
/// ```
#[derive(Clone, Debug)]
pub struct OpenOptions {
inner: imp::OpenOptions,
}
impl OpenOptions {
/// Create a set of options set to their default values.
pub fn new() -> Self {
Self {
inner: imp::OpenOptions::new(),
}
}
/// Sets the option for read access.
///
/// If `true`, the file will be readable (other than being writeable) once opened using, for
/// example, the [`Read`] trait. Note that if opening an already-existing file, the original
/// file contents will not be readable. Only the new contents of the file will be readable.
///
/// If `false` (the default), the file is opened in write-only mode.
///
/// # Examples
///
/// ```
/// # fn main() -> std::io::Result<()> {
/// # let test_dir = option_env!("TEST_DIR").unwrap_or("target/test-files");
/// # std::fs::create_dir_all(&test_dir).expect("failed to create test dir");
/// # std::env::set_current_dir(test_dir).expect("failed to move to test dir");
/// use std::io::Seek;
/// use std::io::Write;
/// use std::io::read_to_string;
/// use atomic_write_file::OpenOptions;
///
/// let mut file = OpenOptions::new().read(true).open("foo.txt")?;
/// writeln!(file, "hello")?;
///
/// file.rewind()?;
/// assert_eq!(read_to_string(&file)?, "hello\n");
/// # Ok(())
/// # }
/// ```
pub fn read(&mut self, read: bool) -> &mut Self {
self.inner.read = read;
self
}
/// Opens the file at `path` with this set of options.
///
/// This has the same semantics as [`std::fs::OpenOptions::open()`], except that it returns an
/// [`AtomicWriteFile`] instead of a [`File`].
///
/// # Examples
///
/// ```
/// # let test_dir = option_env!("TEST_DIR").unwrap_or("target/test-files");
/// # std::fs::create_dir_all(&test_dir).expect("failed to create test dir");
/// # std::env::set_current_dir(test_dir).expect("failed to move to test dir");
/// use atomic_write_file::OpenOptions;
/// let file = OpenOptions::new().read(true).open("foo.txt");
/// # std::mem::drop(file)
/// ```
pub fn open<P: AsRef<Path>>(&self, path: P) -> Result<AtomicWriteFile> {
let path = path.as_ref().to_path_buf();
let temporary_file = imp::TemporaryFile::open(&self.inner, &path)?;
Ok(AtomicWriteFile {
temporary_file,
finalized: false,
})
}
}
impl Default for OpenOptions {
fn default() -> Self {
Self::new()
}
}
/// A file whose contents become visible to users only after the file is committed.
///
/// An `AtomicWriteFile` is a file that is assigned to a path, but whose contents won't appear at
/// that path until the file is [committed](AtomicWriteFile::commit). If `AtomicWriteFile` is used
/// to open a file that already exists, the contents of the existing file will remain available
/// until the `AtomicWriteFile` is committed. During that time, the `AtomicWriteFile` may be used
/// to write new contents, but these new contents won't be visible until after the file is
/// committed.
///
/// Internally, `AtomicWriteFile` is implemented by initally opening a temporary file, and then
/// renaming the temporary file to its final path on commit. See the [module-level
/// documentation](crate) for more details about the implementation.
///
/// An `AtomicWriteFile` is automatically discarded when it goes out of scope (when it gets
/// dropped). Any error that occurs on drop is ignored. For this reason, if the file should not be
/// committed, it is highly recommended that `AtomicWriteFile` is discarded explicitly using the
/// [`discard()`](AtomicWriteFile::discard) method, which allows callers to detect errors on
/// cleanup. See [committing or discarding changes](#committing-or-discarding-changes) below for
/// more information.
///
/// # Opening an `AtomicWriteFile`
///
/// There are two ways to obtain an `AtomicWriteFile` struct:
///
/// * [`AtomicWriteFile::open()`]
/// * [`OpenOptions::open()`]
///
/// The first method opens a file at the specified path with some default options. The second
/// method using [`OpenOptions`] allows configuring how the file is opened.
///
/// # Compatibility with `std::fs::File`
///
/// `AtomicWriteFile` implements the same methods and traits of [`std::fs::File`], and aims to be
/// as much compatible with `File` as possible. In fact, `AtomicWriteFile` can be
/// [dereferenced](Deref) into a `File` struct: this means that you can use all methods provided by
/// [`File`] directly on an `AtomicWriteFile` (just like you can use all of [`str`] methods on a
/// [`String`]).
///
/// A reference to the wrapped `File` struct may also be explicitly obtained using
/// [`as_file()`](AtomicWriteFile::as_file) and [`as_file_mut()`](AtomicWriteFile::as_file_mut).
///
/// # Committing or discarding changes
///
/// `AtomicWriteFile` provides two additional methods that are not provided by [`File`]:
/// [`commit()`](AtomicWriteFile::commit) and [`discard()`](AtomicWriteFile::discard). These
/// methods can be called to save the new contents to the file path, or to destroy the new contents
/// and leave the original file (if any) unchaged, respectively.
///
/// Changes are automatically discarded also when `AtomicWriteFile` is dropped. Therefore calling
/// [`discard()`](AtomicWriteFile::discard) is not mandatory, but it is highly recommended because
/// the [`Drop`] implementation ignores all errors.
///
/// # Cloning
///
/// Cloning a `AtomicWriteFile` is not possible, because this would result in ambiguity and race
/// conditions when committing the file and its clones. It is however possible to clone the
/// underlaying [`File`] struct using [`try_clone()`](File::try_clone). Writes to this cloned
/// [`File`] however won't be atomic after the `AtomicWriteFile` is committed.
///
/// # Examples
///
/// Opening a file, writing new contents, and committing the changes:
///
/// ```
/// # fn main() -> std::io::Result<()> {
/// # let test_dir = option_env!("TEST_DIR").unwrap_or("target/test-files");
/// # std::fs::create_dir_all(&test_dir).expect("failed to create test dir");
/// # std::env::set_current_dir(test_dir).expect("failed to move to test dir");
/// use std::io::Write;
/// use atomic_write_file::AtomicWriteFile;
///
/// let mut file = AtomicWriteFile::open("foo.txt")?; // if "foo.txt" already exists, it is not
/// // initially truncated or deleted
/// writeln!(file, "hello")?; // "hello" is written to a temporary location; "foo.txt" (if it
/// // exists) keeps its old contents after this write
///
/// file.commit()?; // only now "foo.txt" gets swapped with the new contents ("hello")
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct AtomicWriteFile {
temporary_file: imp::TemporaryFile,
finalized: bool,
}
impl AtomicWriteFile {
/// Opens an atomically-written file at `path`.
///
/// See [`OpenOptions`] for more details.
///
/// # Examples
///
/// ```
/// # let test_dir = option_env!("TEST_DIR").unwrap_or("target/test-files");
/// # std::fs::create_dir_all(&test_dir).expect("failed to create test dir");
/// # std::env::set_current_dir(test_dir).expect("failed to move to test dir");
/// use atomic_write_file::AtomicWriteFile;
/// let file = AtomicWriteFile::open("foo.txt");
/// # std::mem::drop(file);
/// ```
#[inline]
pub fn open<P: AsRef<Path>>(path: P) -> Result<AtomicWriteFile> {
OpenOptions::new().open(path)
}
/// Creates a new [`OpenOptions`] with default options.
///
/// This is equivalent to [`OpenOptions::new()`], but allows for more readable code.
///
/// # Examples
///
/// ```
/// # let test_dir = option_env!("TEST_DIR").unwrap_or("target/test-files");
/// # std::fs::create_dir_all(&test_dir).expect("failed to create test dir");
/// # std::env::set_current_dir(test_dir).expect("failed to move to test dir");
/// use atomic_write_file::AtomicWriteFile;
/// let file = AtomicWriteFile::options().read(true).open("foo.txt");
/// # std::mem::drop(file)
/// ```
#[inline]
pub fn options() -> OpenOptions {
OpenOptions::new()
}
/// Returns a reference to the underlaying [`File`] struct.
///
/// The returned reference can be used to inspect or manipulate the contents and metadata of
/// this `AtomicWriteFile`.
///
/// # Examples
///
/// ```
/// # #[cfg(any(unix, target_os = "wasi"))]
/// # fn main() -> std::io::Result<()> {
/// # let test_dir = option_env!("TEST_DIR").unwrap_or("target/test-files");
/// # std::fs::create_dir_all(&test_dir).expect("failed to create test dir");
/// # std::env::set_current_dir(test_dir).expect("failed to move to test dir");
/// use std::os::fd::AsRawFd;
/// use atomic_write_file::AtomicWriteFile;
///
/// let file = AtomicWriteFile::open("foo.txt")?;
/// assert_eq!(file.as_raw_fd(), file.as_file().as_raw_fd());
/// # Ok(())
/// # }
/// # #[cfg(not(any(unix, target_os = "wasi")))]
/// # fn main() -> std::io::Result<()> {
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn as_file(&self) -> &File {
&self.temporary_file.file
}
/// Returns a mutable reference to the underlaying [`File`] struct.
///
/// The returned reference can be used to inspect or manipulate the contents and metadata of
/// this `AtomicWriteFile`.
///
/// # Examples
///
/// ```
/// # fn main() -> std::io::Result<()> {
/// # let test_dir = option_env!("TEST_DIR").unwrap_or("target/test-files");
/// # std::fs::create_dir_all(&test_dir).expect("failed to create test dir");
/// # std::env::set_current_dir(test_dir).expect("failed to move to test dir");
/// use std::io::Write;
/// use atomic_write_file::AtomicWriteFile;
///
/// let mut file = AtomicWriteFile::open("foo.txt")?;
/// writeln!(file.as_file_mut(), "hello")?;
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn as_file_mut(&mut self) -> &mut File {
&mut self.temporary_file.file
}
/// Returns a borrowed reference to the directory containing the file.
///
/// This method allows you to obtain the directory file descriptor, without having to open it
/// through a call to `open(2)`. This method is guaranteed to make no system calls.
///
/// The returned struct supports only two operations:
/// - conversion to a borrowed directory file descriptor through
/// [`AsFd::as_fd()`](std::os::fd::AsFd::as_fd)
/// - conversion to a raw directory file descriptor through
/// [`AsRawFd::as_raw_fd()`](std::os::fd::AsRawFd::as_raw_fd)
///
/// This method will return a result only if the platform supports directory file descriptors,
/// and if the `AtomicWriteFile` implementation makes use of them. In all other cases, this
/// method returns `None`.
///
/// # Examples
///
/// ```
/// # fn main() -> std::io::Result<()> {
/// # let test_dir = option_env!("TEST_DIR").unwrap_or("target/test-files");
/// # std::fs::create_dir_all(&test_dir).expect("failed to create test dir");
/// # std::env::set_current_dir(test_dir).expect("failed to move to test dir");
/// # #[cfg(any(unix, target_os = "wasi"))]
/// use std::os::fd::AsFd;
/// use atomic_write_file::AtomicWriteFile;
///
/// let file = AtomicWriteFile::open("foo.txt")?;
/// if let Some(dir) = file.directory() {
/// # #[cfg(any(unix, target_os = "wasi"))]
/// let borrowed_fd = dir.as_fd();
/// # #[cfg(any(unix, target_os = "wasi"))]
/// println!("directory fd: {:?}", borrowed_fd);
/// # #[cfg(not(any(unix, target_os = "wasi")))]
/// # let _ = dir;
/// }
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn directory(&self) -> Option<Directory<'_>> {
self.temporary_file.directory().map(Directory::new)
}
/// Saves the contents of this file to its path.
///
/// After calling `commit()`, the `AtomicWriteFile` is consumed and can no longer be used.
/// Clones of the underlaying [`File`] may still be used after calling `commit()`, although any
/// write from that point onwards will no longer be atomic.
///
/// See the documentation for [`AtomicWriteFile`] and the [module-level documentation](crate)
/// for details about the internal implementation of `commit()`, as well as platform-specific
/// details.
///
/// This method is automatically called when `AtomicWriteFile` is dropped, although in that
/// case any error produced by `commit()` is ignored.
///
/// See also [`AtomicWriteFile::discard()`].
///
/// # Examples
///
/// ```
/// # fn main() -> std::io::Result<()> {
/// # let test_dir = option_env!("TEST_DIR").unwrap_or("target/test-files");
/// # std::fs::create_dir_all(&test_dir).expect("failed to create test dir");
/// # std::env::set_current_dir(test_dir).expect("failed to move to test dir");
/// use std::io::Write;
/// use atomic_write_file::AtomicWriteFile;
///
/// let file = AtomicWriteFile::open("foo.txt")?;
/// writeln!(&file, "hello")?;
/// file.commit()?;
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn commit(mut self) -> Result<()> {
self._commit()
}
fn _commit(&mut self) -> Result<()> {
if self.finalized {
return Ok(());
}
self.finalized = true;
self.sync_all()?;
self.temporary_file.rename_file()
}
/// Discard the contents of this file, and leave its path unchanged.
///
/// After calling `discard()`, the `AtomicWriteFile` is consumed and can no longer be used.
/// Clones of the underlaying [`File`] may still be used after calling `discard()`.
///
/// See also [`AtomicWriteFile::commit()`].
///
/// # Examples
///
/// ```
/// # fn main() -> std::io::Result<()> {
/// # let test_dir = option_env!("TEST_DIR").unwrap_or("target/test-files");
/// # std::fs::create_dir_all(&test_dir).expect("failed to create test dir");
/// # std::env::set_current_dir(test_dir).expect("failed to move to test dir");
/// use std::io::Write;
/// use atomic_write_file::AtomicWriteFile;
///
/// let file = AtomicWriteFile::open("foo.txt")?;
/// writeln!(&file, "hello")?;
/// file.discard()?;
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn discard(mut self) -> Result<()> {
self._discard()
}
fn _discard(&mut self) -> Result<()> {
if self.finalized {
return Ok(());
}
self.finalized = true;
self.temporary_file.remove_file()
}
}
impl Drop for AtomicWriteFile {
#[inline]
fn drop(&mut self) {
// Ignore all errors
let _ = self._discard();
}
}
impl Deref for AtomicWriteFile {
type Target = File;
#[inline]
fn deref(&self) -> &Self::Target {
self.as_file()
}
}
impl DerefMut for AtomicWriteFile {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_file_mut()
}
}
impl Read for AtomicWriteFile {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
self.temporary_file.file.read(buf)
}
#[inline]
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize> {
self.temporary_file.file.read_vectored(bufs)
}
#[inline]
#[cfg(feature = "unstable-can_vector")]
fn is_read_vectored(&self) -> bool {
self.temporary_file.file.is_read_vectored()
}
#[inline]
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
self.temporary_file.file.read_to_end(buf)
}
#[inline]
fn read_to_string(&mut self, buf: &mut String) -> Result<usize> {
self.temporary_file.file.read_to_string(buf)
}
#[inline]
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
self.temporary_file.file.read_exact(buf)
}
#[inline]
#[cfg(feature = "unstable-read_buf")]
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<()> {
self.temporary_file.file.read_buf(buf)
}
#[inline]
#[cfg(feature = "unstable-read_buf")]
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<()> {
self.temporary_file.file.read_buf_exact(cursor)
}
}
impl Read for &AtomicWriteFile {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
(&self.temporary_file.file).read(buf)
}
#[inline]
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize> {
(&self.temporary_file.file).read_vectored(bufs)
}
#[inline]
#[cfg(feature = "unstable-can_vector")]
fn is_read_vectored(&self) -> bool {
self.temporary_file.file.is_read_vectored()
}
#[inline]
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
(&self.temporary_file.file).read_to_end(buf)
}
#[inline]
fn read_to_string(&mut self, buf: &mut String) -> Result<usize> {
(&self.temporary_file.file).read_to_string(buf)
}
#[inline]
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
(&self.temporary_file.file).read_exact(buf)
}
#[inline]
#[cfg(feature = "unstable-read_buf")]
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<()> {
(&self.temporary_file.file).read_buf(buf)
}
#[inline]
#[cfg(feature = "unstable-read_buf")]
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<()> {
(&self.temporary_file.file).read_buf_exact(cursor)
}
}
impl Write for AtomicWriteFile {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize> {
self.temporary_file.file.write(buf)
}
#[inline]
fn flush(&mut self) -> Result<()> {
self.temporary_file.file.flush()
}
#[inline]
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize> {
self.temporary_file.file.write_vectored(bufs)
}
#[inline]
#[cfg(feature = "unstable-can_vector")]
fn is_write_vectored(&self) -> bool {
self.temporary_file.file.is_write_vectored()
}
#[inline]
fn write_all(&mut self, buf: &[u8]) -> Result<()> {
self.temporary_file.file.write_all(buf)
}
#[inline]
#[cfg(feature = "unstable-write_all_vectored")]
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()> {
self.temporary_file.file.write_all_vectored(bufs)
}
#[inline]
fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<()> {
self.temporary_file.file.write_fmt(fmt)
}
}
impl Write for &AtomicWriteFile {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize> {
(&self.temporary_file.file).write(buf)
}
#[inline]
fn flush(&mut self) -> Result<()> {
(&self.temporary_file.file).flush()
}
#[inline]
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize> {
(&self.temporary_file.file).write_vectored(bufs)
}
#[inline]
#[cfg(feature = "unstable-can_vector")]
fn is_write_vectored(&self) -> bool {
(&self.temporary_file.file).is_write_vectored()
}
#[inline]
fn write_all(&mut self, buf: &[u8]) -> Result<()> {
(&self.temporary_file.file).write_all(buf)
}
#[inline]
#[cfg(feature = "unstable-write_all_vectored")]
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()> {
(&self.temporary_file.file).write_all_vectored(bufs)
}
#[inline]
fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<()> {
(&self.temporary_file.file).write_fmt(fmt)
}
}
impl Seek for AtomicWriteFile {
#[inline]
fn seek(&mut self, pos: SeekFrom) -> Result<u64> {
self.temporary_file.file.seek(pos)
}
#[inline]
fn rewind(&mut self) -> Result<()> {
self.temporary_file.file.rewind()
}
#[inline]
#[cfg(feature = "unstable-seek_stream_len")]
fn stream_len(&mut self) -> Result<u64> {
self.temporary_file.file.stream_len()
}
#[inline]
fn stream_position(&mut self) -> Result<u64> {
self.temporary_file.file.stream_position()
}
}
impl Seek for &AtomicWriteFile {
#[inline]
fn seek(&mut self, pos: SeekFrom) -> Result<u64> {
(&self.temporary_file.file).seek(pos)
}
#[inline]
fn rewind(&mut self) -> Result<()> {
(&self.temporary_file.file).rewind()
}
#[inline]
#[cfg(feature = "unstable-seek_stream_len")]
fn stream_len(&mut self) -> Result<u64> {
(&self.temporary_file.file).stream_len()
}
#[inline]
fn stream_position(&mut self) -> Result<u64> {
(&self.temporary_file.file).stream_position()
}
}