1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Filesystem manipulation operations
13 //! This module contains basic methods to manipulate the contents of the local
14 //! filesystem. All methods in this module represent cross-platform filesystem
15 //! operations. Extra platform-specific functionality can be found in the
16 //! extension traits of `std::os::$platform`.
18 #![stable(feature = "rust1", since = "1.0.0")]
22 use io::{self, Error, ErrorKind, SeekFrom, Seek, Read, Write};
23 use path::{Path, PathBuf};
24 use sys::fs2 as fs_imp;
25 use sys_common::{AsInnerMut, FromInner, AsInner};
29 pub use self::tempdir::TempDir;
33 /// A reference to an open file on the filesystem.
35 /// An instance of a `File` can be read and/or written depending on what options
36 /// it was opened with. Files also implement `Seek` to alter the logical cursor
37 /// that the file contains internally.
42 /// use std::io::prelude::*;
43 /// use std::fs::File;
45 /// # fn foo() -> std::io::Result<()> {
46 /// let mut f = try!(File::create("foo.txt"));
47 /// try!(f.write_all(b"Hello, world!"));
49 /// let mut f = try!(File::open("foo.txt"));
50 /// let mut s = String::new();
51 /// try!(f.read_to_string(&mut s));
52 /// assert_eq!(s, "Hello, world!");
56 #[stable(feature = "rust1", since = "1.0.0")]
59 path: Option<PathBuf>,
62 /// Metadata information about a file.
64 /// This structure is returned from the `metadata` function or method and
65 /// represents known metadata about a file such as its permissions, size,
66 /// modification times, etc.
67 #[stable(feature = "rust1", since = "1.0.0")]
68 pub struct Metadata(fs_imp::FileAttr);
70 /// Iterator over the entries in a directory.
72 /// This iterator is returned from the `read_dir` function of this module and
73 /// will yield instances of `io::Result<DirEntry>`. Through a `DirEntry`
74 /// information like the entry's path and possibly other metadata can be
79 /// This `io::Result` will be an `Err` if there's some sort of intermittent
80 /// IO error during iteration.
81 #[stable(feature = "rust1", since = "1.0.0")]
82 pub struct ReadDir(fs_imp::ReadDir);
84 /// Entries returned by the `ReadDir` iterator.
86 /// An instance of `DirEntry` represents an entry inside of a directory on the
87 /// filesystem. Each entry can be inspected via methods to learn about the full
88 /// path or possibly other metadata through per-platform extension traits.
89 #[stable(feature = "rust1", since = "1.0.0")]
90 pub struct DirEntry(fs_imp::DirEntry);
92 /// An iterator that recursively walks over the contents of a directory.
93 #[unstable(feature = "fs_walk",
94 reason = "the precise semantics and defaults for a recursive walk \
95 may change and this may end up accounting for files such \
96 as symlinks differently")]
99 stack: Vec<io::Result<ReadDir>>,
102 /// Options and flags which can be used to configure how a file is opened.
104 /// This builder exposes the ability to configure how a `File` is opened and
105 /// what operations are permitted on the open file. The `File::open` and
106 /// `File::create` methods are aliases for commonly used options using this
109 #[stable(feature = "rust1", since = "1.0.0")]
110 pub struct OpenOptions(fs_imp::OpenOptions);
112 /// Representation of the various permissions on a file.
114 /// This module only currently provides one bit of information, `readonly`,
115 /// which is exposed on all currently supported platforms. Unix-specific
116 /// functionality, such as mode bits, is available through the
117 /// `os::unix::PermissionsExt` trait.
118 #[derive(Clone, PartialEq, Eq, Debug)]
119 #[stable(feature = "rust1", since = "1.0.0")]
120 pub struct Permissions(fs_imp::FilePermissions);
123 /// Attempts to open a file in read-only mode.
125 /// See the `OpenOptions::open` method for more details.
129 /// This function will return an error if `path` does not already exist.
130 /// Other errors may also be returned according to `OpenOptions::open`.
135 /// use std::fs::File;
137 /// # fn foo() -> std::io::Result<()> {
138 /// let mut f = try!(File::open("foo.txt"));
142 #[stable(feature = "rust1", since = "1.0.0")]
143 pub fn open<P: AsRef<Path>>(path: P) -> io::Result<File> {
144 OpenOptions::new().read(true).open(path)
147 /// Open a file in write-only mode.
149 /// This function will create a file if it does not exist,
150 /// and will truncate it if it does.
152 /// See the `OpenOptions::open` function for more details.
157 /// use std::fs::File;
159 /// # fn foo() -> std::io::Result<()> {
160 /// let mut f = try!(File::create("foo.txt"));
164 #[stable(feature = "rust1", since = "1.0.0")]
165 pub fn create<P: AsRef<Path>>(path: P) -> io::Result<File> {
166 OpenOptions::new().write(true).create(true).truncate(true).open(path)
169 /// Returns the original path that was used to open this file.
170 #[unstable(feature = "file_path",
171 reason = "this abstraction is imposed by this library instead \
172 of the underlying OS and may be removed")]
173 pub fn path(&self) -> Option<&Path> {
174 self.path.as_ref().map(|p| &**p)
177 /// Attempt to sync all OS-internal metadata to disk.
179 /// This function will attempt to ensure that all in-core data reaches the
180 /// filesystem before returning.
185 /// use std::fs::File;
186 /// use std::io::prelude::*;
188 /// # fn foo() -> std::io::Result<()> {
189 /// let mut f = try!(File::create("foo.txt"));
190 /// try!(f.write_all(b"Hello, world!"));
192 /// try!(f.sync_all());
196 #[stable(feature = "rust1", since = "1.0.0")]
197 pub fn sync_all(&self) -> io::Result<()> {
201 /// This function is similar to `sync_all`, except that it may not
202 /// synchronize file metadata to the filesystem.
204 /// This is intended for use cases that must synchronize content, but don't
205 /// need the metadata on disk. The goal of this method is to reduce disk
208 /// Note that some platforms may simply implement this in terms of
214 /// use std::fs::File;
215 /// use std::io::prelude::*;
217 /// # fn foo() -> std::io::Result<()> {
218 /// let mut f = try!(File::create("foo.txt"));
219 /// try!(f.write_all(b"Hello, world!"));
221 /// try!(f.sync_data());
225 #[stable(feature = "rust1", since = "1.0.0")]
226 pub fn sync_data(&self) -> io::Result<()> {
227 self.inner.datasync()
230 /// Truncates or extends the underlying file, updating the size of
231 /// this file to become `size`.
233 /// If the `size` is less than the current file's size, then the file will
234 /// be shrunk. If it is greater than the current file's size, then the file
235 /// will be extended to `size` and have all of the intermediate data filled
241 /// use std::fs::File;
243 /// # fn foo() -> std::io::Result<()> {
244 /// let mut f = try!(File::open("foo.txt"));
245 /// try!(f.set_len(0));
249 #[stable(feature = "rust1", since = "1.0.0")]
250 pub fn set_len(&self, size: u64) -> io::Result<()> {
251 self.inner.truncate(size)
254 /// Queries metadata about the underlying file.
259 /// use std::fs::File;
261 /// # fn foo() -> std::io::Result<()> {
262 /// let mut f = try!(File::open("foo.txt"));
263 /// let metadata = try!(f.metadata());
267 #[stable(feature = "rust1", since = "1.0.0")]
268 pub fn metadata(&self) -> io::Result<Metadata> {
269 self.inner.file_attr().map(Metadata)
273 impl AsInner<fs_imp::File> for File {
274 fn as_inner(&self) -> &fs_imp::File { &self.inner }
276 impl FromInner<fs_imp::File> for File {
277 fn from_inner(f: fs_imp::File) -> File {
278 File { inner: f, path: None }
282 #[stable(feature = "rust1", since = "1.0.0")]
284 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
288 #[stable(feature = "rust1", since = "1.0.0")]
289 impl Write for File {
290 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
291 self.inner.write(buf)
293 fn flush(&mut self) -> io::Result<()> { self.inner.flush() }
295 #[stable(feature = "rust1", since = "1.0.0")]
297 fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
301 #[stable(feature = "rust1", since = "1.0.0")]
302 impl<'a> Read for &'a File {
303 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
307 #[stable(feature = "rust1", since = "1.0.0")]
308 impl<'a> Write for &'a File {
309 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
310 self.inner.write(buf)
312 fn flush(&mut self) -> io::Result<()> { self.inner.flush() }
314 #[stable(feature = "rust1", since = "1.0.0")]
315 impl<'a> Seek for &'a File {
316 fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
322 /// Creates a blank net set of options ready for configuration.
324 /// All options are initially set to `false`.
325 #[stable(feature = "rust1", since = "1.0.0")]
326 pub fn new() -> OpenOptions {
327 OpenOptions(fs_imp::OpenOptions::new())
330 /// Set the option for read access.
332 /// This option, when true, will indicate that the file should be
333 /// `read`-able if opened.
334 #[stable(feature = "rust1", since = "1.0.0")]
335 pub fn read(&mut self, read: bool) -> &mut OpenOptions {
336 self.0.read(read); self
339 /// Set the option for write access.
341 /// This option, when true, will indicate that the file should be
342 /// `write`-able if opened.
343 #[stable(feature = "rust1", since = "1.0.0")]
344 pub fn write(&mut self, write: bool) -> &mut OpenOptions {
345 self.0.write(write); self
348 /// Set the option for the append mode.
350 /// This option, when true, means that writes will append to a file instead
351 /// of overwriting previous contents.
352 #[stable(feature = "rust1", since = "1.0.0")]
353 pub fn append(&mut self, append: bool) -> &mut OpenOptions {
354 self.0.append(append); self
357 /// Set the option for truncating a previous file.
359 /// If a file is successfully opened with this option set it will truncate
360 /// the file to 0 length if it already exists.
361 #[stable(feature = "rust1", since = "1.0.0")]
362 pub fn truncate(&mut self, truncate: bool) -> &mut OpenOptions {
363 self.0.truncate(truncate); self
366 /// Set the option for creating a new file.
368 /// This option indicates whether a new file will be created if the file
369 /// does not yet already exist.
370 #[stable(feature = "rust1", since = "1.0.0")]
371 pub fn create(&mut self, create: bool) -> &mut OpenOptions {
372 self.0.create(create); self
375 /// Open a file at `path` with the options specified by `self`.
379 /// This function will return an error under a number of different
380 /// circumstances, to include but not limited to:
382 /// * Opening a file that does not exist with read access.
383 /// * Attempting to open a file with access that the user lacks
385 /// * Filesystem-level errors (full disk, etc)
386 #[stable(feature = "rust1", since = "1.0.0")]
387 pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> {
388 let path = path.as_ref();
389 let inner = try!(fs_imp::File::open(path, &self.0));
390 Ok(File { path: Some(path.to_path_buf()), inner: inner })
394 impl AsInnerMut<fs_imp::OpenOptions> for OpenOptions {
395 fn as_inner_mut(&mut self) -> &mut fs_imp::OpenOptions { &mut self.0 }
399 /// Returns whether this metadata is for a directory.
400 #[stable(feature = "rust1", since = "1.0.0")]
401 pub fn is_dir(&self) -> bool { self.0.is_dir() }
403 /// Returns whether this metadata is for a regular file.
404 #[stable(feature = "rust1", since = "1.0.0")]
405 pub fn is_file(&self) -> bool { self.0.is_file() }
407 /// Returns the size of the file, in bytes, this metadata is for.
408 #[stable(feature = "rust1", since = "1.0.0")]
409 pub fn len(&self) -> u64 { self.0.size() }
411 /// Returns the permissions of the file this metadata is for.
412 #[stable(feature = "rust1", since = "1.0.0")]
413 pub fn permissions(&self) -> Permissions {
414 Permissions(self.0.perm())
417 /// Returns the most recent access time for a file.
419 /// The return value is in milliseconds since the epoch.
420 #[unstable(feature = "fs_time",
421 reason = "the return type of u64 is not quite appropriate for \
422 this method and may change if the standard library \
423 gains a type to represent a moment in time")]
424 pub fn accessed(&self) -> u64 { self.0.accessed() }
426 /// Returns the most recent modification time for a file.
428 /// The return value is in milliseconds since the epoch.
429 #[unstable(feature = "fs_time",
430 reason = "the return type of u64 is not quite appropriate for \
431 this method and may change if the standard library \
432 gains a type to represent a moment in time")]
433 pub fn modified(&self) -> u64 { self.0.modified() }
437 /// Returns whether these permissions describe a readonly file.
438 #[stable(feature = "rust1", since = "1.0.0")]
439 pub fn readonly(&self) -> bool { self.0.readonly() }
441 /// Modify the readonly flag for this set of permissions.
443 /// This operation does **not** modify the filesystem. To modify the
444 /// filesystem use the `fs::set_permissions` function.
445 #[stable(feature = "rust1", since = "1.0.0")]
446 pub fn set_readonly(&mut self, readonly: bool) {
447 self.0.set_readonly(readonly)
451 impl FromInner<fs_imp::FilePermissions> for Permissions {
452 fn from_inner(f: fs_imp::FilePermissions) -> Permissions {
457 impl AsInner<fs_imp::FilePermissions> for Permissions {
458 fn as_inner(&self) -> &fs_imp::FilePermissions { &self.0 }
461 #[stable(feature = "rust1", since = "1.0.0")]
462 impl Iterator for ReadDir {
463 type Item = io::Result<DirEntry>;
465 fn next(&mut self) -> Option<io::Result<DirEntry>> {
466 self.0.next().map(|entry| entry.map(DirEntry))
470 #[stable(feature = "rust1", since = "1.0.0")]
472 /// Returns the full path to the file that this entry represents.
474 /// The full path is created by joining the original path to `read_dir` or
475 /// `walk_dir` with the filename of this entry.
476 #[stable(feature = "rust1", since = "1.0.0")]
477 pub fn path(&self) -> PathBuf { self.0.path() }
480 /// Remove a file from the underlying filesystem.
487 /// fs::remove_file("/some/file/path.txt");
490 /// Note that, just because an unlink call was successful, it is not
491 /// guaranteed that a file is immediately deleted (e.g. depending on
492 /// platform, other open file descriptors may prevent immediate removal).
496 /// This function will return an error if `path` points to a directory, if the
497 /// user lacks permissions to remove the file, or if some other filesystem-level
499 #[stable(feature = "rust1", since = "1.0.0")]
500 pub fn remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> {
501 fs_imp::unlink(path.as_ref())
504 /// Given a path, query the file system to get information about a file,
507 /// This function will traverse soft links to query information about the
508 /// destination file.
513 /// # fn foo() -> std::io::Result<()> {
516 /// let attr = try!(fs::metadata("/some/file/path.txt"));
517 /// // inspect attr ...
524 /// This function will return an error if the user lacks the requisite
525 /// permissions to perform a `metadata` call on the given `path` or if there
526 /// is no entry in the filesystem at the provided path.
527 #[stable(feature = "rust1", since = "1.0.0")]
528 pub fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
529 fs_imp::stat(path.as_ref()).map(Metadata)
532 /// Rename a file or directory to a new name.
539 /// fs::rename("foo", "bar");
544 /// This function will return an error if the provided `from` doesn't exist, if
545 /// the process lacks permissions to view the contents, if `from` and `to`
546 /// reside on separate filesystems, or if some other intermittent I/O error
548 #[stable(feature = "rust1", since = "1.0.0")]
549 pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
550 fs_imp::rename(from.as_ref(), to.as_ref())
553 /// Copies the contents of one file to another. This function will also
554 /// copy the permission bits of the original file to the destination file.
556 /// This function will **overwrite** the contents of `to`.
558 /// Note that if `from` and `to` both point to the same file, then the file
559 /// will likely get truncated by this operation.
566 /// fs::copy("foo.txt", "bar.txt");
571 /// This function will return an error in the following situations, but is not
572 /// limited to just these cases:
574 /// * The `from` path is not a file
575 /// * The `from` file does not exist
576 /// * The current process does not have the permission rights to access
577 /// `from` or write `to`
578 #[stable(feature = "rust1", since = "1.0.0")]
579 pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
580 let from = from.as_ref();
581 let to = to.as_ref();
583 return Err(Error::new(ErrorKind::InvalidInput,
584 "the source path is not an existing file",
588 let mut reader = try!(File::open(from));
589 let mut writer = try!(File::create(to));
590 let perm = try!(reader.metadata()).permissions();
592 let ret = try!(io::copy(&mut reader, &mut writer));
593 try!(set_permissions(to, perm));
597 /// Creates a new hard link on the filesystem.
599 /// The `dst` path will be a link pointing to the `src` path. Note that systems
600 /// often require these two paths to both be located on the same filesystem.
601 #[stable(feature = "rust1", since = "1.0.0")]
602 pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
603 fs_imp::link(src.as_ref(), dst.as_ref())
606 /// Creates a new soft link on the filesystem.
608 /// The `dst` path will be a soft link pointing to the `src` path.
609 #[stable(feature = "rust1", since = "1.0.0")]
610 pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
611 fs_imp::symlink(src.as_ref(), dst.as_ref())
614 /// Reads a soft link, returning the file that the link points to.
618 /// This function will return an error on failure. Failure conditions include
619 /// reading a file that does not exist or reading a file that is not a soft
621 #[stable(feature = "rust1", since = "1.0.0")]
622 pub fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
623 fs_imp::readlink(path.as_ref())
626 /// Create a new, empty directory at the provided path
633 /// fs::create_dir("/some/dir");
638 /// This function will return an error if the user lacks permissions to make a
639 /// new directory at the provided `path`, or if the directory already exists.
640 #[stable(feature = "rust1", since = "1.0.0")]
641 pub fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
642 fs_imp::mkdir(path.as_ref())
645 /// Recursively create a directory and all of its parent components if they
650 /// This function will fail if any directory in the path specified by `path`
651 /// does not already exist and it could not be created otherwise. The specific
652 /// error conditions for when a directory is being created (after it is
653 /// determined to not exist) are outlined by `fs::create_dir`.
654 #[stable(feature = "rust1", since = "1.0.0")]
655 pub fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
656 let path = path.as_ref();
657 if path == Path::new("") || path.is_dir() { return Ok(()) }
658 if let Some(p) = path.parent() { try!(create_dir_all(p)) }
662 /// Remove an existing, empty directory
669 /// fs::remove_dir("/some/dir");
674 /// This function will return an error if the user lacks permissions to remove
675 /// the directory at the provided `path`, or if the directory isn't empty.
676 #[stable(feature = "rust1", since = "1.0.0")]
677 pub fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
678 fs_imp::rmdir(path.as_ref())
681 /// Removes a directory at this path, after removing all its contents. Use
684 /// This function does **not** follow soft links and it will simply remove the
685 /// soft link itself.
689 /// See `file::remove_file` and `fs::remove_dir`
690 #[stable(feature = "rust1", since = "1.0.0")]
691 pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
692 let path = path.as_ref();
693 for child in try!(read_dir(path)) {
694 let child = try!(child).path();
695 let stat = try!(lstat(&*child));
697 try!(remove_dir_all(&*child));
699 try!(remove_file(&*child));
702 return remove_dir(path);
705 fn lstat(path: &Path) -> io::Result<fs_imp::FileAttr> { fs_imp::lstat(path) }
707 fn lstat(path: &Path) -> io::Result<fs_imp::FileAttr> { fs_imp::stat(path) }
710 /// Returns an iterator over the entries within a directory.
712 /// The iterator will yield instances of `io::Result<DirEntry>`. New errors may
713 /// be encountered after an iterator is initially constructed.
718 /// # #![feature(path_ext)]
720 /// use std::fs::{self, PathExt, DirEntry};
721 /// use std::path::Path;
723 /// // one possible implementation of fs::walk_dir only visiting files
724 /// fn visit_dirs(dir: &Path, cb: &mut FnMut(DirEntry)) -> io::Result<()> {
725 /// if dir.is_dir() {
726 /// for entry in try!(fs::read_dir(dir)) {
727 /// let entry = try!(entry);
728 /// if entry.path().is_dir() {
729 /// try!(visit_dirs(&entry.path(), cb));
741 /// This function will return an error if the provided `path` doesn't exist, if
742 /// the process lacks permissions to view the contents or if the `path` points
743 /// at a non-directory file
744 #[stable(feature = "rust1", since = "1.0.0")]
745 pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
746 fs_imp::readdir(path.as_ref()).map(ReadDir)
749 /// Returns an iterator that will recursively walk the directory structure
750 /// rooted at `path`.
752 /// The path given will not be iterated over, and this will perform iteration in
753 /// some top-down order. The contents of unreadable subdirectories are ignored.
755 /// The iterator will yield instances of `io::Result<DirEntry>`. New errors may
756 /// be encountered after an iterator is initially constructed.
757 #[unstable(feature = "fs_walk",
758 reason = "the precise semantics and defaults for a recursive walk \
759 may change and this may end up accounting for files such \
760 as symlinks differently")]
761 pub fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<WalkDir> {
762 let start = try!(read_dir(path));
763 Ok(WalkDir { cur: Some(start), stack: Vec::new() })
766 #[unstable(feature = "fs_walk")]
767 impl Iterator for WalkDir {
768 type Item = io::Result<DirEntry>;
770 fn next(&mut self) -> Option<io::Result<DirEntry>> {
772 if let Some(ref mut cur) = self.cur {
774 Some(Err(e)) => return Some(Err(e)),
776 let path = next.path();
778 self.stack.push(read_dir(&*path));
780 return Some(Ok(next))
786 match self.stack.pop() {
787 Some(Err(e)) => return Some(Err(e)),
788 Some(Ok(next)) => self.cur = Some(next),
795 /// Utility methods for paths.
796 #[unstable(feature = "path_ext",
797 reason = "the precise set of methods exposed on this trait may \
798 change and some methods may be removed")]
800 /// Get information on the file, directory, etc at this path.
802 /// Consult the `fs::stat` documentation for more info.
804 /// This call preserves identical runtime/error semantics with `file::stat`.
805 fn metadata(&self) -> io::Result<Metadata>;
807 /// Boolean value indicator whether the underlying file exists on the local
808 /// filesystem. Returns false in exactly the cases where `fs::stat` fails.
809 fn exists(&self) -> bool;
811 /// Whether the underlying implementation (be it a file path, or something
812 /// else) points at a "regular file" on the FS. Will return false for paths
813 /// to non-existent locations or directories or other non-regular files
814 /// (named pipes, etc). Follows links when making this determination.
815 fn is_file(&self) -> bool;
817 /// Whether the underlying implementation (be it a file path, or something
818 /// else) is pointing at a directory in the underlying FS. Will return
819 /// false for paths to non-existent locations or if the item is not a
820 /// directory (eg files, named pipes, etc). Follows links when making this
822 fn is_dir(&self) -> bool;
825 impl PathExt for Path {
826 fn metadata(&self) -> io::Result<Metadata> { metadata(self) }
828 fn exists(&self) -> bool { metadata(self).is_ok() }
830 fn is_file(&self) -> bool {
831 metadata(self).map(|s| s.is_file()).unwrap_or(false)
833 fn is_dir(&self) -> bool {
834 metadata(self).map(|s| s.is_dir()).unwrap_or(false)
838 /// Changes the timestamps for a file's last modification and access time.
840 /// The file at the path specified will have its last access time set to
841 /// `atime` and its modification time set to `mtime`. The times specified should
842 /// be in milliseconds.
843 #[unstable(feature = "fs_time",
844 reason = "the argument type of u64 is not quite appropriate for \
845 this function and may change if the standard library \
846 gains a type to represent a moment in time")]
847 pub fn set_file_times<P: AsRef<Path>>(path: P, accessed: u64,
848 modified: u64) -> io::Result<()> {
849 fs_imp::utimes(path.as_ref(), accessed, modified)
852 /// Changes the permissions found on a file or a directory.
857 /// # #![feature(fs)]
858 /// # fn foo() -> std::io::Result<()> {
861 /// let mut perms = try!(fs::metadata("foo.txt")).permissions();
862 /// perms.set_readonly(true);
863 /// try!(fs::set_permissions("foo.txt", perms));
870 /// This function will return an error if the provided `path` doesn't exist, if
871 /// the process lacks permissions to change the attributes of the file, or if
872 /// some other I/O error is encountered.
873 #[unstable(feature = "fs",
874 reason = "a more granual ability to set specific permissions may \
875 be exposed on the Permissions structure itself and this \
876 method may not always exist")]
877 pub fn set_permissions<P: AsRef<Path>>(path: P, perm: Permissions) -> io::Result<()> {
878 fs_imp::set_perm(path.as_ref(), perm.0)
883 #![allow(deprecated)] //rand
889 use fs::{self, File, OpenOptions};
890 use io::{ErrorKind, SeekFrom};
892 use path::Path as Path2;
894 use rand::{self, StdRng, Rng};
897 macro_rules! check { ($e:expr) => (
900 Err(e) => panic!("{} failed with: {}", stringify!($e), e),
904 macro_rules! error { ($e:expr, $s:expr) => (
906 Ok(_) => panic!("Unexpected success. Should've been: {:?}", $s),
907 Err(ref err) => assert!(err.to_string().contains($s),
908 format!("`{}` did not contain `{}`", err, $s))
912 pub struct TempDir(PathBuf);
915 fn join(&self, path: &str) -> PathBuf {
916 let TempDir(ref p) = *self;
920 fn path<'a>(&'a self) -> &'a Path2 {
921 let TempDir(ref p) = *self;
926 impl Drop for TempDir {
928 // Gee, seeing how we're testing the fs module I sure hope that we
929 // at least implement this correctly!
930 let TempDir(ref p) = *self;
931 check!(fs::remove_dir_all(p));
935 pub fn tmpdir() -> TempDir {
936 let p = env::temp_dir();
937 let ret = p.join(&format!("rust-{}", rand::random::<u32>()));
938 check!(fs::create_dir(&ret));
943 fn file_test_io_smoke_test() {
944 let message = "it's alright. have a good time";
945 let tmpdir = tmpdir();
946 let filename = &tmpdir.join("file_rt_io_file_test.txt");
948 let mut write_stream = check!(File::create(filename));
949 check!(write_stream.write(message.as_bytes()));
952 let mut read_stream = check!(File::open(filename));
953 let mut read_buf = [0; 1028];
954 let read_str = match check!(read_stream.read(&mut read_buf)) {
955 -1|0 => panic!("shouldn't happen"),
956 n => str::from_utf8(&read_buf[..n]).unwrap().to_string()
958 assert_eq!(read_str, message);
960 check!(fs::remove_file(filename));
964 fn invalid_path_raises() {
965 let tmpdir = tmpdir();
966 let filename = &tmpdir.join("file_that_does_not_exist.txt");
967 let result = File::open(filename);
970 error!(result, "o such file or directory");
972 // error!(result, "couldn't open path as file");
973 // error!(result, format!("path={}; mode=open; access=read", filename.display()));
977 fn file_test_iounlinking_invalid_path_should_raise_condition() {
978 let tmpdir = tmpdir();
979 let filename = &tmpdir.join("file_another_file_that_does_not_exist.txt");
981 let result = fs::remove_file(filename);
984 error!(result, "o such file or directory");
986 // error!(result, "couldn't unlink path");
987 // error!(result, format!("path={}", filename.display()));
991 fn file_test_io_non_positional_read() {
992 let message: &str = "ten-four";
993 let mut read_mem = [0; 8];
994 let tmpdir = tmpdir();
995 let filename = &tmpdir.join("file_rt_io_file_test_positional.txt");
997 let mut rw_stream = check!(File::create(filename));
998 check!(rw_stream.write(message.as_bytes()));
1001 let mut read_stream = check!(File::open(filename));
1003 let read_buf = &mut read_mem[0..4];
1004 check!(read_stream.read(read_buf));
1007 let read_buf = &mut read_mem[4..8];
1008 check!(read_stream.read(read_buf));
1011 check!(fs::remove_file(filename));
1012 let read_str = str::from_utf8(&read_mem).unwrap();
1013 assert_eq!(read_str, message);
1017 fn file_test_io_seek_and_tell_smoke_test() {
1018 let message = "ten-four";
1019 let mut read_mem = [0; 4];
1020 let set_cursor = 4 as u64;
1021 let mut tell_pos_pre_read;
1022 let mut tell_pos_post_read;
1023 let tmpdir = tmpdir();
1024 let filename = &tmpdir.join("file_rt_io_file_test_seeking.txt");
1026 let mut rw_stream = check!(File::create(filename));
1027 check!(rw_stream.write(message.as_bytes()));
1030 let mut read_stream = check!(File::open(filename));
1031 check!(read_stream.seek(SeekFrom::Start(set_cursor)));
1032 tell_pos_pre_read = check!(read_stream.seek(SeekFrom::Current(0)));
1033 check!(read_stream.read(&mut read_mem));
1034 tell_pos_post_read = check!(read_stream.seek(SeekFrom::Current(0)));
1036 check!(fs::remove_file(filename));
1037 let read_str = str::from_utf8(&read_mem).unwrap();
1038 assert_eq!(read_str, &message[4..8]);
1039 assert_eq!(tell_pos_pre_read, set_cursor);
1040 assert_eq!(tell_pos_post_read, message.len() as u64);
1044 fn file_test_io_seek_and_write() {
1045 let initial_msg = "food-is-yummy";
1046 let overwrite_msg = "-the-bar!!";
1047 let final_msg = "foo-the-bar!!";
1049 let mut read_mem = [0; 13];
1050 let tmpdir = tmpdir();
1051 let filename = &tmpdir.join("file_rt_io_file_test_seek_and_write.txt");
1053 let mut rw_stream = check!(File::create(filename));
1054 check!(rw_stream.write(initial_msg.as_bytes()));
1055 check!(rw_stream.seek(SeekFrom::Start(seek_idx)));
1056 check!(rw_stream.write(overwrite_msg.as_bytes()));
1059 let mut read_stream = check!(File::open(filename));
1060 check!(read_stream.read(&mut read_mem));
1062 check!(fs::remove_file(filename));
1063 let read_str = str::from_utf8(&read_mem).unwrap();
1064 assert!(read_str == final_msg);
1068 fn file_test_io_seek_shakedown() {
1070 let initial_msg = "qwer-asdf-zxcv";
1071 let chunk_one: &str = "qwer";
1072 let chunk_two: &str = "asdf";
1073 let chunk_three: &str = "zxcv";
1074 let mut read_mem = [0; 4];
1075 let tmpdir = tmpdir();
1076 let filename = &tmpdir.join("file_rt_io_file_test_seek_shakedown.txt");
1078 let mut rw_stream = check!(File::create(filename));
1079 check!(rw_stream.write(initial_msg.as_bytes()));
1082 let mut read_stream = check!(File::open(filename));
1084 check!(read_stream.seek(SeekFrom::End(-4)));
1085 check!(read_stream.read(&mut read_mem));
1086 assert_eq!(str::from_utf8(&read_mem).unwrap(), chunk_three);
1088 check!(read_stream.seek(SeekFrom::Current(-9)));
1089 check!(read_stream.read(&mut read_mem));
1090 assert_eq!(str::from_utf8(&read_mem).unwrap(), chunk_two);
1092 check!(read_stream.seek(SeekFrom::Start(0)));
1093 check!(read_stream.read(&mut read_mem));
1094 assert_eq!(str::from_utf8(&read_mem).unwrap(), chunk_one);
1096 check!(fs::remove_file(filename));
1100 fn file_test_stat_is_correct_on_is_file() {
1101 let tmpdir = tmpdir();
1102 let filename = &tmpdir.join("file_stat_correct_on_is_file.txt");
1104 let mut opts = OpenOptions::new();
1105 let mut fs = check!(opts.read(true).write(true)
1106 .create(true).open(filename));
1108 fs.write(msg.as_bytes()).unwrap();
1110 let fstat_res = check!(fs.metadata());
1111 assert!(fstat_res.is_file());
1113 let stat_res_fn = check!(fs::metadata(filename));
1114 assert!(stat_res_fn.is_file());
1115 let stat_res_meth = check!(filename.metadata());
1116 assert!(stat_res_meth.is_file());
1117 check!(fs::remove_file(filename));
1121 fn file_test_stat_is_correct_on_is_dir() {
1122 let tmpdir = tmpdir();
1123 let filename = &tmpdir.join("file_stat_correct_on_is_dir");
1124 check!(fs::create_dir(filename));
1125 let stat_res_fn = check!(fs::metadata(filename));
1126 assert!(stat_res_fn.is_dir());
1127 let stat_res_meth = check!(filename.metadata());
1128 assert!(stat_res_meth.is_dir());
1129 check!(fs::remove_dir(filename));
1133 fn file_test_fileinfo_false_when_checking_is_file_on_a_directory() {
1134 let tmpdir = tmpdir();
1135 let dir = &tmpdir.join("fileinfo_false_on_dir");
1136 check!(fs::create_dir(dir));
1137 assert!(dir.is_file() == false);
1138 check!(fs::remove_dir(dir));
1142 fn file_test_fileinfo_check_exists_before_and_after_file_creation() {
1143 let tmpdir = tmpdir();
1144 let file = &tmpdir.join("fileinfo_check_exists_b_and_a.txt");
1145 check!(check!(File::create(file)).write(b"foo"));
1146 assert!(file.exists());
1147 check!(fs::remove_file(file));
1148 assert!(!file.exists());
1152 fn file_test_directoryinfo_check_exists_before_and_after_mkdir() {
1153 let tmpdir = tmpdir();
1154 let dir = &tmpdir.join("before_and_after_dir");
1155 assert!(!dir.exists());
1156 check!(fs::create_dir(dir));
1157 assert!(dir.exists());
1158 assert!(dir.is_dir());
1159 check!(fs::remove_dir(dir));
1160 assert!(!dir.exists());
1164 fn file_test_directoryinfo_readdir() {
1165 let tmpdir = tmpdir();
1166 let dir = &tmpdir.join("di_readdir");
1167 check!(fs::create_dir(dir));
1170 let f = dir.join(&format!("{}.txt", n));
1171 let mut w = check!(File::create(&f));
1172 let msg_str = format!("{}{}", prefix, n.to_string());
1173 let msg = msg_str.as_bytes();
1174 check!(w.write(msg));
1176 let files = check!(fs::read_dir(dir));
1177 let mut mem = [0; 4];
1179 let f = f.unwrap().path();
1181 let n = f.file_stem().unwrap();
1182 check!(check!(File::open(&f)).read(&mut mem));
1183 let read_str = str::from_utf8(&mem).unwrap();
1184 let expected = format!("{}{}", prefix, n.to_str().unwrap());
1185 assert_eq!(expected, read_str);
1187 check!(fs::remove_file(&f));
1189 check!(fs::remove_dir(dir));
1193 fn file_test_walk_dir() {
1194 let tmpdir = tmpdir();
1195 let dir = &tmpdir.join("walk_dir");
1196 check!(fs::create_dir(dir));
1198 let dir1 = &dir.join("01/02/03");
1199 check!(fs::create_dir_all(dir1));
1200 check!(File::create(&dir1.join("04")));
1202 let dir2 = &dir.join("11/12/13");
1203 check!(fs::create_dir_all(dir2));
1204 check!(File::create(&dir2.join("14")));
1206 let files = check!(fs::walk_dir(dir));
1207 let mut cur = [0; 2];
1209 let f = f.unwrap().path();
1210 let stem = f.file_stem().unwrap().to_str().unwrap();
1211 let root = stem.as_bytes()[0] - b'0';
1212 let name = stem.as_bytes()[1] - b'0';
1213 assert!(cur[root as usize] < name);
1214 cur[root as usize] = name;
1217 check!(fs::remove_dir_all(dir));
1221 fn mkdir_path_already_exists_error() {
1222 let tmpdir = tmpdir();
1223 let dir = &tmpdir.join("mkdir_error_twice");
1224 check!(fs::create_dir(dir));
1225 let e = fs::create_dir(dir).err().unwrap();
1226 assert_eq!(e.kind(), ErrorKind::AlreadyExists);
1230 fn recursive_mkdir() {
1231 let tmpdir = tmpdir();
1232 let dir = tmpdir.join("d1/d2");
1233 check!(fs::create_dir_all(&dir));
1234 assert!(dir.is_dir())
1238 fn recursive_mkdir_failure() {
1239 let tmpdir = tmpdir();
1240 let dir = tmpdir.join("d1");
1241 let file = dir.join("f1");
1243 check!(fs::create_dir_all(&dir));
1244 check!(File::create(&file));
1246 let result = fs::create_dir_all(&file);
1248 assert!(result.is_err());
1249 // error!(result, "couldn't recursively mkdir");
1250 // error!(result, "couldn't create directory");
1251 // error!(result, "mode=0700");
1252 // error!(result, format!("path={}", file.display()));
1256 fn recursive_mkdir_slash() {
1257 check!(fs::create_dir_all(&Path2::new("/")));
1260 // FIXME(#12795) depends on lstat to work on windows
1261 #[cfg(not(windows))]
1263 fn recursive_rmdir() {
1264 let tmpdir = tmpdir();
1265 let d1 = tmpdir.join("d1");
1266 let dt = d1.join("t");
1267 let dtt = dt.join("t");
1268 let d2 = tmpdir.join("d2");
1269 let canary = d2.join("do_not_delete");
1270 check!(fs::create_dir_all(&dtt));
1271 check!(fs::create_dir_all(&d2));
1272 check!(check!(File::create(&canary)).write(b"foo"));
1273 check!(fs::soft_link(&d2, &dt.join("d2")));
1274 check!(fs::remove_dir_all(&d1));
1276 assert!(!d1.is_dir());
1277 assert!(canary.exists());
1281 fn unicode_path_is_dir() {
1282 assert!(Path2::new(".").is_dir());
1283 assert!(!Path2::new("test/stdtest/fs.rs").is_dir());
1285 let tmpdir = tmpdir();
1287 let mut dirpath = tmpdir.path().to_path_buf();
1288 dirpath.push(&format!("test-가一ー你好"));
1289 check!(fs::create_dir(&dirpath));
1290 assert!(dirpath.is_dir());
1292 let mut filepath = dirpath;
1293 filepath.push("unicode-file-\u{ac00}\u{4e00}\u{30fc}\u{4f60}\u{597d}.rs");
1294 check!(File::create(&filepath)); // ignore return; touch only
1295 assert!(!filepath.is_dir());
1296 assert!(filepath.exists());
1300 fn unicode_path_exists() {
1301 assert!(Path2::new(".").exists());
1302 assert!(!Path2::new("test/nonexistent-bogus-path").exists());
1304 let tmpdir = tmpdir();
1305 let unicode = tmpdir.path();
1306 let unicode = unicode.join(&format!("test-각丁ー再见"));
1307 check!(fs::create_dir(&unicode));
1308 assert!(unicode.exists());
1309 assert!(!Path2::new("test/unicode-bogus-path-각丁ー再见").exists());
1313 fn copy_file_does_not_exist() {
1314 let from = Path2::new("test/nonexistent-bogus-path");
1315 let to = Path2::new("test/other-bogus-path");
1317 match fs::copy(&from, &to) {
1320 assert!(!from.exists());
1321 assert!(!to.exists());
1328 let tmpdir = tmpdir();
1329 let input = tmpdir.join("in.txt");
1330 let out = tmpdir.join("out.txt");
1332 check!(check!(File::create(&input)).write(b"hello"));
1333 check!(fs::copy(&input, &out));
1334 let mut v = Vec::new();
1335 check!(check!(File::open(&out)).read_to_end(&mut v));
1336 assert_eq!(v.as_slice(), b"hello");
1338 assert_eq!(check!(input.metadata()).permissions(),
1339 check!(out.metadata()).permissions());
1343 fn copy_file_dst_dir() {
1344 let tmpdir = tmpdir();
1345 let out = tmpdir.join("out");
1347 check!(File::create(&out));
1348 match fs::copy(&*out, tmpdir.path()) {
1349 Ok(..) => panic!(), Err(..) => {}
1354 fn copy_file_dst_exists() {
1355 let tmpdir = tmpdir();
1356 let input = tmpdir.join("in");
1357 let output = tmpdir.join("out");
1359 check!(check!(File::create(&input)).write("foo".as_bytes()));
1360 check!(check!(File::create(&output)).write("bar".as_bytes()));
1361 check!(fs::copy(&input, &output));
1363 let mut v = Vec::new();
1364 check!(check!(File::open(&output)).read_to_end(&mut v));
1365 assert_eq!(v, b"foo".to_vec());
1369 fn copy_file_src_dir() {
1370 let tmpdir = tmpdir();
1371 let out = tmpdir.join("out");
1373 match fs::copy(tmpdir.path(), &out) {
1374 Ok(..) => panic!(), Err(..) => {}
1376 assert!(!out.exists());
1380 fn copy_file_preserves_perm_bits() {
1381 let tmpdir = tmpdir();
1382 let input = tmpdir.join("in.txt");
1383 let out = tmpdir.join("out.txt");
1385 let attr = check!(check!(File::create(&input)).metadata());
1386 let mut p = attr.permissions();
1387 p.set_readonly(true);
1388 check!(fs::set_permissions(&input, p));
1389 check!(fs::copy(&input, &out));
1390 assert!(check!(out.metadata()).permissions().readonly());
1391 check!(fs::set_permissions(&input, attr.permissions()));
1392 check!(fs::set_permissions(&out, attr.permissions()));
1395 #[cfg(not(windows))] // FIXME(#10264) operation not permitted?
1397 fn symlinks_work() {
1398 let tmpdir = tmpdir();
1399 let input = tmpdir.join("in.txt");
1400 let out = tmpdir.join("out.txt");
1402 check!(check!(File::create(&input)).write("foobar".as_bytes()));
1403 check!(fs::soft_link(&input, &out));
1404 // if cfg!(not(windows)) {
1405 // assert_eq!(check!(lstat(&out)).kind, FileType::Symlink);
1406 // assert_eq!(check!(out.lstat()).kind, FileType::Symlink);
1408 assert_eq!(check!(fs::metadata(&out)).len(),
1409 check!(fs::metadata(&input)).len());
1410 let mut v = Vec::new();
1411 check!(check!(File::open(&out)).read_to_end(&mut v));
1412 assert_eq!(v, b"foobar".to_vec());
1415 #[cfg(not(windows))] // apparently windows doesn't like symlinks
1417 fn symlink_noexist() {
1418 let tmpdir = tmpdir();
1419 // symlinks can point to things that don't exist
1420 check!(fs::soft_link(&tmpdir.join("foo"), &tmpdir.join("bar")));
1421 assert_eq!(check!(fs::read_link(&tmpdir.join("bar"))),
1422 tmpdir.join("foo"));
1426 fn readlink_not_symlink() {
1427 let tmpdir = tmpdir();
1428 match fs::read_link(tmpdir.path()) {
1429 Ok(..) => panic!("wanted a failure"),
1436 let tmpdir = tmpdir();
1437 let input = tmpdir.join("in.txt");
1438 let out = tmpdir.join("out.txt");
1440 check!(check!(File::create(&input)).write("foobar".as_bytes()));
1441 check!(fs::hard_link(&input, &out));
1442 assert_eq!(check!(fs::metadata(&out)).len(),
1443 check!(fs::metadata(&input)).len());
1444 assert_eq!(check!(fs::metadata(&out)).len(),
1445 check!(input.metadata()).len());
1446 let mut v = Vec::new();
1447 check!(check!(File::open(&out)).read_to_end(&mut v));
1448 assert_eq!(v, b"foobar".to_vec());
1450 // can't link to yourself
1451 match fs::hard_link(&input, &input) {
1452 Ok(..) => panic!("wanted a failure"),
1455 // can't link to something that doesn't exist
1456 match fs::hard_link(&tmpdir.join("foo"), &tmpdir.join("bar")) {
1457 Ok(..) => panic!("wanted a failure"),
1464 let tmpdir = tmpdir();
1465 let file = tmpdir.join("in.txt");
1467 check!(File::create(&file));
1468 let attr = check!(fs::metadata(&file));
1469 assert!(!attr.permissions().readonly());
1470 let mut p = attr.permissions();
1471 p.set_readonly(true);
1472 check!(fs::set_permissions(&file, p.clone()));
1473 let attr = check!(fs::metadata(&file));
1474 assert!(attr.permissions().readonly());
1476 match fs::set_permissions(&tmpdir.join("foo"), p.clone()) {
1477 Ok(..) => panic!("wanted an error"),
1481 p.set_readonly(false);
1482 check!(fs::set_permissions(&file, p));
1486 fn sync_doesnt_kill_anything() {
1487 let tmpdir = tmpdir();
1488 let path = tmpdir.join("in.txt");
1490 let mut file = check!(File::create(&path));
1491 check!(file.sync_all());
1492 check!(file.sync_data());
1493 check!(file.write(b"foo"));
1494 check!(file.sync_all());
1495 check!(file.sync_data());
1499 fn truncate_works() {
1500 let tmpdir = tmpdir();
1501 let path = tmpdir.join("in.txt");
1503 let mut file = check!(File::create(&path));
1504 check!(file.write(b"foo"));
1505 check!(file.sync_all());
1507 // Do some simple things with truncation
1508 assert_eq!(check!(file.metadata()).len(), 3);
1509 check!(file.set_len(10));
1510 assert_eq!(check!(file.metadata()).len(), 10);
1511 check!(file.write(b"bar"));
1512 check!(file.sync_all());
1513 assert_eq!(check!(file.metadata()).len(), 10);
1515 let mut v = Vec::new();
1516 check!(check!(File::open(&path)).read_to_end(&mut v));
1517 assert_eq!(v, b"foobar\0\0\0\0".to_vec());
1519 // Truncate to a smaller length, don't seek, and then write something.
1520 // Ensure that the intermediate zeroes are all filled in (we have `seek`ed
1521 // past the end of the file).
1522 check!(file.set_len(2));
1523 assert_eq!(check!(file.metadata()).len(), 2);
1524 check!(file.write(b"wut"));
1525 check!(file.sync_all());
1526 assert_eq!(check!(file.metadata()).len(), 9);
1527 let mut v = Vec::new();
1528 check!(check!(File::open(&path)).read_to_end(&mut v));
1529 assert_eq!(v, b"fo\0\0\0\0wut".to_vec());
1534 use fs::OpenOptions as OO;
1535 fn c<T: Clone>(t: &T) -> T { t.clone() }
1537 let tmpdir = tmpdir();
1539 let mut r = OO::new(); r.read(true);
1540 let mut w = OO::new(); w.write(true);
1541 let mut rw = OO::new(); rw.write(true).read(true);
1543 match r.open(&tmpdir.join("a")) {
1544 Ok(..) => panic!(), Err(..) => {}
1547 // Perform each one twice to make sure that it succeeds the second time
1548 // (where the file exists)
1549 check!(c(&w).create(true).open(&tmpdir.join("b")));
1550 assert!(tmpdir.join("b").exists());
1551 check!(c(&w).create(true).open(&tmpdir.join("b")));
1552 check!(w.open(&tmpdir.join("b")));
1554 check!(c(&rw).create(true).open(&tmpdir.join("c")));
1555 assert!(tmpdir.join("c").exists());
1556 check!(c(&rw).create(true).open(&tmpdir.join("c")));
1557 check!(rw.open(&tmpdir.join("c")));
1559 check!(c(&w).append(true).create(true).open(&tmpdir.join("d")));
1560 assert!(tmpdir.join("d").exists());
1561 check!(c(&w).append(true).create(true).open(&tmpdir.join("d")));
1562 check!(c(&w).append(true).open(&tmpdir.join("d")));
1564 check!(c(&rw).append(true).create(true).open(&tmpdir.join("e")));
1565 assert!(tmpdir.join("e").exists());
1566 check!(c(&rw).append(true).create(true).open(&tmpdir.join("e")));
1567 check!(c(&rw).append(true).open(&tmpdir.join("e")));
1569 check!(c(&w).truncate(true).create(true).open(&tmpdir.join("f")));
1570 assert!(tmpdir.join("f").exists());
1571 check!(c(&w).truncate(true).create(true).open(&tmpdir.join("f")));
1572 check!(c(&w).truncate(true).open(&tmpdir.join("f")));
1574 check!(c(&rw).truncate(true).create(true).open(&tmpdir.join("g")));
1575 assert!(tmpdir.join("g").exists());
1576 check!(c(&rw).truncate(true).create(true).open(&tmpdir.join("g")));
1577 check!(c(&rw).truncate(true).open(&tmpdir.join("g")));
1579 check!(check!(File::create(&tmpdir.join("h"))).write("foo".as_bytes()));
1580 check!(r.open(&tmpdir.join("h")));
1582 let mut f = check!(r.open(&tmpdir.join("h")));
1583 assert!(f.write("wut".as_bytes()).is_err());
1585 assert_eq!(check!(fs::metadata(&tmpdir.join("h"))).len(), 3);
1587 let mut f = check!(c(&w).append(true).open(&tmpdir.join("h")));
1588 check!(f.write("bar".as_bytes()));
1590 assert_eq!(check!(fs::metadata(&tmpdir.join("h"))).len(), 6);
1592 let mut f = check!(c(&w).truncate(true).open(&tmpdir.join("h")));
1593 check!(f.write("bar".as_bytes()));
1595 assert_eq!(check!(fs::metadata(&tmpdir.join("h"))).len(), 3);
1600 let tmpdir = tmpdir();
1601 let path = tmpdir.join("a");
1602 check!(File::create(&path));
1603 // These numbers have to be bigger than the time in the day to account
1604 // for timezones Windows in particular will fail in certain timezones
1605 // with small enough values
1606 check!(fs::set_file_times(&path, 100000, 200000));
1607 assert_eq!(check!(path.metadata()).accessed(), 100000);
1608 assert_eq!(check!(path.metadata()).modified(), 200000);
1612 fn utime_noexist() {
1613 let tmpdir = tmpdir();
1615 match fs::set_file_times(&tmpdir.join("a"), 100, 200) {
1623 let mut bytes = [0; 1024];
1624 StdRng::new().unwrap().fill_bytes(&mut bytes);
1626 let tmpdir = tmpdir();
1628 check!(check!(File::create(&tmpdir.join("test"))).write(&bytes));
1629 let mut v = Vec::new();
1630 check!(check!(File::open(&tmpdir.join("test"))).read_to_end(&mut v));
1631 assert!(v == bytes.as_slice());
1635 #[cfg(not(windows))]
1636 fn unlink_readonly() {
1637 let tmpdir = tmpdir();
1638 let path = tmpdir.join("file");
1639 check!(File::create(&path));
1640 let mut perm = check!(fs::metadata(&path)).permissions();
1641 perm.set_readonly(true);
1642 check!(fs::set_permissions(&path, perm));
1643 check!(fs::remove_file(&path));
1647 fn mkdir_trailing_slash() {
1648 let tmpdir = tmpdir();
1649 let path = tmpdir.join("file");
1650 check!(fs::create_dir_all(&path.join("a/")));