1 // Copyright 2013-2014 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 /*! Synchronous File I/O
13 This module provides a set of functions and traits for working
14 with regular files & directories on a filesystem.
16 At the top-level of the module are a set of freestanding functions, associated
17 with various filesystem operations. They all operate on `Path` objects.
19 All operations in this module, including those as part of `File` et al
20 block the task during execution. In the event of failure, all functions/methods
21 will return an `IoResult` type with an `Err` value.
23 Also included in this module is an implementation block on the `Path` object
24 defined in `std::path::Path`. The impl adds useful methods about inspecting the
25 metadata of a file. This includes getting the `stat` information, reading off
26 particular bits of it, etc.
31 # #![allow(unused_must_use)]
32 use std::io::{File, fs};
34 let path = Path::new("foo.txt");
36 // create the file, whether it exists or not
37 let mut file = File::create(&path);
38 file.write(b"foobar");
41 // open the file in read-only mode
42 let mut file = File::open(&path);
45 println!("{}", path.stat().unwrap().size);
54 use collections::Collection;
55 use io::standard_error;
56 use io::{FilePermission, Write, UnstableFileStat, Open, FileAccess, FileMode};
57 use io::{IoResult, IoError, FileStat, SeekStyle, Seek, Writer, Reader};
58 use io::{Read, Truncate, SeekCur, SeekSet, ReadWrite, SeekEnd, Append};
59 use io::UpdateIoError;
64 use option::{Some, None, Option};
66 use path::{Path, GenericPath};
68 use result::{Err, Ok};
69 use rt::rtio::LocalIo;
71 use slice::ImmutableVector;
75 /// Unconstrained file access type that exposes read and write operations
77 /// Can be constructed via `File::open()`, `File::create()`, and
78 /// `File::open_mode()`.
82 /// This type will return errors as an `IoResult<T>` if operations are
83 /// attempted against it for which its underlying file descriptor was not
84 /// configured at creation time, via the `FileAccess` parameter to
85 /// `File::open_mode()`.
87 fd: Box<rtio::RtioFileStream + Send>,
93 /// Open a file at `path` in the mode specified by the `mode` and `access`
98 /// ```rust,should_fail
99 /// use std::io::{File, Open, ReadWrite};
101 /// let p = Path::new("/some/file/path.txt");
103 /// let file = match File::open_mode(&p, Open, ReadWrite) {
105 /// Err(e) => fail!("file error: {}", e),
107 /// // do some stuff with that file
109 /// // the file will be closed at the end of this block
112 /// `FileMode` and `FileAccess` provide information about the permissions
113 /// context in which a given stream is created. More information about them
114 /// can be found in `std::io`'s docs. If a file is opened with `Write`
115 /// or `ReadWrite` access, then it will be created it does not already
118 /// Note that, with this function, a `File` is returned regardless of the
119 /// access-limitations indicated by `FileAccess` (e.g. calling `write` on a
120 /// `File` opened as `Read` will return an error at runtime).
124 /// This function will return an error under a number of different
125 /// circumstances, to include but not limited to:
127 /// * Opening a file that does not exist with `Read` access.
128 /// * Attempting to open a file with a `FileAccess` that the user lacks
130 /// * Filesystem-level errors (full disk, etc)
131 pub fn open_mode(path: &Path,
133 access: FileAccess) -> IoResult<File> {
134 let rtio_mode = match mode {
136 Append => rtio::Append,
137 Truncate => rtio::Truncate,
139 let rtio_access = match access {
141 Write => rtio::Write,
142 ReadWrite => rtio::ReadWrite,
144 let err = LocalIo::maybe_raise(|io| {
145 io.fs_open(&path.to_c_str(), rtio_mode, rtio_access).map(|fd| {
152 }).map_err(IoError::from_rtio_error);
153 err.update_err("couldn't open file", |e| {
154 format!("{}; path={}; mode={}; access={}", e, path.display(),
155 mode_string(mode), access_string(access))
159 /// Attempts to open a file in read-only mode. This function is equivalent to
160 /// `File::open_mode(path, Open, Read)`, and will raise all of the same
161 /// errors that `File::open_mode` does.
163 /// For more information, see the `File::open_mode` function.
168 /// use std::io::File;
170 /// let contents = File::open(&Path::new("foo.txt")).read_to_end();
172 pub fn open(path: &Path) -> IoResult<File> {
173 File::open_mode(path, Open, Read)
176 /// Attempts to create a file in write-only mode. This function is
177 /// equivalent to `File::open_mode(path, Truncate, Write)`, and will
178 /// raise all of the same errors that `File::open_mode` does.
180 /// For more information, see the `File::open_mode` function.
185 /// # #![allow(unused_must_use)]
186 /// use std::io::File;
188 /// let mut f = File::create(&Path::new("foo.txt"));
189 /// f.write(b"This is a sample file");
191 /// # ::std::io::fs::unlink(&Path::new("foo.txt"));
193 pub fn create(path: &Path) -> IoResult<File> {
194 File::open_mode(path, Truncate, Write)
195 .update_desc("couldn't create file")
198 /// Returns the original path which was used to open this file.
199 pub fn path<'a>(&'a self) -> &'a Path {
203 /// Synchronizes all modifications to this file to its permanent storage
204 /// device. This will flush any internal buffers necessary to perform this
206 pub fn fsync(&mut self) -> IoResult<()> {
207 let err = self.fd.fsync().map_err(IoError::from_rtio_error);
208 err.update_err("couldn't fsync file",
209 |e| format!("{}; path={}", e, self.path.display()))
212 /// This function is similar to `fsync`, except that it may not synchronize
213 /// file metadata to the filesystem. This is intended for use case which
214 /// must synchronize content, but don't need the metadata on disk. The goal
215 /// of this method is to reduce disk operations.
216 pub fn datasync(&mut self) -> IoResult<()> {
217 let err = self.fd.datasync().map_err(IoError::from_rtio_error);
218 err.update_err("couldn't datasync file",
219 |e| format!("{}; path={}", e, self.path.display()))
222 /// Either truncates or extends the underlying file, updating the size of
223 /// this file to become `size`. This is equivalent to unix's `truncate`
226 /// If the `size` is less than the current file's size, then the file will
227 /// be shrunk. If it is greater than the current file's size, then the file
228 /// will be extended to `size` and have all of the intermediate data filled
230 pub fn truncate(&mut self, size: i64) -> IoResult<()> {
231 let err = self.fd.truncate(size).map_err(IoError::from_rtio_error);
232 err.update_err("couldn't truncate file", |e| {
233 format!("{}; path={}; size={}", e, self.path.display(), size)
237 /// Tests whether this stream has reached EOF.
239 /// If true, then this file will no longer continue to return data via
241 pub fn eof(&self) -> bool {
245 /// Queries information about the underlying file.
246 pub fn stat(&mut self) -> IoResult<FileStat> {
247 let err = match self.fd.fstat() {
248 Ok(s) => Ok(from_rtio(s)),
249 Err(e) => Err(IoError::from_rtio_error(e)),
251 err.update_err("couldn't fstat file",
252 |e| format!("{}; path={}", e, self.path.display()))
256 /// Unlink a file from the underlying filesystem.
261 /// # #![allow(unused_must_use)]
264 /// let p = Path::new("/some/file/path.txt");
268 /// Note that, just because an unlink call was successful, it is not
269 /// guaranteed that a file is immediately deleted (e.g. depending on
270 /// platform, other open file descriptors may prevent immediate removal)
274 /// This function will return an error if the path points to a directory, the
275 /// user lacks permissions to remove the file, or if some other filesystem-level
277 pub fn unlink(path: &Path) -> IoResult<()> {
278 let err = LocalIo::maybe_raise(|io| {
279 io.fs_unlink(&path.to_c_str())
280 }).map_err(IoError::from_rtio_error);
281 err.update_err("couldn't unlink path",
282 |e| format!("{}; path={}", e, path.display()))
285 /// Given a path, query the file system to get information about a file,
286 /// directory, etc. This function will traverse symlinks to query
287 /// information about the destination file.
294 /// let p = Path::new("/some/file/path.txt");
295 /// match fs::stat(&p) {
296 /// Ok(stat) => { /* ... */ }
297 /// Err(e) => { /* handle error */ }
303 /// This call will return an error if the user lacks the requisite permissions
304 /// to perform a `stat` call on the given path or if there is no entry in the
305 /// filesystem at the provided path.
306 pub fn stat(path: &Path) -> IoResult<FileStat> {
307 let err = match LocalIo::maybe_raise(|io| io.fs_stat(&path.to_c_str())) {
308 Ok(s) => Ok(from_rtio(s)),
309 Err(e) => Err(IoError::from_rtio_error(e)),
311 err.update_err("couldn't stat path",
312 |e| format!("{}; path={}", e, path.display()))
315 /// Perform the same operation as the `stat` function, except that this
316 /// function does not traverse through symlinks. This will return
317 /// information about the symlink file instead of the file that it points
323 pub fn lstat(path: &Path) -> IoResult<FileStat> {
324 let err = match LocalIo::maybe_raise(|io| io.fs_lstat(&path.to_c_str())) {
325 Ok(s) => Ok(from_rtio(s)),
326 Err(e) => Err(IoError::from_rtio_error(e)),
328 err.update_err("couldn't lstat path",
329 |e| format!("{}; path={}", e, path.display()))
332 fn from_rtio(s: rtio::FileStat) -> FileStat {
334 size, kind, perm, created, modified,
335 accessed, device, inode, rdev,
336 nlink, uid, gid, blksize, blocks, flags, gen
341 kind: match (kind as libc::c_int) & libc::S_IFMT {
342 libc::S_IFREG => io::TypeFile,
343 libc::S_IFDIR => io::TypeDirectory,
344 libc::S_IFIFO => io::TypeNamedPipe,
345 libc::S_IFBLK => io::TypeBlockSpecial,
346 libc::S_IFLNK => io::TypeSymlink,
347 _ => io::TypeUnknown,
349 perm: FilePermission::from_bits_truncate(perm as u32),
353 unstable: UnstableFileStat {
368 /// Rename a file or directory to a new name.
373 /// # #![allow(unused_must_use)]
376 /// fs::rename(&Path::new("foo"), &Path::new("bar"));
381 /// Will return an error if the provided `path` doesn't exist, the process lacks
382 /// permissions to view the contents, or if some other intermittent I/O error
384 pub fn rename(from: &Path, to: &Path) -> IoResult<()> {
385 let err = LocalIo::maybe_raise(|io| {
386 io.fs_rename(&from.to_c_str(), &to.to_c_str())
387 }).map_err(IoError::from_rtio_error);
388 err.update_err("couldn't rename path", |e| {
389 format!("{}; from={}; to={}", e, from.display(), to.display())
393 /// Copies the contents of one file to another. This function will also
394 /// copy the permission bits of the original file to the destination file.
396 /// Note that if `from` and `to` both point to the same file, then the file
397 /// will likely get truncated by this operation.
402 /// # #![allow(unused_must_use)]
405 /// fs::copy(&Path::new("foo.txt"), &Path::new("bar.txt"));
410 /// Will return an error in the following situations, but is not limited to
411 /// just these cases:
413 /// * The `from` path is not a file
414 /// * The `from` file does not exist
415 /// * The current process does not have the permission rights to access
416 /// `from` or write `to`
418 /// Note that this copy is not atomic in that once the destination is
419 /// ensured to not exist, there is nothing preventing the destination from
420 /// being created and then destroyed by this operation.
421 pub fn copy(from: &Path, to: &Path) -> IoResult<()> {
422 fn update_err<T>(result: IoResult<T>, from: &Path, to: &Path) -> IoResult<T> {
423 result.update_err("couldn't copy path",
424 |e| format!("{}; from={}; to={}", e, from.display(), to.display()))
428 return update_err(Err(IoError {
429 kind: io::MismatchedFileTypeForOperation,
430 desc: "the source path is not an existing file",
435 let mut reader = try!(File::open(from));
436 let mut writer = try!(File::create(to));
437 let mut buf = [0, ..io::DEFAULT_BUF_SIZE];
440 let amt = match reader.read(buf) {
442 Err(ref e) if e.kind == io::EndOfFile => { break }
443 Err(e) => return update_err(Err(e), from, to)
445 try!(writer.write(buf.slice_to(amt)));
448 chmod(to, try!(update_err(from.stat(), from, to)).perm)
451 /// Changes the permission mode bits found on a file or a directory. This
452 /// function takes a mask from the `io` module
457 /// # #![allow(unused_must_use)]
461 /// fs::chmod(&Path::new("file.txt"), io::UserFile);
462 /// fs::chmod(&Path::new("file.txt"), io::UserRead | io::UserWrite);
463 /// fs::chmod(&Path::new("dir"), io::UserDir);
464 /// fs::chmod(&Path::new("file.exe"), io::UserExec);
469 /// If this function encounters an I/O error, it will return an `Err` value.
470 /// Some possible error situations are not having the permission to
471 /// change the attributes of a file or the file not existing.
472 pub fn chmod(path: &Path, mode: io::FilePermission) -> IoResult<()> {
473 let err = LocalIo::maybe_raise(|io| {
474 io.fs_chmod(&path.to_c_str(), mode.bits() as uint)
475 }).map_err(IoError::from_rtio_error);
476 err.update_err("couldn't chmod path", |e| {
477 format!("{}; path={}; mode={}", e, path.display(), mode)
481 /// Change the user and group owners of a file at the specified path.
482 pub fn chown(path: &Path, uid: int, gid: int) -> IoResult<()> {
483 let err = LocalIo::maybe_raise(|io| {
484 io.fs_chown(&path.to_c_str(), uid, gid)
485 }).map_err(IoError::from_rtio_error);
486 err.update_err("couldn't chown path", |e| {
487 format!("{}; path={}; uid={}; gid={}", e, path.display(), uid, gid)
491 /// Creates a new hard link on the filesystem. The `dst` path will be a
492 /// link pointing to the `src` path. Note that systems often require these
493 /// two paths to both be located on the same filesystem.
494 pub fn link(src: &Path, dst: &Path) -> IoResult<()> {
495 let err = LocalIo::maybe_raise(|io| {
496 io.fs_link(&src.to_c_str(), &dst.to_c_str())
497 }).map_err(IoError::from_rtio_error);
498 err.update_err("couldn't link path", |e| {
499 format!("{}; src={}; dest={}", e, src.display(), dst.display())
503 /// Creates a new symbolic link on the filesystem. The `dst` path will be a
504 /// symlink pointing to the `src` path.
505 pub fn symlink(src: &Path, dst: &Path) -> IoResult<()> {
506 let err = LocalIo::maybe_raise(|io| {
507 io.fs_symlink(&src.to_c_str(), &dst.to_c_str())
508 }).map_err(IoError::from_rtio_error);
509 err.update_err("couldn't symlink path", |e| {
510 format!("{}; src={}; dest={}", e, src.display(), dst.display())
514 /// Reads a symlink, returning the file that the symlink points to.
518 /// This function will return an error on failure. Failure conditions include
519 /// reading a file that does not exist or reading a file which is not a symlink.
520 pub fn readlink(path: &Path) -> IoResult<Path> {
521 let err = LocalIo::maybe_raise(|io| {
522 Ok(Path::new(try!(io.fs_readlink(&path.to_c_str()))))
523 }).map_err(IoError::from_rtio_error);
524 err.update_err("couldn't resolve symlink for path",
525 |e| format!("{}; path={}", e, path.display()))
528 /// Create a new, empty directory at the provided path
533 /// # #![allow(unused_must_use)]
537 /// let p = Path::new("/some/dir");
538 /// fs::mkdir(&p, io::UserRWX);
543 /// This call will return an error if the user lacks permissions to make a new
544 /// directory at the provided path, or if the directory already exists.
545 pub fn mkdir(path: &Path, mode: FilePermission) -> IoResult<()> {
546 let err = LocalIo::maybe_raise(|io| {
547 io.fs_mkdir(&path.to_c_str(), mode.bits() as uint)
548 }).map_err(IoError::from_rtio_error);
549 err.update_err("couldn't create directory", |e| {
550 format!("{}; path={}; mode={}", e, path.display(), mode)
554 /// Remove an existing, empty directory
559 /// # #![allow(unused_must_use)]
562 /// let p = Path::new("/some/dir");
568 /// This call will return an error if the user lacks permissions to remove the
569 /// directory at the provided path, or if the directory isn't empty.
570 pub fn rmdir(path: &Path) -> IoResult<()> {
571 let err = LocalIo::maybe_raise(|io| {
572 io.fs_rmdir(&path.to_c_str())
573 }).map_err(IoError::from_rtio_error);
574 err.update_err("couldn't remove directory",
575 |e| format!("{}; path={}", e, path.display()))
578 /// Retrieve a vector containing all entries within a provided directory
586 /// // one possible implementation of fs::walk_dir only visiting files
587 /// fn visit_dirs(dir: &Path, cb: |&Path|) -> io::IoResult<()> {
588 /// if dir.is_dir() {
589 /// let contents = try!(fs::readdir(dir));
590 /// for entry in contents.iter() {
591 /// if entry.is_dir() {
592 /// try!(visit_dirs(entry, |p| cb(p)));
599 /// Err(io::standard_error(io::InvalidInput))
606 /// Will return an error if the provided `from` doesn't exist, the process lacks
607 /// permissions to view the contents or if the `path` points at a non-directory
609 pub fn readdir(path: &Path) -> IoResult<Vec<Path>> {
610 let err = LocalIo::maybe_raise(|io| {
611 Ok(try!(io.fs_readdir(&path.to_c_str(), 0)).move_iter().map(|a| {
614 }).map_err(IoError::from_rtio_error);
615 err.update_err("couldn't read directory",
616 |e| format!("{}; path={}", e, path.display()))
619 /// Returns an iterator which will recursively walk the directory structure
620 /// rooted at `path`. The path given will not be iterated over, and this will
621 /// perform iteration in some top-down order. The contents of unreadable
622 /// subdirectories are ignored.
623 pub fn walk_dir(path: &Path) -> IoResult<Directories> {
625 stack: try!(readdir(path).update_err("couldn't walk directory",
626 |e| format!("{}; path={}",
631 /// An iterator which walks over a directory
632 pub struct Directories {
636 impl Iterator<Path> for Directories {
637 fn next(&mut self) -> Option<Path> {
638 match self.stack.pop() {
641 let result = readdir(&path)
642 .update_err("couldn't advance Directories iterator",
643 |e| format!("{}; path={}",
647 Ok(dirs) => { self.stack.push_all_move(dirs); }
658 /// Recursively create a directory and all of its parent components if they
663 /// This function will return an `Err` value if an error happens, see
664 /// `fs::mkdir` for more information about error conditions and performance.
665 pub fn mkdir_recursive(path: &Path, mode: FilePermission) -> IoResult<()> {
666 // tjc: if directory exists but with different permissions,
667 // should we return false?
672 let mut comps = path.components();
673 let mut curpath = path.root_path().unwrap_or(Path::new("."));
678 let result = mkdir(&curpath, mode)
679 .update_err("couldn't recursively mkdir",
680 |e| format!("{}; path={}", e, path.display()));
685 if try!(stat(&curpath)).kind != io::TypeDirectory {
686 return Err(mkdir_err);
696 /// Removes a directory at this path, after removing all its contents. Use
701 /// This function will return an `Err` value if an error happens. See
702 /// `file::unlink` and `fs::readdir` for possible error conditions.
703 pub fn rmdir_recursive(path: &Path) -> IoResult<()> {
704 let mut rm_stack = Vec::new();
705 rm_stack.push(path.clone());
707 fn rmdir_failed(err: &IoError, path: &Path) -> String {
708 format!("rmdir_recursive failed; path={}; cause={}",
712 fn update_err<T>(err: IoResult<T>, path: &Path) -> IoResult<T> {
713 err.update_err("couldn't recursively rmdir",
714 |e| rmdir_failed(e, path))
717 while !rm_stack.is_empty() {
718 let children = try!(readdir(rm_stack.last().unwrap())
719 .update_detail(|e| rmdir_failed(e, path)));
721 let mut has_child_dir = false;
723 // delete all regular files in the way and push subdirs
725 for child in children.move_iter() {
726 // FIXME(#12795) we should use lstat in all cases
727 let child_type = match cfg!(windows) {
728 true => try!(update_err(stat(&child), path)),
729 false => try!(update_err(lstat(&child), path))
732 if child_type.kind == io::TypeDirectory {
733 rm_stack.push(child);
734 has_child_dir = true;
736 // we can carry on safely if the file is already gone
737 // (eg: deleted by someone else since readdir)
738 match update_err(unlink(&child), path) {
740 Err(ref e) if e.kind == io::FileNotFound => (),
741 Err(e) => return Err(e)
746 // if no subdir was found, let's pop and delete
748 let result = update_err(rmdir(&rm_stack.pop().unwrap()), path);
751 Err(ref e) if e.kind == io::FileNotFound => (),
752 Err(e) => return Err(e)
760 /// Changes the timestamps for a file's last modification and access time.
761 /// The file at the path specified will have its last access time set to
762 /// `atime` and its modification time set to `mtime`. The times specified should
763 /// be in milliseconds.
764 // FIXME(#10301) these arguments should not be u64
765 pub fn change_file_times(path: &Path, atime: u64, mtime: u64) -> IoResult<()> {
766 let err = LocalIo::maybe_raise(|io| {
767 io.fs_utime(&path.to_c_str(), atime, mtime)
768 }).map_err(IoError::from_rtio_error);
769 err.update_err("couldn't change_file_times",
770 |e| format!("{}; path={}", e, path.display()))
773 impl Reader for File {
774 fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
775 fn update_err<T>(result: IoResult<T>, file: &File) -> IoResult<T> {
776 result.update_err("couldn't read file",
777 |e| format!("{}; path={}",
778 e, file.path.display()))
781 let result = update_err(self.fd.read(buf)
782 .map_err(IoError::from_rtio_error), self);
786 self.last_nread = read;
788 0 => update_err(Err(standard_error(io::EndOfFile)), self),
789 _ => Ok(read as uint)
797 impl Writer for File {
798 fn write(&mut self, buf: &[u8]) -> IoResult<()> {
799 let err = self.fd.write(buf).map_err(IoError::from_rtio_error);
800 err.update_err("couldn't write to file",
801 |e| format!("{}; path={}", e, self.path.display()))
806 fn tell(&self) -> IoResult<u64> {
807 let err = self.fd.tell().map_err(IoError::from_rtio_error);
808 err.update_err("couldn't retrieve file cursor (`tell`)",
809 |e| format!("{}; path={}", e, self.path.display()))
812 fn seek(&mut self, pos: i64, style: SeekStyle) -> IoResult<()> {
813 let style = match style {
814 SeekSet => rtio::SeekSet,
815 SeekCur => rtio::SeekCur,
816 SeekEnd => rtio::SeekEnd,
818 let err = match self.fd.seek(pos, style) {
820 // successful seek resets EOF indicator
821 self.last_nread = -1;
824 Err(e) => Err(IoError::from_rtio_error(e)),
826 err.update_err("couldn't seek in file",
827 |e| format!("{}; path={}", e, self.path.display()))
832 /// Get information on the file, directory, etc at this path.
834 /// Consult the `fs::stat` documentation for more info.
836 /// This call preserves identical runtime/error semantics with `file::stat`.
837 pub fn stat(&self) -> IoResult<FileStat> { stat(self) }
839 /// Get information on the file, directory, etc at this path, not following
842 /// Consult the `fs::lstat` documentation for more info.
844 /// This call preserves identical runtime/error semantics with `file::lstat`.
845 pub fn lstat(&self) -> IoResult<FileStat> { lstat(self) }
847 /// Boolean value indicator whether the underlying file exists on the local
848 /// filesystem. Returns false in exactly the cases where `fs::stat` fails.
849 pub fn exists(&self) -> bool {
853 /// Whether the underlying implementation (be it a file path, or something
854 /// else) points at a "regular file" on the FS. Will return false for paths
855 /// to non-existent locations or directories or other non-regular files
856 /// (named pipes, etc). Follows links when making this determination.
857 pub fn is_file(&self) -> bool {
859 Ok(s) => s.kind == io::TypeFile,
864 /// Whether the underlying implementation (be it a file path, or something
865 /// else) is pointing at a directory in the underlying FS. Will return
866 /// false for paths to non-existent locations or if the item is not a
867 /// directory (eg files, named pipes, etc). Follows links when making this
869 pub fn is_dir(&self) -> bool {
871 Ok(s) => s.kind == io::TypeDirectory,
877 fn mode_string(mode: FileMode) -> &'static str {
879 super::Open => "open",
880 super::Append => "append",
881 super::Truncate => "truncate"
885 fn access_string(access: FileAccess) -> &'static str {
887 super::Read => "read",
888 super::Write => "write",
889 super::ReadWrite => "readwrite"
894 #[allow(unused_imports)]
897 use io::{SeekSet, SeekCur, SeekEnd, Read, Open, ReadWrite};
900 use io::fs::{File, rmdir, mkdir, readdir, rmdir_recursive,
901 mkdir_recursive, copy, unlink, stat, symlink, link,
902 readlink, chmod, lstat, change_file_times};
908 macro_rules! check( ($e:expr) => (
911 Err(e) => fail!("{} failed with: {}", stringify!($e), e),
915 macro_rules! error( ($e:expr, $s:expr) => (
917 Ok(val) => fail!("Should have been an error, was {:?}", val),
918 Err(ref err) => assert!(err.to_string().as_slice().contains($s.as_slice()),
919 format!("`{}` did not contain `{}`", err, $s))
923 struct TempDir(Path);
926 fn join(&self, path: &str) -> Path {
927 let TempDir(ref p) = *self;
931 fn path<'a>(&'a self) -> &'a Path {
932 let TempDir(ref p) = *self;
937 impl Drop for TempDir {
939 // Gee, seeing how we're testing the fs module I sure hope that we
940 // at least implement this correctly!
941 let TempDir(ref p) = *self;
942 check!(io::fs::rmdir_recursive(p));
946 pub fn tmpdir() -> TempDir {
949 let ret = os::tmpdir().join(format!("rust-{}", rand::random::<u32>()));
950 check!(io::fs::mkdir(&ret, io::UserRWX));
954 iotest!(fn file_test_io_smoke_test() {
955 let message = "it's alright. have a good time";
956 let tmpdir = tmpdir();
957 let filename = &tmpdir.join("file_rt_io_file_test.txt");
959 let mut write_stream = File::open_mode(filename, Open, ReadWrite);
960 check!(write_stream.write(message.as_bytes()));
963 let mut read_stream = File::open_mode(filename, Open, Read);
964 let mut read_buf = [0, .. 1028];
965 let read_str = match check!(read_stream.read(read_buf)) {
966 -1|0 => fail!("shouldn't happen"),
967 n => str::from_utf8(read_buf.slice_to(n)).unwrap().to_owned()
969 assert_eq!(read_str, message.to_owned());
971 check!(unlink(filename));
974 iotest!(fn invalid_path_raises() {
975 let tmpdir = tmpdir();
976 let filename = &tmpdir.join("file_that_does_not_exist.txt");
977 let result = File::open_mode(filename, Open, Read);
979 error!(result, "couldn't open file");
981 error!(result, "no such file or directory");
983 error!(result, format!("path={}; mode=open; access=read", filename.display()));
986 iotest!(fn file_test_iounlinking_invalid_path_should_raise_condition() {
987 let tmpdir = tmpdir();
988 let filename = &tmpdir.join("file_another_file_that_does_not_exist.txt");
990 let result = unlink(filename);
992 error!(result, "couldn't unlink path");
994 error!(result, "no such file or directory");
996 error!(result, format!("path={}", filename.display()));
999 iotest!(fn file_test_io_non_positional_read() {
1000 let message: &str = "ten-four";
1001 let mut read_mem = [0, .. 8];
1002 let tmpdir = tmpdir();
1003 let filename = &tmpdir.join("file_rt_io_file_test_positional.txt");
1005 let mut rw_stream = File::open_mode(filename, Open, ReadWrite);
1006 check!(rw_stream.write(message.as_bytes()));
1009 let mut read_stream = File::open_mode(filename, Open, Read);
1011 let read_buf = read_mem.mut_slice(0, 4);
1012 check!(read_stream.read(read_buf));
1015 let read_buf = read_mem.mut_slice(4, 8);
1016 check!(read_stream.read(read_buf));
1019 check!(unlink(filename));
1020 let read_str = str::from_utf8(read_mem).unwrap();
1021 assert_eq!(read_str, message);
1024 iotest!(fn file_test_io_seek_and_tell_smoke_test() {
1025 let message = "ten-four";
1026 let mut read_mem = [0, .. 4];
1027 let set_cursor = 4 as u64;
1028 let mut tell_pos_pre_read;
1029 let mut tell_pos_post_read;
1030 let tmpdir = tmpdir();
1031 let filename = &tmpdir.join("file_rt_io_file_test_seeking.txt");
1033 let mut rw_stream = File::open_mode(filename, Open, ReadWrite);
1034 check!(rw_stream.write(message.as_bytes()));
1037 let mut read_stream = File::open_mode(filename, Open, Read);
1038 check!(read_stream.seek(set_cursor as i64, SeekSet));
1039 tell_pos_pre_read = check!(read_stream.tell());
1040 check!(read_stream.read(read_mem));
1041 tell_pos_post_read = check!(read_stream.tell());
1043 check!(unlink(filename));
1044 let read_str = str::from_utf8(read_mem).unwrap();
1045 assert_eq!(read_str, message.slice(4, 8));
1046 assert_eq!(tell_pos_pre_read, set_cursor);
1047 assert_eq!(tell_pos_post_read, message.len() as u64);
1050 iotest!(fn file_test_io_seek_and_write() {
1051 let initial_msg = "food-is-yummy";
1052 let overwrite_msg = "-the-bar!!";
1053 let final_msg = "foo-the-bar!!";
1055 let mut read_mem = [0, .. 13];
1056 let tmpdir = tmpdir();
1057 let filename = &tmpdir.join("file_rt_io_file_test_seek_and_write.txt");
1059 let mut rw_stream = File::open_mode(filename, Open, ReadWrite);
1060 check!(rw_stream.write(initial_msg.as_bytes()));
1061 check!(rw_stream.seek(seek_idx as i64, SeekSet));
1062 check!(rw_stream.write(overwrite_msg.as_bytes()));
1065 let mut read_stream = File::open_mode(filename, Open, Read);
1066 check!(read_stream.read(read_mem));
1068 check!(unlink(filename));
1069 let read_str = str::from_utf8(read_mem).unwrap();
1070 assert!(read_str.as_slice() == final_msg.as_slice());
1073 iotest!(fn file_test_io_seek_shakedown() {
1074 use str; // 01234567890123
1075 let initial_msg = "qwer-asdf-zxcv";
1076 let chunk_one: &str = "qwer";
1077 let chunk_two: &str = "asdf";
1078 let chunk_three: &str = "zxcv";
1079 let mut read_mem = [0, .. 4];
1080 let tmpdir = tmpdir();
1081 let filename = &tmpdir.join("file_rt_io_file_test_seek_shakedown.txt");
1083 let mut rw_stream = File::open_mode(filename, Open, ReadWrite);
1084 check!(rw_stream.write(initial_msg.as_bytes()));
1087 let mut read_stream = File::open_mode(filename, Open, Read);
1089 check!(read_stream.seek(-4, SeekEnd));
1090 check!(read_stream.read(read_mem));
1091 assert_eq!(str::from_utf8(read_mem).unwrap(), chunk_three);
1093 check!(read_stream.seek(-9, SeekCur));
1094 check!(read_stream.read(read_mem));
1095 assert_eq!(str::from_utf8(read_mem).unwrap(), chunk_two);
1097 check!(read_stream.seek(0, SeekSet));
1098 check!(read_stream.read(read_mem));
1099 assert_eq!(str::from_utf8(read_mem).unwrap(), chunk_one);
1101 check!(unlink(filename));
1104 iotest!(fn file_test_stat_is_correct_on_is_file() {
1105 let tmpdir = tmpdir();
1106 let filename = &tmpdir.join("file_stat_correct_on_is_file.txt");
1108 let mut fs = check!(File::open_mode(filename, Open, ReadWrite));
1110 fs.write(msg.as_bytes()).unwrap();
1112 let fstat_res = check!(fs.stat());
1113 assert_eq!(fstat_res.kind, io::TypeFile);
1115 let stat_res_fn = check!(stat(filename));
1116 assert_eq!(stat_res_fn.kind, io::TypeFile);
1117 let stat_res_meth = check!(filename.stat());
1118 assert_eq!(stat_res_meth.kind, io::TypeFile);
1119 check!(unlink(filename));
1122 iotest!(fn file_test_stat_is_correct_on_is_dir() {
1123 let tmpdir = tmpdir();
1124 let filename = &tmpdir.join("file_stat_correct_on_is_dir");
1125 check!(mkdir(filename, io::UserRWX));
1126 let stat_res_fn = check!(stat(filename));
1127 assert!(stat_res_fn.kind == io::TypeDirectory);
1128 let stat_res_meth = check!(filename.stat());
1129 assert!(stat_res_meth.kind == io::TypeDirectory);
1130 check!(rmdir(filename));
1133 iotest!(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!(mkdir(dir, io::UserRWX));
1137 assert!(dir.is_file() == false);
1141 iotest!(fn file_test_fileinfo_check_exists_before_and_after_file_creation() {
1142 let tmpdir = tmpdir();
1143 let file = &tmpdir.join("fileinfo_check_exists_b_and_a.txt");
1144 check!(File::create(file).write(b"foo"));
1145 assert!(file.exists());
1146 check!(unlink(file));
1147 assert!(!file.exists());
1150 iotest!(fn file_test_directoryinfo_check_exists_before_and_after_mkdir() {
1151 let tmpdir = tmpdir();
1152 let dir = &tmpdir.join("before_and_after_dir");
1153 assert!(!dir.exists());
1154 check!(mkdir(dir, io::UserRWX));
1155 assert!(dir.exists());
1156 assert!(dir.is_dir());
1158 assert!(!dir.exists());
1161 iotest!(fn file_test_directoryinfo_readdir() {
1163 let tmpdir = tmpdir();
1164 let dir = &tmpdir.join("di_readdir");
1165 check!(mkdir(dir, io::UserRWX));
1167 for n in range(0i,3) {
1168 let f = dir.join(format!("{}.txt", n));
1169 let mut w = check!(File::create(&f));
1170 let msg_str = format!("{}{}", prefix, n.to_string());
1171 let msg = msg_str.as_slice().as_bytes();
1172 check!(w.write(msg));
1174 let files = check!(readdir(dir));
1175 let mut mem = [0u8, .. 4];
1176 for f in files.iter() {
1178 let n = f.filestem_str();
1179 check!(File::open(f).read(mem));
1180 let read_str = str::from_utf8(mem).unwrap();
1181 let expected = match n {
1182 None|Some("") => fail!("really shouldn't happen.."),
1183 Some(n) => format!("{}{}", prefix, n),
1185 assert_eq!(expected.as_slice(), read_str);
1192 iotest!(fn file_test_walk_dir() {
1193 let tmpdir = tmpdir();
1194 let dir = &tmpdir.join("walk_dir");
1195 check!(mkdir(dir, io::UserRWX));
1197 let dir1 = &dir.join("01/02/03");
1198 check!(mkdir_recursive(dir1, io::UserRWX));
1199 check!(File::create(&dir1.join("04")));
1201 let dir2 = &dir.join("11/12/13");
1202 check!(mkdir_recursive(dir2, io::UserRWX));
1203 check!(File::create(&dir2.join("14")));
1205 let mut files = check!(walk_dir(dir));
1206 let mut cur = [0u8, .. 2];
1208 let stem = f.filestem_str().unwrap();
1209 let root = stem.as_bytes()[0] - ('0' as u8);
1210 let name = stem.as_bytes()[1] - ('0' as u8);
1211 assert!(cur[root as uint] < name);
1212 cur[root as uint] = name;
1215 check!(rmdir_recursive(dir));
1218 iotest!(fn recursive_mkdir() {
1219 let tmpdir = tmpdir();
1220 let dir = tmpdir.join("d1/d2");
1221 check!(mkdir_recursive(&dir, io::UserRWX));
1222 assert!(dir.is_dir())
1225 iotest!(fn recursive_mkdir_failure() {
1226 let tmpdir = tmpdir();
1227 let dir = tmpdir.join("d1");
1228 let file = dir.join("f1");
1230 check!(mkdir_recursive(&dir, io::UserRWX));
1231 check!(File::create(&file));
1233 let result = mkdir_recursive(&file, io::UserRWX);
1235 error!(result, "couldn't recursively mkdir");
1236 error!(result, "couldn't create directory");
1237 error!(result, "mode=FilePermission { bits: 448 }");
1238 error!(result, format!("path={}", file.display()));
1241 iotest!(fn recursive_mkdir_slash() {
1242 check!(mkdir_recursive(&Path::new("/"), io::UserRWX));
1245 // FIXME(#12795) depends on lstat to work on windows
1246 #[cfg(not(windows))]
1247 iotest!(fn recursive_rmdir() {
1248 let tmpdir = tmpdir();
1249 let d1 = tmpdir.join("d1");
1250 let dt = d1.join("t");
1251 let dtt = dt.join("t");
1252 let d2 = tmpdir.join("d2");
1253 let canary = d2.join("do_not_delete");
1254 check!(mkdir_recursive(&dtt, io::UserRWX));
1255 check!(mkdir_recursive(&d2, io::UserRWX));
1256 check!(File::create(&canary).write(b"foo"));
1257 check!(symlink(&d2, &dt.join("d2")));
1258 check!(rmdir_recursive(&d1));
1260 assert!(!d1.is_dir());
1261 assert!(canary.exists());
1264 iotest!(fn unicode_path_is_dir() {
1265 assert!(Path::new(".").is_dir());
1266 assert!(!Path::new("test/stdtest/fs.rs").is_dir());
1268 let tmpdir = tmpdir();
1270 let mut dirpath = tmpdir.path().clone();
1271 dirpath.push(format!("test-가一ー你好"));
1272 check!(mkdir(&dirpath, io::UserRWX));
1273 assert!(dirpath.is_dir());
1275 let mut filepath = dirpath;
1276 filepath.push("unicode-file-\uac00\u4e00\u30fc\u4f60\u597d.rs");
1277 check!(File::create(&filepath)); // ignore return; touch only
1278 assert!(!filepath.is_dir());
1279 assert!(filepath.exists());
1282 iotest!(fn unicode_path_exists() {
1283 assert!(Path::new(".").exists());
1284 assert!(!Path::new("test/nonexistent-bogus-path").exists());
1286 let tmpdir = tmpdir();
1287 let unicode = tmpdir.path();
1288 let unicode = unicode.join(format!("test-각丁ー再见"));
1289 check!(mkdir(&unicode, io::UserRWX));
1290 assert!(unicode.exists());
1291 assert!(!Path::new("test/unicode-bogus-path-각丁ー再见").exists());
1294 iotest!(fn copy_file_does_not_exist() {
1295 let from = Path::new("test/nonexistent-bogus-path");
1296 let to = Path::new("test/other-bogus-path");
1298 error!(copy(&from, &to),
1299 format!("couldn't copy path (the source path is not an \
1300 existing file; from={}; to={})",
1301 from.display(), to.display()));
1303 match copy(&from, &to) {
1306 assert!(!from.exists());
1307 assert!(!to.exists());
1312 iotest!(fn copy_file_ok() {
1313 let tmpdir = tmpdir();
1314 let input = tmpdir.join("in.txt");
1315 let out = tmpdir.join("out.txt");
1317 check!(File::create(&input).write(b"hello"));
1318 check!(copy(&input, &out));
1319 let contents = check!(File::open(&out).read_to_end());
1320 assert_eq!(contents.as_slice(), b"hello");
1322 assert_eq!(check!(input.stat()).perm, check!(out.stat()).perm);
1325 iotest!(fn copy_file_dst_dir() {
1326 let tmpdir = tmpdir();
1327 let out = tmpdir.join("out");
1329 check!(File::create(&out));
1330 match copy(&out, tmpdir.path()) {
1331 Ok(..) => fail!(), Err(..) => {}
1335 iotest!(fn copy_file_dst_exists() {
1336 let tmpdir = tmpdir();
1337 let input = tmpdir.join("in");
1338 let output = tmpdir.join("out");
1340 check!(File::create(&input).write("foo".as_bytes()));
1341 check!(File::create(&output).write("bar".as_bytes()));
1342 check!(copy(&input, &output));
1344 assert_eq!(check!(File::open(&output).read_to_end()),
1345 (Vec::from_slice(b"foo")));
1348 iotest!(fn copy_file_src_dir() {
1349 let tmpdir = tmpdir();
1350 let out = tmpdir.join("out");
1352 match copy(tmpdir.path(), &out) {
1353 Ok(..) => fail!(), Err(..) => {}
1355 assert!(!out.exists());
1358 iotest!(fn copy_file_preserves_perm_bits() {
1359 let tmpdir = tmpdir();
1360 let input = tmpdir.join("in.txt");
1361 let out = tmpdir.join("out.txt");
1363 check!(File::create(&input));
1364 check!(chmod(&input, io::UserRead));
1365 check!(copy(&input, &out));
1366 assert!(!check!(out.stat()).perm.intersects(io::UserWrite));
1368 check!(chmod(&input, io::UserFile));
1369 check!(chmod(&out, io::UserFile));
1372 #[cfg(not(windows))] // FIXME(#10264) operation not permitted?
1373 iotest!(fn symlinks_work() {
1374 let tmpdir = tmpdir();
1375 let input = tmpdir.join("in.txt");
1376 let out = tmpdir.join("out.txt");
1378 check!(File::create(&input).write("foobar".as_bytes()));
1379 check!(symlink(&input, &out));
1380 if cfg!(not(windows)) {
1381 assert_eq!(check!(lstat(&out)).kind, io::TypeSymlink);
1382 assert_eq!(check!(out.lstat()).kind, io::TypeSymlink);
1384 assert_eq!(check!(stat(&out)).size, check!(stat(&input)).size);
1385 assert_eq!(check!(File::open(&out).read_to_end()),
1386 (Vec::from_slice(b"foobar")));
1389 #[cfg(not(windows))] // apparently windows doesn't like symlinks
1390 iotest!(fn symlink_noexist() {
1391 let tmpdir = tmpdir();
1392 // symlinks can point to things that don't exist
1393 check!(symlink(&tmpdir.join("foo"), &tmpdir.join("bar")));
1394 assert!(check!(readlink(&tmpdir.join("bar"))) == tmpdir.join("foo"));
1397 iotest!(fn readlink_not_symlink() {
1398 let tmpdir = tmpdir();
1399 match readlink(tmpdir.path()) {
1400 Ok(..) => fail!("wanted a failure"),
1405 iotest!(fn links_work() {
1406 let tmpdir = tmpdir();
1407 let input = tmpdir.join("in.txt");
1408 let out = tmpdir.join("out.txt");
1410 check!(File::create(&input).write("foobar".as_bytes()));
1411 check!(link(&input, &out));
1412 if cfg!(not(windows)) {
1413 assert_eq!(check!(lstat(&out)).kind, io::TypeFile);
1414 assert_eq!(check!(out.lstat()).kind, io::TypeFile);
1415 assert_eq!(check!(stat(&out)).unstable.nlink, 2);
1416 assert_eq!(check!(out.stat()).unstable.nlink, 2);
1418 assert_eq!(check!(stat(&out)).size, check!(stat(&input)).size);
1419 assert_eq!(check!(stat(&out)).size, check!(input.stat()).size);
1420 assert_eq!(check!(File::open(&out).read_to_end()),
1421 (Vec::from_slice(b"foobar")));
1423 // can't link to yourself
1424 match link(&input, &input) {
1425 Ok(..) => fail!("wanted a failure"),
1428 // can't link to something that doesn't exist
1429 match link(&tmpdir.join("foo"), &tmpdir.join("bar")) {
1430 Ok(..) => fail!("wanted a failure"),
1435 iotest!(fn chmod_works() {
1436 let tmpdir = tmpdir();
1437 let file = tmpdir.join("in.txt");
1439 check!(File::create(&file));
1440 assert!(check!(stat(&file)).perm.contains(io::UserWrite));
1441 check!(chmod(&file, io::UserRead));
1442 assert!(!check!(stat(&file)).perm.contains(io::UserWrite));
1444 match chmod(&tmpdir.join("foo"), io::UserRWX) {
1445 Ok(..) => fail!("wanted a failure"),
1449 check!(chmod(&file, io::UserFile));
1452 iotest!(fn sync_doesnt_kill_anything() {
1453 let tmpdir = tmpdir();
1454 let path = tmpdir.join("in.txt");
1456 let mut file = check!(File::open_mode(&path, io::Open, io::ReadWrite));
1457 check!(file.fsync());
1458 check!(file.datasync());
1459 check!(file.write(b"foo"));
1460 check!(file.fsync());
1461 check!(file.datasync());
1465 iotest!(fn truncate_works() {
1466 let tmpdir = tmpdir();
1467 let path = tmpdir.join("in.txt");
1469 let mut file = check!(File::open_mode(&path, io::Open, io::ReadWrite));
1470 check!(file.write(b"foo"));
1471 check!(file.fsync());
1473 // Do some simple things with truncation
1474 assert_eq!(check!(file.stat()).size, 3);
1475 check!(file.truncate(10));
1476 assert_eq!(check!(file.stat()).size, 10);
1477 check!(file.write(b"bar"));
1478 check!(file.fsync());
1479 assert_eq!(check!(file.stat()).size, 10);
1480 assert_eq!(check!(File::open(&path).read_to_end()),
1481 (Vec::from_slice(b"foobar\0\0\0\0")));
1483 // Truncate to a smaller length, don't seek, and then write something.
1484 // Ensure that the intermediate zeroes are all filled in (we're seeked
1485 // past the end of the file).
1486 check!(file.truncate(2));
1487 assert_eq!(check!(file.stat()).size, 2);
1488 check!(file.write(b"wut"));
1489 check!(file.fsync());
1490 assert_eq!(check!(file.stat()).size, 9);
1491 assert_eq!(check!(File::open(&path).read_to_end()),
1492 (Vec::from_slice(b"fo\0\0\0\0wut")));
1496 iotest!(fn open_flavors() {
1497 let tmpdir = tmpdir();
1499 match File::open_mode(&tmpdir.join("a"), io::Open, io::Read) {
1500 Ok(..) => fail!(), Err(..) => {}
1503 // Perform each one twice to make sure that it succeeds the second time
1504 // (where the file exists)
1505 check!(File::open_mode(&tmpdir.join("b"), io::Open, io::Write));
1506 assert!(tmpdir.join("b").exists());
1507 check!(File::open_mode(&tmpdir.join("b"), io::Open, io::Write));
1509 check!(File::open_mode(&tmpdir.join("c"), io::Open, io::ReadWrite));
1510 assert!(tmpdir.join("c").exists());
1511 check!(File::open_mode(&tmpdir.join("c"), io::Open, io::ReadWrite));
1513 check!(File::open_mode(&tmpdir.join("d"), io::Append, io::Write));
1514 assert!(tmpdir.join("d").exists());
1515 check!(File::open_mode(&tmpdir.join("d"), io::Append, io::Write));
1517 check!(File::open_mode(&tmpdir.join("e"), io::Append, io::ReadWrite));
1518 assert!(tmpdir.join("e").exists());
1519 check!(File::open_mode(&tmpdir.join("e"), io::Append, io::ReadWrite));
1521 check!(File::open_mode(&tmpdir.join("f"), io::Truncate, io::Write));
1522 assert!(tmpdir.join("f").exists());
1523 check!(File::open_mode(&tmpdir.join("f"), io::Truncate, io::Write));
1525 check!(File::open_mode(&tmpdir.join("g"), io::Truncate, io::ReadWrite));
1526 assert!(tmpdir.join("g").exists());
1527 check!(File::open_mode(&tmpdir.join("g"), io::Truncate, io::ReadWrite));
1529 check!(File::create(&tmpdir.join("h")).write("foo".as_bytes()));
1530 check!(File::open_mode(&tmpdir.join("h"), io::Open, io::Read));
1532 let mut f = check!(File::open_mode(&tmpdir.join("h"), io::Open,
1534 match f.write("wut".as_bytes()) {
1535 Ok(..) => fail!(), Err(..) => {}
1538 assert!(check!(stat(&tmpdir.join("h"))).size == 3,
1539 "write/stat failed");
1541 let mut f = check!(File::open_mode(&tmpdir.join("h"), io::Append,
1543 check!(f.write("bar".as_bytes()));
1545 assert!(check!(stat(&tmpdir.join("h"))).size == 6,
1546 "append didn't append");
1548 let mut f = check!(File::open_mode(&tmpdir.join("h"), io::Truncate,
1550 check!(f.write("bar".as_bytes()));
1552 assert!(check!(stat(&tmpdir.join("h"))).size == 3,
1553 "truncate didn't truncate");
1558 let tmpdir = tmpdir();
1559 let path = tmpdir.join("a");
1560 check!(File::create(&path));
1562 check!(change_file_times(&path, 1000, 2000));
1563 assert_eq!(check!(path.stat()).accessed, 1000);
1564 assert_eq!(check!(path.stat()).modified, 2000);
1568 fn utime_noexist() {
1569 let tmpdir = tmpdir();
1571 match change_file_times(&tmpdir.join("a"), 100, 200) {
1577 iotest!(fn binary_file() {
1578 use rand::{StdRng, Rng};
1580 let mut bytes = [0, ..1024];
1581 StdRng::new().ok().unwrap().fill_bytes(bytes);
1583 let tmpdir = tmpdir();
1585 check!(File::create(&tmpdir.join("test")).write(bytes));
1586 let actual = check!(File::open(&tmpdir.join("test")).read_to_end());
1587 assert!(actual.as_slice() == bytes);