1 // ignore-tidy-filelength
3 //! Filesystem manipulation operations.
5 //! This module contains basic methods to manipulate the contents of the local
6 //! filesystem. All methods in this module represent cross-platform filesystem
7 //! operations. Extra platform-specific functionality can be found in the
8 //! extension traits of `std::os::$platform`.
10 #![stable(feature = "rust1", since = "1.0.0")]
13 use crate::ffi::OsString;
14 use crate::io::{self, SeekFrom, Seek, Read, Initializer, Write, IoSlice, IoSliceMut};
15 use crate::path::{Path, PathBuf};
16 use crate::sys::fs as fs_imp;
17 use crate::sys_common::{AsInnerMut, FromInner, AsInner, IntoInner};
18 use crate::time::SystemTime;
20 /// A reference to an open file on the filesystem.
22 /// An instance of a `File` can be read and/or written depending on what options
23 /// it was opened with. Files also implement [`Seek`] to alter the logical cursor
24 /// that the file contains internally.
26 /// Files are automatically closed when they go out of scope. Errors detected
27 /// on closing are ignored by the implementation of `Drop`. Use the method
28 /// [`sync_all`] if these errors must be manually handled.
32 /// Creates a new file and write bytes to it:
35 /// use std::fs::File;
36 /// use std::io::prelude::*;
38 /// fn main() -> std::io::Result<()> {
39 /// let mut file = File::create("foo.txt")?;
40 /// file.write_all(b"Hello, world!")?;
45 /// Read the contents of a file into a [`String`]:
48 /// use std::fs::File;
49 /// use std::io::prelude::*;
51 /// fn main() -> std::io::Result<()> {
52 /// let mut file = File::open("foo.txt")?;
53 /// let mut contents = String::new();
54 /// file.read_to_string(&mut contents)?;
55 /// assert_eq!(contents, "Hello, world!");
60 /// It can be more efficient to read the contents of a file with a buffered
61 /// [`Read`]er. This can be accomplished with [`BufReader<R>`]:
64 /// use std::fs::File;
65 /// use std::io::BufReader;
66 /// use std::io::prelude::*;
68 /// fn main() -> std::io::Result<()> {
69 /// let file = File::open("foo.txt")?;
70 /// let mut buf_reader = BufReader::new(file);
71 /// let mut contents = String::new();
72 /// buf_reader.read_to_string(&mut contents)?;
73 /// assert_eq!(contents, "Hello, world!");
78 /// Note that, although read and write methods require a `&mut File`, because
79 /// of the interfaces for [`Read`] and [`Write`], the holder of a `&File` can
80 /// still modify the file, either through methods that take `&File` or by
81 /// retrieving the underlying OS object and modifying the file that way.
82 /// Additionally, many operating systems allow concurrent modification of files
83 /// by different processes. Avoid assuming that holding a `&File` means that the
84 /// file will not change.
86 /// [`Seek`]: ../io/trait.Seek.html
87 /// [`String`]: ../string/struct.String.html
88 /// [`Read`]: ../io/trait.Read.html
89 /// [`Write`]: ../io/trait.Write.html
90 /// [`BufReader<R>`]: ../io/struct.BufReader.html
91 /// [`sync_all`]: struct.File.html#method.sync_all
92 #[stable(feature = "rust1", since = "1.0.0")]
97 /// Metadata information about a file.
99 /// This structure is returned from the [`metadata`] or
100 /// [`symlink_metadata`] function or method and represents known
101 /// metadata about a file such as its permissions, size, modification
104 /// [`metadata`]: fn.metadata.html
105 /// [`symlink_metadata`]: fn.symlink_metadata.html
106 #[stable(feature = "rust1", since = "1.0.0")]
108 pub struct Metadata(fs_imp::FileAttr);
110 /// Iterator over the entries in a directory.
112 /// This iterator is returned from the [`read_dir`] function of this module and
113 /// will yield instances of [`io::Result`]`<`[`DirEntry`]`>`. Through a [`DirEntry`]
114 /// information like the entry's path and possibly other metadata can be
119 /// This [`io::Result`] will be an [`Err`] if there's some sort of intermittent
120 /// IO error during iteration.
122 /// [`read_dir`]: fn.read_dir.html
123 /// [`DirEntry`]: struct.DirEntry.html
124 /// [`io::Result`]: ../io/type.Result.html
125 /// [`Err`]: ../result/enum.Result.html#variant.Err
126 #[stable(feature = "rust1", since = "1.0.0")]
128 pub struct ReadDir(fs_imp::ReadDir);
130 /// Entries returned by the [`ReadDir`] iterator.
132 /// [`ReadDir`]: struct.ReadDir.html
134 /// An instance of `DirEntry` represents an entry inside of a directory on the
135 /// filesystem. Each entry can be inspected via methods to learn about the full
136 /// path or possibly other metadata through per-platform extension traits.
137 #[stable(feature = "rust1", since = "1.0.0")]
138 pub struct DirEntry(fs_imp::DirEntry);
140 /// Options and flags which can be used to configure how a file is opened.
142 /// This builder exposes the ability to configure how a [`File`] is opened and
143 /// what operations are permitted on the open file. The [`File::open`] and
144 /// [`File::create`] methods are aliases for commonly used options using this
147 /// [`File`]: struct.File.html
148 /// [`File::open`]: struct.File.html#method.open
149 /// [`File::create`]: struct.File.html#method.create
151 /// Generally speaking, when using `OpenOptions`, you'll first call [`new`],
152 /// then chain calls to methods to set each option, then call [`open`],
153 /// passing the path of the file you're trying to open. This will give you a
154 /// [`io::Result`][result] with a [`File`][file] inside that you can further
157 /// [`new`]: struct.OpenOptions.html#method.new
158 /// [`open`]: struct.OpenOptions.html#method.open
159 /// [result]: ../io/type.Result.html
160 /// [file]: struct.File.html
164 /// Opening a file to read:
167 /// use std::fs::OpenOptions;
169 /// let file = OpenOptions::new().read(true).open("foo.txt");
172 /// Opening a file for both reading and writing, as well as creating it if it
176 /// use std::fs::OpenOptions;
178 /// let file = OpenOptions::new()
182 /// .open("foo.txt");
184 #[derive(Clone, Debug)]
185 #[stable(feature = "rust1", since = "1.0.0")]
186 pub struct OpenOptions(fs_imp::OpenOptions);
188 /// Representation of the various permissions on a file.
190 /// This module only currently provides one bit of information, [`readonly`],
191 /// which is exposed on all currently supported platforms. Unix-specific
192 /// functionality, such as mode bits, is available through the
193 /// [`PermissionsExt`] trait.
195 /// [`readonly`]: struct.Permissions.html#method.readonly
196 /// [`PermissionsExt`]: ../os/unix/fs/trait.PermissionsExt.html
197 #[derive(Clone, PartialEq, Eq, Debug)]
198 #[stable(feature = "rust1", since = "1.0.0")]
199 pub struct Permissions(fs_imp::FilePermissions);
201 /// A structure representing a type of file with accessors for each file type.
202 /// It is returned by [`Metadata::file_type`] method.
204 /// [`Metadata::file_type`]: struct.Metadata.html#method.file_type
205 #[stable(feature = "file_type", since = "1.1.0")]
206 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
207 pub struct FileType(fs_imp::FileType);
209 /// A builder used to create directories in various manners.
211 /// This builder also supports platform-specific options.
212 #[stable(feature = "dir_builder", since = "1.6.0")]
214 pub struct DirBuilder {
215 inner: fs_imp::DirBuilder,
219 /// Indicates how large a buffer to pre-allocate before reading the entire file.
220 fn initial_buffer_size(file: &File) -> usize {
221 // Allocate one extra byte so the buffer doesn't need to grow before the
222 // final `read` call at the end of the file. Don't worry about `usize`
223 // overflow because reading will fail regardless in that case.
224 file.metadata().map(|m| m.len() as usize + 1).unwrap_or(0)
227 /// Read the entire contents of a file into a bytes vector.
229 /// This is a convenience function for using [`File::open`] and [`read_to_end`]
230 /// with fewer imports and without an intermediate variable. It pre-allocates a
231 /// buffer based on the file size when available, so it is generally faster than
232 /// reading into a vector created with `Vec::new()`.
234 /// [`File::open`]: struct.File.html#method.open
235 /// [`read_to_end`]: ../io/trait.Read.html#method.read_to_end
239 /// This function will return an error if `path` does not already exist.
240 /// Other errors may also be returned according to [`OpenOptions::open`].
242 /// [`OpenOptions::open`]: struct.OpenOptions.html#method.open
244 /// It will also return an error if it encounters while reading an error
245 /// of a kind other than [`ErrorKind::Interrupted`].
247 /// [`ErrorKind::Interrupted`]: ../../std/io/enum.ErrorKind.html#variant.Interrupted
253 /// use std::net::SocketAddr;
255 /// fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
256 /// let foo: SocketAddr = String::from_utf8_lossy(&fs::read("address.txt")?).parse()?;
260 #[stable(feature = "fs_read_write_bytes", since = "1.26.0")]
261 pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
262 fn inner(path: &Path) -> io::Result<Vec<u8>> {
263 let mut file = File::open(path)?;
264 let mut bytes = Vec::with_capacity(initial_buffer_size(&file));
265 file.read_to_end(&mut bytes)?;
271 /// Read the entire contents of a file into a string.
273 /// This is a convenience function for using [`File::open`] and [`read_to_string`]
274 /// with fewer imports and without an intermediate variable. It pre-allocates a
275 /// buffer based on the file size when available, so it is generally faster than
276 /// reading into a string created with `String::new()`.
278 /// [`File::open`]: struct.File.html#method.open
279 /// [`read_to_string`]: ../io/trait.Read.html#method.read_to_string
283 /// This function will return an error if `path` does not already exist.
284 /// Other errors may also be returned according to [`OpenOptions::open`].
286 /// [`OpenOptions::open`]: struct.OpenOptions.html#method.open
288 /// It will also return an error if it encounters while reading an error
289 /// of a kind other than [`ErrorKind::Interrupted`],
290 /// or if the contents of the file are not valid UTF-8.
292 /// [`ErrorKind::Interrupted`]: ../../std/io/enum.ErrorKind.html#variant.Interrupted
298 /// use std::net::SocketAddr;
300 /// fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
301 /// let foo: SocketAddr = fs::read_to_string("address.txt")?.parse()?;
305 #[stable(feature = "fs_read_write", since = "1.26.0")]
306 pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
307 fn inner(path: &Path) -> io::Result<String> {
308 let mut file = File::open(path)?;
309 let mut string = String::with_capacity(initial_buffer_size(&file));
310 file.read_to_string(&mut string)?;
316 /// Write a slice as the entire contents of a file.
318 /// This function will create a file if it does not exist,
319 /// and will entirely replace its contents if it does.
321 /// This is a convenience function for using [`File::create`] and [`write_all`]
322 /// with fewer imports.
324 /// [`File::create`]: struct.File.html#method.create
325 /// [`write_all`]: ../io/trait.Write.html#method.write_all
332 /// fn main() -> std::io::Result<()> {
333 /// fs::write("foo.txt", b"Lorem ipsum")?;
334 /// fs::write("bar.txt", "dolor sit")?;
338 #[stable(feature = "fs_read_write_bytes", since = "1.26.0")]
339 pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result<()> {
340 fn inner(path: &Path, contents: &[u8]) -> io::Result<()> {
341 File::create(path)?.write_all(contents)
343 inner(path.as_ref(), contents.as_ref())
347 /// Attempts to open a file in read-only mode.
349 /// See the [`OpenOptions::open`] method for more details.
353 /// This function will return an error if `path` does not already exist.
354 /// Other errors may also be returned according to [`OpenOptions::open`].
356 /// [`OpenOptions::open`]: struct.OpenOptions.html#method.open
361 /// use std::fs::File;
363 /// fn main() -> std::io::Result<()> {
364 /// let mut f = File::open("foo.txt")?;
368 #[stable(feature = "rust1", since = "1.0.0")]
369 pub fn open<P: AsRef<Path>>(path: P) -> io::Result<File> {
370 OpenOptions::new().read(true).open(path.as_ref())
373 /// Opens a file in write-only mode.
375 /// This function will create a file if it does not exist,
376 /// and will truncate it if it does.
378 /// See the [`OpenOptions::open`] function for more details.
380 /// [`OpenOptions::open`]: struct.OpenOptions.html#method.open
385 /// use std::fs::File;
387 /// fn main() -> std::io::Result<()> {
388 /// let mut f = File::create("foo.txt")?;
392 #[stable(feature = "rust1", since = "1.0.0")]
393 pub fn create<P: AsRef<Path>>(path: P) -> io::Result<File> {
394 OpenOptions::new().write(true).create(true).truncate(true).open(path.as_ref())
397 /// Attempts to sync all OS-internal metadata to disk.
399 /// This function will attempt to ensure that all in-memory data reaches the
400 /// filesystem before returning.
402 /// This can be used to handle errors that would otherwise only be caught
403 /// when the `File` is closed. Dropping a file will ignore errors in
404 /// synchronizing this in-memory data.
409 /// use std::fs::File;
410 /// use std::io::prelude::*;
412 /// fn main() -> std::io::Result<()> {
413 /// let mut f = File::create("foo.txt")?;
414 /// f.write_all(b"Hello, world!")?;
420 #[stable(feature = "rust1", since = "1.0.0")]
421 pub fn sync_all(&self) -> io::Result<()> {
425 /// This function is similar to [`sync_all`], except that it may not
426 /// synchronize file metadata to the filesystem.
428 /// This is intended for use cases that must synchronize content, but don't
429 /// need the metadata on disk. The goal of this method is to reduce disk
432 /// Note that some platforms may simply implement this in terms of
435 /// [`sync_all`]: struct.File.html#method.sync_all
440 /// use std::fs::File;
441 /// use std::io::prelude::*;
443 /// fn main() -> std::io::Result<()> {
444 /// let mut f = File::create("foo.txt")?;
445 /// f.write_all(b"Hello, world!")?;
451 #[stable(feature = "rust1", since = "1.0.0")]
452 pub fn sync_data(&self) -> io::Result<()> {
453 self.inner.datasync()
456 /// Truncates or extends the underlying file, updating the size of
457 /// this file to become `size`.
459 /// If the `size` is less than the current file's size, then the file will
460 /// be shrunk. If it is greater than the current file's size, then the file
461 /// will be extended to `size` and have all of the intermediate data filled
464 /// The file's cursor isn't changed. In particular, if the cursor was at the
465 /// end and the file is shrunk using this operation, the cursor will now be
470 /// This function will return an error if the file is not opened for writing.
475 /// use std::fs::File;
477 /// fn main() -> std::io::Result<()> {
478 /// let mut f = File::create("foo.txt")?;
484 /// Note that this method alters the content of the underlying file, even
485 /// though it takes `&self` rather than `&mut self`.
486 #[stable(feature = "rust1", since = "1.0.0")]
487 pub fn set_len(&self, size: u64) -> io::Result<()> {
488 self.inner.truncate(size)
491 /// Queries metadata about the underlying file.
496 /// use std::fs::File;
498 /// fn main() -> std::io::Result<()> {
499 /// let mut f = File::open("foo.txt")?;
500 /// let metadata = f.metadata()?;
504 #[stable(feature = "rust1", since = "1.0.0")]
505 pub fn metadata(&self) -> io::Result<Metadata> {
506 self.inner.file_attr().map(Metadata)
509 /// Creates a new `File` instance that shares the same underlying file handle
510 /// as the existing `File` instance. Reads, writes, and seeks will affect
511 /// both `File` instances simultaneously.
515 /// Creates two handles for a file named `foo.txt`:
518 /// use std::fs::File;
520 /// fn main() -> std::io::Result<()> {
521 /// let mut file = File::open("foo.txt")?;
522 /// let file_copy = file.try_clone()?;
527 /// Assuming there’s a file named `foo.txt` with contents `abcdef\n`, create
528 /// two handles, seek one of them, and read the remaining bytes from the
532 /// use std::fs::File;
533 /// use std::io::SeekFrom;
534 /// use std::io::prelude::*;
536 /// fn main() -> std::io::Result<()> {
537 /// let mut file = File::open("foo.txt")?;
538 /// let mut file_copy = file.try_clone()?;
540 /// file.seek(SeekFrom::Start(3))?;
542 /// let mut contents = vec![];
543 /// file_copy.read_to_end(&mut contents)?;
544 /// assert_eq!(contents, b"def\n");
548 #[stable(feature = "file_try_clone", since = "1.9.0")]
549 pub fn try_clone(&self) -> io::Result<File> {
551 inner: self.inner.duplicate()?
555 /// Changes the permissions on the underlying file.
557 /// # Platform-specific behavior
559 /// This function currently corresponds to the `fchmod` function on Unix and
560 /// the `SetFileInformationByHandle` function on Windows. Note that, this
561 /// [may change in the future][changes].
563 /// [changes]: ../io/index.html#platform-specific-behavior
567 /// This function will return an error if the user lacks permission change
568 /// attributes on the underlying file. It may also return an error in other
569 /// os-specific unspecified cases.
574 /// fn main() -> std::io::Result<()> {
575 /// use std::fs::File;
577 /// let file = File::open("foo.txt")?;
578 /// let mut perms = file.metadata()?.permissions();
579 /// perms.set_readonly(true);
580 /// file.set_permissions(perms)?;
585 /// Note that this method alters the permissions of the underlying file,
586 /// even though it takes `&self` rather than `&mut self`.
587 #[stable(feature = "set_permissions_atomic", since = "1.16.0")]
588 pub fn set_permissions(&self, perm: Permissions) -> io::Result<()> {
589 self.inner.set_permissions(perm.0)
593 impl AsInner<fs_imp::File> for File {
594 fn as_inner(&self) -> &fs_imp::File { &self.inner }
596 impl FromInner<fs_imp::File> for File {
597 fn from_inner(f: fs_imp::File) -> File {
601 impl IntoInner<fs_imp::File> for File {
602 fn into_inner(self) -> fs_imp::File {
607 #[stable(feature = "rust1", since = "1.0.0")]
608 impl fmt::Debug for File {
609 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
614 #[stable(feature = "rust1", since = "1.0.0")]
616 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
620 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
621 self.inner.read_vectored(bufs)
625 unsafe fn initializer(&self) -> Initializer {
629 #[stable(feature = "rust1", since = "1.0.0")]
630 impl Write for File {
631 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
632 self.inner.write(buf)
635 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
636 self.inner.write_vectored(bufs)
639 fn flush(&mut self) -> io::Result<()> { self.inner.flush() }
641 #[stable(feature = "rust1", since = "1.0.0")]
643 fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
647 #[stable(feature = "rust1", since = "1.0.0")]
648 impl Read for &File {
649 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
653 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
654 self.inner.read_vectored(bufs)
658 unsafe fn initializer(&self) -> Initializer {
662 #[stable(feature = "rust1", since = "1.0.0")]
663 impl Write for &File {
664 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
665 self.inner.write(buf)
668 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
669 self.inner.write_vectored(bufs)
672 fn flush(&mut self) -> io::Result<()> { self.inner.flush() }
674 #[stable(feature = "rust1", since = "1.0.0")]
675 impl Seek for &File {
676 fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
682 /// Creates a blank new set of options ready for configuration.
684 /// All options are initially set to `false`.
689 /// use std::fs::OpenOptions;
691 /// let mut options = OpenOptions::new();
692 /// let file = options.read(true).open("foo.txt");
694 #[stable(feature = "rust1", since = "1.0.0")]
695 pub fn new() -> OpenOptions {
696 OpenOptions(fs_imp::OpenOptions::new())
699 /// Sets the option for read access.
701 /// This option, when true, will indicate that the file should be
702 /// `read`-able if opened.
707 /// use std::fs::OpenOptions;
709 /// let file = OpenOptions::new().read(true).open("foo.txt");
711 #[stable(feature = "rust1", since = "1.0.0")]
712 pub fn read(&mut self, read: bool) -> &mut OpenOptions {
713 self.0.read(read); self
716 /// Sets the option for write access.
718 /// This option, when true, will indicate that the file should be
719 /// `write`-able if opened.
721 /// If the file already exists, any write calls on it will overwrite its
722 /// contents, without truncating it.
727 /// use std::fs::OpenOptions;
729 /// let file = OpenOptions::new().write(true).open("foo.txt");
731 #[stable(feature = "rust1", since = "1.0.0")]
732 pub fn write(&mut self, write: bool) -> &mut OpenOptions {
733 self.0.write(write); self
736 /// Sets the option for the append mode.
738 /// This option, when true, means that writes will append to a file instead
739 /// of overwriting previous contents.
740 /// Note that setting `.write(true).append(true)` has the same effect as
741 /// setting only `.append(true)`.
743 /// For most filesystems, the operating system guarantees that all writes are
744 /// atomic: no writes get mangled because another process writes at the same
747 /// One maybe obvious note when using append-mode: make sure that all data
748 /// that belongs together is written to the file in one operation. This
749 /// can be done by concatenating strings before passing them to [`write()`],
750 /// or using a buffered writer (with a buffer of adequate size),
751 /// and calling [`flush()`] when the message is complete.
753 /// If a file is opened with both read and append access, beware that after
754 /// opening, and after every write, the position for reading may be set at the
755 /// end of the file. So, before writing, save the current position (using
756 /// [`seek`]`(`[`SeekFrom`]`::`[`Current`]`(0))`), and restore it before the next read.
760 /// This function doesn't create the file if it doesn't exist. Use the [`create`]
763 /// [`write()`]: ../../std/fs/struct.File.html#method.write
764 /// [`flush()`]: ../../std/fs/struct.File.html#method.flush
765 /// [`seek`]: ../../std/fs/struct.File.html#method.seek
766 /// [`SeekFrom`]: ../../std/io/enum.SeekFrom.html
767 /// [`Current`]: ../../std/io/enum.SeekFrom.html#variant.Current
768 /// [`create`]: #method.create
773 /// use std::fs::OpenOptions;
775 /// let file = OpenOptions::new().append(true).open("foo.txt");
777 #[stable(feature = "rust1", since = "1.0.0")]
778 pub fn append(&mut self, append: bool) -> &mut OpenOptions {
779 self.0.append(append); self
782 /// Sets the option for truncating a previous file.
784 /// If a file is successfully opened with this option set it will truncate
785 /// the file to 0 length if it already exists.
787 /// The file must be opened with write access for truncate to work.
792 /// use std::fs::OpenOptions;
794 /// let file = OpenOptions::new().write(true).truncate(true).open("foo.txt");
796 #[stable(feature = "rust1", since = "1.0.0")]
797 pub fn truncate(&mut self, truncate: bool) -> &mut OpenOptions {
798 self.0.truncate(truncate); self
801 /// Sets the option for creating a new file.
803 /// This option indicates whether a new file will be created if the file
804 /// does not yet already exist.
806 /// In order for the file to be created, [`write`] or [`append`] access must
809 /// [`write`]: #method.write
810 /// [`append`]: #method.append
815 /// use std::fs::OpenOptions;
817 /// let file = OpenOptions::new().write(true).create(true).open("foo.txt");
819 #[stable(feature = "rust1", since = "1.0.0")]
820 pub fn create(&mut self, create: bool) -> &mut OpenOptions {
821 self.0.create(create); self
824 /// Sets the option to always create a new file.
826 /// This option indicates whether a new file will be created.
827 /// No file is allowed to exist at the target location, also no (dangling)
830 /// This option is useful because it is atomic. Otherwise between checking
831 /// whether a file exists and creating a new one, the file may have been
832 /// created by another process (a TOCTOU race condition / attack).
834 /// If `.create_new(true)` is set, [`.create()`] and [`.truncate()`] are
837 /// The file must be opened with write or append access in order to create
840 /// [`.create()`]: #method.create
841 /// [`.truncate()`]: #method.truncate
846 /// use std::fs::OpenOptions;
848 /// let file = OpenOptions::new().write(true)
849 /// .create_new(true)
850 /// .open("foo.txt");
852 #[stable(feature = "expand_open_options2", since = "1.9.0")]
853 pub fn create_new(&mut self, create_new: bool) -> &mut OpenOptions {
854 self.0.create_new(create_new); self
857 /// Opens a file at `path` with the options specified by `self`.
861 /// This function will return an error under a number of different
862 /// circumstances. Some of these error conditions are listed here, together
863 /// with their [`ErrorKind`]. The mapping to [`ErrorKind`]s is not part of
864 /// the compatibility contract of the function, especially the `Other` kind
865 /// might change to more specific kinds in the future.
867 /// * [`NotFound`]: The specified file does not exist and neither `create`
868 /// or `create_new` is set.
869 /// * [`NotFound`]: One of the directory components of the file path does
871 /// * [`PermissionDenied`]: The user lacks permission to get the specified
872 /// access rights for the file.
873 /// * [`PermissionDenied`]: The user lacks permission to open one of the
874 /// directory components of the specified path.
875 /// * [`AlreadyExists`]: `create_new` was specified and the file already
877 /// * [`InvalidInput`]: Invalid combinations of open options (truncate
878 /// without write access, no access mode set, etc.).
879 /// * [`Other`]: One of the directory components of the specified file path
880 /// was not, in fact, a directory.
881 /// * [`Other`]: Filesystem-level errors: full disk, write permission
882 /// requested on a read-only file system, exceeded disk quota, too many
883 /// open files, too long filename, too many symbolic links in the
884 /// specified path (Unix-like systems only), etc.
889 /// use std::fs::OpenOptions;
891 /// let file = OpenOptions::new().open("foo.txt");
894 /// [`ErrorKind`]: ../io/enum.ErrorKind.html
895 /// [`AlreadyExists`]: ../io/enum.ErrorKind.html#variant.AlreadyExists
896 /// [`InvalidInput`]: ../io/enum.ErrorKind.html#variant.InvalidInput
897 /// [`NotFound`]: ../io/enum.ErrorKind.html#variant.NotFound
898 /// [`Other`]: ../io/enum.ErrorKind.html#variant.Other
899 /// [`PermissionDenied`]: ../io/enum.ErrorKind.html#variant.PermissionDenied
900 #[stable(feature = "rust1", since = "1.0.0")]
901 pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> {
902 self._open(path.as_ref())
905 fn _open(&self, path: &Path) -> io::Result<File> {
906 fs_imp::File::open(path, &self.0).map(|inner| File { inner })
910 impl AsInner<fs_imp::OpenOptions> for OpenOptions {
911 fn as_inner(&self) -> &fs_imp::OpenOptions { &self.0 }
914 impl AsInnerMut<fs_imp::OpenOptions> for OpenOptions {
915 fn as_inner_mut(&mut self) -> &mut fs_imp::OpenOptions { &mut self.0 }
919 /// Returns the file type for this metadata.
924 /// fn main() -> std::io::Result<()> {
927 /// let metadata = fs::metadata("foo.txt")?;
929 /// println!("{:?}", metadata.file_type());
933 #[stable(feature = "file_type", since = "1.1.0")]
934 pub fn file_type(&self) -> FileType {
935 FileType(self.0.file_type())
938 /// Returns `true` if this metadata is for a directory. The
939 /// result is mutually exclusive to the result of
940 /// [`is_file`], and will be false for symlink metadata
941 /// obtained from [`symlink_metadata`].
943 /// [`is_file`]: struct.Metadata.html#method.is_file
944 /// [`symlink_metadata`]: fn.symlink_metadata.html
949 /// fn main() -> std::io::Result<()> {
952 /// let metadata = fs::metadata("foo.txt")?;
954 /// assert!(!metadata.is_dir());
958 #[stable(feature = "rust1", since = "1.0.0")]
959 pub fn is_dir(&self) -> bool { self.file_type().is_dir() }
961 /// Returns `true` if this metadata is for a regular file. The
962 /// result is mutually exclusive to the result of
963 /// [`is_dir`], and will be false for symlink metadata
964 /// obtained from [`symlink_metadata`].
966 /// [`is_dir`]: struct.Metadata.html#method.is_dir
967 /// [`symlink_metadata`]: fn.symlink_metadata.html
974 /// fn main() -> std::io::Result<()> {
975 /// let metadata = fs::metadata("foo.txt")?;
977 /// assert!(metadata.is_file());
981 #[stable(feature = "rust1", since = "1.0.0")]
982 pub fn is_file(&self) -> bool { self.file_type().is_file() }
984 /// Returns the size of the file, in bytes, this metadata is for.
991 /// fn main() -> std::io::Result<()> {
992 /// let metadata = fs::metadata("foo.txt")?;
994 /// assert_eq!(0, metadata.len());
998 #[stable(feature = "rust1", since = "1.0.0")]
999 pub fn len(&self) -> u64 { self.0.size() }
1001 /// Returns the permissions of the file this metadata is for.
1008 /// fn main() -> std::io::Result<()> {
1009 /// let metadata = fs::metadata("foo.txt")?;
1011 /// assert!(!metadata.permissions().readonly());
1015 #[stable(feature = "rust1", since = "1.0.0")]
1016 pub fn permissions(&self) -> Permissions {
1017 Permissions(self.0.perm())
1020 /// Returns the last modification time listed in this metadata.
1022 /// The returned value corresponds to the `mtime` field of `stat` on Unix
1023 /// platforms and the `ftLastWriteTime` field on Windows platforms.
1027 /// This field may not be available on all platforms, and will return an
1028 /// `Err` on platforms where it is not available.
1035 /// fn main() -> std::io::Result<()> {
1036 /// let metadata = fs::metadata("foo.txt")?;
1038 /// if let Ok(time) = metadata.modified() {
1039 /// println!("{:?}", time);
1041 /// println!("Not supported on this platform");
1046 #[stable(feature = "fs_time", since = "1.10.0")]
1047 pub fn modified(&self) -> io::Result<SystemTime> {
1048 self.0.modified().map(FromInner::from_inner)
1051 /// Returns the last access time of this metadata.
1053 /// The returned value corresponds to the `atime` field of `stat` on Unix
1054 /// platforms and the `ftLastAccessTime` field on Windows platforms.
1056 /// Note that not all platforms will keep this field update in a file's
1057 /// metadata, for example Windows has an option to disable updating this
1058 /// time when files are accessed and Linux similarly has `noatime`.
1062 /// This field may not be available on all platforms, and will return an
1063 /// `Err` on platforms where it is not available.
1070 /// fn main() -> std::io::Result<()> {
1071 /// let metadata = fs::metadata("foo.txt")?;
1073 /// if let Ok(time) = metadata.accessed() {
1074 /// println!("{:?}", time);
1076 /// println!("Not supported on this platform");
1081 #[stable(feature = "fs_time", since = "1.10.0")]
1082 pub fn accessed(&self) -> io::Result<SystemTime> {
1083 self.0.accessed().map(FromInner::from_inner)
1086 /// Returns the creation time listed in this metadata.
1088 /// The returned value corresponds to the `birthtime` field of `stat` on
1089 /// Unix platforms and the `ftCreationTime` field on Windows platforms.
1093 /// This field may not be available on all platforms, and will return an
1094 /// `Err` on platforms where it is not available.
1101 /// fn main() -> std::io::Result<()> {
1102 /// let metadata = fs::metadata("foo.txt")?;
1104 /// if let Ok(time) = metadata.created() {
1105 /// println!("{:?}", time);
1107 /// println!("Not supported on this platform");
1112 #[stable(feature = "fs_time", since = "1.10.0")]
1113 pub fn created(&self) -> io::Result<SystemTime> {
1114 self.0.created().map(FromInner::from_inner)
1118 #[stable(feature = "std_debug", since = "1.16.0")]
1119 impl fmt::Debug for Metadata {
1120 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1121 f.debug_struct("Metadata")
1122 .field("file_type", &self.file_type())
1123 .field("is_dir", &self.is_dir())
1124 .field("is_file", &self.is_file())
1125 .field("permissions", &self.permissions())
1126 .field("modified", &self.modified())
1127 .field("accessed", &self.accessed())
1128 .field("created", &self.created())
1133 impl AsInner<fs_imp::FileAttr> for Metadata {
1134 fn as_inner(&self) -> &fs_imp::FileAttr { &self.0 }
1137 impl FromInner<fs_imp::FileAttr> for Metadata {
1138 fn from_inner(attr: fs_imp::FileAttr) -> Metadata { Metadata(attr) }
1142 /// Returns `true` if these permissions describe a readonly (unwritable) file.
1147 /// use std::fs::File;
1149 /// fn main() -> std::io::Result<()> {
1150 /// let mut f = File::create("foo.txt")?;
1151 /// let metadata = f.metadata()?;
1153 /// assert_eq!(false, metadata.permissions().readonly());
1157 #[stable(feature = "rust1", since = "1.0.0")]
1158 pub fn readonly(&self) -> bool { self.0.readonly() }
1160 /// Modifies the readonly flag for this set of permissions. If the
1161 /// `readonly` argument is `true`, using the resulting `Permission` will
1162 /// update file permissions to forbid writing. Conversely, if it's `false`,
1163 /// using the resulting `Permission` will update file permissions to allow
1166 /// This operation does **not** modify the filesystem. To modify the
1167 /// filesystem use the [`fs::set_permissions`] function.
1169 /// [`fs::set_permissions`]: fn.set_permissions.html
1174 /// use std::fs::File;
1176 /// fn main() -> std::io::Result<()> {
1177 /// let f = File::create("foo.txt")?;
1178 /// let metadata = f.metadata()?;
1179 /// let mut permissions = metadata.permissions();
1181 /// permissions.set_readonly(true);
1183 /// // filesystem doesn't change
1184 /// assert_eq!(false, metadata.permissions().readonly());
1186 /// // just this particular `permissions`.
1187 /// assert_eq!(true, permissions.readonly());
1191 #[stable(feature = "rust1", since = "1.0.0")]
1192 pub fn set_readonly(&mut self, readonly: bool) {
1193 self.0.set_readonly(readonly)
1198 /// Tests whether this file type represents a directory. The
1199 /// result is mutually exclusive to the results of
1200 /// [`is_file`] and [`is_symlink`]; only zero or one of these
1203 /// [`is_file`]: struct.FileType.html#method.is_file
1204 /// [`is_symlink`]: struct.FileType.html#method.is_symlink
1209 /// fn main() -> std::io::Result<()> {
1212 /// let metadata = fs::metadata("foo.txt")?;
1213 /// let file_type = metadata.file_type();
1215 /// assert_eq!(file_type.is_dir(), false);
1219 #[stable(feature = "file_type", since = "1.1.0")]
1220 pub fn is_dir(&self) -> bool { self.0.is_dir() }
1222 /// Tests whether this file type represents a regular file.
1223 /// The result is mutually exclusive to the results of
1224 /// [`is_dir`] and [`is_symlink`]; only zero or one of these
1227 /// [`is_dir`]: struct.FileType.html#method.is_dir
1228 /// [`is_symlink`]: struct.FileType.html#method.is_symlink
1233 /// fn main() -> std::io::Result<()> {
1236 /// let metadata = fs::metadata("foo.txt")?;
1237 /// let file_type = metadata.file_type();
1239 /// assert_eq!(file_type.is_file(), true);
1243 #[stable(feature = "file_type", since = "1.1.0")]
1244 pub fn is_file(&self) -> bool { self.0.is_file() }
1246 /// Tests whether this file type represents a symbolic link.
1247 /// The result is mutually exclusive to the results of
1248 /// [`is_dir`] and [`is_file`]; only zero or one of these
1251 /// The underlying [`Metadata`] struct needs to be retrieved
1252 /// with the [`fs::symlink_metadata`] function and not the
1253 /// [`fs::metadata`] function. The [`fs::metadata`] function
1254 /// follows symbolic links, so [`is_symlink`] would always
1255 /// return `false` for the target file.
1257 /// [`Metadata`]: struct.Metadata.html
1258 /// [`fs::metadata`]: fn.metadata.html
1259 /// [`fs::symlink_metadata`]: fn.symlink_metadata.html
1260 /// [`is_dir`]: struct.FileType.html#method.is_dir
1261 /// [`is_file`]: struct.FileType.html#method.is_file
1262 /// [`is_symlink`]: struct.FileType.html#method.is_symlink
1269 /// fn main() -> std::io::Result<()> {
1270 /// let metadata = fs::symlink_metadata("foo.txt")?;
1271 /// let file_type = metadata.file_type();
1273 /// assert_eq!(file_type.is_symlink(), false);
1277 #[stable(feature = "file_type", since = "1.1.0")]
1278 pub fn is_symlink(&self) -> bool { self.0.is_symlink() }
1281 impl AsInner<fs_imp::FileType> for FileType {
1282 fn as_inner(&self) -> &fs_imp::FileType { &self.0 }
1285 impl FromInner<fs_imp::FilePermissions> for Permissions {
1286 fn from_inner(f: fs_imp::FilePermissions) -> Permissions {
1291 impl AsInner<fs_imp::FilePermissions> for Permissions {
1292 fn as_inner(&self) -> &fs_imp::FilePermissions { &self.0 }
1295 #[stable(feature = "rust1", since = "1.0.0")]
1296 impl Iterator for ReadDir {
1297 type Item = io::Result<DirEntry>;
1299 fn next(&mut self) -> Option<io::Result<DirEntry>> {
1300 self.0.next().map(|entry| entry.map(DirEntry))
1305 /// Returns the full path to the file that this entry represents.
1307 /// The full path is created by joining the original path to `read_dir`
1308 /// with the filename of this entry.
1315 /// fn main() -> std::io::Result<()> {
1316 /// for entry in fs::read_dir(".")? {
1317 /// let dir = entry?;
1318 /// println!("{:?}", dir.path());
1324 /// This prints output like:
1327 /// "./whatever.txt"
1329 /// "./hello_world.rs"
1332 /// The exact text, of course, depends on what files you have in `.`.
1333 #[stable(feature = "rust1", since = "1.0.0")]
1334 pub fn path(&self) -> PathBuf { self.0.path() }
1336 /// Returns the metadata for the file that this entry points at.
1338 /// This function will not traverse symlinks if this entry points at a
1341 /// # Platform-specific behavior
1343 /// On Windows this function is cheap to call (no extra system calls
1344 /// needed), but on Unix platforms this function is the equivalent of
1345 /// calling `symlink_metadata` on the path.
1352 /// if let Ok(entries) = fs::read_dir(".") {
1353 /// for entry in entries {
1354 /// if let Ok(entry) = entry {
1355 /// // Here, `entry` is a `DirEntry`.
1356 /// if let Ok(metadata) = entry.metadata() {
1357 /// // Now let's show our entry's permissions!
1358 /// println!("{:?}: {:?}", entry.path(), metadata.permissions());
1360 /// println!("Couldn't get metadata for {:?}", entry.path());
1366 #[stable(feature = "dir_entry_ext", since = "1.1.0")]
1367 pub fn metadata(&self) -> io::Result<Metadata> {
1368 self.0.metadata().map(Metadata)
1371 /// Returns the file type for the file that this entry points at.
1373 /// This function will not traverse symlinks if this entry points at a
1376 /// # Platform-specific behavior
1378 /// On Windows and most Unix platforms this function is free (no extra
1379 /// system calls needed), but some Unix platforms may require the equivalent
1380 /// call to `symlink_metadata` to learn about the target file type.
1387 /// if let Ok(entries) = fs::read_dir(".") {
1388 /// for entry in entries {
1389 /// if let Ok(entry) = entry {
1390 /// // Here, `entry` is a `DirEntry`.
1391 /// if let Ok(file_type) = entry.file_type() {
1392 /// // Now let's show our entry's file type!
1393 /// println!("{:?}: {:?}", entry.path(), file_type);
1395 /// println!("Couldn't get file type for {:?}", entry.path());
1401 #[stable(feature = "dir_entry_ext", since = "1.1.0")]
1402 pub fn file_type(&self) -> io::Result<FileType> {
1403 self.0.file_type().map(FileType)
1406 /// Returns the bare file name of this directory entry without any other
1407 /// leading path component.
1414 /// if let Ok(entries) = fs::read_dir(".") {
1415 /// for entry in entries {
1416 /// if let Ok(entry) = entry {
1417 /// // Here, `entry` is a `DirEntry`.
1418 /// println!("{:?}", entry.file_name());
1423 #[stable(feature = "dir_entry_ext", since = "1.1.0")]
1424 pub fn file_name(&self) -> OsString {
1429 #[stable(feature = "dir_entry_debug", since = "1.13.0")]
1430 impl fmt::Debug for DirEntry {
1431 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1432 f.debug_tuple("DirEntry")
1433 .field(&self.path())
1438 impl AsInner<fs_imp::DirEntry> for DirEntry {
1439 fn as_inner(&self) -> &fs_imp::DirEntry { &self.0 }
1442 /// Removes a file from the filesystem.
1444 /// Note that there is no
1445 /// guarantee that the file is immediately deleted (e.g., depending on
1446 /// platform, other open file descriptors may prevent immediate removal).
1448 /// # Platform-specific behavior
1450 /// This function currently corresponds to the `unlink` function on Unix
1451 /// and the `DeleteFile` function on Windows.
1452 /// Note that, this [may change in the future][changes].
1454 /// [changes]: ../io/index.html#platform-specific-behavior
1458 /// This function will return an error in the following situations, but is not
1459 /// limited to just these cases:
1461 /// * `path` points to a directory.
1462 /// * The user lacks permissions to remove the file.
1469 /// fn main() -> std::io::Result<()> {
1470 /// fs::remove_file("a.txt")?;
1474 #[stable(feature = "rust1", since = "1.0.0")]
1475 pub fn remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> {
1476 fs_imp::unlink(path.as_ref())
1479 /// Given a path, query the file system to get information about a file,
1482 /// This function will traverse symbolic links to query information about the
1483 /// destination file.
1485 /// # Platform-specific behavior
1487 /// This function currently corresponds to the `stat` function on Unix
1488 /// and the `GetFileAttributesEx` function on Windows.
1489 /// Note that, this [may change in the future][changes].
1491 /// [changes]: ../io/index.html#platform-specific-behavior
1495 /// This function will return an error in the following situations, but is not
1496 /// limited to just these cases:
1498 /// * The user lacks permissions to perform `metadata` call on `path`.
1499 /// * `path` does not exist.
1506 /// fn main() -> std::io::Result<()> {
1507 /// let attr = fs::metadata("/some/file/path.txt")?;
1508 /// // inspect attr ...
1512 #[stable(feature = "rust1", since = "1.0.0")]
1513 pub fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
1514 fs_imp::stat(path.as_ref()).map(Metadata)
1517 /// Query the metadata about a file without following symlinks.
1519 /// # Platform-specific behavior
1521 /// This function currently corresponds to the `lstat` function on Unix
1522 /// and the `GetFileAttributesEx` function on Windows.
1523 /// Note that, this [may change in the future][changes].
1525 /// [changes]: ../io/index.html#platform-specific-behavior
1529 /// This function will return an error in the following situations, but is not
1530 /// limited to just these cases:
1532 /// * The user lacks permissions to perform `metadata` call on `path`.
1533 /// * `path` does not exist.
1540 /// fn main() -> std::io::Result<()> {
1541 /// let attr = fs::symlink_metadata("/some/file/path.txt")?;
1542 /// // inspect attr ...
1546 #[stable(feature = "symlink_metadata", since = "1.1.0")]
1547 pub fn symlink_metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
1548 fs_imp::lstat(path.as_ref()).map(Metadata)
1551 /// Rename a file or directory to a new name, replacing the original file if
1552 /// `to` already exists.
1554 /// This will not work if the new name is on a different mount point.
1556 /// # Platform-specific behavior
1558 /// This function currently corresponds to the `rename` function on Unix
1559 /// and the `MoveFileEx` function with the `MOVEFILE_REPLACE_EXISTING` flag on Windows.
1561 /// Because of this, the behavior when both `from` and `to` exist differs. On
1562 /// Unix, if `from` is a directory, `to` must also be an (empty) directory. If
1563 /// `from` is not a directory, `to` must also be not a directory. In contrast,
1564 /// on Windows, `from` can be anything, but `to` must *not* be a directory.
1566 /// Note that, this [may change in the future][changes].
1568 /// [changes]: ../io/index.html#platform-specific-behavior
1572 /// This function will return an error in the following situations, but is not
1573 /// limited to just these cases:
1575 /// * `from` does not exist.
1576 /// * The user lacks permissions to view contents.
1577 /// * `from` and `to` are on separate filesystems.
1584 /// fn main() -> std::io::Result<()> {
1585 /// fs::rename("a.txt", "b.txt")?; // Rename a.txt to b.txt
1589 #[stable(feature = "rust1", since = "1.0.0")]
1590 pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
1591 fs_imp::rename(from.as_ref(), to.as_ref())
1594 /// Copies the contents of one file to another. This function will also
1595 /// copy the permission bits of the original file to the destination file.
1597 /// This function will **overwrite** the contents of `to`.
1599 /// Note that if `from` and `to` both point to the same file, then the file
1600 /// will likely get truncated by this operation.
1602 /// On success, the total number of bytes copied is returned and it is equal to
1603 /// the length of the `to` file as reported by `metadata`.
1605 /// If you’re wanting to copy the contents of one file to another and you’re
1606 /// working with [`File`]s, see the [`io::copy`] function.
1608 /// [`io::copy`]: ../io/fn.copy.html
1609 /// [`File`]: ./struct.File.html
1611 /// # Platform-specific behavior
1613 /// This function currently corresponds to the `open` function in Unix
1614 /// with `O_RDONLY` for `from` and `O_WRONLY`, `O_CREAT`, and `O_TRUNC` for `to`.
1615 /// `O_CLOEXEC` is set for returned file descriptors.
1616 /// On Windows, this function currently corresponds to `CopyFileEx`. Alternate
1617 /// NTFS streams are copied but only the size of the main stream is returned by
1618 /// this function. On MacOS, this function corresponds to `fclonefileat` and
1620 /// Note that, this [may change in the future][changes].
1622 /// [changes]: ../io/index.html#platform-specific-behavior
1626 /// This function will return an error in the following situations, but is not
1627 /// limited to just these cases:
1629 /// * The `from` path is not a file.
1630 /// * The `from` file does not exist.
1631 /// * The current process does not have the permission rights to access
1632 /// `from` or write `to`.
1639 /// fn main() -> std::io::Result<()> {
1640 /// fs::copy("foo.txt", "bar.txt")?; // Copy foo.txt to bar.txt
1644 #[stable(feature = "rust1", since = "1.0.0")]
1645 pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
1646 fs_imp::copy(from.as_ref(), to.as_ref())
1649 /// Creates a new hard link on the filesystem.
1651 /// The `dst` path will be a link pointing to the `src` path. Note that systems
1652 /// often require these two paths to both be located on the same filesystem.
1654 /// # Platform-specific behavior
1656 /// This function currently corresponds to the `link` function on Unix
1657 /// and the `CreateHardLink` function on Windows.
1658 /// Note that, this [may change in the future][changes].
1660 /// [changes]: ../io/index.html#platform-specific-behavior
1664 /// This function will return an error in the following situations, but is not
1665 /// limited to just these cases:
1667 /// * The `src` path is not a file or doesn't exist.
1674 /// fn main() -> std::io::Result<()> {
1675 /// fs::hard_link("a.txt", "b.txt")?; // Hard link a.txt to b.txt
1679 #[stable(feature = "rust1", since = "1.0.0")]
1680 pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
1681 fs_imp::link(src.as_ref(), dst.as_ref())
1684 /// Creates a new symbolic link on the filesystem.
1686 /// The `dst` path will be a symbolic link pointing to the `src` path.
1687 /// On Windows, this will be a file symlink, not a directory symlink;
1688 /// for this reason, the platform-specific [`std::os::unix::fs::symlink`]
1689 /// and [`std::os::windows::fs::symlink_file`] or [`symlink_dir`] should be
1690 /// used instead to make the intent explicit.
1692 /// [`std::os::unix::fs::symlink`]: ../os/unix/fs/fn.symlink.html
1693 /// [`std::os::windows::fs::symlink_file`]: ../os/windows/fs/fn.symlink_file.html
1694 /// [`symlink_dir`]: ../os/windows/fs/fn.symlink_dir.html
1702 /// fn main() -> std::io::Result<()> {
1703 /// fs::soft_link("a.txt", "b.txt")?;
1707 #[stable(feature = "rust1", since = "1.0.0")]
1708 #[rustc_deprecated(since = "1.1.0",
1709 reason = "replaced with std::os::unix::fs::symlink and \
1710 std::os::windows::fs::{symlink_file, symlink_dir}")]
1711 pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
1712 fs_imp::symlink(src.as_ref(), dst.as_ref())
1715 /// Reads a symbolic link, returning the file that the link points to.
1717 /// # Platform-specific behavior
1719 /// This function currently corresponds to the `readlink` function on Unix
1720 /// and the `CreateFile` function with `FILE_FLAG_OPEN_REPARSE_POINT` and
1721 /// `FILE_FLAG_BACKUP_SEMANTICS` flags on Windows.
1722 /// Note that, this [may change in the future][changes].
1724 /// [changes]: ../io/index.html#platform-specific-behavior
1728 /// This function will return an error in the following situations, but is not
1729 /// limited to just these cases:
1731 /// * `path` is not a symbolic link.
1732 /// * `path` does not exist.
1739 /// fn main() -> std::io::Result<()> {
1740 /// let path = fs::read_link("a.txt")?;
1744 #[stable(feature = "rust1", since = "1.0.0")]
1745 pub fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
1746 fs_imp::readlink(path.as_ref())
1749 /// Returns the canonical, absolute form of a path with all intermediate
1750 /// components normalized and symbolic links resolved.
1752 /// # Platform-specific behavior
1754 /// This function currently corresponds to the `realpath` function on Unix
1755 /// and the `CreateFile` and `GetFinalPathNameByHandle` functions on Windows.
1756 /// Note that, this [may change in the future][changes].
1758 /// On Windows, this converts the path to use [extended length path][path]
1759 /// syntax, which allows your program to use longer path names, but means you
1760 /// can only join backslash-delimited paths to it, and it may be incompatible
1761 /// with other applications (if passed to the application on the command-line,
1762 /// or written to a file another application may read).
1764 /// [changes]: ../io/index.html#platform-specific-behavior
1765 /// [path]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#maxpath
1769 /// This function will return an error in the following situations, but is not
1770 /// limited to just these cases:
1772 /// * `path` does not exist.
1773 /// * A non-final component in path is not a directory.
1780 /// fn main() -> std::io::Result<()> {
1781 /// let path = fs::canonicalize("../a/../foo.txt")?;
1785 #[stable(feature = "fs_canonicalize", since = "1.5.0")]
1786 pub fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
1787 fs_imp::canonicalize(path.as_ref())
1790 /// Creates a new, empty directory at the provided path
1792 /// # Platform-specific behavior
1794 /// This function currently corresponds to the `mkdir` function on Unix
1795 /// and the `CreateDirectory` function on Windows.
1796 /// Note that, this [may change in the future][changes].
1798 /// [changes]: ../io/index.html#platform-specific-behavior
1800 /// **NOTE**: If a parent of the given path doesn't exist, this function will
1801 /// return an error. To create a directory and all its missing parents at the
1802 /// same time, use the [`create_dir_all`] function.
1806 /// This function will return an error in the following situations, but is not
1807 /// limited to just these cases:
1809 /// * User lacks permissions to create directory at `path`.
1810 /// * A parent of the given path doesn't exist. (To create a directory and all
1811 /// its missing parents at the same time, use the [`create_dir_all`]
1813 /// * `path` already exists.
1815 /// [`create_dir_all`]: fn.create_dir_all.html
1822 /// fn main() -> std::io::Result<()> {
1823 /// fs::create_dir("/some/dir")?;
1827 #[stable(feature = "rust1", since = "1.0.0")]
1828 pub fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
1829 DirBuilder::new().create(path.as_ref())
1832 /// Recursively create a directory and all of its parent components if they
1835 /// # Platform-specific behavior
1837 /// This function currently corresponds to the `mkdir` function on Unix
1838 /// and the `CreateDirectory` function on Windows.
1839 /// Note that, this [may change in the future][changes].
1841 /// [changes]: ../io/index.html#platform-specific-behavior
1845 /// This function will return an error in the following situations, but is not
1846 /// limited to just these cases:
1848 /// * If any directory in the path specified by `path`
1849 /// does not already exist and it could not be created otherwise. The specific
1850 /// error conditions for when a directory is being created (after it is
1851 /// determined to not exist) are outlined by [`fs::create_dir`].
1853 /// Notable exception is made for situations where any of the directories
1854 /// specified in the `path` could not be created as it was being created concurrently.
1855 /// Such cases are considered to be successful. That is, calling `create_dir_all`
1856 /// concurrently from multiple threads or processes is guaranteed not to fail
1857 /// due to a race condition with itself.
1859 /// [`fs::create_dir`]: fn.create_dir.html
1866 /// fn main() -> std::io::Result<()> {
1867 /// fs::create_dir_all("/some/dir")?;
1871 #[stable(feature = "rust1", since = "1.0.0")]
1872 pub fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
1873 DirBuilder::new().recursive(true).create(path.as_ref())
1876 /// Removes an existing, empty directory.
1878 /// # Platform-specific behavior
1880 /// This function currently corresponds to the `rmdir` function on Unix
1881 /// and the `RemoveDirectory` function on Windows.
1882 /// Note that, this [may change in the future][changes].
1884 /// [changes]: ../io/index.html#platform-specific-behavior
1888 /// This function will return an error in the following situations, but is not
1889 /// limited to just these cases:
1891 /// * The user lacks permissions to remove the directory at the provided `path`.
1892 /// * The directory isn't empty.
1899 /// fn main() -> std::io::Result<()> {
1900 /// fs::remove_dir("/some/dir")?;
1904 #[stable(feature = "rust1", since = "1.0.0")]
1905 pub fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
1906 fs_imp::rmdir(path.as_ref())
1909 /// Removes a directory at this path, after removing all its contents. Use
1912 /// This function does **not** follow symbolic links and it will simply remove the
1913 /// symbolic link itself.
1915 /// # Platform-specific behavior
1917 /// This function currently corresponds to `opendir`, `lstat`, `rm` and `rmdir` functions on Unix
1918 /// and the `FindFirstFile`, `GetFileAttributesEx`, `DeleteFile`, and `RemoveDirectory` functions
1920 /// Note that, this [may change in the future][changes].
1922 /// [changes]: ../io/index.html#platform-specific-behavior
1926 /// See [`fs::remove_file`] and [`fs::remove_dir`].
1928 /// [`fs::remove_file`]: fn.remove_file.html
1929 /// [`fs::remove_dir`]: fn.remove_dir.html
1936 /// fn main() -> std::io::Result<()> {
1937 /// fs::remove_dir_all("/some/dir")?;
1941 #[stable(feature = "rust1", since = "1.0.0")]
1942 pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
1943 fs_imp::remove_dir_all(path.as_ref())
1946 /// Returns an iterator over the entries within a directory.
1948 /// The iterator will yield instances of [`io::Result`]`<`[`DirEntry`]`>`.
1949 /// New errors may be encountered after an iterator is initially constructed.
1951 /// [`io::Result`]: ../io/type.Result.html
1952 /// [`DirEntry`]: struct.DirEntry.html
1954 /// # Platform-specific behavior
1956 /// This function currently corresponds to the `opendir` function on Unix
1957 /// and the `FindFirstFile` function on Windows.
1958 /// Note that, this [may change in the future][changes].
1960 /// [changes]: ../io/index.html#platform-specific-behavior
1964 /// This function will return an error in the following situations, but is not
1965 /// limited to just these cases:
1967 /// * The provided `path` doesn't exist.
1968 /// * The process lacks permissions to view the contents.
1969 /// * The `path` points at a non-directory file.
1975 /// use std::fs::{self, DirEntry};
1976 /// use std::path::Path;
1978 /// // one possible implementation of walking a directory only visiting files
1979 /// fn visit_dirs(dir: &Path, cb: &dyn Fn(&DirEntry)) -> io::Result<()> {
1980 /// if dir.is_dir() {
1981 /// for entry in fs::read_dir(dir)? {
1982 /// let entry = entry?;
1983 /// let path = entry.path();
1984 /// if path.is_dir() {
1985 /// visit_dirs(&path, cb)?;
1994 #[stable(feature = "rust1", since = "1.0.0")]
1995 pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
1996 fs_imp::readdir(path.as_ref()).map(ReadDir)
1999 /// Changes the permissions found on a file or a directory.
2001 /// # Platform-specific behavior
2003 /// This function currently corresponds to the `chmod` function on Unix
2004 /// and the `SetFileAttributes` function on Windows.
2005 /// Note that, this [may change in the future][changes].
2007 /// [changes]: ../io/index.html#platform-specific-behavior
2011 /// This function will return an error in the following situations, but is not
2012 /// limited to just these cases:
2014 /// * `path` does not exist.
2015 /// * The user lacks the permission to change attributes of the file.
2022 /// fn main() -> std::io::Result<()> {
2023 /// let mut perms = fs::metadata("foo.txt")?.permissions();
2024 /// perms.set_readonly(true);
2025 /// fs::set_permissions("foo.txt", perms)?;
2029 #[stable(feature = "set_permissions", since = "1.1.0")]
2030 pub fn set_permissions<P: AsRef<Path>>(path: P, perm: Permissions)
2032 fs_imp::set_perm(path.as_ref(), perm.0)
2036 /// Creates a new set of options with default mode/security settings for all
2037 /// platforms and also non-recursive.
2042 /// use std::fs::DirBuilder;
2044 /// let builder = DirBuilder::new();
2046 #[stable(feature = "dir_builder", since = "1.6.0")]
2047 pub fn new() -> DirBuilder {
2049 inner: fs_imp::DirBuilder::new(),
2054 /// Indicates that directories should be created recursively, creating all
2055 /// parent directories. Parents that do not exist are created with the same
2056 /// security and permissions settings.
2058 /// This option defaults to `false`.
2063 /// use std::fs::DirBuilder;
2065 /// let mut builder = DirBuilder::new();
2066 /// builder.recursive(true);
2068 #[stable(feature = "dir_builder", since = "1.6.0")]
2069 pub fn recursive(&mut self, recursive: bool) -> &mut Self {
2070 self.recursive = recursive;
2074 /// Creates the specified directory with the options configured in this
2077 /// It is considered an error if the directory already exists unless
2078 /// recursive mode is enabled.
2083 /// use std::fs::{self, DirBuilder};
2085 /// let path = "/tmp/foo/bar/baz";
2086 /// DirBuilder::new()
2087 /// .recursive(true)
2088 /// .create(path).unwrap();
2090 /// assert!(fs::metadata(path).unwrap().is_dir());
2092 #[stable(feature = "dir_builder", since = "1.6.0")]
2093 pub fn create<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
2094 self._create(path.as_ref())
2097 fn _create(&self, path: &Path) -> io::Result<()> {
2099 self.create_dir_all(path)
2101 self.inner.mkdir(path)
2105 fn create_dir_all(&self, path: &Path) -> io::Result<()> {
2106 if path == Path::new("") {
2110 match self.inner.mkdir(path) {
2111 Ok(()) => return Ok(()),
2112 Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
2113 Err(_) if path.is_dir() => return Ok(()),
2114 Err(e) => return Err(e),
2116 match path.parent() {
2117 Some(p) => self.create_dir_all(p)?,
2118 None => return Err(io::Error::new(io::ErrorKind::Other, "failed to create whole tree")),
2120 match self.inner.mkdir(path) {
2122 Err(_) if path.is_dir() => Ok(()),
2128 impl AsInnerMut<fs_imp::DirBuilder> for DirBuilder {
2129 fn as_inner_mut(&mut self) -> &mut fs_imp::DirBuilder {
2134 #[cfg(all(test, not(any(target_os = "cloudabi", target_os = "emscripten", target_env = "sgx"))))]
2136 use crate::io::prelude::*;
2138 use crate::fs::{self, File, OpenOptions};
2139 use crate::io::{ErrorKind, SeekFrom};
2140 use crate::path::Path;
2142 use crate::sys_common::io::test::{TempDir, tmpdir};
2145 use rand::{rngs::StdRng, FromEntropy, RngCore};
2148 use crate::os::windows::fs::{symlink_dir, symlink_file};
2150 use crate::sys::fs::symlink_junction;
2152 use crate::os::unix::fs::symlink as symlink_dir;
2154 use crate::os::unix::fs::symlink as symlink_file;
2156 use crate::os::unix::fs::symlink as symlink_junction;
2158 macro_rules! check { ($e:expr) => (
2161 Err(e) => panic!("{} failed with: {}", stringify!($e), e),
2166 macro_rules! error { ($e:expr, $s:expr) => (
2168 Ok(_) => panic!("Unexpected success. Should've been: {:?}", $s),
2169 Err(ref err) => assert!(err.raw_os_error() == Some($s),
2170 format!("`{}` did not have a code of `{}`", err, $s))
2175 macro_rules! error { ($e:expr, $s:expr) => ( error_contains!($e, $s) ) }
2177 macro_rules! error_contains { ($e:expr, $s:expr) => (
2179 Ok(_) => panic!("Unexpected success. Should've been: {:?}", $s),
2180 Err(ref err) => assert!(err.to_string().contains($s),
2181 format!("`{}` did not contain `{}`", err, $s))
2185 // Several test fail on windows if the user does not have permission to
2186 // create symlinks (the `SeCreateSymbolicLinkPrivilege`). Instead of
2187 // disabling these test on Windows, use this function to test whether we
2188 // have permission, and return otherwise. This way, we still don't run these
2189 // tests most of the time, but at least we do if the user has the right
2191 pub fn got_symlink_permission(tmpdir: &TempDir) -> bool {
2192 if cfg!(unix) { return true }
2193 let link = tmpdir.join("some_hopefully_unique_link_name");
2195 match symlink_file(r"nonexisting_target", link) {
2197 // ERROR_PRIVILEGE_NOT_HELD = 1314
2198 Err(ref err) if err.raw_os_error() == Some(1314) => false,
2204 fn file_test_io_smoke_test() {
2205 let message = "it's alright. have a good time";
2206 let tmpdir = tmpdir();
2207 let filename = &tmpdir.join("file_rt_io_file_test.txt");
2209 let mut write_stream = check!(File::create(filename));
2210 check!(write_stream.write(message.as_bytes()));
2213 let mut read_stream = check!(File::open(filename));
2214 let mut read_buf = [0; 1028];
2215 let read_str = match check!(read_stream.read(&mut read_buf)) {
2216 0 => panic!("shouldn't happen"),
2217 n => str::from_utf8(&read_buf[..n]).unwrap().to_string()
2219 assert_eq!(read_str, message);
2221 check!(fs::remove_file(filename));
2225 fn invalid_path_raises() {
2226 let tmpdir = tmpdir();
2227 let filename = &tmpdir.join("file_that_does_not_exist.txt");
2228 let result = File::open(filename);
2231 error!(result, "No such file or directory");
2233 error!(result, 2); // ERROR_FILE_NOT_FOUND
2237 fn file_test_iounlinking_invalid_path_should_raise_condition() {
2238 let tmpdir = tmpdir();
2239 let filename = &tmpdir.join("file_another_file_that_does_not_exist.txt");
2241 let result = fs::remove_file(filename);
2244 error!(result, "No such file or directory");
2246 error!(result, 2); // ERROR_FILE_NOT_FOUND
2250 fn file_test_io_non_positional_read() {
2251 let message: &str = "ten-four";
2252 let mut read_mem = [0; 8];
2253 let tmpdir = tmpdir();
2254 let filename = &tmpdir.join("file_rt_io_file_test_positional.txt");
2256 let mut rw_stream = check!(File::create(filename));
2257 check!(rw_stream.write(message.as_bytes()));
2260 let mut read_stream = check!(File::open(filename));
2262 let read_buf = &mut read_mem[0..4];
2263 check!(read_stream.read(read_buf));
2266 let read_buf = &mut read_mem[4..8];
2267 check!(read_stream.read(read_buf));
2270 check!(fs::remove_file(filename));
2271 let read_str = str::from_utf8(&read_mem).unwrap();
2272 assert_eq!(read_str, message);
2276 fn file_test_io_seek_and_tell_smoke_test() {
2277 let message = "ten-four";
2278 let mut read_mem = [0; 4];
2279 let set_cursor = 4 as u64;
2280 let tell_pos_pre_read;
2281 let tell_pos_post_read;
2282 let tmpdir = tmpdir();
2283 let filename = &tmpdir.join("file_rt_io_file_test_seeking.txt");
2285 let mut rw_stream = check!(File::create(filename));
2286 check!(rw_stream.write(message.as_bytes()));
2289 let mut read_stream = check!(File::open(filename));
2290 check!(read_stream.seek(SeekFrom::Start(set_cursor)));
2291 tell_pos_pre_read = check!(read_stream.seek(SeekFrom::Current(0)));
2292 check!(read_stream.read(&mut read_mem));
2293 tell_pos_post_read = check!(read_stream.seek(SeekFrom::Current(0)));
2295 check!(fs::remove_file(filename));
2296 let read_str = str::from_utf8(&read_mem).unwrap();
2297 assert_eq!(read_str, &message[4..8]);
2298 assert_eq!(tell_pos_pre_read, set_cursor);
2299 assert_eq!(tell_pos_post_read, message.len() as u64);
2303 fn file_test_io_seek_and_write() {
2304 let initial_msg = "food-is-yummy";
2305 let overwrite_msg = "-the-bar!!";
2306 let final_msg = "foo-the-bar!!";
2308 let mut read_mem = [0; 13];
2309 let tmpdir = tmpdir();
2310 let filename = &tmpdir.join("file_rt_io_file_test_seek_and_write.txt");
2312 let mut rw_stream = check!(File::create(filename));
2313 check!(rw_stream.write(initial_msg.as_bytes()));
2314 check!(rw_stream.seek(SeekFrom::Start(seek_idx)));
2315 check!(rw_stream.write(overwrite_msg.as_bytes()));
2318 let mut read_stream = check!(File::open(filename));
2319 check!(read_stream.read(&mut read_mem));
2321 check!(fs::remove_file(filename));
2322 let read_str = str::from_utf8(&read_mem).unwrap();
2323 assert!(read_str == final_msg);
2327 fn file_test_io_seek_shakedown() {
2329 let initial_msg = "qwer-asdf-zxcv";
2330 let chunk_one: &str = "qwer";
2331 let chunk_two: &str = "asdf";
2332 let chunk_three: &str = "zxcv";
2333 let mut read_mem = [0; 4];
2334 let tmpdir = tmpdir();
2335 let filename = &tmpdir.join("file_rt_io_file_test_seek_shakedown.txt");
2337 let mut rw_stream = check!(File::create(filename));
2338 check!(rw_stream.write(initial_msg.as_bytes()));
2341 let mut read_stream = check!(File::open(filename));
2343 check!(read_stream.seek(SeekFrom::End(-4)));
2344 check!(read_stream.read(&mut read_mem));
2345 assert_eq!(str::from_utf8(&read_mem).unwrap(), chunk_three);
2347 check!(read_stream.seek(SeekFrom::Current(-9)));
2348 check!(read_stream.read(&mut read_mem));
2349 assert_eq!(str::from_utf8(&read_mem).unwrap(), chunk_two);
2351 check!(read_stream.seek(SeekFrom::Start(0)));
2352 check!(read_stream.read(&mut read_mem));
2353 assert_eq!(str::from_utf8(&read_mem).unwrap(), chunk_one);
2355 check!(fs::remove_file(filename));
2359 fn file_test_io_eof() {
2360 let tmpdir = tmpdir();
2361 let filename = tmpdir.join("file_rt_io_file_test_eof.txt");
2362 let mut buf = [0; 256];
2364 let oo = OpenOptions::new().create_new(true).write(true).read(true).clone();
2365 let mut rw = check!(oo.open(&filename));
2366 assert_eq!(check!(rw.read(&mut buf)), 0);
2367 assert_eq!(check!(rw.read(&mut buf)), 0);
2369 check!(fs::remove_file(&filename));
2374 fn file_test_io_read_write_at() {
2375 use crate::os::unix::fs::FileExt;
2377 let tmpdir = tmpdir();
2378 let filename = tmpdir.join("file_rt_io_file_test_read_write_at.txt");
2379 let mut buf = [0; 256];
2380 let write1 = "asdf";
2381 let write2 = "qwer-";
2382 let write3 = "-zxcv";
2383 let content = "qwer-asdf-zxcv";
2385 let oo = OpenOptions::new().create_new(true).write(true).read(true).clone();
2386 let mut rw = check!(oo.open(&filename));
2387 assert_eq!(check!(rw.write_at(write1.as_bytes(), 5)), write1.len());
2388 assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 0);
2389 assert_eq!(check!(rw.read_at(&mut buf, 5)), write1.len());
2390 assert_eq!(str::from_utf8(&buf[..write1.len()]), Ok(write1));
2391 assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 0);
2392 assert_eq!(check!(rw.read_at(&mut buf[..write2.len()], 0)), write2.len());
2393 assert_eq!(str::from_utf8(&buf[..write2.len()]), Ok("\0\0\0\0\0"));
2394 assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 0);
2395 assert_eq!(check!(rw.write(write2.as_bytes())), write2.len());
2396 assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 5);
2397 assert_eq!(check!(rw.read(&mut buf)), write1.len());
2398 assert_eq!(str::from_utf8(&buf[..write1.len()]), Ok(write1));
2399 assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 9);
2400 assert_eq!(check!(rw.read_at(&mut buf[..write2.len()], 0)), write2.len());
2401 assert_eq!(str::from_utf8(&buf[..write2.len()]), Ok(write2));
2402 assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 9);
2403 assert_eq!(check!(rw.write_at(write3.as_bytes(), 9)), write3.len());
2404 assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 9);
2407 let mut read = check!(File::open(&filename));
2408 assert_eq!(check!(read.read_at(&mut buf, 0)), content.len());
2409 assert_eq!(str::from_utf8(&buf[..content.len()]), Ok(content));
2410 assert_eq!(check!(read.seek(SeekFrom::Current(0))), 0);
2411 assert_eq!(check!(read.seek(SeekFrom::End(-5))), 9);
2412 assert_eq!(check!(read.read_at(&mut buf, 0)), content.len());
2413 assert_eq!(str::from_utf8(&buf[..content.len()]), Ok(content));
2414 assert_eq!(check!(read.seek(SeekFrom::Current(0))), 9);
2415 assert_eq!(check!(read.read(&mut buf)), write3.len());
2416 assert_eq!(str::from_utf8(&buf[..write3.len()]), Ok(write3));
2417 assert_eq!(check!(read.seek(SeekFrom::Current(0))), 14);
2418 assert_eq!(check!(read.read_at(&mut buf, 0)), content.len());
2419 assert_eq!(str::from_utf8(&buf[..content.len()]), Ok(content));
2420 assert_eq!(check!(read.seek(SeekFrom::Current(0))), 14);
2421 assert_eq!(check!(read.read_at(&mut buf, 14)), 0);
2422 assert_eq!(check!(read.read_at(&mut buf, 15)), 0);
2423 assert_eq!(check!(read.seek(SeekFrom::Current(0))), 14);
2425 check!(fs::remove_file(&filename));
2430 fn set_get_unix_permissions() {
2431 use crate::os::unix::fs::PermissionsExt;
2433 let tmpdir = tmpdir();
2434 let filename = &tmpdir.join("set_get_unix_permissions");
2435 check!(fs::create_dir(filename));
2438 check!(fs::set_permissions(filename,
2439 fs::Permissions::from_mode(0)));
2440 let metadata0 = check!(fs::metadata(filename));
2441 assert_eq!(mask & metadata0.permissions().mode(), 0);
2443 check!(fs::set_permissions(filename,
2444 fs::Permissions::from_mode(0o1777)));
2445 let metadata1 = check!(fs::metadata(filename));
2446 assert_eq!(mask & metadata1.permissions().mode(), 0o1777);
2451 fn file_test_io_seek_read_write() {
2452 use crate::os::windows::fs::FileExt;
2454 let tmpdir = tmpdir();
2455 let filename = tmpdir.join("file_rt_io_file_test_seek_read_write.txt");
2456 let mut buf = [0; 256];
2457 let write1 = "asdf";
2458 let write2 = "qwer-";
2459 let write3 = "-zxcv";
2460 let content = "qwer-asdf-zxcv";
2462 let oo = OpenOptions::new().create_new(true).write(true).read(true).clone();
2463 let mut rw = check!(oo.open(&filename));
2464 assert_eq!(check!(rw.seek_write(write1.as_bytes(), 5)), write1.len());
2465 assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 9);
2466 assert_eq!(check!(rw.seek_read(&mut buf, 5)), write1.len());
2467 assert_eq!(str::from_utf8(&buf[..write1.len()]), Ok(write1));
2468 assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 9);
2469 assert_eq!(check!(rw.seek(SeekFrom::Start(0))), 0);
2470 assert_eq!(check!(rw.write(write2.as_bytes())), write2.len());
2471 assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 5);
2472 assert_eq!(check!(rw.read(&mut buf)), write1.len());
2473 assert_eq!(str::from_utf8(&buf[..write1.len()]), Ok(write1));
2474 assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 9);
2475 assert_eq!(check!(rw.seek_read(&mut buf[..write2.len()], 0)), write2.len());
2476 assert_eq!(str::from_utf8(&buf[..write2.len()]), Ok(write2));
2477 assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 5);
2478 assert_eq!(check!(rw.seek_write(write3.as_bytes(), 9)), write3.len());
2479 assert_eq!(check!(rw.seek(SeekFrom::Current(0))), 14);
2482 let mut read = check!(File::open(&filename));
2483 assert_eq!(check!(read.seek_read(&mut buf, 0)), content.len());
2484 assert_eq!(str::from_utf8(&buf[..content.len()]), Ok(content));
2485 assert_eq!(check!(read.seek(SeekFrom::Current(0))), 14);
2486 assert_eq!(check!(read.seek(SeekFrom::End(-5))), 9);
2487 assert_eq!(check!(read.seek_read(&mut buf, 0)), content.len());
2488 assert_eq!(str::from_utf8(&buf[..content.len()]), Ok(content));
2489 assert_eq!(check!(read.seek(SeekFrom::Current(0))), 14);
2490 assert_eq!(check!(read.seek(SeekFrom::End(-5))), 9);
2491 assert_eq!(check!(read.read(&mut buf)), write3.len());
2492 assert_eq!(str::from_utf8(&buf[..write3.len()]), Ok(write3));
2493 assert_eq!(check!(read.seek(SeekFrom::Current(0))), 14);
2494 assert_eq!(check!(read.seek_read(&mut buf, 0)), content.len());
2495 assert_eq!(str::from_utf8(&buf[..content.len()]), Ok(content));
2496 assert_eq!(check!(read.seek(SeekFrom::Current(0))), 14);
2497 assert_eq!(check!(read.seek_read(&mut buf, 14)), 0);
2498 assert_eq!(check!(read.seek_read(&mut buf, 15)), 0);
2500 check!(fs::remove_file(&filename));
2504 fn file_test_stat_is_correct_on_is_file() {
2505 let tmpdir = tmpdir();
2506 let filename = &tmpdir.join("file_stat_correct_on_is_file.txt");
2508 let mut opts = OpenOptions::new();
2509 let mut fs = check!(opts.read(true).write(true)
2510 .create(true).open(filename));
2512 fs.write(msg.as_bytes()).unwrap();
2514 let fstat_res = check!(fs.metadata());
2515 assert!(fstat_res.is_file());
2517 let stat_res_fn = check!(fs::metadata(filename));
2518 assert!(stat_res_fn.is_file());
2519 let stat_res_meth = check!(filename.metadata());
2520 assert!(stat_res_meth.is_file());
2521 check!(fs::remove_file(filename));
2525 fn file_test_stat_is_correct_on_is_dir() {
2526 let tmpdir = tmpdir();
2527 let filename = &tmpdir.join("file_stat_correct_on_is_dir");
2528 check!(fs::create_dir(filename));
2529 let stat_res_fn = check!(fs::metadata(filename));
2530 assert!(stat_res_fn.is_dir());
2531 let stat_res_meth = check!(filename.metadata());
2532 assert!(stat_res_meth.is_dir());
2533 check!(fs::remove_dir(filename));
2537 fn file_test_fileinfo_false_when_checking_is_file_on_a_directory() {
2538 let tmpdir = tmpdir();
2539 let dir = &tmpdir.join("fileinfo_false_on_dir");
2540 check!(fs::create_dir(dir));
2541 assert!(!dir.is_file());
2542 check!(fs::remove_dir(dir));
2546 fn file_test_fileinfo_check_exists_before_and_after_file_creation() {
2547 let tmpdir = tmpdir();
2548 let file = &tmpdir.join("fileinfo_check_exists_b_and_a.txt");
2549 check!(check!(File::create(file)).write(b"foo"));
2550 assert!(file.exists());
2551 check!(fs::remove_file(file));
2552 assert!(!file.exists());
2556 fn file_test_directoryinfo_check_exists_before_and_after_mkdir() {
2557 let tmpdir = tmpdir();
2558 let dir = &tmpdir.join("before_and_after_dir");
2559 assert!(!dir.exists());
2560 check!(fs::create_dir(dir));
2561 assert!(dir.exists());
2562 assert!(dir.is_dir());
2563 check!(fs::remove_dir(dir));
2564 assert!(!dir.exists());
2568 fn file_test_directoryinfo_readdir() {
2569 let tmpdir = tmpdir();
2570 let dir = &tmpdir.join("di_readdir");
2571 check!(fs::create_dir(dir));
2574 let f = dir.join(&format!("{}.txt", n));
2575 let mut w = check!(File::create(&f));
2576 let msg_str = format!("{}{}", prefix, n.to_string());
2577 let msg = msg_str.as_bytes();
2578 check!(w.write(msg));
2580 let files = check!(fs::read_dir(dir));
2581 let mut mem = [0; 4];
2583 let f = f.unwrap().path();
2585 let n = f.file_stem().unwrap();
2586 check!(check!(File::open(&f)).read(&mut mem));
2587 let read_str = str::from_utf8(&mem).unwrap();
2588 let expected = format!("{}{}", prefix, n.to_str().unwrap());
2589 assert_eq!(expected, read_str);
2591 check!(fs::remove_file(&f));
2593 check!(fs::remove_dir(dir));
2597 fn file_create_new_already_exists_error() {
2598 let tmpdir = tmpdir();
2599 let file = &tmpdir.join("file_create_new_error_exists");
2600 check!(fs::File::create(file));
2601 let e = fs::OpenOptions::new().write(true).create_new(true).open(file).unwrap_err();
2602 assert_eq!(e.kind(), ErrorKind::AlreadyExists);
2606 fn mkdir_path_already_exists_error() {
2607 let tmpdir = tmpdir();
2608 let dir = &tmpdir.join("mkdir_error_twice");
2609 check!(fs::create_dir(dir));
2610 let e = fs::create_dir(dir).unwrap_err();
2611 assert_eq!(e.kind(), ErrorKind::AlreadyExists);
2615 fn recursive_mkdir() {
2616 let tmpdir = tmpdir();
2617 let dir = tmpdir.join("d1/d2");
2618 check!(fs::create_dir_all(&dir));
2619 assert!(dir.is_dir())
2623 fn recursive_mkdir_failure() {
2624 let tmpdir = tmpdir();
2625 let dir = tmpdir.join("d1");
2626 let file = dir.join("f1");
2628 check!(fs::create_dir_all(&dir));
2629 check!(File::create(&file));
2631 let result = fs::create_dir_all(&file);
2633 assert!(result.is_err());
2637 fn concurrent_recursive_mkdir() {
2640 let mut dir = dir.join("a");
2642 dir = dir.join("a");
2644 let mut join = vec!();
2646 let dir = dir.clone();
2647 join.push(thread::spawn(move || {
2648 check!(fs::create_dir_all(&dir));
2652 // No `Display` on result of `join()`
2653 join.drain(..).map(|join| join.join().unwrap()).count();
2658 fn recursive_mkdir_slash() {
2659 check!(fs::create_dir_all(Path::new("/")));
2663 fn recursive_mkdir_dot() {
2664 check!(fs::create_dir_all(Path::new(".")));
2668 fn recursive_mkdir_empty() {
2669 check!(fs::create_dir_all(Path::new("")));
2673 fn recursive_rmdir() {
2674 let tmpdir = tmpdir();
2675 let d1 = tmpdir.join("d1");
2676 let dt = d1.join("t");
2677 let dtt = dt.join("t");
2678 let d2 = tmpdir.join("d2");
2679 let canary = d2.join("do_not_delete");
2680 check!(fs::create_dir_all(&dtt));
2681 check!(fs::create_dir_all(&d2));
2682 check!(check!(File::create(&canary)).write(b"foo"));
2683 check!(symlink_junction(&d2, &dt.join("d2")));
2684 let _ = symlink_file(&canary, &d1.join("canary"));
2685 check!(fs::remove_dir_all(&d1));
2687 assert!(!d1.is_dir());
2688 assert!(canary.exists());
2692 fn recursive_rmdir_of_symlink() {
2693 // test we do not recursively delete a symlink but only dirs.
2694 let tmpdir = tmpdir();
2695 let link = tmpdir.join("d1");
2696 let dir = tmpdir.join("d2");
2697 let canary = dir.join("do_not_delete");
2698 check!(fs::create_dir_all(&dir));
2699 check!(check!(File::create(&canary)).write(b"foo"));
2700 check!(symlink_junction(&dir, &link));
2701 check!(fs::remove_dir_all(&link));
2703 assert!(!link.is_dir());
2704 assert!(canary.exists());
2708 // only Windows makes a distinction between file and directory symlinks.
2710 fn recursive_rmdir_of_file_symlink() {
2711 let tmpdir = tmpdir();
2712 if !got_symlink_permission(&tmpdir) { return };
2714 let f1 = tmpdir.join("f1");
2715 let f2 = tmpdir.join("f2");
2716 check!(check!(File::create(&f1)).write(b"foo"));
2717 check!(symlink_file(&f1, &f2));
2718 match fs::remove_dir_all(&f2) {
2719 Ok(..) => panic!("wanted a failure"),
2725 fn unicode_path_is_dir() {
2726 assert!(Path::new(".").is_dir());
2727 assert!(!Path::new("test/stdtest/fs.rs").is_dir());
2729 let tmpdir = tmpdir();
2731 let mut dirpath = tmpdir.path().to_path_buf();
2732 dirpath.push("test-가一ー你好");
2733 check!(fs::create_dir(&dirpath));
2734 assert!(dirpath.is_dir());
2736 let mut filepath = dirpath;
2737 filepath.push("unicode-file-\u{ac00}\u{4e00}\u{30fc}\u{4f60}\u{597d}.rs");
2738 check!(File::create(&filepath)); // ignore return; touch only
2739 assert!(!filepath.is_dir());
2740 assert!(filepath.exists());
2744 fn unicode_path_exists() {
2745 assert!(Path::new(".").exists());
2746 assert!(!Path::new("test/nonexistent-bogus-path").exists());
2748 let tmpdir = tmpdir();
2749 let unicode = tmpdir.path();
2750 let unicode = unicode.join("test-각丁ー再见");
2751 check!(fs::create_dir(&unicode));
2752 assert!(unicode.exists());
2753 assert!(!Path::new("test/unicode-bogus-path-각丁ー再见").exists());
2757 fn copy_file_does_not_exist() {
2758 let from = Path::new("test/nonexistent-bogus-path");
2759 let to = Path::new("test/other-bogus-path");
2761 match fs::copy(&from, &to) {
2764 assert!(!from.exists());
2765 assert!(!to.exists());
2771 fn copy_src_does_not_exist() {
2772 let tmpdir = tmpdir();
2773 let from = Path::new("test/nonexistent-bogus-path");
2774 let to = tmpdir.join("out.txt");
2775 check!(check!(File::create(&to)).write(b"hello"));
2776 assert!(fs::copy(&from, &to).is_err());
2777 assert!(!from.exists());
2778 let mut v = Vec::new();
2779 check!(check!(File::open(&to)).read_to_end(&mut v));
2780 assert_eq!(v, b"hello");
2785 let tmpdir = tmpdir();
2786 let input = tmpdir.join("in.txt");
2787 let out = tmpdir.join("out.txt");
2789 check!(check!(File::create(&input)).write(b"hello"));
2790 check!(fs::copy(&input, &out));
2791 let mut v = Vec::new();
2792 check!(check!(File::open(&out)).read_to_end(&mut v));
2793 assert_eq!(v, b"hello");
2795 assert_eq!(check!(input.metadata()).permissions(),
2796 check!(out.metadata()).permissions());
2800 fn copy_file_dst_dir() {
2801 let tmpdir = tmpdir();
2802 let out = tmpdir.join("out");
2804 check!(File::create(&out));
2805 match fs::copy(&*out, tmpdir.path()) {
2806 Ok(..) => panic!(), Err(..) => {}
2811 fn copy_file_dst_exists() {
2812 let tmpdir = tmpdir();
2813 let input = tmpdir.join("in");
2814 let output = tmpdir.join("out");
2816 check!(check!(File::create(&input)).write("foo".as_bytes()));
2817 check!(check!(File::create(&output)).write("bar".as_bytes()));
2818 check!(fs::copy(&input, &output));
2820 let mut v = Vec::new();
2821 check!(check!(File::open(&output)).read_to_end(&mut v));
2822 assert_eq!(v, b"foo".to_vec());
2826 fn copy_file_src_dir() {
2827 let tmpdir = tmpdir();
2828 let out = tmpdir.join("out");
2830 match fs::copy(tmpdir.path(), &out) {
2831 Ok(..) => panic!(), Err(..) => {}
2833 assert!(!out.exists());
2837 fn copy_file_preserves_perm_bits() {
2838 let tmpdir = tmpdir();
2839 let input = tmpdir.join("in.txt");
2840 let out = tmpdir.join("out.txt");
2842 let attr = check!(check!(File::create(&input)).metadata());
2843 let mut p = attr.permissions();
2844 p.set_readonly(true);
2845 check!(fs::set_permissions(&input, p));
2846 check!(fs::copy(&input, &out));
2847 assert!(check!(out.metadata()).permissions().readonly());
2848 check!(fs::set_permissions(&input, attr.permissions()));
2849 check!(fs::set_permissions(&out, attr.permissions()));
2854 fn copy_file_preserves_streams() {
2856 check!(check!(File::create(tmp.join("in.txt:bunny"))).write("carrot".as_bytes()));
2857 assert_eq!(check!(fs::copy(tmp.join("in.txt"), tmp.join("out.txt"))), 0);
2858 assert_eq!(check!(tmp.join("out.txt").metadata()).len(), 0);
2859 let mut v = Vec::new();
2860 check!(check!(File::open(tmp.join("out.txt:bunny"))).read_to_end(&mut v));
2861 assert_eq!(v, b"carrot".to_vec());
2865 fn copy_file_returns_metadata_len() {
2867 let in_path = tmp.join("in.txt");
2868 let out_path = tmp.join("out.txt");
2869 check!(check!(File::create(&in_path)).write(b"lettuce"));
2871 check!(check!(File::create(tmp.join("in.txt:bunny"))).write(b"carrot"));
2872 let copied_len = check!(fs::copy(&in_path, &out_path));
2873 assert_eq!(check!(out_path.metadata()).len(), copied_len);
2877 fn copy_file_follows_dst_symlink() {
2879 if !got_symlink_permission(&tmp) { return };
2881 let in_path = tmp.join("in.txt");
2882 let out_path = tmp.join("out.txt");
2883 let out_path_symlink = tmp.join("out_symlink.txt");
2885 check!(fs::write(&in_path, "foo"));
2886 check!(fs::write(&out_path, "bar"));
2887 check!(symlink_file(&out_path, &out_path_symlink));
2889 check!(fs::copy(&in_path, &out_path_symlink));
2891 assert!(check!(out_path_symlink.symlink_metadata()).file_type().is_symlink());
2892 assert_eq!(check!(fs::read(&out_path_symlink)), b"foo".to_vec());
2893 assert_eq!(check!(fs::read(&out_path)), b"foo".to_vec());
2897 fn symlinks_work() {
2898 let tmpdir = tmpdir();
2899 if !got_symlink_permission(&tmpdir) { return };
2901 let input = tmpdir.join("in.txt");
2902 let out = tmpdir.join("out.txt");
2904 check!(check!(File::create(&input)).write("foobar".as_bytes()));
2905 check!(symlink_file(&input, &out));
2906 assert!(check!(out.symlink_metadata()).file_type().is_symlink());
2907 assert_eq!(check!(fs::metadata(&out)).len(),
2908 check!(fs::metadata(&input)).len());
2909 let mut v = Vec::new();
2910 check!(check!(File::open(&out)).read_to_end(&mut v));
2911 assert_eq!(v, b"foobar".to_vec());
2915 fn symlink_noexist() {
2916 // Symlinks can point to things that don't exist
2917 let tmpdir = tmpdir();
2918 if !got_symlink_permission(&tmpdir) { return };
2920 // Use a relative path for testing. Symlinks get normalized by Windows,
2921 // so we may not get the same path back for absolute paths
2922 check!(symlink_file(&"foo", &tmpdir.join("bar")));
2923 assert_eq!(check!(fs::read_link(&tmpdir.join("bar"))).to_str().unwrap(),
2930 // directory symlink
2931 assert_eq!(check!(fs::read_link(r"C:\Users\All Users")).to_str().unwrap(),
2934 assert_eq!(check!(fs::read_link(r"C:\Users\Default User")).to_str().unwrap(),
2935 r"C:\Users\Default");
2936 // junction with special permissions
2937 assert_eq!(check!(fs::read_link(r"C:\Documents and Settings\")).to_str().unwrap(),
2940 let tmpdir = tmpdir();
2941 let link = tmpdir.join("link");
2942 if !got_symlink_permission(&tmpdir) { return };
2943 check!(symlink_file(&"foo", &link));
2944 assert_eq!(check!(fs::read_link(&link)).to_str().unwrap(), "foo");
2948 fn readlink_not_symlink() {
2949 let tmpdir = tmpdir();
2950 match fs::read_link(tmpdir.path()) {
2951 Ok(..) => panic!("wanted a failure"),
2958 let tmpdir = tmpdir();
2959 let input = tmpdir.join("in.txt");
2960 let out = tmpdir.join("out.txt");
2962 check!(check!(File::create(&input)).write("foobar".as_bytes()));
2963 check!(fs::hard_link(&input, &out));
2964 assert_eq!(check!(fs::metadata(&out)).len(),
2965 check!(fs::metadata(&input)).len());
2966 assert_eq!(check!(fs::metadata(&out)).len(),
2967 check!(input.metadata()).len());
2968 let mut v = Vec::new();
2969 check!(check!(File::open(&out)).read_to_end(&mut v));
2970 assert_eq!(v, b"foobar".to_vec());
2972 // can't link to yourself
2973 match fs::hard_link(&input, &input) {
2974 Ok(..) => panic!("wanted a failure"),
2977 // can't link to something that doesn't exist
2978 match fs::hard_link(&tmpdir.join("foo"), &tmpdir.join("bar")) {
2979 Ok(..) => panic!("wanted a failure"),
2986 let tmpdir = tmpdir();
2987 let file = tmpdir.join("in.txt");
2989 check!(File::create(&file));
2990 let attr = check!(fs::metadata(&file));
2991 assert!(!attr.permissions().readonly());
2992 let mut p = attr.permissions();
2993 p.set_readonly(true);
2994 check!(fs::set_permissions(&file, p.clone()));
2995 let attr = check!(fs::metadata(&file));
2996 assert!(attr.permissions().readonly());
2998 match fs::set_permissions(&tmpdir.join("foo"), p.clone()) {
2999 Ok(..) => panic!("wanted an error"),
3003 p.set_readonly(false);
3004 check!(fs::set_permissions(&file, p));
3009 let tmpdir = tmpdir();
3010 let path = tmpdir.join("in.txt");
3012 let file = check!(File::create(&path));
3013 let attr = check!(fs::metadata(&path));
3014 assert!(!attr.permissions().readonly());
3015 let mut p = attr.permissions();
3016 p.set_readonly(true);
3017 check!(file.set_permissions(p.clone()));
3018 let attr = check!(fs::metadata(&path));
3019 assert!(attr.permissions().readonly());
3021 p.set_readonly(false);
3022 check!(file.set_permissions(p));
3026 fn sync_doesnt_kill_anything() {
3027 let tmpdir = tmpdir();
3028 let path = tmpdir.join("in.txt");
3030 let mut file = check!(File::create(&path));
3031 check!(file.sync_all());
3032 check!(file.sync_data());
3033 check!(file.write(b"foo"));
3034 check!(file.sync_all());
3035 check!(file.sync_data());
3039 fn truncate_works() {
3040 let tmpdir = tmpdir();
3041 let path = tmpdir.join("in.txt");
3043 let mut file = check!(File::create(&path));
3044 check!(file.write(b"foo"));
3045 check!(file.sync_all());
3047 // Do some simple things with truncation
3048 assert_eq!(check!(file.metadata()).len(), 3);
3049 check!(file.set_len(10));
3050 assert_eq!(check!(file.metadata()).len(), 10);
3051 check!(file.write(b"bar"));
3052 check!(file.sync_all());
3053 assert_eq!(check!(file.metadata()).len(), 10);
3055 let mut v = Vec::new();
3056 check!(check!(File::open(&path)).read_to_end(&mut v));
3057 assert_eq!(v, b"foobar\0\0\0\0".to_vec());
3059 // Truncate to a smaller length, don't seek, and then write something.
3060 // Ensure that the intermediate zeroes are all filled in (we have `seek`ed
3061 // past the end of the file).
3062 check!(file.set_len(2));
3063 assert_eq!(check!(file.metadata()).len(), 2);
3064 check!(file.write(b"wut"));
3065 check!(file.sync_all());
3066 assert_eq!(check!(file.metadata()).len(), 9);
3067 let mut v = Vec::new();
3068 check!(check!(File::open(&path)).read_to_end(&mut v));
3069 assert_eq!(v, b"fo\0\0\0\0wut".to_vec());
3074 use crate::fs::OpenOptions as OO;
3075 fn c<T: Clone>(t: &T) -> T { t.clone() }
3077 let tmpdir = tmpdir();
3079 let mut r = OO::new(); r.read(true);
3080 let mut w = OO::new(); w.write(true);
3081 let mut rw = OO::new(); rw.read(true).write(true);
3082 let mut a = OO::new(); a.append(true);
3083 let mut ra = OO::new(); ra.read(true).append(true);
3086 let invalid_options = 87; // ERROR_INVALID_PARAMETER
3088 let invalid_options = "Invalid argument";
3090 // Test various combinations of creation modes and access modes.
3093 // creation mode | read | write | read-write | append | read-append |
3094 // :-----------------------|:-----:|:-----:|:----------:|:------:|:-----------:|
3095 // not set (open existing) | X | X | X | X | X |
3096 // create | | X | X | X | X |
3097 // truncate | | X | X | | |
3098 // create and truncate | | X | X | | |
3099 // create_new | | X | X | X | X |
3101 // tested in reverse order, so 'create_new' creates the file, and 'open existing' opens it.
3104 check!(c(&w).create_new(true).open(&tmpdir.join("a")));
3105 check!(c(&w).create(true).truncate(true).open(&tmpdir.join("a")));
3106 check!(c(&w).truncate(true).open(&tmpdir.join("a")));
3107 check!(c(&w).create(true).open(&tmpdir.join("a")));
3108 check!(c(&w).open(&tmpdir.join("a")));
3111 error!(c(&r).create_new(true).open(&tmpdir.join("b")), invalid_options);
3112 error!(c(&r).create(true).truncate(true).open(&tmpdir.join("b")), invalid_options);
3113 error!(c(&r).truncate(true).open(&tmpdir.join("b")), invalid_options);
3114 error!(c(&r).create(true).open(&tmpdir.join("b")), invalid_options);
3115 check!(c(&r).open(&tmpdir.join("a"))); // try opening the file created with write_only
3118 check!(c(&rw).create_new(true).open(&tmpdir.join("c")));
3119 check!(c(&rw).create(true).truncate(true).open(&tmpdir.join("c")));
3120 check!(c(&rw).truncate(true).open(&tmpdir.join("c")));
3121 check!(c(&rw).create(true).open(&tmpdir.join("c")));
3122 check!(c(&rw).open(&tmpdir.join("c")));
3125 check!(c(&a).create_new(true).open(&tmpdir.join("d")));
3126 error!(c(&a).create(true).truncate(true).open(&tmpdir.join("d")), invalid_options);
3127 error!(c(&a).truncate(true).open(&tmpdir.join("d")), invalid_options);
3128 check!(c(&a).create(true).open(&tmpdir.join("d")));
3129 check!(c(&a).open(&tmpdir.join("d")));
3132 check!(c(&ra).create_new(true).open(&tmpdir.join("e")));
3133 error!(c(&ra).create(true).truncate(true).open(&tmpdir.join("e")), invalid_options);
3134 error!(c(&ra).truncate(true).open(&tmpdir.join("e")), invalid_options);
3135 check!(c(&ra).create(true).open(&tmpdir.join("e")));
3136 check!(c(&ra).open(&tmpdir.join("e")));
3138 // Test opening a file without setting an access mode
3139 let mut blank = OO::new();
3140 error!(blank.create(true).open(&tmpdir.join("f")), invalid_options);
3143 check!(check!(File::create(&tmpdir.join("h"))).write("foobar".as_bytes()));
3145 // Test write fails for read-only
3146 check!(r.open(&tmpdir.join("h")));
3148 let mut f = check!(r.open(&tmpdir.join("h")));
3149 assert!(f.write("wut".as_bytes()).is_err());
3152 // Test write overwrites
3154 let mut f = check!(c(&w).open(&tmpdir.join("h")));
3155 check!(f.write("baz".as_bytes()));
3158 let mut f = check!(c(&r).open(&tmpdir.join("h")));
3159 let mut b = vec![0; 6];
3160 check!(f.read(&mut b));
3161 assert_eq!(b, "bazbar".as_bytes());
3164 // Test truncate works
3166 let mut f = check!(c(&w).truncate(true).open(&tmpdir.join("h")));
3167 check!(f.write("foo".as_bytes()));
3169 assert_eq!(check!(fs::metadata(&tmpdir.join("h"))).len(), 3);
3171 // Test append works
3172 assert_eq!(check!(fs::metadata(&tmpdir.join("h"))).len(), 3);
3174 let mut f = check!(c(&a).open(&tmpdir.join("h")));
3175 check!(f.write("bar".as_bytes()));
3177 assert_eq!(check!(fs::metadata(&tmpdir.join("h"))).len(), 6);
3179 // Test .append(true) equals .write(true).append(true)
3181 let mut f = check!(c(&w).append(true).open(&tmpdir.join("h")));
3182 check!(f.write("baz".as_bytes()));
3184 assert_eq!(check!(fs::metadata(&tmpdir.join("h"))).len(), 9);
3188 fn _assert_send_sync() {
3189 fn _assert_send_sync<T: Send + Sync>() {}
3190 _assert_send_sync::<OpenOptions>();
3195 let mut bytes = [0; 1024];
3196 StdRng::from_entropy().fill_bytes(&mut bytes);
3198 let tmpdir = tmpdir();
3200 check!(check!(File::create(&tmpdir.join("test"))).write(&bytes));
3201 let mut v = Vec::new();
3202 check!(check!(File::open(&tmpdir.join("test"))).read_to_end(&mut v));
3203 assert!(v == &bytes[..]);
3207 fn write_then_read() {
3208 let mut bytes = [0; 1024];
3209 StdRng::from_entropy().fill_bytes(&mut bytes);
3211 let tmpdir = tmpdir();
3213 check!(fs::write(&tmpdir.join("test"), &bytes[..]));
3214 let v = check!(fs::read(&tmpdir.join("test")));
3215 assert!(v == &bytes[..]);
3217 check!(fs::write(&tmpdir.join("not-utf8"), &[0xFF]));
3218 error_contains!(fs::read_to_string(&tmpdir.join("not-utf8")),
3219 "stream did not contain valid UTF-8");
3222 check!(fs::write(&tmpdir.join("utf8"), s.as_bytes()));
3223 let string = check!(fs::read_to_string(&tmpdir.join("utf8")));
3224 assert_eq!(string, s);
3228 fn file_try_clone() {
3229 let tmpdir = tmpdir();
3231 let mut f1 = check!(OpenOptions::new()
3235 .open(&tmpdir.join("test")));
3236 let mut f2 = check!(f1.try_clone());
3238 check!(f1.write_all(b"hello world"));
3239 check!(f1.seek(SeekFrom::Start(2)));
3241 let mut buf = vec![];
3242 check!(f2.read_to_end(&mut buf));
3243 assert_eq!(buf, b"llo world");
3246 check!(f1.write_all(b"!"));
3250 #[cfg(not(windows))]
3251 fn unlink_readonly() {
3252 let tmpdir = tmpdir();
3253 let path = tmpdir.join("file");
3254 check!(File::create(&path));
3255 let mut perm = check!(fs::metadata(&path)).permissions();
3256 perm.set_readonly(true);
3257 check!(fs::set_permissions(&path, perm));
3258 check!(fs::remove_file(&path));
3262 fn mkdir_trailing_slash() {
3263 let tmpdir = tmpdir();
3264 let path = tmpdir.join("file");
3265 check!(fs::create_dir_all(&path.join("a/")));
3269 fn canonicalize_works_simple() {
3270 let tmpdir = tmpdir();
3271 let tmpdir = fs::canonicalize(tmpdir.path()).unwrap();
3272 let file = tmpdir.join("test");
3273 File::create(&file).unwrap();
3274 assert_eq!(fs::canonicalize(&file).unwrap(), file);
3278 fn realpath_works() {
3279 let tmpdir = tmpdir();
3280 if !got_symlink_permission(&tmpdir) { return };
3282 let tmpdir = fs::canonicalize(tmpdir.path()).unwrap();
3283 let file = tmpdir.join("test");
3284 let dir = tmpdir.join("test2");
3285 let link = dir.join("link");
3286 let linkdir = tmpdir.join("test3");
3288 File::create(&file).unwrap();
3289 fs::create_dir(&dir).unwrap();
3290 symlink_file(&file, &link).unwrap();
3291 symlink_dir(&dir, &linkdir).unwrap();
3293 assert!(link.symlink_metadata().unwrap().file_type().is_symlink());
3295 assert_eq!(fs::canonicalize(&tmpdir).unwrap(), tmpdir);
3296 assert_eq!(fs::canonicalize(&file).unwrap(), file);
3297 assert_eq!(fs::canonicalize(&link).unwrap(), file);
3298 assert_eq!(fs::canonicalize(&linkdir).unwrap(), dir);
3299 assert_eq!(fs::canonicalize(&linkdir.join("link")).unwrap(), file);
3303 fn realpath_works_tricky() {
3304 let tmpdir = tmpdir();
3305 if !got_symlink_permission(&tmpdir) { return };
3307 let tmpdir = fs::canonicalize(tmpdir.path()).unwrap();
3308 let a = tmpdir.join("a");
3309 let b = a.join("b");
3310 let c = b.join("c");
3311 let d = a.join("d");
3312 let e = d.join("e");
3313 let f = a.join("f");
3315 fs::create_dir_all(&b).unwrap();
3316 fs::create_dir_all(&d).unwrap();
3317 File::create(&f).unwrap();
3318 if cfg!(not(windows)) {
3319 symlink_dir("../d/e", &c).unwrap();
3320 symlink_file("../f", &e).unwrap();
3323 symlink_dir(r"..\d\e", &c).unwrap();
3324 symlink_file(r"..\f", &e).unwrap();
3327 assert_eq!(fs::canonicalize(&c).unwrap(), f);
3328 assert_eq!(fs::canonicalize(&e).unwrap(), f);
3332 fn dir_entry_methods() {
3333 let tmpdir = tmpdir();
3335 fs::create_dir_all(&tmpdir.join("a")).unwrap();
3336 File::create(&tmpdir.join("b")).unwrap();
3338 for file in tmpdir.path().read_dir().unwrap().map(|f| f.unwrap()) {
3339 let fname = file.file_name();
3340 match fname.to_str() {
3342 assert!(file.file_type().unwrap().is_dir());
3343 assert!(file.metadata().unwrap().is_dir());
3346 assert!(file.file_type().unwrap().is_file());
3347 assert!(file.metadata().unwrap().is_file());
3349 f => panic!("unknown file name: {:?}", f),
3355 fn dir_entry_debug() {
3356 let tmpdir = tmpdir();
3357 File::create(&tmpdir.join("b")).unwrap();
3358 let mut read_dir = tmpdir.path().read_dir().unwrap();
3359 let dir_entry = read_dir.next().unwrap().unwrap();
3360 let actual = format!("{:?}", dir_entry);
3361 let expected = format!("DirEntry({:?})", dir_entry.0.path());
3362 assert_eq!(actual, expected);
3366 fn read_dir_not_found() {
3367 let res = fs::read_dir("/path/that/does/not/exist");
3368 assert_eq!(res.err().unwrap().kind(), ErrorKind::NotFound);
3372 fn create_dir_all_with_junctions() {
3373 let tmpdir = tmpdir();
3374 let target = tmpdir.join("target");
3376 let junction = tmpdir.join("junction");
3377 let b = junction.join("a/b");
3379 let link = tmpdir.join("link");
3380 let d = link.join("c/d");
3382 fs::create_dir(&target).unwrap();
3384 check!(symlink_junction(&target, &junction));
3385 check!(fs::create_dir_all(&b));
3386 // the junction itself is not a directory, but `is_dir()` on a Path
3388 assert!(junction.is_dir());
3389 assert!(b.exists());
3391 if !got_symlink_permission(&tmpdir) { return };
3392 check!(symlink_dir(&target, &link));
3393 check!(fs::create_dir_all(&d));
3394 assert!(link.is_dir());
3395 assert!(d.exists());
3399 fn metadata_access_times() {
3400 let tmpdir = tmpdir();
3402 let b = tmpdir.join("b");
3403 File::create(&b).unwrap();
3405 let a = check!(fs::metadata(&tmpdir.path()));
3406 let b = check!(fs::metadata(&b));
3408 assert_eq!(check!(a.accessed()), check!(a.accessed()));
3409 assert_eq!(check!(a.modified()), check!(a.modified()));
3410 assert_eq!(check!(b.accessed()), check!(b.modified()));
3412 if cfg!(target_os = "macos") || cfg!(target_os = "windows") {
3413 check!(a.created());
3414 check!(b.created());