2014-04-25 12:18:06 -05:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-03-13 22:02:48 -05:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
2014-07-21 17:57:14 -05:00
|
|
|
//
|
|
|
|
// ignore-lexer-test FIXME #15679
|
2013-03-13 22:02:48 -05:00
|
|
|
|
2013-09-17 01:10:03 -05:00
|
|
|
/*! Synchronous File I/O
|
|
|
|
|
|
|
|
This module provides a set of functions and traits for working
|
|
|
|
with regular files & directories on a filesystem.
|
|
|
|
|
2013-10-25 19:04:37 -05:00
|
|
|
At the top-level of the module are a set of freestanding functions, associated
|
2014-01-30 18:55:20 -06:00
|
|
|
with various filesystem operations. They all operate on `Path` objects.
|
2013-09-17 01:10:03 -05:00
|
|
|
|
2013-10-30 01:31:07 -05:00
|
|
|
All operations in this module, including those as part of `File` et al
|
2014-01-30 18:55:20 -06:00
|
|
|
block the task during execution. In the event of failure, all functions/methods
|
|
|
|
will return an `IoResult` type with an `Err` value.
|
2013-09-17 01:10:03 -05:00
|
|
|
|
2013-10-25 19:04:37 -05:00
|
|
|
Also included in this module is an implementation block on the `Path` object
|
|
|
|
defined in `std::path::Path`. The impl adds useful methods about inspecting the
|
|
|
|
metadata of a file. This includes getting the `stat` information, reading off
|
|
|
|
particular bits of it, etc.
|
|
|
|
|
2013-10-31 17:15:30 -05:00
|
|
|
# Example
|
|
|
|
|
2014-01-29 18:33:57 -06:00
|
|
|
```rust
|
2014-04-14 10:30:31 -05:00
|
|
|
# #![allow(unused_must_use)]
|
2014-09-11 00:26:41 -05:00
|
|
|
use std::io::fs::PathExtensions;
|
2014-01-29 18:33:57 -06:00
|
|
|
use std::io::{File, fs};
|
2013-10-31 17:15:30 -05:00
|
|
|
|
2014-01-29 18:33:57 -06:00
|
|
|
let path = Path::new("foo.txt");
|
2013-10-31 17:15:30 -05:00
|
|
|
|
2014-01-29 18:33:57 -06:00
|
|
|
// create the file, whether it exists or not
|
|
|
|
let mut file = File::create(&path);
|
2014-06-18 13:25:36 -05:00
|
|
|
file.write(b"foobar");
|
2014-01-29 18:33:57 -06:00
|
|
|
# drop(file);
|
2013-10-31 17:15:30 -05:00
|
|
|
|
2014-01-29 18:33:57 -06:00
|
|
|
// open the file in read-only mode
|
|
|
|
let mut file = File::open(&path);
|
|
|
|
file.read_to_end();
|
2013-10-31 17:15:30 -05:00
|
|
|
|
2014-01-30 18:55:20 -06:00
|
|
|
println!("{}", path.stat().unwrap().size);
|
2014-01-29 18:33:57 -06:00
|
|
|
# drop(file);
|
|
|
|
fs::unlink(&path);
|
|
|
|
```
|
2013-10-31 17:15:30 -05:00
|
|
|
|
2013-09-17 01:10:03 -05:00
|
|
|
*/
|
|
|
|
|
2013-10-16 18:48:30 -05:00
|
|
|
use c_str::ToCStr;
|
2013-11-11 00:46:32 -06:00
|
|
|
use clone::Clone;
|
2014-07-11 12:12:38 -05:00
|
|
|
use collections::{Collection, MutableSeq};
|
2014-06-11 12:24:04 -05:00
|
|
|
use io::standard_error;
|
2014-06-06 12:27:49 -05:00
|
|
|
use io::{FilePermission, Write, UnstableFileStat, Open, FileAccess, FileMode};
|
|
|
|
use io::{IoResult, IoError, FileStat, SeekStyle, Seek, Writer, Reader};
|
|
|
|
use io::{Read, Truncate, SeekCur, SeekSet, ReadWrite, SeekEnd, Append};
|
2014-06-11 12:24:04 -05:00
|
|
|
use io::UpdateIoError;
|
2014-06-03 14:33:18 -05:00
|
|
|
use io;
|
2014-09-17 14:56:31 -05:00
|
|
|
use iter::{Iterator, Extendable};
|
2014-03-08 20:21:49 -06:00
|
|
|
use kinds::Send;
|
2014-06-11 12:24:04 -05:00
|
|
|
use libc;
|
2013-10-25 19:04:37 -05:00
|
|
|
use option::{Some, None, Option};
|
2014-07-10 16:19:17 -05:00
|
|
|
use boxed::Box;
|
2013-10-25 19:04:37 -05:00
|
|
|
use path::{Path, GenericPath};
|
2014-06-03 22:09:39 -05:00
|
|
|
use path;
|
2014-06-06 12:27:49 -05:00
|
|
|
use result::{Err, Ok};
|
|
|
|
use rt::rtio::LocalIo;
|
2014-06-03 22:09:39 -05:00
|
|
|
use rt::rtio;
|
2014-08-06 20:58:43 -05:00
|
|
|
use slice::ImmutableSlice;
|
2014-06-11 12:24:04 -05:00
|
|
|
use string::String;
|
2014-03-20 02:35:51 -05:00
|
|
|
use vec::Vec;
|
2013-09-17 01:10:03 -05:00
|
|
|
|
2013-10-30 01:31:07 -05:00
|
|
|
/// Unconstrained file access type that exposes read and write operations
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-10-31 17:15:30 -05:00
|
|
|
/// Can be constructed via `File::open()`, `File::create()`, and
|
|
|
|
/// `File::open_mode()`.
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2014-01-30 18:55:20 -06:00
|
|
|
/// # Error
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2014-01-30 18:55:20 -06:00
|
|
|
/// This type will return errors as an `IoResult<T>` if operations are
|
|
|
|
/// attempted against it for which its underlying file descriptor was not
|
|
|
|
/// configured at creation time, via the `FileAccess` parameter to
|
|
|
|
/// `File::open_mode()`.
|
2013-10-30 01:31:07 -05:00
|
|
|
pub struct File {
|
2014-06-14 13:03:34 -05:00
|
|
|
fd: Box<rtio::RtioFileStream + Send>,
|
2014-03-27 17:09:47 -05:00
|
|
|
path: Path,
|
|
|
|
last_nread: int,
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl File {
|
|
|
|
/// Open a file at `path` in the mode specified by the `mode` and `access`
|
|
|
|
/// arguments
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2014-01-30 18:55:20 -06:00
|
|
|
/// ```rust,should_fail
|
2014-01-29 18:33:57 -06:00
|
|
|
/// use std::io::{File, Open, ReadWrite};
|
2013-10-30 01:31:07 -05:00
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// let p = Path::new("/some/file/path.txt");
|
2013-10-30 01:31:07 -05:00
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// let file = match File::open_mode(&p, Open, ReadWrite) {
|
|
|
|
/// Ok(f) => f,
|
|
|
|
/// Err(e) => fail!("file error: {}", e),
|
|
|
|
/// };
|
|
|
|
/// // do some stuff with that file
|
2013-10-30 01:31:07 -05:00
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// // the file will be closed at the end of this block
|
|
|
|
/// ```
|
2013-10-30 01:31:07 -05:00
|
|
|
///
|
|
|
|
/// `FileMode` and `FileAccess` provide information about the permissions
|
|
|
|
/// context in which a given stream is created. More information about them
|
2013-11-11 00:46:32 -06:00
|
|
|
/// can be found in `std::io`'s docs. If a file is opened with `Write`
|
2014-07-29 12:47:58 -05:00
|
|
|
/// or `ReadWrite` access, then it will be created if it does not already
|
2013-10-30 01:31:07 -05:00
|
|
|
/// exist.
|
|
|
|
///
|
|
|
|
/// Note that, with this function, a `File` is returned regardless of the
|
|
|
|
/// access-limitations indicated by `FileAccess` (e.g. calling `write` on a
|
2014-01-30 18:55:20 -06:00
|
|
|
/// `File` opened as `Read` will return an error at runtime).
|
2013-10-30 01:31:07 -05:00
|
|
|
///
|
2014-01-30 18:55:20 -06:00
|
|
|
/// # Error
|
2013-10-30 01:31:07 -05:00
|
|
|
///
|
2014-01-30 18:55:20 -06:00
|
|
|
/// This function will return an error under a number of different
|
|
|
|
/// circumstances, to include but not limited to:
|
2013-10-30 01:31:07 -05:00
|
|
|
///
|
|
|
|
/// * Opening a file that does not exist with `Read` access.
|
|
|
|
/// * Attempting to open a file with a `FileAccess` that the user lacks
|
|
|
|
/// permissions for
|
|
|
|
/// * Filesystem-level errors (full disk, etc)
|
|
|
|
pub fn open_mode(path: &Path,
|
|
|
|
mode: FileMode,
|
2014-01-29 18:33:57 -06:00
|
|
|
access: FileAccess) -> IoResult<File> {
|
2014-06-11 12:24:04 -05:00
|
|
|
let rtio_mode = match mode {
|
2014-06-03 22:09:39 -05:00
|
|
|
Open => rtio::Open,
|
|
|
|
Append => rtio::Append,
|
|
|
|
Truncate => rtio::Truncate,
|
|
|
|
};
|
2014-06-11 12:24:04 -05:00
|
|
|
let rtio_access = match access {
|
2014-06-03 22:09:39 -05:00
|
|
|
Read => rtio::Read,
|
|
|
|
Write => rtio::Write,
|
|
|
|
ReadWrite => rtio::ReadWrite,
|
|
|
|
};
|
2014-06-03 14:33:18 -05:00
|
|
|
let err = LocalIo::maybe_raise(|io| {
|
2014-06-11 12:24:04 -05:00
|
|
|
io.fs_open(&path.to_c_str(), rtio_mode, rtio_access).map(|fd| {
|
2013-12-12 19:30:41 -06:00
|
|
|
File {
|
|
|
|
path: path.clone(),
|
|
|
|
fd: fd,
|
|
|
|
last_nread: -1
|
|
|
|
}
|
|
|
|
})
|
2014-06-03 14:33:18 -05:00
|
|
|
}).map_err(IoError::from_rtio_error);
|
|
|
|
err.update_err("couldn't open file", |e| {
|
|
|
|
format!("{}; path={}; mode={}; access={}", e, path.display(),
|
|
|
|
mode_string(mode), access_string(access))
|
|
|
|
})
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
2013-10-25 19:04:37 -05:00
|
|
|
|
2013-10-30 01:31:07 -05:00
|
|
|
/// Attempts to open a file in read-only mode. This function is equivalent to
|
|
|
|
/// `File::open_mode(path, Open, Read)`, and will raise all of the same
|
|
|
|
/// errors that `File::open_mode` does.
|
|
|
|
///
|
|
|
|
/// For more information, see the `File::open_mode` function.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// ```rust
|
|
|
|
/// use std::io::File;
|
2013-10-30 01:31:07 -05:00
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// let contents = File::open(&Path::new("foo.txt")).read_to_end();
|
|
|
|
/// ```
|
|
|
|
pub fn open(path: &Path) -> IoResult<File> {
|
2013-10-30 01:31:07 -05:00
|
|
|
File::open_mode(path, Open, Read)
|
|
|
|
}
|
2013-10-25 19:04:37 -05:00
|
|
|
|
2013-10-30 01:31:07 -05:00
|
|
|
/// Attempts to create a file in write-only mode. This function is
|
|
|
|
/// equivalent to `File::open_mode(path, Truncate, Write)`, and will
|
|
|
|
/// raise all of the same errors that `File::open_mode` does.
|
|
|
|
///
|
|
|
|
/// For more information, see the `File::open_mode` function.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// ```rust
|
2014-04-14 10:30:31 -05:00
|
|
|
/// # #![allow(unused_must_use)]
|
2014-01-29 18:33:57 -06:00
|
|
|
/// use std::io::File;
|
2013-10-30 01:31:07 -05:00
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// let mut f = File::create(&Path::new("foo.txt"));
|
2014-06-18 13:25:36 -05:00
|
|
|
/// f.write(b"This is a sample file");
|
2014-01-29 18:33:57 -06:00
|
|
|
/// # drop(f);
|
2014-01-30 18:55:20 -06:00
|
|
|
/// # ::std::io::fs::unlink(&Path::new("foo.txt"));
|
2014-01-29 18:33:57 -06:00
|
|
|
/// ```
|
|
|
|
pub fn create(path: &Path) -> IoResult<File> {
|
2013-10-30 01:31:07 -05:00
|
|
|
File::open_mode(path, Truncate, Write)
|
2014-06-03 14:33:18 -05:00
|
|
|
.update_desc("couldn't create file")
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the original path which was used to open this file.
|
|
|
|
pub fn path<'a>(&'a self) -> &'a Path {
|
|
|
|
&self.path
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Synchronizes all modifications to this file to its permanent storage
|
|
|
|
/// device. This will flush any internal buffers necessary to perform this
|
|
|
|
/// operation.
|
2014-01-29 18:33:57 -06:00
|
|
|
pub fn fsync(&mut self) -> IoResult<()> {
|
2014-06-03 14:33:18 -05:00
|
|
|
let err = self.fd.fsync().map_err(IoError::from_rtio_error);
|
|
|
|
err.update_err("couldn't fsync file",
|
|
|
|
|e| format!("{}; path={}", e, self.path.display()))
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This function is similar to `fsync`, except that it may not synchronize
|
|
|
|
/// file metadata to the filesystem. This is intended for use case which
|
|
|
|
/// must synchronize content, but don't need the metadata on disk. The goal
|
|
|
|
/// of this method is to reduce disk operations.
|
2014-01-29 18:33:57 -06:00
|
|
|
pub fn datasync(&mut self) -> IoResult<()> {
|
2014-06-03 14:33:18 -05:00
|
|
|
let err = self.fd.datasync().map_err(IoError::from_rtio_error);
|
|
|
|
err.update_err("couldn't datasync file",
|
|
|
|
|e| format!("{}; path={}", e, self.path.display()))
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
|
|
|
|
2013-11-13 16:48:45 -06:00
|
|
|
/// Either truncates or extends the underlying file, updating the size of
|
|
|
|
/// this file to become `size`. This is equivalent to unix's `truncate`
|
2013-10-30 01:31:07 -05:00
|
|
|
/// function.
|
|
|
|
///
|
2013-11-13 16:48:45 -06:00
|
|
|
/// If the `size` is less than the current file's size, then the file will
|
|
|
|
/// be shrunk. If it is greater than the current file's size, then the file
|
|
|
|
/// will be extended to `size` and have all of the intermediate data filled
|
|
|
|
/// in with 0s.
|
2014-01-29 18:33:57 -06:00
|
|
|
pub fn truncate(&mut self, size: i64) -> IoResult<()> {
|
2014-06-03 14:33:18 -05:00
|
|
|
let err = self.fd.truncate(size).map_err(IoError::from_rtio_error);
|
|
|
|
err.update_err("couldn't truncate file", |e| {
|
|
|
|
format!("{}; path={}; size={}", e, self.path.display(), size)
|
|
|
|
})
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
2014-01-07 11:47:53 -06:00
|
|
|
|
2014-08-18 12:17:27 -05:00
|
|
|
/// Returns true if the stream has reached the end of the file.
|
2014-01-07 11:47:53 -06:00
|
|
|
///
|
|
|
|
/// If true, then this file will no longer continue to return data via
|
|
|
|
/// `read`.
|
2014-08-18 12:17:27 -05:00
|
|
|
///
|
|
|
|
/// Note that the operating system will not return an `EOF` indicator
|
|
|
|
/// until you have attempted to read past the end of the file, so if
|
|
|
|
/// you've read _exactly_ the number of bytes in the file, this will
|
|
|
|
/// return `false`, not `true`.
|
2014-01-07 11:47:53 -06:00
|
|
|
pub fn eof(&self) -> bool {
|
|
|
|
self.last_nread == 0
|
|
|
|
}
|
2014-05-12 00:31:22 -05:00
|
|
|
|
|
|
|
/// Queries information about the underlying file.
|
|
|
|
pub fn stat(&mut self) -> IoResult<FileStat> {
|
2014-06-03 14:33:18 -05:00
|
|
|
let err = match self.fd.fstat() {
|
2014-06-03 22:09:39 -05:00
|
|
|
Ok(s) => Ok(from_rtio(s)),
|
|
|
|
Err(e) => Err(IoError::from_rtio_error(e)),
|
2014-06-03 14:33:18 -05:00
|
|
|
};
|
|
|
|
err.update_err("couldn't fstat file",
|
|
|
|
|e| format!("{}; path={}", e, self.path.display()))
|
2014-05-12 00:31:22 -05:00
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
2013-03-13 22:02:48 -05:00
|
|
|
|
2013-10-31 17:15:30 -05:00
|
|
|
/// Unlink a file from the underlying filesystem.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// ```rust
|
2014-04-14 10:30:31 -05:00
|
|
|
/// # #![allow(unused_must_use)]
|
2014-01-29 18:33:57 -06:00
|
|
|
/// use std::io::fs;
|
2013-10-31 17:15:30 -05:00
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// let p = Path::new("/some/file/path.txt");
|
|
|
|
/// fs::unlink(&p);
|
|
|
|
/// ```
|
2013-10-31 17:15:30 -05:00
|
|
|
///
|
|
|
|
/// Note that, just because an unlink call was successful, it is not
|
|
|
|
/// guaranteed that a file is immediately deleted (e.g. depending on
|
|
|
|
/// platform, other open file descriptors may prevent immediate removal)
|
|
|
|
///
|
2014-01-30 18:55:20 -06:00
|
|
|
/// # Error
|
2013-10-31 17:15:30 -05:00
|
|
|
///
|
2014-07-28 13:14:56 -05:00
|
|
|
/// This function will return an error if `path` points to a directory, if the
|
2014-01-30 18:55:20 -06:00
|
|
|
/// user lacks permissions to remove the file, or if some other filesystem-level
|
|
|
|
/// error occurs.
|
2014-01-29 18:33:57 -06:00
|
|
|
pub fn unlink(path: &Path) -> IoResult<()> {
|
2014-06-27 16:39:01 -05:00
|
|
|
return match do_unlink(path) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(e) => {
|
|
|
|
// On unix, a readonly file can be successfully removed. On windows,
|
|
|
|
// however, it cannot. To keep the two platforms in line with
|
|
|
|
// respect to their behavior, catch this case on windows, attempt to
|
|
|
|
// change it to read-write, and then remove the file.
|
|
|
|
if cfg!(windows) && e.kind == io::PermissionDenied {
|
|
|
|
let stat = match stat(path) {
|
|
|
|
Ok(stat) => stat,
|
|
|
|
Err(..) => return Err(e),
|
|
|
|
};
|
|
|
|
if stat.perm.intersects(io::UserWrite) { return Err(e) }
|
|
|
|
|
|
|
|
match chmod(path, stat.perm | io::UserWrite) {
|
|
|
|
Ok(()) => do_unlink(path),
|
|
|
|
Err(..) => {
|
|
|
|
// Try to put it back as we found it
|
|
|
|
let _ = chmod(path, stat.perm);
|
|
|
|
Err(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fn do_unlink(path: &Path) -> IoResult<()> {
|
|
|
|
let err = LocalIo::maybe_raise(|io| {
|
|
|
|
io.fs_unlink(&path.to_c_str())
|
|
|
|
}).map_err(IoError::from_rtio_error);
|
|
|
|
err.update_err("couldn't unlink path",
|
|
|
|
|e| format!("{}; path={}", e, path.display()))
|
|
|
|
}
|
2013-10-31 17:15:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Given a path, query the file system to get information about a file,
|
|
|
|
/// directory, etc. This function will traverse symlinks to query
|
|
|
|
/// information about the destination file.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// ```rust
|
|
|
|
/// use std::io::fs;
|
2013-10-31 17:15:30 -05:00
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// let p = Path::new("/some/file/path.txt");
|
|
|
|
/// match fs::stat(&p) {
|
|
|
|
/// Ok(stat) => { /* ... */ }
|
|
|
|
/// Err(e) => { /* handle error */ }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2013-10-31 17:15:30 -05:00
|
|
|
///
|
2014-01-30 18:55:20 -06:00
|
|
|
/// # Error
|
2013-10-31 17:15:30 -05:00
|
|
|
///
|
2014-07-28 13:14:56 -05:00
|
|
|
/// This function will return an error if the user lacks the requisite permissions
|
|
|
|
/// to perform a `stat` call on the given `path` or if there is no entry in the
|
2014-01-30 18:55:20 -06:00
|
|
|
/// filesystem at the provided path.
|
2014-01-29 18:33:57 -06:00
|
|
|
pub fn stat(path: &Path) -> IoResult<FileStat> {
|
2014-06-03 14:33:18 -05:00
|
|
|
let err = match LocalIo::maybe_raise(|io| io.fs_stat(&path.to_c_str())) {
|
2014-06-03 22:09:39 -05:00
|
|
|
Ok(s) => Ok(from_rtio(s)),
|
|
|
|
Err(e) => Err(IoError::from_rtio_error(e)),
|
2014-06-03 14:33:18 -05:00
|
|
|
};
|
|
|
|
err.update_err("couldn't stat path",
|
|
|
|
|e| format!("{}; path={}", e, path.display()))
|
2013-10-31 17:15:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Perform the same operation as the `stat` function, except that this
|
|
|
|
/// function does not traverse through symlinks. This will return
|
|
|
|
/// information about the symlink file instead of the file that it points
|
|
|
|
/// to.
|
|
|
|
///
|
2014-01-30 18:55:20 -06:00
|
|
|
/// # Error
|
2013-10-31 17:15:30 -05:00
|
|
|
///
|
|
|
|
/// See `stat`
|
2014-01-29 18:33:57 -06:00
|
|
|
pub fn lstat(path: &Path) -> IoResult<FileStat> {
|
2014-06-03 14:33:18 -05:00
|
|
|
let err = match LocalIo::maybe_raise(|io| io.fs_lstat(&path.to_c_str())) {
|
2014-06-03 22:09:39 -05:00
|
|
|
Ok(s) => Ok(from_rtio(s)),
|
|
|
|
Err(e) => Err(IoError::from_rtio_error(e)),
|
2014-06-03 14:33:18 -05:00
|
|
|
};
|
|
|
|
err.update_err("couldn't lstat path",
|
|
|
|
|e| format!("{}; path={}", e, path.display()))
|
2014-06-03 22:09:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn from_rtio(s: rtio::FileStat) -> FileStat {
|
2014-07-10 12:33:15 -05:00
|
|
|
#[cfg(windows)]
|
|
|
|
type Mode = libc::c_int;
|
|
|
|
#[cfg(unix)]
|
|
|
|
type Mode = libc::mode_t;
|
|
|
|
|
2014-06-03 22:09:39 -05:00
|
|
|
let rtio::FileStat {
|
|
|
|
size, kind, perm, created, modified,
|
|
|
|
accessed, device, inode, rdev,
|
|
|
|
nlink, uid, gid, blksize, blocks, flags, gen
|
|
|
|
} = s;
|
|
|
|
|
|
|
|
FileStat {
|
|
|
|
size: size,
|
2014-07-10 12:33:15 -05:00
|
|
|
kind: match (kind as Mode) & libc::S_IFMT {
|
2014-06-03 22:09:39 -05:00
|
|
|
libc::S_IFREG => io::TypeFile,
|
|
|
|
libc::S_IFDIR => io::TypeDirectory,
|
|
|
|
libc::S_IFIFO => io::TypeNamedPipe,
|
|
|
|
libc::S_IFBLK => io::TypeBlockSpecial,
|
|
|
|
libc::S_IFLNK => io::TypeSymlink,
|
|
|
|
_ => io::TypeUnknown,
|
|
|
|
},
|
|
|
|
perm: FilePermission::from_bits_truncate(perm as u32),
|
|
|
|
created: created,
|
|
|
|
modified: modified,
|
|
|
|
accessed: accessed,
|
|
|
|
unstable: UnstableFileStat {
|
|
|
|
device: device,
|
|
|
|
inode: inode,
|
|
|
|
rdev: rdev,
|
|
|
|
nlink: nlink,
|
|
|
|
uid: uid,
|
|
|
|
gid: gid,
|
|
|
|
blksize: blksize,
|
|
|
|
blocks: blocks,
|
|
|
|
flags: flags,
|
|
|
|
gen: gen,
|
|
|
|
},
|
|
|
|
}
|
2013-10-31 17:15:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Rename a file or directory to a new name.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// ```rust
|
2014-04-14 10:30:31 -05:00
|
|
|
/// # #![allow(unused_must_use)]
|
2014-01-29 18:33:57 -06:00
|
|
|
/// use std::io::fs;
|
2013-10-31 17:15:30 -05:00
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// fs::rename(&Path::new("foo"), &Path::new("bar"));
|
|
|
|
/// ```
|
2013-10-31 17:15:30 -05:00
|
|
|
///
|
2014-01-30 18:55:20 -06:00
|
|
|
/// # Error
|
2013-10-31 17:15:30 -05:00
|
|
|
///
|
2014-07-28 13:14:56 -05:00
|
|
|
/// This function will return an error if the provided `from` doesn't exist, if
|
|
|
|
/// the process lacks permissions to view the contents, or if some other
|
|
|
|
/// intermittent I/O error occurs.
|
2014-01-29 18:33:57 -06:00
|
|
|
pub fn rename(from: &Path, to: &Path) -> IoResult<()> {
|
2014-06-03 14:33:18 -05:00
|
|
|
let err = LocalIo::maybe_raise(|io| {
|
2014-06-03 22:09:39 -05:00
|
|
|
io.fs_rename(&from.to_c_str(), &to.to_c_str())
|
2014-06-03 14:33:18 -05:00
|
|
|
}).map_err(IoError::from_rtio_error);
|
|
|
|
err.update_err("couldn't rename path", |e| {
|
|
|
|
format!("{}; from={}; to={}", e, from.display(), to.display())
|
|
|
|
})
|
2013-10-31 17:15:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Copies the contents of one file to another. This function will also
|
|
|
|
/// copy the permission bits of the original file to the destination file.
|
|
|
|
///
|
|
|
|
/// Note that if `from` and `to` both point to the same file, then the file
|
|
|
|
/// will likely get truncated by this operation.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// ```rust
|
2014-04-14 10:30:31 -05:00
|
|
|
/// # #![allow(unused_must_use)]
|
2014-01-29 18:33:57 -06:00
|
|
|
/// use std::io::fs;
|
2013-10-31 17:15:30 -05:00
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// fs::copy(&Path::new("foo.txt"), &Path::new("bar.txt"));
|
|
|
|
/// ```
|
2013-10-31 17:15:30 -05:00
|
|
|
///
|
2014-01-30 18:55:20 -06:00
|
|
|
/// # Error
|
2013-10-31 17:15:30 -05:00
|
|
|
///
|
2014-07-28 13:14:56 -05:00
|
|
|
/// This function will return an error in the following situations, but is not
|
|
|
|
/// limited to just these cases:
|
2013-10-31 17:15:30 -05:00
|
|
|
///
|
|
|
|
/// * The `from` path is not a file
|
|
|
|
/// * The `from` file does not exist
|
|
|
|
/// * The current process does not have the permission rights to access
|
|
|
|
/// `from` or write `to`
|
|
|
|
///
|
|
|
|
/// Note that this copy is not atomic in that once the destination is
|
|
|
|
/// ensured to not exist, there is nothing preventing the destination from
|
|
|
|
/// being created and then destroyed by this operation.
|
2014-01-29 18:33:57 -06:00
|
|
|
pub fn copy(from: &Path, to: &Path) -> IoResult<()> {
|
2014-06-03 14:33:18 -05:00
|
|
|
fn update_err<T>(result: IoResult<T>, from: &Path, to: &Path) -> IoResult<T> {
|
|
|
|
result.update_err("couldn't copy path",
|
|
|
|
|e| format!("{}; from={}; to={}", e, from.display(), to.display()))
|
|
|
|
}
|
|
|
|
|
2013-10-31 17:15:30 -05:00
|
|
|
if !from.is_file() {
|
2014-06-03 14:33:18 -05:00
|
|
|
return update_err(Err(IoError {
|
2013-10-31 17:15:30 -05:00
|
|
|
kind: io::MismatchedFileTypeForOperation,
|
|
|
|
desc: "the source path is not an existing file",
|
2014-06-03 14:33:18 -05:00
|
|
|
detail: None
|
|
|
|
}), from, to)
|
2013-10-31 17:15:30 -05:00
|
|
|
}
|
|
|
|
|
2014-02-19 12:07:49 -06:00
|
|
|
let mut reader = try!(File::open(from));
|
|
|
|
let mut writer = try!(File::create(to));
|
2013-10-31 17:15:30 -05:00
|
|
|
let mut buf = [0, ..io::DEFAULT_BUF_SIZE];
|
|
|
|
|
|
|
|
loop {
|
2014-01-29 18:33:57 -06:00
|
|
|
let amt = match reader.read(buf) {
|
|
|
|
Ok(n) => n,
|
|
|
|
Err(ref e) if e.kind == io::EndOfFile => { break }
|
2014-06-03 14:33:18 -05:00
|
|
|
Err(e) => return update_err(Err(e), from, to)
|
2014-01-29 18:33:57 -06:00
|
|
|
};
|
2014-02-19 12:07:49 -06:00
|
|
|
try!(writer.write(buf.slice_to(amt)));
|
2013-10-31 17:15:30 -05:00
|
|
|
}
|
|
|
|
|
2014-06-03 14:33:18 -05:00
|
|
|
chmod(to, try!(update_err(from.stat(), from, to)).perm)
|
2013-10-31 17:15:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Changes the permission mode bits found on a file or a directory. This
|
|
|
|
/// function takes a mask from the `io` module
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// ```rust
|
2014-04-14 10:30:31 -05:00
|
|
|
/// # #![allow(unused_must_use)]
|
2014-01-29 18:33:57 -06:00
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::fs;
|
2013-10-31 17:15:30 -05:00
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// fs::chmod(&Path::new("file.txt"), io::UserFile);
|
|
|
|
/// fs::chmod(&Path::new("file.txt"), io::UserRead | io::UserWrite);
|
|
|
|
/// fs::chmod(&Path::new("dir"), io::UserDir);
|
|
|
|
/// fs::chmod(&Path::new("file.exe"), io::UserExec);
|
|
|
|
/// ```
|
2013-10-31 17:15:30 -05:00
|
|
|
///
|
2014-01-30 18:55:20 -06:00
|
|
|
/// # Error
|
2013-10-31 17:15:30 -05:00
|
|
|
///
|
2014-07-28 13:14:56 -05:00
|
|
|
/// This function will return an error if the provided `path` doesn't exist, if
|
|
|
|
/// the process lacks permissions to change the attributes of the file, or if
|
|
|
|
/// some other I/O error is encountered.
|
2014-01-29 18:33:57 -06:00
|
|
|
pub fn chmod(path: &Path, mode: io::FilePermission) -> IoResult<()> {
|
2014-06-03 14:33:18 -05:00
|
|
|
let err = LocalIo::maybe_raise(|io| {
|
2014-06-03 22:09:39 -05:00
|
|
|
io.fs_chmod(&path.to_c_str(), mode.bits() as uint)
|
2014-06-03 14:33:18 -05:00
|
|
|
}).map_err(IoError::from_rtio_error);
|
|
|
|
err.update_err("couldn't chmod path", |e| {
|
|
|
|
format!("{}; path={}; mode={}", e, path.display(), mode)
|
|
|
|
})
|
2013-10-31 17:15:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Change the user and group owners of a file at the specified path.
|
2014-01-29 18:33:57 -06:00
|
|
|
pub fn chown(path: &Path, uid: int, gid: int) -> IoResult<()> {
|
2014-06-03 14:33:18 -05:00
|
|
|
let err = LocalIo::maybe_raise(|io| {
|
2014-06-03 22:09:39 -05:00
|
|
|
io.fs_chown(&path.to_c_str(), uid, gid)
|
2014-06-03 14:33:18 -05:00
|
|
|
}).map_err(IoError::from_rtio_error);
|
|
|
|
err.update_err("couldn't chown path", |e| {
|
|
|
|
format!("{}; path={}; uid={}; gid={}", e, path.display(), uid, gid)
|
|
|
|
})
|
2013-10-31 17:15:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new hard link on the filesystem. The `dst` path will be a
|
|
|
|
/// link pointing to the `src` path. Note that systems often require these
|
|
|
|
/// two paths to both be located on the same filesystem.
|
2014-01-29 18:33:57 -06:00
|
|
|
pub fn link(src: &Path, dst: &Path) -> IoResult<()> {
|
2014-06-03 14:33:18 -05:00
|
|
|
let err = LocalIo::maybe_raise(|io| {
|
2014-06-03 22:09:39 -05:00
|
|
|
io.fs_link(&src.to_c_str(), &dst.to_c_str())
|
2014-06-03 14:33:18 -05:00
|
|
|
}).map_err(IoError::from_rtio_error);
|
|
|
|
err.update_err("couldn't link path", |e| {
|
|
|
|
format!("{}; src={}; dest={}", e, src.display(), dst.display())
|
|
|
|
})
|
2013-10-31 17:15:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new symbolic link on the filesystem. The `dst` path will be a
|
|
|
|
/// symlink pointing to the `src` path.
|
2014-01-29 18:33:57 -06:00
|
|
|
pub fn symlink(src: &Path, dst: &Path) -> IoResult<()> {
|
2014-06-03 14:33:18 -05:00
|
|
|
let err = LocalIo::maybe_raise(|io| {
|
2014-06-03 22:09:39 -05:00
|
|
|
io.fs_symlink(&src.to_c_str(), &dst.to_c_str())
|
2014-06-03 14:33:18 -05:00
|
|
|
}).map_err(IoError::from_rtio_error);
|
|
|
|
err.update_err("couldn't symlink path", |e| {
|
|
|
|
format!("{}; src={}; dest={}", e, src.display(), dst.display())
|
|
|
|
})
|
2013-10-31 17:15:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Reads a symlink, returning the file that the symlink points to.
|
|
|
|
///
|
2014-01-30 18:55:20 -06:00
|
|
|
/// # Error
|
2013-10-31 17:15:30 -05:00
|
|
|
///
|
2014-01-30 18:55:20 -06:00
|
|
|
/// This function will return an error on failure. Failure conditions include
|
|
|
|
/// reading a file that does not exist or reading a file which is not a symlink.
|
2014-01-29 18:33:57 -06:00
|
|
|
pub fn readlink(path: &Path) -> IoResult<Path> {
|
2014-06-03 14:33:18 -05:00
|
|
|
let err = LocalIo::maybe_raise(|io| {
|
2014-06-03 00:11:19 -05:00
|
|
|
Ok(Path::new(try!(io.fs_readlink(&path.to_c_str()))))
|
2014-06-03 14:33:18 -05:00
|
|
|
}).map_err(IoError::from_rtio_error);
|
|
|
|
err.update_err("couldn't resolve symlink for path",
|
|
|
|
|e| format!("{}; path={}", e, path.display()))
|
2013-10-31 17:15:30 -05:00
|
|
|
}
|
|
|
|
|
2013-09-17 01:36:39 -05:00
|
|
|
/// Create a new, empty directory at the provided path
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// # Example
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// ```rust
|
2014-04-14 10:30:31 -05:00
|
|
|
/// # #![allow(unused_must_use)]
|
2014-01-29 18:33:57 -06:00
|
|
|
/// use std::io;
|
|
|
|
/// use std::io::fs;
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// let p = Path::new("/some/dir");
|
|
|
|
/// fs::mkdir(&p, io::UserRWX);
|
|
|
|
/// ```
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2014-01-30 18:55:20 -06:00
|
|
|
/// # Error
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2014-07-28 13:14:56 -05:00
|
|
|
/// This function will return an error if the user lacks permissions to make a
|
|
|
|
/// new directory at the provided `path`, or if the directory already exists.
|
2014-01-29 18:33:57 -06:00
|
|
|
pub fn mkdir(path: &Path, mode: FilePermission) -> IoResult<()> {
|
2014-06-03 14:33:18 -05:00
|
|
|
let err = LocalIo::maybe_raise(|io| {
|
2014-06-03 22:09:39 -05:00
|
|
|
io.fs_mkdir(&path.to_c_str(), mode.bits() as uint)
|
2014-06-03 14:33:18 -05:00
|
|
|
}).map_err(IoError::from_rtio_error);
|
|
|
|
err.update_err("couldn't create directory", |e| {
|
|
|
|
format!("{}; path={}; mode={}", e, path.display(), mode)
|
|
|
|
})
|
2013-09-14 11:33:53 -05:00
|
|
|
}
|
2013-09-17 01:10:03 -05:00
|
|
|
|
2013-09-17 01:36:39 -05:00
|
|
|
/// Remove an existing, empty directory
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// # Example
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// ```rust
|
2014-04-14 10:30:31 -05:00
|
|
|
/// # #![allow(unused_must_use)]
|
2014-01-29 18:33:57 -06:00
|
|
|
/// use std::io::fs;
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// let p = Path::new("/some/dir");
|
|
|
|
/// fs::rmdir(&p);
|
|
|
|
/// ```
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2014-01-30 18:55:20 -06:00
|
|
|
/// # Error
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2014-07-28 13:14:56 -05:00
|
|
|
/// This function will return an error if the user lacks permissions to remove
|
|
|
|
/// the directory at the provided `path`, or if the directory isn't empty.
|
2014-01-29 18:33:57 -06:00
|
|
|
pub fn rmdir(path: &Path) -> IoResult<()> {
|
2014-06-03 14:33:18 -05:00
|
|
|
let err = LocalIo::maybe_raise(|io| {
|
2014-06-03 22:09:39 -05:00
|
|
|
io.fs_rmdir(&path.to_c_str())
|
2014-06-03 14:33:18 -05:00
|
|
|
}).map_err(IoError::from_rtio_error);
|
|
|
|
err.update_err("couldn't remove directory",
|
|
|
|
|e| format!("{}; path={}", e, path.display()))
|
2013-09-14 11:33:53 -05:00
|
|
|
}
|
|
|
|
|
2013-09-17 01:36:39 -05:00
|
|
|
/// Retrieve a vector containing all entries within a provided directory
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// ```rust
|
2014-09-11 00:26:41 -05:00
|
|
|
/// use std::io::fs::PathExtensions;
|
2014-01-29 18:33:57 -06:00
|
|
|
/// use std::io::fs;
|
2014-09-11 00:26:41 -05:00
|
|
|
/// use std::io;
|
2013-09-17 01:36:39 -05:00
|
|
|
///
|
2014-01-29 18:33:57 -06:00
|
|
|
/// // one possible implementation of fs::walk_dir only visiting files
|
2014-01-30 18:55:20 -06:00
|
|
|
/// fn visit_dirs(dir: &Path, cb: |&Path|) -> io::IoResult<()> {
|
2014-01-29 18:33:57 -06:00
|
|
|
/// if dir.is_dir() {
|
2014-02-19 12:07:49 -06:00
|
|
|
/// let contents = try!(fs::readdir(dir));
|
2014-01-29 18:33:57 -06:00
|
|
|
/// for entry in contents.iter() {
|
2014-01-30 18:55:20 -06:00
|
|
|
/// if entry.is_dir() {
|
2014-02-19 12:07:49 -06:00
|
|
|
/// try!(visit_dirs(entry, |p| cb(p)));
|
2014-01-30 18:55:20 -06:00
|
|
|
/// } else {
|
|
|
|
/// cb(entry);
|
|
|
|
/// }
|
2013-09-17 01:36:39 -05:00
|
|
|
/// }
|
2014-01-30 18:55:20 -06:00
|
|
|
/// Ok(())
|
|
|
|
/// } else {
|
|
|
|
/// Err(io::standard_error(io::InvalidInput))
|
2013-09-17 01:36:39 -05:00
|
|
|
/// }
|
2014-01-29 18:33:57 -06:00
|
|
|
/// }
|
|
|
|
/// ```
|
2013-09-17 01:36:39 -05:00
|
|
|
///
|
2014-01-30 18:55:20 -06:00
|
|
|
/// # Error
|
2013-09-17 01:36:39 -05:00
|
|
|
///
|
2014-07-28 13:14:56 -05:00
|
|
|
/// This function will return an error if the provided `path` doesn't exist, if
|
|
|
|
/// the process lacks permissions to view the contents or if the `path` points
|
|
|
|
/// at a non-directory file
|
2014-04-08 20:45:20 -05:00
|
|
|
pub fn readdir(path: &Path) -> IoResult<Vec<Path>> {
|
2014-06-03 14:33:18 -05:00
|
|
|
let err = LocalIo::maybe_raise(|io| {
|
2014-09-14 22:27:36 -05:00
|
|
|
Ok(try!(io.fs_readdir(&path.to_c_str(), 0)).into_iter().map(|a| {
|
2014-06-03 00:11:19 -05:00
|
|
|
Path::new(a)
|
|
|
|
}).collect())
|
2014-06-03 14:33:18 -05:00
|
|
|
}).map_err(IoError::from_rtio_error);
|
|
|
|
err.update_err("couldn't read directory",
|
|
|
|
|e| format!("{}; path={}", e, path.display()))
|
2013-09-16 15:25:10 -05:00
|
|
|
}
|
|
|
|
|
2013-10-30 01:31:07 -05:00
|
|
|
/// Returns an iterator which will recursively walk the directory structure
|
|
|
|
/// rooted at `path`. The path given will not be iterated over, and this will
|
2014-04-23 19:09:58 -05:00
|
|
|
/// perform iteration in some top-down order. The contents of unreadable
|
|
|
|
/// subdirectories are ignored.
|
2014-01-29 18:33:57 -06:00
|
|
|
pub fn walk_dir(path: &Path) -> IoResult<Directories> {
|
2014-06-03 14:33:18 -05:00
|
|
|
Ok(Directories {
|
|
|
|
stack: try!(readdir(path).update_err("couldn't walk directory",
|
|
|
|
|e| format!("{}; path={}",
|
|
|
|
e, path.display())))
|
|
|
|
})
|
2013-10-25 19:04:37 -05:00
|
|
|
}
|
2013-09-14 11:33:53 -05:00
|
|
|
|
2013-10-30 01:31:07 -05:00
|
|
|
/// An iterator which walks over a directory
|
2014-01-14 21:32:24 -06:00
|
|
|
pub struct Directories {
|
2014-04-08 20:45:20 -05:00
|
|
|
stack: Vec<Path>,
|
2013-09-14 11:33:53 -05:00
|
|
|
}
|
|
|
|
|
2014-01-14 21:32:24 -06:00
|
|
|
impl Iterator<Path> for Directories {
|
2013-10-30 01:31:07 -05:00
|
|
|
fn next(&mut self) -> Option<Path> {
|
2014-04-23 19:09:58 -05:00
|
|
|
match self.stack.pop() {
|
2013-10-30 01:31:07 -05:00
|
|
|
Some(path) => {
|
|
|
|
if path.is_dir() {
|
2014-06-03 14:33:18 -05:00
|
|
|
let result = readdir(&path)
|
|
|
|
.update_err("couldn't advance Directories iterator",
|
|
|
|
|e| format!("{}; path={}",
|
|
|
|
e, path.display()));
|
|
|
|
|
|
|
|
match result {
|
2014-09-17 14:56:31 -05:00
|
|
|
Ok(dirs) => { self.stack.extend(dirs.into_iter()); }
|
2014-01-29 18:33:57 -06:00
|
|
|
Err(..) => {}
|
|
|
|
}
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
|
|
|
Some(path)
|
2013-10-25 19:04:37 -05:00
|
|
|
}
|
2013-10-30 01:31:07 -05:00
|
|
|
None => None
|
2013-10-25 19:04:37 -05:00
|
|
|
}
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
2013-10-25 19:04:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Recursively create a directory and all of its parent components if they
|
|
|
|
/// are missing.
|
|
|
|
///
|
2014-01-30 18:55:20 -06:00
|
|
|
/// # Error
|
2013-10-25 19:04:37 -05:00
|
|
|
///
|
2014-07-28 13:14:56 -05:00
|
|
|
/// See `fs::mkdir`.
|
2014-01-29 18:33:57 -06:00
|
|
|
pub fn mkdir_recursive(path: &Path, mode: FilePermission) -> IoResult<()> {
|
2013-10-25 19:04:37 -05:00
|
|
|
// tjc: if directory exists but with different permissions,
|
|
|
|
// should we return false?
|
|
|
|
if path.is_dir() {
|
2014-01-29 18:33:57 -06:00
|
|
|
return Ok(())
|
2013-10-25 19:04:37 -05:00
|
|
|
}
|
2014-02-26 10:50:02 -06:00
|
|
|
|
|
|
|
let mut comps = path.components();
|
|
|
|
let mut curpath = path.root_path().unwrap_or(Path::new("."));
|
|
|
|
|
|
|
|
for c in comps {
|
|
|
|
curpath.push(c);
|
|
|
|
|
2014-06-03 14:33:18 -05:00
|
|
|
let result = mkdir(&curpath, mode)
|
|
|
|
.update_err("couldn't recursively mkdir",
|
|
|
|
|e| format!("{}; path={}", e, path.display()));
|
|
|
|
|
|
|
|
match result {
|
2014-02-26 10:50:02 -06:00
|
|
|
Err(mkdir_err) => {
|
|
|
|
// already exists ?
|
|
|
|
if try!(stat(&curpath)).kind != io::TypeDirectory {
|
|
|
|
return Err(mkdir_err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(()) => ()
|
|
|
|
}
|
2013-09-14 11:33:53 -05:00
|
|
|
}
|
2014-02-26 10:50:02 -06:00
|
|
|
|
|
|
|
Ok(())
|
2013-09-14 11:33:53 -05:00
|
|
|
}
|
|
|
|
|
2013-10-25 19:04:37 -05:00
|
|
|
/// Removes a directory at this path, after removing all its contents. Use
|
|
|
|
/// carefully!
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2014-01-30 18:55:20 -06:00
|
|
|
/// # Error
|
2013-10-25 19:04:37 -05:00
|
|
|
///
|
2014-07-28 13:14:56 -05:00
|
|
|
/// See `file::unlink` and `fs::readdir`
|
2014-01-29 18:33:57 -06:00
|
|
|
pub fn rmdir_recursive(path: &Path) -> IoResult<()> {
|
2014-02-26 12:16:53 -06:00
|
|
|
let mut rm_stack = Vec::new();
|
|
|
|
rm_stack.push(path.clone());
|
|
|
|
|
2014-06-03 14:33:18 -05:00
|
|
|
fn rmdir_failed(err: &IoError, path: &Path) -> String {
|
|
|
|
format!("rmdir_recursive failed; path={}; cause={}",
|
|
|
|
path.display(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_err<T>(err: IoResult<T>, path: &Path) -> IoResult<T> {
|
|
|
|
err.update_err("couldn't recursively rmdir",
|
|
|
|
|e| rmdir_failed(e, path))
|
|
|
|
}
|
|
|
|
|
2014-02-26 12:16:53 -06:00
|
|
|
while !rm_stack.is_empty() {
|
2014-06-03 14:33:18 -05:00
|
|
|
let children = try!(readdir(rm_stack.last().unwrap())
|
|
|
|
.update_detail(|e| rmdir_failed(e, path)));
|
|
|
|
|
2014-02-26 12:16:53 -06:00
|
|
|
let mut has_child_dir = false;
|
|
|
|
|
|
|
|
// delete all regular files in the way and push subdirs
|
|
|
|
// on the stack
|
2014-09-14 22:27:36 -05:00
|
|
|
for child in children.into_iter() {
|
2014-02-26 12:16:53 -06:00
|
|
|
// FIXME(#12795) we should use lstat in all cases
|
|
|
|
let child_type = match cfg!(windows) {
|
2014-06-03 14:33:18 -05:00
|
|
|
true => try!(update_err(stat(&child), path)),
|
|
|
|
false => try!(update_err(lstat(&child), path))
|
2014-02-26 12:16:53 -06:00
|
|
|
};
|
|
|
|
|
2014-06-03 14:33:18 -05:00
|
|
|
if child_type.kind == io::TypeDirectory {
|
2014-02-26 12:16:53 -06:00
|
|
|
rm_stack.push(child);
|
|
|
|
has_child_dir = true;
|
|
|
|
} else {
|
|
|
|
// we can carry on safely if the file is already gone
|
|
|
|
// (eg: deleted by someone else since readdir)
|
2014-06-03 14:33:18 -05:00
|
|
|
match update_err(unlink(&child), path) {
|
2014-02-26 12:16:53 -06:00
|
|
|
Ok(()) => (),
|
|
|
|
Err(ref e) if e.kind == io::FileNotFound => (),
|
|
|
|
Err(e) => return Err(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if no subdir was found, let's pop and delete
|
|
|
|
if !has_child_dir {
|
2014-06-03 14:33:18 -05:00
|
|
|
let result = update_err(rmdir(&rm_stack.pop().unwrap()), path);
|
|
|
|
match result {
|
2014-02-26 12:16:53 -06:00
|
|
|
Ok(()) => (),
|
|
|
|
Err(ref e) if e.kind == io::FileNotFound => (),
|
|
|
|
Err(e) => return Err(e)
|
|
|
|
}
|
2013-10-25 19:04:37 -05:00
|
|
|
}
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
2014-02-26 12:16:53 -06:00
|
|
|
|
|
|
|
Ok(())
|
2013-10-25 19:04:37 -05:00
|
|
|
}
|
|
|
|
|
2013-11-05 17:48:27 -06:00
|
|
|
/// Changes the timestamps for a file's last modification and access time.
|
|
|
|
/// The file at the path specified will have its last access time set to
|
2013-11-06 01:29:11 -06:00
|
|
|
/// `atime` and its modification time set to `mtime`. The times specified should
|
|
|
|
/// be in milliseconds.
|
2013-11-05 17:48:27 -06:00
|
|
|
// FIXME(#10301) these arguments should not be u64
|
2014-01-29 18:33:57 -06:00
|
|
|
pub fn change_file_times(path: &Path, atime: u64, mtime: u64) -> IoResult<()> {
|
2014-06-03 14:33:18 -05:00
|
|
|
let err = LocalIo::maybe_raise(|io| {
|
2014-06-03 22:09:39 -05:00
|
|
|
io.fs_utime(&path.to_c_str(), atime, mtime)
|
2014-06-03 14:33:18 -05:00
|
|
|
}).map_err(IoError::from_rtio_error);
|
|
|
|
err.update_err("couldn't change_file_times",
|
|
|
|
|e| format!("{}; path={}", e, path.display()))
|
2013-11-05 17:48:27 -06:00
|
|
|
}
|
|
|
|
|
2013-10-30 01:31:07 -05:00
|
|
|
impl Reader for File {
|
2014-01-29 18:33:57 -06:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
2014-06-03 14:33:18 -05:00
|
|
|
fn update_err<T>(result: IoResult<T>, file: &File) -> IoResult<T> {
|
|
|
|
result.update_err("couldn't read file",
|
|
|
|
|e| format!("{}; path={}",
|
|
|
|
e, file.path.display()))
|
|
|
|
}
|
|
|
|
|
2014-06-11 12:24:04 -05:00
|
|
|
let result = update_err(self.fd.read(buf)
|
|
|
|
.map_err(IoError::from_rtio_error), self);
|
2014-06-03 14:33:18 -05:00
|
|
|
|
|
|
|
match result {
|
2013-08-19 23:57:47 -05:00
|
|
|
Ok(read) => {
|
|
|
|
self.last_nread = read;
|
|
|
|
match read {
|
2014-06-03 14:33:18 -05:00
|
|
|
0 => update_err(Err(standard_error(io::EndOfFile)), self),
|
2014-01-29 18:33:57 -06:00
|
|
|
_ => Ok(read as uint)
|
2013-08-19 23:57:47 -05:00
|
|
|
}
|
|
|
|
},
|
2014-06-11 12:24:04 -05:00
|
|
|
Err(e) => Err(e)
|
2013-08-19 23:57:47 -05:00
|
|
|
}
|
2013-03-13 22:02:48 -05:00
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
2013-03-13 22:02:48 -05:00
|
|
|
|
2013-10-30 01:31:07 -05:00
|
|
|
impl Writer for File {
|
2014-06-03 22:09:39 -05:00
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
|
2014-06-11 12:24:04 -05:00
|
|
|
let err = self.fd.write(buf).map_err(IoError::from_rtio_error);
|
2014-06-03 14:33:18 -05:00
|
|
|
err.update_err("couldn't write to file",
|
|
|
|
|e| format!("{}; path={}", e, self.path.display()))
|
2014-06-03 22:09:39 -05:00
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-10-30 01:31:07 -05:00
|
|
|
impl Seek for File {
|
2014-01-29 18:33:57 -06:00
|
|
|
fn tell(&self) -> IoResult<u64> {
|
2014-06-03 14:33:18 -05:00
|
|
|
let err = self.fd.tell().map_err(IoError::from_rtio_error);
|
|
|
|
err.update_err("couldn't retrieve file cursor (`tell`)",
|
|
|
|
|e| format!("{}; path={}", e, self.path.display()))
|
2013-08-20 17:38:41 -05:00
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2014-01-29 18:33:57 -06:00
|
|
|
fn seek(&mut self, pos: i64, style: SeekStyle) -> IoResult<()> {
|
2014-06-03 22:09:39 -05:00
|
|
|
let style = match style {
|
|
|
|
SeekSet => rtio::SeekSet,
|
|
|
|
SeekCur => rtio::SeekCur,
|
|
|
|
SeekEnd => rtio::SeekEnd,
|
|
|
|
};
|
2014-06-03 14:33:18 -05:00
|
|
|
let err = match self.fd.seek(pos, style) {
|
2013-08-20 17:38:41 -05:00
|
|
|
Ok(_) => {
|
2013-08-22 17:03:28 -05:00
|
|
|
// successful seek resets EOF indicator
|
2013-08-20 17:38:41 -05:00
|
|
|
self.last_nread = -1;
|
2014-01-29 18:33:57 -06:00
|
|
|
Ok(())
|
2013-08-20 17:38:41 -05:00
|
|
|
}
|
2014-06-03 22:09:39 -05:00
|
|
|
Err(e) => Err(IoError::from_rtio_error(e)),
|
2014-06-03 14:33:18 -05:00
|
|
|
};
|
|
|
|
err.update_err("couldn't seek in file",
|
|
|
|
|e| format!("{}; path={}", e, self.path.display()))
|
2013-08-20 17:38:41 -05:00
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2014-09-11 00:26:41 -05:00
|
|
|
/// Utility methods for paths.
|
|
|
|
pub trait PathExtensions {
|
2013-10-25 19:04:37 -05:00
|
|
|
/// Get information on the file, directory, etc at this path.
|
2013-09-17 01:36:39 -05:00
|
|
|
///
|
2014-04-25 12:18:06 -05:00
|
|
|
/// Consult the `fs::stat` documentation for more info.
|
2013-09-17 01:36:39 -05:00
|
|
|
///
|
2013-10-25 19:04:37 -05:00
|
|
|
/// This call preserves identical runtime/error semantics with `file::stat`.
|
2014-09-11 00:26:41 -05:00
|
|
|
fn stat(&self) -> IoResult<FileStat>;
|
2013-09-14 11:33:53 -05:00
|
|
|
|
2014-04-25 12:18:06 -05:00
|
|
|
/// Get information on the file, directory, etc at this path, not following
|
|
|
|
/// symlinks.
|
2013-09-17 01:10:03 -05:00
|
|
|
///
|
2014-04-25 12:18:06 -05:00
|
|
|
/// Consult the `fs::lstat` documentation for more info.
|
2013-09-17 01:10:03 -05:00
|
|
|
///
|
2014-04-25 12:18:06 -05:00
|
|
|
/// This call preserves identical runtime/error semantics with `file::lstat`.
|
2014-09-11 00:26:41 -05:00
|
|
|
fn lstat(&self) -> IoResult<FileStat>;
|
2014-04-25 12:18:06 -05:00
|
|
|
|
|
|
|
/// Boolean value indicator whether the underlying file exists on the local
|
|
|
|
/// filesystem. Returns false in exactly the cases where `fs::stat` fails.
|
2014-09-11 00:26:41 -05:00
|
|
|
fn exists(&self) -> bool;
|
2013-09-14 11:33:53 -05:00
|
|
|
|
2013-12-14 23:26:09 -06:00
|
|
|
/// Whether the underlying implementation (be it a file path, or something
|
2013-10-25 19:04:37 -05:00
|
|
|
/// else) points at a "regular file" on the FS. Will return false for paths
|
|
|
|
/// to non-existent locations or directories or other non-regular files
|
2014-04-25 12:18:06 -05:00
|
|
|
/// (named pipes, etc). Follows links when making this determination.
|
2014-09-11 00:26:41 -05:00
|
|
|
fn is_file(&self) -> bool;
|
2013-09-17 01:10:03 -05:00
|
|
|
|
2014-04-25 12:18:06 -05:00
|
|
|
/// Whether the underlying implementation (be it a file path, or something
|
|
|
|
/// else) is pointing at a directory in the underlying FS. Will return
|
|
|
|
/// false for paths to non-existent locations or if the item is not a
|
|
|
|
/// directory (eg files, named pipes, etc). Follows links when making this
|
|
|
|
/// determination.
|
2014-09-11 00:26:41 -05:00
|
|
|
fn is_dir(&self) -> bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PathExtensions for path::Path {
|
|
|
|
fn stat(&self) -> IoResult<FileStat> { stat(self) }
|
|
|
|
fn lstat(&self) -> IoResult<FileStat> { lstat(self) }
|
|
|
|
fn exists(&self) -> bool {
|
|
|
|
self.stat().is_ok()
|
|
|
|
}
|
|
|
|
fn is_file(&self) -> bool {
|
|
|
|
match self.stat() {
|
|
|
|
Ok(s) => s.kind == io::TypeFile,
|
|
|
|
Err(..) => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn is_dir(&self) -> bool {
|
2014-01-29 18:33:57 -06:00
|
|
|
match self.stat() {
|
2013-10-30 01:31:07 -05:00
|
|
|
Ok(s) => s.kind == io::TypeDirectory,
|
2013-11-28 14:22:53 -06:00
|
|
|
Err(..) => false
|
2013-09-15 14:23:53 -05:00
|
|
|
}
|
2013-08-26 09:24:10 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-15 14:23:53 -05:00
|
|
|
|
2014-06-03 14:33:18 -05:00
|
|
|
fn mode_string(mode: FileMode) -> &'static str {
|
|
|
|
match mode {
|
|
|
|
super::Open => "open",
|
|
|
|
super::Append => "append",
|
|
|
|
super::Truncate => "truncate"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn access_string(access: FileAccess) -> &'static str {
|
|
|
|
match access {
|
|
|
|
super::Read => "read",
|
|
|
|
super::Write => "write",
|
|
|
|
super::ReadWrite => "readwrite"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-21 16:15:26 -05:00
|
|
|
#[cfg(test)]
|
2013-11-13 16:48:45 -06:00
|
|
|
#[allow(unused_imports)]
|
2013-09-21 16:15:26 -05:00
|
|
|
mod test {
|
2013-10-30 01:31:07 -05:00
|
|
|
use prelude::*;
|
2014-01-30 16:10:53 -06:00
|
|
|
use io::{SeekSet, SeekCur, SeekEnd, Read, Open, ReadWrite};
|
2013-11-11 00:46:32 -06:00
|
|
|
use io;
|
2013-10-30 01:31:07 -05:00
|
|
|
use str;
|
2013-11-13 16:48:45 -06:00
|
|
|
use io::fs::{File, rmdir, mkdir, readdir, rmdir_recursive,
|
|
|
|
mkdir_recursive, copy, unlink, stat, symlink, link,
|
|
|
|
readlink, chmod, lstat, change_file_times};
|
|
|
|
use path::Path;
|
|
|
|
use io;
|
|
|
|
use ops::Drop;
|
2014-04-15 20:17:48 -05:00
|
|
|
use str::StrSlice;
|
2013-11-13 16:48:45 -06:00
|
|
|
|
2014-02-26 14:55:23 -06:00
|
|
|
macro_rules! check( ($e:expr) => (
|
|
|
|
match $e {
|
|
|
|
Ok(t) => t,
|
|
|
|
Err(e) => fail!("{} failed with: {}", stringify!($e), e),
|
|
|
|
}
|
|
|
|
) )
|
|
|
|
|
2014-06-03 14:33:18 -05:00
|
|
|
macro_rules! error( ($e:expr, $s:expr) => (
|
|
|
|
match $e {
|
|
|
|
Ok(val) => fail!("Should have been an error, was {:?}", val),
|
2014-06-21 05:39:03 -05:00
|
|
|
Err(ref err) => assert!(err.to_string().as_slice().contains($s.as_slice()),
|
2014-06-03 14:33:18 -05:00
|
|
|
format!("`{}` did not contain `{}`", err, $s))
|
|
|
|
}
|
|
|
|
) )
|
|
|
|
|
2014-07-29 18:31:39 -05:00
|
|
|
pub struct TempDir(Path);
|
2013-11-13 16:48:45 -06:00
|
|
|
|
2013-11-01 20:06:31 -05:00
|
|
|
impl TempDir {
|
|
|
|
fn join(&self, path: &str) -> Path {
|
|
|
|
let TempDir(ref p) = *self;
|
|
|
|
p.join(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path<'a>(&'a self) -> &'a Path {
|
|
|
|
let TempDir(ref p) = *self;
|
|
|
|
p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-13 16:48:45 -06:00
|
|
|
impl Drop for TempDir {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
// Gee, seeing how we're testing the fs module I sure hope that we
|
|
|
|
// at least implement this correctly!
|
2013-11-01 20:06:31 -05:00
|
|
|
let TempDir(ref p) = *self;
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(io::fs::rmdir_recursive(p));
|
2013-11-13 16:48:45 -06:00
|
|
|
}
|
|
|
|
}
|
2013-10-25 19:04:37 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
pub fn tmpdir() -> TempDir {
|
2013-10-25 19:04:37 -05:00
|
|
|
use os;
|
|
|
|
use rand;
|
2014-05-27 22:44:58 -05:00
|
|
|
let ret = os::tmpdir().join(format!("rust-{}", rand::random::<u32>()));
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(io::fs::mkdir(&ret, io::UserRWX));
|
2013-11-13 16:48:45 -06:00
|
|
|
TempDir(ret)
|
2013-10-25 19:04:37 -05:00
|
|
|
}
|
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn file_test_io_smoke_test() {
|
2013-10-30 01:31:07 -05:00
|
|
|
let message = "it's alright. have a good time";
|
2013-11-13 16:48:45 -06:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let filename = &tmpdir.join("file_rt_io_file_test.txt");
|
2013-10-30 01:31:07 -05:00
|
|
|
{
|
|
|
|
let mut write_stream = File::open_mode(filename, Open, ReadWrite);
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(write_stream.write(message.as_bytes()));
|
2013-08-19 23:57:47 -05:00
|
|
|
}
|
2013-10-30 01:31:07 -05:00
|
|
|
{
|
|
|
|
let mut read_stream = File::open_mode(filename, Open, Read);
|
|
|
|
let mut read_buf = [0, .. 1028];
|
2014-02-26 14:55:23 -06:00
|
|
|
let read_str = match check!(read_stream.read(read_buf)) {
|
2013-10-30 01:31:07 -05:00
|
|
|
-1|0 => fail!("shouldn't happen"),
|
2014-06-30 09:41:30 -05:00
|
|
|
n => str::from_utf8(read_buf.slice_to(n)).unwrap().to_string()
|
2013-10-30 01:31:07 -05:00
|
|
|
};
|
2014-06-30 09:41:30 -05:00
|
|
|
assert_eq!(read_str.as_slice(), message);
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(unlink(filename));
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-08-19 23:57:47 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn invalid_path_raises() {
|
2013-11-13 16:48:45 -06:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let filename = &tmpdir.join("file_that_does_not_exist.txt");
|
2014-01-30 16:10:53 -06:00
|
|
|
let result = File::open_mode(filename, Open, Read);
|
2014-06-03 14:33:18 -05:00
|
|
|
|
|
|
|
error!(result, "couldn't open file");
|
2014-06-12 23:34:32 -05:00
|
|
|
if cfg!(unix) {
|
|
|
|
error!(result, "no such file or directory");
|
|
|
|
}
|
2014-06-03 14:33:18 -05:00
|
|
|
error!(result, format!("path={}; mode=open; access=read", filename.display()));
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-08-20 02:34:50 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn file_test_iounlinking_invalid_path_should_raise_condition() {
|
2013-11-13 16:48:45 -06:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let filename = &tmpdir.join("file_another_file_that_does_not_exist.txt");
|
2014-06-03 14:33:18 -05:00
|
|
|
|
|
|
|
let result = unlink(filename);
|
|
|
|
|
|
|
|
error!(result, "couldn't unlink path");
|
2014-06-12 23:34:32 -05:00
|
|
|
if cfg!(unix) {
|
|
|
|
error!(result, "no such file or directory");
|
|
|
|
}
|
2014-06-03 14:33:18 -05:00
|
|
|
error!(result, format!("path={}", filename.display()));
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-08-20 17:38:41 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn file_test_io_non_positional_read() {
|
2013-11-28 06:52:11 -06:00
|
|
|
let message: &str = "ten-four";
|
2013-10-30 01:31:07 -05:00
|
|
|
let mut read_mem = [0, .. 8];
|
2013-11-13 16:48:45 -06:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let filename = &tmpdir.join("file_rt_io_file_test_positional.txt");
|
2013-10-30 01:31:07 -05:00
|
|
|
{
|
|
|
|
let mut rw_stream = File::open_mode(filename, Open, ReadWrite);
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(rw_stream.write(message.as_bytes()));
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
|
|
|
{
|
|
|
|
let mut read_stream = File::open_mode(filename, Open, Read);
|
2013-08-20 17:38:41 -05:00
|
|
|
{
|
2014-09-14 22:27:36 -05:00
|
|
|
let read_buf = read_mem.slice_mut(0, 4);
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(read_stream.read(read_buf));
|
2013-08-20 17:38:41 -05:00
|
|
|
}
|
|
|
|
{
|
2014-09-14 22:27:36 -05:00
|
|
|
let read_buf = read_mem.slice_mut(4, 8);
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(read_stream.read(read_buf));
|
2013-08-20 17:38:41 -05:00
|
|
|
}
|
|
|
|
}
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(unlink(filename));
|
2013-12-23 10:30:49 -06:00
|
|
|
let read_str = str::from_utf8(read_mem).unwrap();
|
2013-11-28 06:52:11 -06:00
|
|
|
assert_eq!(read_str, message);
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-09-15 09:10:56 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn file_test_io_seek_and_tell_smoke_test() {
|
2013-10-30 01:31:07 -05:00
|
|
|
let message = "ten-four";
|
|
|
|
let mut read_mem = [0, .. 4];
|
|
|
|
let set_cursor = 4 as u64;
|
|
|
|
let mut tell_pos_pre_read;
|
|
|
|
let mut tell_pos_post_read;
|
2013-11-13 16:48:45 -06:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let filename = &tmpdir.join("file_rt_io_file_test_seeking.txt");
|
2013-10-30 01:31:07 -05:00
|
|
|
{
|
|
|
|
let mut rw_stream = File::open_mode(filename, Open, ReadWrite);
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(rw_stream.write(message.as_bytes()));
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
|
|
|
{
|
|
|
|
let mut read_stream = File::open_mode(filename, Open, Read);
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(read_stream.seek(set_cursor as i64, SeekSet));
|
|
|
|
tell_pos_pre_read = check!(read_stream.tell());
|
|
|
|
check!(read_stream.read(read_mem));
|
|
|
|
tell_pos_post_read = check!(read_stream.tell());
|
2013-09-21 16:15:26 -05:00
|
|
|
}
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(unlink(filename));
|
2013-12-23 10:30:49 -06:00
|
|
|
let read_str = str::from_utf8(read_mem).unwrap();
|
2013-11-28 06:52:11 -06:00
|
|
|
assert_eq!(read_str, message.slice(4, 8));
|
|
|
|
assert_eq!(tell_pos_pre_read, set_cursor);
|
|
|
|
assert_eq!(tell_pos_post_read, message.len() as u64);
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-08-21 23:22:53 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn file_test_io_seek_and_write() {
|
2013-10-30 01:31:07 -05:00
|
|
|
let initial_msg = "food-is-yummy";
|
|
|
|
let overwrite_msg = "-the-bar!!";
|
|
|
|
let final_msg = "foo-the-bar!!";
|
2014-06-27 14:30:25 -05:00
|
|
|
let seek_idx = 3i;
|
2013-10-30 01:31:07 -05:00
|
|
|
let mut read_mem = [0, .. 13];
|
2013-11-13 16:48:45 -06:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let filename = &tmpdir.join("file_rt_io_file_test_seek_and_write.txt");
|
2013-10-30 01:31:07 -05:00
|
|
|
{
|
|
|
|
let mut rw_stream = File::open_mode(filename, Open, ReadWrite);
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(rw_stream.write(initial_msg.as_bytes()));
|
|
|
|
check!(rw_stream.seek(seek_idx as i64, SeekSet));
|
|
|
|
check!(rw_stream.write(overwrite_msg.as_bytes()));
|
2013-09-21 16:15:26 -05:00
|
|
|
}
|
2013-10-30 01:31:07 -05:00
|
|
|
{
|
|
|
|
let mut read_stream = File::open_mode(filename, Open, Read);
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(read_stream.read(read_mem));
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(unlink(filename));
|
2013-12-23 10:30:49 -06:00
|
|
|
let read_str = str::from_utf8(read_mem).unwrap();
|
2014-05-20 01:19:56 -05:00
|
|
|
assert!(read_str.as_slice() == final_msg.as_slice());
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-08-21 23:22:53 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn file_test_io_seek_shakedown() {
|
2014-05-25 18:21:07 -05:00
|
|
|
use str; // 01234567890123
|
2013-10-30 01:31:07 -05:00
|
|
|
let initial_msg = "qwer-asdf-zxcv";
|
2013-11-28 06:52:11 -06:00
|
|
|
let chunk_one: &str = "qwer";
|
|
|
|
let chunk_two: &str = "asdf";
|
|
|
|
let chunk_three: &str = "zxcv";
|
2013-10-30 01:31:07 -05:00
|
|
|
let mut read_mem = [0, .. 4];
|
2013-11-13 16:48:45 -06:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let filename = &tmpdir.join("file_rt_io_file_test_seek_shakedown.txt");
|
2013-10-30 01:31:07 -05:00
|
|
|
{
|
|
|
|
let mut rw_stream = File::open_mode(filename, Open, ReadWrite);
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(rw_stream.write(initial_msg.as_bytes()));
|
2013-08-21 23:22:53 -05:00
|
|
|
}
|
2013-10-30 01:31:07 -05:00
|
|
|
{
|
|
|
|
let mut read_stream = File::open_mode(filename, Open, Read);
|
|
|
|
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(read_stream.seek(-4, SeekEnd));
|
|
|
|
check!(read_stream.read(read_mem));
|
2013-12-23 10:30:49 -06:00
|
|
|
assert_eq!(str::from_utf8(read_mem).unwrap(), chunk_three);
|
2013-10-30 01:31:07 -05:00
|
|
|
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(read_stream.seek(-9, SeekCur));
|
|
|
|
check!(read_stream.read(read_mem));
|
2013-12-23 10:30:49 -06:00
|
|
|
assert_eq!(str::from_utf8(read_mem).unwrap(), chunk_two);
|
2013-10-30 01:31:07 -05:00
|
|
|
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(read_stream.seek(0, SeekSet));
|
|
|
|
check!(read_stream.read(read_mem));
|
2013-12-23 10:30:49 -06:00
|
|
|
assert_eq!(str::from_utf8(read_mem).unwrap(), chunk_one);
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(unlink(filename));
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-09-15 09:10:56 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn file_test_stat_is_correct_on_is_file() {
|
2013-11-13 16:48:45 -06:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let filename = &tmpdir.join("file_stat_correct_on_is_file.txt");
|
2013-10-30 01:31:07 -05:00
|
|
|
{
|
2014-05-12 00:31:22 -05:00
|
|
|
let mut fs = check!(File::open_mode(filename, Open, ReadWrite));
|
2013-10-30 01:31:07 -05:00
|
|
|
let msg = "hw";
|
2014-01-30 16:10:53 -06:00
|
|
|
fs.write(msg.as_bytes()).unwrap();
|
2014-05-12 00:31:22 -05:00
|
|
|
|
|
|
|
let fstat_res = check!(fs.stat());
|
|
|
|
assert_eq!(fstat_res.kind, io::TypeFile);
|
2013-09-21 16:15:26 -05:00
|
|
|
}
|
2014-04-25 12:18:06 -05:00
|
|
|
let stat_res_fn = check!(stat(filename));
|
|
|
|
assert_eq!(stat_res_fn.kind, io::TypeFile);
|
|
|
|
let stat_res_meth = check!(filename.stat());
|
|
|
|
assert_eq!(stat_res_meth.kind, io::TypeFile);
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(unlink(filename));
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-08-26 09:24:10 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn file_test_stat_is_correct_on_is_dir() {
|
2013-11-13 16:48:45 -06:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let filename = &tmpdir.join("file_stat_correct_on_is_dir");
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(mkdir(filename, io::UserRWX));
|
2014-04-25 12:18:06 -05:00
|
|
|
let stat_res_fn = check!(stat(filename));
|
|
|
|
assert!(stat_res_fn.kind == io::TypeDirectory);
|
|
|
|
let stat_res_meth = check!(filename.stat());
|
|
|
|
assert!(stat_res_meth.kind == io::TypeDirectory);
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(rmdir(filename));
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-08-26 09:24:10 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn file_test_fileinfo_false_when_checking_is_file_on_a_directory() {
|
2013-11-13 16:48:45 -06:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let dir = &tmpdir.join("fileinfo_false_on_dir");
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(mkdir(dir, io::UserRWX));
|
2013-10-30 01:31:07 -05:00
|
|
|
assert!(dir.is_file() == false);
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(rmdir(dir));
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-08-26 09:24:10 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn file_test_fileinfo_check_exists_before_and_after_file_creation() {
|
2013-11-13 16:48:45 -06:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let file = &tmpdir.join("fileinfo_check_exists_b_and_a.txt");
|
2014-06-18 13:25:36 -05:00
|
|
|
check!(File::create(file).write(b"foo"));
|
2013-10-30 01:31:07 -05:00
|
|
|
assert!(file.exists());
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(unlink(file));
|
2013-10-30 01:31:07 -05:00
|
|
|
assert!(!file.exists());
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-09-15 14:23:53 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn file_test_directoryinfo_check_exists_before_and_after_mkdir() {
|
2013-11-13 16:48:45 -06:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let dir = &tmpdir.join("before_and_after_dir");
|
2013-10-30 01:31:07 -05:00
|
|
|
assert!(!dir.exists());
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(mkdir(dir, io::UserRWX));
|
2013-10-30 01:31:07 -05:00
|
|
|
assert!(dir.exists());
|
|
|
|
assert!(dir.is_dir());
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(rmdir(dir));
|
2013-10-30 01:31:07 -05:00
|
|
|
assert!(!dir.exists());
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-09-16 15:25:10 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn file_test_directoryinfo_readdir() {
|
2014-05-25 18:21:07 -05:00
|
|
|
use str;
|
2013-11-13 16:48:45 -06:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let dir = &tmpdir.join("di_readdir");
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(mkdir(dir, io::UserRWX));
|
2013-10-30 01:31:07 -05:00
|
|
|
let prefix = "foo";
|
2014-04-21 16:58:52 -05:00
|
|
|
for n in range(0i,3) {
|
2014-05-27 22:44:58 -05:00
|
|
|
let f = dir.join(format!("{}.txt", n));
|
2014-02-26 14:55:23 -06:00
|
|
|
let mut w = check!(File::create(&f));
|
2014-06-21 05:39:03 -05:00
|
|
|
let msg_str = format!("{}{}", prefix, n.to_string());
|
2014-05-20 01:19:56 -05:00
|
|
|
let msg = msg_str.as_slice().as_bytes();
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(w.write(msg));
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
2014-02-26 14:55:23 -06:00
|
|
|
let files = check!(readdir(dir));
|
2013-10-30 01:31:07 -05:00
|
|
|
let mut mem = [0u8, .. 4];
|
|
|
|
for f in files.iter() {
|
|
|
|
{
|
|
|
|
let n = f.filestem_str();
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(File::open(f).read(mem));
|
2013-12-23 10:30:49 -06:00
|
|
|
let read_str = str::from_utf8(mem).unwrap();
|
2013-10-30 01:31:07 -05:00
|
|
|
let expected = match n {
|
|
|
|
None|Some("") => fail!("really shouldn't happen.."),
|
2014-05-20 01:19:56 -05:00
|
|
|
Some(n) => format!("{}{}", prefix, n),
|
2013-10-30 01:31:07 -05:00
|
|
|
};
|
2013-11-28 06:52:11 -06:00
|
|
|
assert_eq!(expected.as_slice(), read_str);
|
2013-09-21 16:15:26 -05:00
|
|
|
}
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(unlink(f));
|
2013-09-16 15:25:10 -05:00
|
|
|
}
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(rmdir(dir));
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-10-25 19:04:37 -05:00
|
|
|
|
2014-04-23 19:09:58 -05:00
|
|
|
iotest!(fn file_test_walk_dir() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let dir = &tmpdir.join("walk_dir");
|
|
|
|
check!(mkdir(dir, io::UserRWX));
|
|
|
|
|
|
|
|
let dir1 = &dir.join("01/02/03");
|
|
|
|
check!(mkdir_recursive(dir1, io::UserRWX));
|
|
|
|
check!(File::create(&dir1.join("04")));
|
|
|
|
|
|
|
|
let dir2 = &dir.join("11/12/13");
|
|
|
|
check!(mkdir_recursive(dir2, io::UserRWX));
|
|
|
|
check!(File::create(&dir2.join("14")));
|
|
|
|
|
|
|
|
let mut files = check!(walk_dir(dir));
|
|
|
|
let mut cur = [0u8, .. 2];
|
|
|
|
for f in files {
|
|
|
|
let stem = f.filestem_str().unwrap();
|
2014-08-06 01:02:50 -05:00
|
|
|
let root = stem.as_bytes()[0] - b'0';
|
|
|
|
let name = stem.as_bytes()[1] - b'0';
|
2014-04-23 19:09:58 -05:00
|
|
|
assert!(cur[root as uint] < name);
|
|
|
|
cur[root as uint] = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
check!(rmdir_recursive(dir));
|
|
|
|
})
|
|
|
|
|
2014-02-28 15:54:07 -06:00
|
|
|
iotest!(fn recursive_mkdir() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let dir = tmpdir.join("d1/d2");
|
|
|
|
check!(mkdir_recursive(&dir, io::UserRWX));
|
|
|
|
assert!(dir.is_dir())
|
|
|
|
})
|
|
|
|
|
2014-06-03 14:33:18 -05:00
|
|
|
iotest!(fn recursive_mkdir_failure() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let dir = tmpdir.join("d1");
|
|
|
|
let file = dir.join("f1");
|
|
|
|
|
|
|
|
check!(mkdir_recursive(&dir, io::UserRWX));
|
|
|
|
check!(File::create(&file));
|
|
|
|
|
|
|
|
let result = mkdir_recursive(&file, io::UserRWX);
|
|
|
|
|
|
|
|
error!(result, "couldn't recursively mkdir");
|
|
|
|
error!(result, "couldn't create directory");
|
2014-09-04 02:01:51 -05:00
|
|
|
error!(result, "mode=0700");
|
2014-06-03 14:33:18 -05:00
|
|
|
error!(result, format!("path={}", file.display()));
|
|
|
|
})
|
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn recursive_mkdir_slash() {
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(mkdir_recursive(&Path::new("/"), io::UserRWX));
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-10-25 19:04:37 -05:00
|
|
|
|
2014-02-28 15:54:07 -06:00
|
|
|
// FIXME(#12795) depends on lstat to work on windows
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
iotest!(fn recursive_rmdir() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let d1 = tmpdir.join("d1");
|
|
|
|
let dt = d1.join("t");
|
|
|
|
let dtt = dt.join("t");
|
|
|
|
let d2 = tmpdir.join("d2");
|
|
|
|
let canary = d2.join("do_not_delete");
|
|
|
|
check!(mkdir_recursive(&dtt, io::UserRWX));
|
|
|
|
check!(mkdir_recursive(&d2, io::UserRWX));
|
2014-06-18 13:25:36 -05:00
|
|
|
check!(File::create(&canary).write(b"foo"));
|
2014-02-28 15:54:07 -06:00
|
|
|
check!(symlink(&d2, &dt.join("d2")));
|
|
|
|
check!(rmdir_recursive(&d1));
|
|
|
|
|
|
|
|
assert!(!d1.is_dir());
|
|
|
|
assert!(canary.exists());
|
|
|
|
})
|
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn unicode_path_is_dir() {
|
2013-12-03 21:15:12 -06:00
|
|
|
assert!(Path::new(".").is_dir());
|
|
|
|
assert!(!Path::new("test/stdtest/fs.rs").is_dir());
|
2013-10-25 19:04:37 -05:00
|
|
|
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
|
2013-11-01 20:06:31 -05:00
|
|
|
let mut dirpath = tmpdir.path().clone();
|
2014-05-27 22:44:58 -05:00
|
|
|
dirpath.push(format!("test-가一ー你好"));
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(mkdir(&dirpath, io::UserRWX));
|
2013-10-25 19:04:37 -05:00
|
|
|
assert!(dirpath.is_dir());
|
|
|
|
|
|
|
|
let mut filepath = dirpath;
|
|
|
|
filepath.push("unicode-file-\uac00\u4e00\u30fc\u4f60\u597d.rs");
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(File::create(&filepath)); // ignore return; touch only
|
2013-10-25 19:04:37 -05:00
|
|
|
assert!(!filepath.is_dir());
|
|
|
|
assert!(filepath.exists());
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-10-25 19:04:37 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn unicode_path_exists() {
|
2013-12-03 21:15:12 -06:00
|
|
|
assert!(Path::new(".").exists());
|
|
|
|
assert!(!Path::new("test/nonexistent-bogus-path").exists());
|
2013-10-25 19:04:37 -05:00
|
|
|
|
|
|
|
let tmpdir = tmpdir();
|
2013-11-01 20:06:31 -05:00
|
|
|
let unicode = tmpdir.path();
|
2014-05-27 22:44:58 -05:00
|
|
|
let unicode = unicode.join(format!("test-각丁ー再见"));
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(mkdir(&unicode, io::UserRWX));
|
2013-10-25 19:04:37 -05:00
|
|
|
assert!(unicode.exists());
|
2013-12-03 21:15:12 -06:00
|
|
|
assert!(!Path::new("test/unicode-bogus-path-각丁ー再见").exists());
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-10-25 19:04:37 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn copy_file_does_not_exist() {
|
2013-12-03 21:15:12 -06:00
|
|
|
let from = Path::new("test/nonexistent-bogus-path");
|
|
|
|
let to = Path::new("test/other-bogus-path");
|
2014-06-03 14:33:18 -05:00
|
|
|
|
|
|
|
error!(copy(&from, &to),
|
|
|
|
format!("couldn't copy path (the source path is not an \
|
|
|
|
existing file; from={}; to={})",
|
|
|
|
from.display(), to.display()));
|
|
|
|
|
2014-01-30 16:10:53 -06:00
|
|
|
match copy(&from, &to) {
|
2013-11-28 14:22:53 -06:00
|
|
|
Ok(..) => fail!(),
|
|
|
|
Err(..) => {
|
2013-10-25 19:04:37 -05:00
|
|
|
assert!(!from.exists());
|
|
|
|
assert!(!to.exists());
|
|
|
|
}
|
|
|
|
}
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-10-25 19:04:37 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn copy_file_ok() {
|
2013-10-25 19:04:37 -05:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let input = tmpdir.join("in.txt");
|
|
|
|
let out = tmpdir.join("out.txt");
|
|
|
|
|
2014-06-18 13:25:36 -05:00
|
|
|
check!(File::create(&input).write(b"hello"));
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(copy(&input, &out));
|
|
|
|
let contents = check!(File::open(&out).read_to_end());
|
2014-06-18 13:25:36 -05:00
|
|
|
assert_eq!(contents.as_slice(), b"hello");
|
2013-10-25 19:04:37 -05:00
|
|
|
|
2014-02-26 14:55:23 -06:00
|
|
|
assert_eq!(check!(input.stat()).perm, check!(out.stat()).perm);
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-10-30 01:31:07 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn copy_file_dst_dir() {
|
2013-10-30 01:31:07 -05:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let out = tmpdir.join("out");
|
|
|
|
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(File::create(&out));
|
2014-01-30 16:10:53 -06:00
|
|
|
match copy(&out, tmpdir.path()) {
|
2013-11-28 14:22:53 -06:00
|
|
|
Ok(..) => fail!(), Err(..) => {}
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-10-30 01:31:07 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn copy_file_dst_exists() {
|
2013-10-30 01:31:07 -05:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let input = tmpdir.join("in");
|
|
|
|
let output = tmpdir.join("out");
|
|
|
|
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(File::create(&input).write("foo".as_bytes()));
|
|
|
|
check!(File::create(&output).write("bar".as_bytes()));
|
|
|
|
check!(copy(&input, &output));
|
2013-10-30 01:31:07 -05:00
|
|
|
|
2014-02-26 14:55:23 -06:00
|
|
|
assert_eq!(check!(File::open(&output).read_to_end()),
|
2014-06-18 13:25:36 -05:00
|
|
|
(Vec::from_slice(b"foo")));
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-10-30 01:31:07 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn copy_file_src_dir() {
|
2013-10-30 01:31:07 -05:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let out = tmpdir.join("out");
|
|
|
|
|
2014-01-30 16:10:53 -06:00
|
|
|
match copy(tmpdir.path(), &out) {
|
2013-11-28 14:22:53 -06:00
|
|
|
Ok(..) => fail!(), Err(..) => {}
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
|
|
|
assert!(!out.exists());
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-10-30 01:31:07 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn copy_file_preserves_perm_bits() {
|
2013-10-30 01:31:07 -05:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let input = tmpdir.join("in.txt");
|
|
|
|
let out = tmpdir.join("out.txt");
|
|
|
|
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(File::create(&input));
|
|
|
|
check!(chmod(&input, io::UserRead));
|
|
|
|
check!(copy(&input, &out));
|
2014-05-02 12:56:26 -05:00
|
|
|
assert!(!check!(out.stat()).perm.intersects(io::UserWrite));
|
2013-10-30 01:31:07 -05:00
|
|
|
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(chmod(&input, io::UserFile));
|
|
|
|
check!(chmod(&out, io::UserFile));
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-10-30 01:31:07 -05:00
|
|
|
|
2013-11-13 16:48:45 -06:00
|
|
|
#[cfg(not(windows))] // FIXME(#10264) operation not permitted?
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn symlinks_work() {
|
2013-10-30 01:31:07 -05:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let input = tmpdir.join("in.txt");
|
|
|
|
let out = tmpdir.join("out.txt");
|
|
|
|
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(File::create(&input).write("foobar".as_bytes()));
|
|
|
|
check!(symlink(&input, &out));
|
2013-11-13 16:48:45 -06:00
|
|
|
if cfg!(not(windows)) {
|
2014-02-26 14:55:23 -06:00
|
|
|
assert_eq!(check!(lstat(&out)).kind, io::TypeSymlink);
|
2014-04-25 12:18:06 -05:00
|
|
|
assert_eq!(check!(out.lstat()).kind, io::TypeSymlink);
|
2013-11-13 16:48:45 -06:00
|
|
|
}
|
2014-02-26 14:55:23 -06:00
|
|
|
assert_eq!(check!(stat(&out)).size, check!(stat(&input)).size);
|
|
|
|
assert_eq!(check!(File::open(&out).read_to_end()),
|
2014-06-18 13:25:36 -05:00
|
|
|
(Vec::from_slice(b"foobar")));
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-10-30 01:31:07 -05:00
|
|
|
|
2013-11-13 16:48:45 -06:00
|
|
|
#[cfg(not(windows))] // apparently windows doesn't like symlinks
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn symlink_noexist() {
|
2013-10-31 17:15:30 -05:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
// symlinks can point to things that don't exist
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(symlink(&tmpdir.join("foo"), &tmpdir.join("bar")));
|
|
|
|
assert!(check!(readlink(&tmpdir.join("bar"))) == tmpdir.join("foo"));
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-10-30 01:31:07 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn readlink_not_symlink() {
|
2013-10-31 17:15:30 -05:00
|
|
|
let tmpdir = tmpdir();
|
2014-01-30 16:10:53 -06:00
|
|
|
match readlink(tmpdir.path()) {
|
2013-11-28 14:22:53 -06:00
|
|
|
Ok(..) => fail!("wanted a failure"),
|
|
|
|
Err(..) => {}
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-10-30 01:31:07 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn links_work() {
|
2013-10-30 01:31:07 -05:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let input = tmpdir.join("in.txt");
|
|
|
|
let out = tmpdir.join("out.txt");
|
|
|
|
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(File::create(&input).write("foobar".as_bytes()));
|
|
|
|
check!(link(&input, &out));
|
2013-11-13 16:48:45 -06:00
|
|
|
if cfg!(not(windows)) {
|
2014-02-26 14:55:23 -06:00
|
|
|
assert_eq!(check!(lstat(&out)).kind, io::TypeFile);
|
2014-04-25 12:18:06 -05:00
|
|
|
assert_eq!(check!(out.lstat()).kind, io::TypeFile);
|
2014-02-26 14:55:23 -06:00
|
|
|
assert_eq!(check!(stat(&out)).unstable.nlink, 2);
|
2014-04-25 12:18:06 -05:00
|
|
|
assert_eq!(check!(out.stat()).unstable.nlink, 2);
|
2013-11-13 16:48:45 -06:00
|
|
|
}
|
2014-02-26 14:55:23 -06:00
|
|
|
assert_eq!(check!(stat(&out)).size, check!(stat(&input)).size);
|
2014-04-25 12:18:06 -05:00
|
|
|
assert_eq!(check!(stat(&out)).size, check!(input.stat()).size);
|
2014-02-26 14:55:23 -06:00
|
|
|
assert_eq!(check!(File::open(&out).read_to_end()),
|
2014-06-18 13:25:36 -05:00
|
|
|
(Vec::from_slice(b"foobar")));
|
2013-10-30 01:31:07 -05:00
|
|
|
|
|
|
|
// can't link to yourself
|
2014-01-30 16:10:53 -06:00
|
|
|
match link(&input, &input) {
|
2013-11-28 14:22:53 -06:00
|
|
|
Ok(..) => fail!("wanted a failure"),
|
|
|
|
Err(..) => {}
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
|
|
|
// can't link to something that doesn't exist
|
2014-01-30 16:10:53 -06:00
|
|
|
match link(&tmpdir.join("foo"), &tmpdir.join("bar")) {
|
2013-11-28 14:22:53 -06:00
|
|
|
Ok(..) => fail!("wanted a failure"),
|
|
|
|
Err(..) => {}
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-10-30 01:31:07 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn chmod_works() {
|
2013-10-30 01:31:07 -05:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let file = tmpdir.join("in.txt");
|
|
|
|
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(File::create(&file));
|
2014-05-02 12:56:26 -05:00
|
|
|
assert!(check!(stat(&file)).perm.contains(io::UserWrite));
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(chmod(&file, io::UserRead));
|
2014-05-02 12:56:26 -05:00
|
|
|
assert!(!check!(stat(&file)).perm.contains(io::UserWrite));
|
2013-10-30 01:31:07 -05:00
|
|
|
|
2014-01-30 16:10:53 -06:00
|
|
|
match chmod(&tmpdir.join("foo"), io::UserRWX) {
|
2013-11-28 14:22:53 -06:00
|
|
|
Ok(..) => fail!("wanted a failure"),
|
|
|
|
Err(..) => {}
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
|
|
|
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(chmod(&file, io::UserFile));
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-10-30 01:31:07 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn sync_doesnt_kill_anything() {
|
2013-10-30 01:31:07 -05:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let path = tmpdir.join("in.txt");
|
|
|
|
|
2014-02-26 14:55:23 -06:00
|
|
|
let mut file = check!(File::open_mode(&path, io::Open, io::ReadWrite));
|
|
|
|
check!(file.fsync());
|
|
|
|
check!(file.datasync());
|
2014-06-18 13:25:36 -05:00
|
|
|
check!(file.write(b"foo"));
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(file.fsync());
|
|
|
|
check!(file.datasync());
|
2013-12-03 00:37:26 -06:00
|
|
|
drop(file);
|
2014-02-26 14:55:23 -06:00
|
|
|
})
|
2013-10-30 01:31:07 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn truncate_works() {
|
2013-10-30 01:31:07 -05:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let path = tmpdir.join("in.txt");
|
|
|
|
|
2014-02-26 14:55:23 -06:00
|
|
|
let mut file = check!(File::open_mode(&path, io::Open, io::ReadWrite));
|
2014-06-18 13:25:36 -05:00
|
|
|
check!(file.write(b"foo"));
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(file.fsync());
|
2013-10-30 01:31:07 -05:00
|
|
|
|
|
|
|
// Do some simple things with truncation
|
2014-05-12 00:31:22 -05:00
|
|
|
assert_eq!(check!(file.stat()).size, 3);
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(file.truncate(10));
|
2014-05-12 00:31:22 -05:00
|
|
|
assert_eq!(check!(file.stat()).size, 10);
|
2014-06-18 13:25:36 -05:00
|
|
|
check!(file.write(b"bar"));
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(file.fsync());
|
2014-05-12 00:31:22 -05:00
|
|
|
assert_eq!(check!(file.stat()).size, 10);
|
2014-02-26 14:55:23 -06:00
|
|
|
assert_eq!(check!(File::open(&path).read_to_end()),
|
2014-06-18 13:25:36 -05:00
|
|
|
(Vec::from_slice(b"foobar\0\0\0\0")));
|
2013-10-30 01:31:07 -05:00
|
|
|
|
|
|
|
// Truncate to a smaller length, don't seek, and then write something.
|
|
|
|
// Ensure that the intermediate zeroes are all filled in (we're seeked
|
|
|
|
// past the end of the file).
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(file.truncate(2));
|
2014-05-12 00:31:22 -05:00
|
|
|
assert_eq!(check!(file.stat()).size, 2);
|
2014-06-18 13:25:36 -05:00
|
|
|
check!(file.write(b"wut"));
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(file.fsync());
|
2014-05-12 00:31:22 -05:00
|
|
|
assert_eq!(check!(file.stat()).size, 9);
|
2014-02-26 14:55:23 -06:00
|
|
|
assert_eq!(check!(File::open(&path).read_to_end()),
|
2014-06-18 13:25:36 -05:00
|
|
|
(Vec::from_slice(b"fo\0\0\0\0wut")));
|
2013-12-03 00:37:26 -06:00
|
|
|
drop(file);
|
2014-02-26 14:55:23 -06:00
|
|
|
})
|
2013-10-30 01:31:07 -05:00
|
|
|
|
2013-12-12 23:38:57 -06:00
|
|
|
iotest!(fn open_flavors() {
|
2013-10-30 01:31:07 -05:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
|
2014-01-30 16:10:53 -06:00
|
|
|
match File::open_mode(&tmpdir.join("a"), io::Open, io::Read) {
|
2013-11-28 14:22:53 -06:00
|
|
|
Ok(..) => fail!(), Err(..) => {}
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
2014-04-30 13:37:01 -05:00
|
|
|
|
|
|
|
// Perform each one twice to make sure that it succeeds the second time
|
|
|
|
// (where the file exists)
|
|
|
|
check!(File::open_mode(&tmpdir.join("b"), io::Open, io::Write));
|
|
|
|
assert!(tmpdir.join("b").exists());
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(File::open_mode(&tmpdir.join("b"), io::Open, io::Write));
|
2014-04-30 13:37:01 -05:00
|
|
|
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(File::open_mode(&tmpdir.join("c"), io::Open, io::ReadWrite));
|
2014-04-30 13:37:01 -05:00
|
|
|
assert!(tmpdir.join("c").exists());
|
|
|
|
check!(File::open_mode(&tmpdir.join("c"), io::Open, io::ReadWrite));
|
|
|
|
|
|
|
|
check!(File::open_mode(&tmpdir.join("d"), io::Append, io::Write));
|
|
|
|
assert!(tmpdir.join("d").exists());
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(File::open_mode(&tmpdir.join("d"), io::Append, io::Write));
|
2014-04-30 13:37:01 -05:00
|
|
|
|
|
|
|
check!(File::open_mode(&tmpdir.join("e"), io::Append, io::ReadWrite));
|
|
|
|
assert!(tmpdir.join("e").exists());
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(File::open_mode(&tmpdir.join("e"), io::Append, io::ReadWrite));
|
2014-04-30 13:37:01 -05:00
|
|
|
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(File::open_mode(&tmpdir.join("f"), io::Truncate, io::Write));
|
2014-04-30 13:37:01 -05:00
|
|
|
assert!(tmpdir.join("f").exists());
|
|
|
|
check!(File::open_mode(&tmpdir.join("f"), io::Truncate, io::Write));
|
|
|
|
|
|
|
|
check!(File::open_mode(&tmpdir.join("g"), io::Truncate, io::ReadWrite));
|
|
|
|
assert!(tmpdir.join("g").exists());
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(File::open_mode(&tmpdir.join("g"), io::Truncate, io::ReadWrite));
|
|
|
|
|
|
|
|
check!(File::create(&tmpdir.join("h")).write("foo".as_bytes()));
|
|
|
|
check!(File::open_mode(&tmpdir.join("h"), io::Open, io::Read));
|
2013-10-30 01:31:07 -05:00
|
|
|
{
|
2014-02-26 14:55:23 -06:00
|
|
|
let mut f = check!(File::open_mode(&tmpdir.join("h"), io::Open,
|
|
|
|
io::Read));
|
2014-01-30 16:10:53 -06:00
|
|
|
match f.write("wut".as_bytes()) {
|
2013-11-28 14:22:53 -06:00
|
|
|
Ok(..) => fail!(), Err(..) => {}
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
|
|
|
}
|
2014-02-26 14:55:23 -06:00
|
|
|
assert!(check!(stat(&tmpdir.join("h"))).size == 3,
|
|
|
|
"write/stat failed");
|
2013-10-30 01:31:07 -05:00
|
|
|
{
|
2014-02-26 14:55:23 -06:00
|
|
|
let mut f = check!(File::open_mode(&tmpdir.join("h"), io::Append,
|
|
|
|
io::Write));
|
|
|
|
check!(f.write("bar".as_bytes()));
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
2014-02-26 14:55:23 -06:00
|
|
|
assert!(check!(stat(&tmpdir.join("h"))).size == 6,
|
|
|
|
"append didn't append");
|
2013-10-30 01:31:07 -05:00
|
|
|
{
|
2014-02-26 14:55:23 -06:00
|
|
|
let mut f = check!(File::open_mode(&tmpdir.join("h"), io::Truncate,
|
|
|
|
io::Write));
|
|
|
|
check!(f.write("bar".as_bytes()));
|
2013-10-30 01:31:07 -05:00
|
|
|
}
|
2014-02-26 14:55:23 -06:00
|
|
|
assert!(check!(stat(&tmpdir.join("h"))).size == 3,
|
|
|
|
"truncate didn't truncate");
|
2013-11-13 16:48:45 -06:00
|
|
|
})
|
2013-11-05 17:48:27 -06:00
|
|
|
|
2014-08-02 12:52:49 -05:00
|
|
|
iotest!(fn utime() {
|
2013-11-05 17:48:27 -06:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let path = tmpdir.join("a");
|
2014-02-26 14:55:23 -06:00
|
|
|
check!(File::create(&path));
|
2014-08-07 03:05:00 -05:00
|
|
|
// These numbers have to be bigger than the time in the day to account for timezones
|
|
|
|
// Windows in particular will fail in certain timezones with small enough values
|
|
|
|
check!(change_file_times(&path, 100000, 200000));
|
|
|
|
assert_eq!(check!(path.stat()).accessed, 100000);
|
|
|
|
assert_eq!(check!(path.stat()).modified, 200000);
|
2014-08-02 12:52:49 -05:00
|
|
|
})
|
2013-11-05 17:48:27 -06:00
|
|
|
|
2014-08-02 12:52:49 -05:00
|
|
|
iotest!(fn utime_noexist() {
|
2013-11-05 17:48:27 -06:00
|
|
|
let tmpdir = tmpdir();
|
|
|
|
|
2014-01-30 16:10:53 -06:00
|
|
|
match change_file_times(&tmpdir.join("a"), 100, 200) {
|
2013-11-28 14:22:53 -06:00
|
|
|
Ok(..) => fail!(),
|
|
|
|
Err(..) => {}
|
2013-11-05 17:48:27 -06:00
|
|
|
}
|
2014-08-02 12:52:49 -05:00
|
|
|
})
|
2014-02-26 14:55:23 -06:00
|
|
|
|
|
|
|
iotest!(fn binary_file() {
|
2014-03-17 16:34:25 -05:00
|
|
|
use rand::{StdRng, Rng};
|
2014-02-26 14:55:23 -06:00
|
|
|
|
|
|
|
let mut bytes = [0, ..1024];
|
2014-05-01 13:12:16 -05:00
|
|
|
StdRng::new().ok().unwrap().fill_bytes(bytes);
|
2014-02-26 14:55:23 -06:00
|
|
|
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
|
|
|
|
check!(File::create(&tmpdir.join("test")).write(bytes));
|
|
|
|
let actual = check!(File::open(&tmpdir.join("test")).read_to_end());
|
2014-02-26 15:03:40 -06:00
|
|
|
assert!(actual.as_slice() == bytes);
|
2014-02-26 14:55:23 -06:00
|
|
|
})
|
2014-06-27 16:39:01 -05:00
|
|
|
|
|
|
|
iotest!(fn unlink_readonly() {
|
|
|
|
let tmpdir = tmpdir();
|
|
|
|
let path = tmpdir.join("file");
|
|
|
|
check!(File::create(&path));
|
|
|
|
check!(chmod(&path, io::UserRead));
|
|
|
|
check!(unlink(&path));
|
|
|
|
})
|
2013-09-23 19:20:36 -05:00
|
|
|
}
|