2013-03-13 22:02:48 -05:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// 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.
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
At the top-level of the module are a set of freestanding functions,
|
|
|
|
associated with various filesystem operations. They all operate
|
2013-10-16 18:48:30 -05:00
|
|
|
on a `ToCStr` object. This trait is already defined for common
|
|
|
|
objects such as strings and `Path` instances.
|
2013-09-17 01:10:03 -05:00
|
|
|
|
|
|
|
All operations in this module, including those as part of `FileStream` et al
|
2013-10-18 16:01:22 -05:00
|
|
|
block the task during execution. Most will raise `std::rt::io::io_error`
|
2013-09-17 01:10:03 -05:00
|
|
|
conditions in the event of failure.
|
|
|
|
|
|
|
|
Also included in this module are the `FileInfo` and `DirectoryInfo` traits. When
|
|
|
|
`use`'d alongside a value whose type implements them (A `std::path::Path` impl is
|
|
|
|
a part of this module), they expose a set of functions for operations against
|
|
|
|
a given file location, depending on whether the path already exists. Whenever
|
|
|
|
possible, the `{FileInfo, DirectoryInfo}` preserve the same semantics as their
|
|
|
|
free function counterparts.
|
|
|
|
*/
|
|
|
|
|
2013-09-17 01:36:39 -05:00
|
|
|
use prelude::*;
|
2013-10-16 18:48:30 -05:00
|
|
|
use c_str::ToCStr;
|
2013-09-17 01:36:39 -05:00
|
|
|
use super::{Reader, Writer, Seek};
|
2013-09-21 16:15:26 -05:00
|
|
|
use super::{SeekStyle, Read, Write};
|
2013-10-16 19:05:28 -05:00
|
|
|
use rt::rtio::{RtioFileStream, IoFactory, with_local_io};
|
2013-10-18 13:52:23 -05:00
|
|
|
use rt::io::{io_error, EndOfFile,
|
2013-09-17 01:36:39 -05:00
|
|
|
FileMode, FileAccess, FileStat, IoError,
|
|
|
|
PathAlreadyExists, PathDoesntExist,
|
|
|
|
MismatchedFileTypeForOperation, ignore_io_error};
|
|
|
|
use option::{Some, None};
|
|
|
|
use path::Path;
|
2013-09-17 01:10:03 -05:00
|
|
|
|
2013-09-17 01:36:39 -05:00
|
|
|
/// Open a file for reading/writing, as indicated by `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
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// use std;
|
|
|
|
/// use std::path::Path;
|
|
|
|
/// use std::rt::io::file::open;
|
|
|
|
/// use std::rt::io::{FileMode, FileAccess};
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// let p = &Path("/some/file/path.txt");
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// do io_error::cond.trap(|_| {
|
|
|
|
/// // hoo-boy...
|
|
|
|
/// }).inside {
|
|
|
|
/// let stream = match open(p, Create, ReadWrite) {
|
|
|
|
/// Some(s) => s,
|
2013-10-22 20:25:07 -05:00
|
|
|
/// None => fail!("whoops! I'm sure this raised, anyways..")
|
|
|
|
/// };
|
2013-09-17 01:36:39 -05:00
|
|
|
/// // do some stuff with that stream
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// // the file stream will be closed at the end of this block
|
|
|
|
/// }
|
|
|
|
/// // ..
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// `FileMode` and `FileAccess` provide information about the permissions
|
|
|
|
/// context in which a given stream is created. More information about them
|
|
|
|
/// can be found in `std::rt::io`'s docs.
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// Note that, with this function, a `FileStream` is returned regardless of
|
|
|
|
/// the access-limitations indicated by `FileAccess` (e.g. calling `write` on a
|
|
|
|
/// `FileStream` opened as `ReadOnly` will raise an `io_error` condition at runtime). If you
|
|
|
|
/// desire a more-correctly-constrained interface to files, use the
|
|
|
|
/// `{open_stream, open_reader, open_writer}` methods that are a part of `FileInfo`
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// # Errors
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// This function will raise an `io_error` condition under a number of different circumstances,
|
|
|
|
/// to include but not limited to:
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// * Opening a file that already exists with `FileMode` of `Create` or vice versa (e.g.
|
|
|
|
/// opening a non-existant file with `FileMode` or `Open`)
|
|
|
|
/// * Attempting to open a file with a `FileAccess` that the user lacks permissions
|
|
|
|
/// for
|
|
|
|
/// * Filesystem-level errors (full disk, etc)
|
2013-10-16 18:48:30 -05:00
|
|
|
pub fn open<P: ToCStr>(path: &P,
|
|
|
|
mode: FileMode,
|
|
|
|
access: FileAccess
|
2013-10-16 19:05:28 -05:00
|
|
|
) -> Option<FileStream> {
|
|
|
|
do with_local_io |io| {
|
|
|
|
match io.fs_open(&path.to_c_str(), mode, access) {
|
|
|
|
Ok(fd) => Some(FileStream {
|
|
|
|
fd: fd,
|
|
|
|
last_nread: -1
|
|
|
|
}),
|
|
|
|
Err(ioerr) => {
|
|
|
|
io_error::cond.raise(ioerr);
|
|
|
|
None
|
|
|
|
}
|
2013-08-22 18:31:23 -05:00
|
|
|
}
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
2013-04-19 14:04:19 -05:00
|
|
|
|
2013-09-17 01:36:39 -05:00
|
|
|
/// Unlink a file from the underlying filesystem.
|
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
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// use std;
|
|
|
|
/// use std::path::Path;
|
|
|
|
/// use std::rt::io::file::unlink;
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// let p = &Path("/some/file/path.txt");
|
|
|
|
/// unlink(p);
|
|
|
|
/// // if we made it here without failing, then the
|
|
|
|
/// // unlink operation was successful
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -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)
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// # Errors
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// This function will raise an `io_error` condition if the user lacks permissions to
|
|
|
|
/// remove the file or if some other filesystem-level error occurs
|
2013-10-16 18:48:30 -05:00
|
|
|
pub fn unlink<P: ToCStr>(path: &P) {
|
2013-10-16 19:05:28 -05:00
|
|
|
do with_local_io |io| {
|
|
|
|
match io.fs_unlink(&path.to_c_str()) {
|
|
|
|
Ok(_) => Some(()),
|
|
|
|
Err(ioerr) => {
|
|
|
|
io_error::cond.raise(ioerr);
|
|
|
|
None
|
|
|
|
}
|
2013-08-22 18:31:23 -05:00
|
|
|
}
|
2013-10-16 19:05:28 -05:00
|
|
|
};
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
2013-03-13 22:02:48 -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
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// use std;
|
|
|
|
/// use std::path::Path;
|
|
|
|
/// use std::rt::io::file::mkdir;
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// let p = &Path("/some/dir");
|
|
|
|
/// mkdir(p);
|
|
|
|
/// // If we got here, our directory exists! Horray!
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// # Errors
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// This call will raise an `io_error` condition if the user lacks permissions to make a
|
|
|
|
/// new directory at the provided path, or if the directory already exists
|
2013-10-16 18:48:30 -05:00
|
|
|
pub fn mkdir<P: ToCStr>(path: &P) {
|
2013-10-16 19:05:28 -05:00
|
|
|
do with_local_io |io| {
|
|
|
|
match io.fs_mkdir(&path.to_c_str()) {
|
|
|
|
Ok(_) => Some(()),
|
|
|
|
Err(ioerr) => {
|
|
|
|
io_error::cond.raise(ioerr);
|
|
|
|
None
|
|
|
|
}
|
2013-09-14 11:33:53 -05:00
|
|
|
}
|
2013-10-16 19:05:28 -05:00
|
|
|
};
|
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
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// use std;
|
|
|
|
/// use std::path::Path;
|
|
|
|
/// use std::rt::io::file::rmdir;
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// let p = &Path("/some/dir");
|
|
|
|
/// rmdir(p);
|
|
|
|
/// // good riddance, you mean ol' directory
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// # Errors
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// This call will raise an `io_error` condition if the user lacks permissions to remove the
|
|
|
|
/// directory at the provided path, or if the directory isn't empty
|
2013-10-16 18:48:30 -05:00
|
|
|
pub fn rmdir<P: ToCStr>(path: &P) {
|
2013-10-16 19:05:28 -05:00
|
|
|
do with_local_io |io| {
|
|
|
|
match io.fs_rmdir(&path.to_c_str()) {
|
|
|
|
Ok(_) => Some(()),
|
|
|
|
Err(ioerr) => {
|
|
|
|
io_error::cond.raise(ioerr);
|
|
|
|
None
|
|
|
|
}
|
2013-09-14 11:33:53 -05:00
|
|
|
}
|
2013-10-16 19:05:28 -05:00
|
|
|
};
|
2013-09-14 11:33:53 -05:00
|
|
|
}
|
|
|
|
|
2013-09-17 01:36:39 -05:00
|
|
|
/// Get information on the file, directory, etc at the provided path
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-10-16 18:48:30 -05:00
|
|
|
/// Given a path, query the file system to get information about a file,
|
|
|
|
/// directory, etc.
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// Returns a `Some(std::rt::io::PathInfo)` on success
|
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
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// use std;
|
|
|
|
/// use std::path::Path;
|
|
|
|
/// use std::rt::io::file::stat;
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// let p = &Path("/some/file/path.txt");
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// do io_error::cond.trap(|_| {
|
|
|
|
/// // hoo-boy...
|
|
|
|
/// }).inside {
|
|
|
|
/// let info = match stat(p) {
|
|
|
|
/// Some(s) => s,
|
2013-10-21 15:08:31 -05:00
|
|
|
/// None => fail!("whoops! I'm sure this raised, anyways..");
|
2013-09-17 01:36:39 -05:00
|
|
|
/// }
|
|
|
|
/// if stat.is_file {
|
|
|
|
/// // just imagine the possibilities ...
|
|
|
|
/// }
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// // the file stream will be closed at the end of this block
|
|
|
|
/// }
|
|
|
|
/// // ..
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// # Errors
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// This call will raise an `io_error` condition if the user lacks the requisite
|
|
|
|
/// permissions to perform a `stat` call on the given path or if there is no
|
|
|
|
/// entry in the filesystem at the provided path.
|
2013-10-16 18:48:30 -05:00
|
|
|
pub fn stat<P: ToCStr>(path: &P) -> Option<FileStat> {
|
2013-10-16 19:05:28 -05:00
|
|
|
do with_local_io |io| {
|
|
|
|
match io.fs_stat(&path.to_c_str()) {
|
|
|
|
Ok(p) => Some(p),
|
|
|
|
Err(ioerr) => {
|
|
|
|
io_error::cond.raise(ioerr);
|
|
|
|
None
|
|
|
|
}
|
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
|
|
|
|
///
|
|
|
|
/// use std;
|
|
|
|
/// use std::path::Path;
|
|
|
|
/// use std::rt::io::file::readdir;
|
|
|
|
///
|
|
|
|
/// fn visit_dirs(dir: &Path, cb: &fn(&Path)) {
|
|
|
|
/// if dir.is_dir() {
|
|
|
|
/// let contents = dir.readdir();
|
|
|
|
/// for entry in contents.iter() {
|
|
|
|
/// if entry.is_dir() { visit_dirs(entry, cb); }
|
|
|
|
/// else { cb(entry); }
|
|
|
|
/// }
|
|
|
|
/// }
|
2013-10-21 15:08:31 -05:00
|
|
|
/// else { fail!("nope"); }
|
2013-09-17 01:36:39 -05:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// Will raise an `io_error` condition if the provided `path` doesn't exist,
|
|
|
|
/// the process lacks permissions to view the contents or if the `path` points
|
|
|
|
/// at a non-directory file
|
2013-10-16 18:48:30 -05:00
|
|
|
pub fn readdir<P: ToCStr>(path: &P) -> Option<~[Path]> {
|
2013-10-16 19:05:28 -05:00
|
|
|
do with_local_io |io| {
|
|
|
|
match io.fs_readdir(&path.to_c_str(), 0) {
|
|
|
|
Ok(p) => Some(p),
|
|
|
|
Err(ioerr) => {
|
|
|
|
io_error::cond.raise(ioerr);
|
|
|
|
None
|
|
|
|
}
|
2013-09-16 15:25:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-17 01:36:39 -05:00
|
|
|
/// Constrained version of `FileStream` that only exposes read-specific operations.
|
|
|
|
///
|
|
|
|
/// Can be retreived via `FileInfo.open_reader()`.
|
2013-09-14 11:33:53 -05:00
|
|
|
pub struct FileReader { priv stream: FileStream }
|
|
|
|
|
2013-09-17 01:10:03 -05:00
|
|
|
/// a `std::rt::io::Reader` trait impl for file I/O.
|
2013-09-14 11:33:53 -05:00
|
|
|
impl Reader for FileReader {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
|
|
|
|
self.stream.read(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eof(&mut self) -> bool {
|
|
|
|
self.stream.eof()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-17 01:10:03 -05:00
|
|
|
/// a `std::rt::io::Seek` trait impl for file I/O.
|
2013-09-14 11:33:53 -05:00
|
|
|
impl Seek for FileReader {
|
|
|
|
fn tell(&self) -> u64 {
|
|
|
|
self.stream.tell()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn seek(&mut self, pos: i64, style: SeekStyle) {
|
|
|
|
self.stream.seek(pos, style);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-17 01:36:39 -05:00
|
|
|
/// Constrained version of `FileStream` that only exposes write-specific operations.
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// Can be retreived via `FileInfo.open_writer()`.
|
2013-09-14 11:33:53 -05:00
|
|
|
pub struct FileWriter { priv stream: FileStream }
|
|
|
|
|
2013-09-17 01:10:03 -05:00
|
|
|
/// a `std::rt::io::Writer` trait impl for file I/O.
|
2013-09-14 11:33:53 -05:00
|
|
|
impl Writer for FileWriter {
|
|
|
|
fn write(&mut self, buf: &[u8]) {
|
|
|
|
self.stream.write(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) {
|
|
|
|
self.stream.flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-17 01:10:03 -05:00
|
|
|
/// a `std::rt::io::Seek` trait impl for file I/O.
|
2013-09-14 11:33:53 -05:00
|
|
|
impl Seek for FileWriter {
|
|
|
|
fn tell(&self) -> u64 {
|
|
|
|
self.stream.tell()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn seek(&mut self, pos: i64, style: SeekStyle) {
|
|
|
|
self.stream.seek(pos, style);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-17 01:36:39 -05:00
|
|
|
/// Unconstrained file access type that exposes read and write operations
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// Can be retreived via `file::open()` and `FileInfo.open_stream()`.
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// # Errors
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// This type will raise an io_error condition 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()`.
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// For this reason, it is best to use the access-constrained wrappers that are
|
|
|
|
/// exposed via `FileInfo.open_reader()` and `FileInfo.open_writer()`.
|
2013-08-19 23:57:47 -05:00
|
|
|
pub struct FileStream {
|
2013-10-19 19:33:09 -05:00
|
|
|
priv fd: ~RtioFileStream,
|
|
|
|
priv last_nread: int,
|
2013-08-19 23:57:47 -05:00
|
|
|
}
|
2013-03-13 22:02:48 -05:00
|
|
|
|
2013-09-17 01:10:03 -05:00
|
|
|
/// a `std::rt::io::Reader` trait impl for file I/O.
|
2013-04-17 19:55:21 -05:00
|
|
|
impl Reader for FileStream {
|
2013-08-19 23:57:47 -05:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
|
2013-08-20 17:38:41 -05:00
|
|
|
match self.fd.read(buf) {
|
2013-08-19 23:57:47 -05:00
|
|
|
Ok(read) => {
|
|
|
|
self.last_nread = read;
|
|
|
|
match read {
|
|
|
|
0 => None,
|
|
|
|
_ => Some(read as uint)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(ioerr) => {
|
|
|
|
// EOF is indicated by returning None
|
|
|
|
if ioerr.kind != EndOfFile {
|
2013-10-18 13:52:23 -05:00
|
|
|
io_error::cond.raise(ioerr);
|
2013-08-19 23:57:47 -05:00
|
|
|
}
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
2013-03-13 22:02:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn eof(&mut self) -> bool {
|
2013-08-19 23:57:47 -05:00
|
|
|
self.last_nread == 0
|
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-09-17 01:10:03 -05:00
|
|
|
/// a `std::rt::io::Writer` trait impl for file I/O.
|
2013-04-17 19:55:21 -05:00
|
|
|
impl Writer for FileStream {
|
2013-08-19 23:57:47 -05:00
|
|
|
fn write(&mut self, buf: &[u8]) {
|
2013-08-20 17:38:41 -05:00
|
|
|
match self.fd.write(buf) {
|
2013-08-19 23:57:47 -05:00
|
|
|
Ok(_) => (),
|
|
|
|
Err(ioerr) => {
|
|
|
|
io_error::cond.raise(ioerr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-08-20 17:38:41 -05:00
|
|
|
fn flush(&mut self) {
|
|
|
|
match self.fd.flush() {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(ioerr) => {
|
2013-10-18 13:52:23 -05:00
|
|
|
io_error::cond.raise(ioerr);
|
2013-08-20 17:38:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-09-17 01:10:03 -05:00
|
|
|
/// a `std::rt::io:Seek` trait impl for file I/O.
|
2013-04-19 16:58:21 -05:00
|
|
|
impl Seek for FileStream {
|
2013-08-20 17:38:41 -05:00
|
|
|
fn tell(&self) -> u64 {
|
|
|
|
let res = self.fd.tell();
|
|
|
|
match res {
|
|
|
|
Ok(cursor) => cursor,
|
|
|
|
Err(ioerr) => {
|
2013-10-18 13:52:23 -05:00
|
|
|
io_error::cond.raise(ioerr);
|
2013-08-20 17:38:41 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-08-20 17:38:41 -05:00
|
|
|
fn seek(&mut self, pos: i64, style: SeekStyle) {
|
2013-08-22 17:03:28 -05:00
|
|
|
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;
|
|
|
|
()
|
|
|
|
},
|
|
|
|
Err(ioerr) => {
|
2013-10-18 13:52:23 -05:00
|
|
|
io_error::cond.raise(ioerr);
|
2013-08-20 17:38:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-09-15 14:23:53 -05:00
|
|
|
/// Shared functionality between `FileInfo` and `DirectoryInfo`
|
|
|
|
pub trait FileSystemInfo {
|
|
|
|
/// Get the filesystem path that this instance points at,
|
|
|
|
/// whether it is valid or not. In this way, it can be used to
|
|
|
|
/// to specify a path of a non-existent file which it
|
|
|
|
/// later creates
|
|
|
|
fn get_path<'a>(&'a self) -> &'a Path;
|
|
|
|
|
2013-09-17 01:36:39 -05:00
|
|
|
/// Get information on the file, directory, etc at the provided path
|
|
|
|
///
|
|
|
|
/// Consult the `file::stat` documentation for more info.
|
|
|
|
///
|
|
|
|
/// This call preserves identical runtime/error semantics with `file::stat`
|
2013-09-14 11:33:53 -05:00
|
|
|
fn stat(&self) -> Option<FileStat> {
|
2013-09-15 14:23:53 -05:00
|
|
|
stat(self.get_path())
|
2013-08-26 09:24:10 -05:00
|
|
|
}
|
2013-09-14 11:33:53 -05:00
|
|
|
|
2013-09-17 01:10:03 -05:00
|
|
|
/// Boolean value indicator whether the underlying file exists on the filesystem
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// Will not raise a condition
|
2013-09-15 14:23:53 -05:00
|
|
|
fn exists(&self) -> bool {
|
2013-09-16 23:56:51 -05:00
|
|
|
match ignore_io_error(|| self.stat()) {
|
2013-09-14 11:33:53 -05:00
|
|
|
Some(_) => true,
|
2013-08-26 09:24:10 -05:00
|
|
|
None => false
|
|
|
|
}
|
|
|
|
}
|
2013-09-14 11:33:53 -05:00
|
|
|
|
2013-09-15 14:23:53 -05:00
|
|
|
}
|
|
|
|
|
2013-09-17 01:36:39 -05:00
|
|
|
/// Represents a file, whose underlying path may or may not be valid
|
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
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// * Check if a file exists, reading from it if so
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2013-09-20 15:52:37 -05:00
|
|
|
/// use std;
|
|
|
|
/// use std::path::Path;
|
|
|
|
/// use std::rt::io::file::{FileInfo, FileReader};
|
|
|
|
///
|
|
|
|
/// let f = &Path("/some/file/path.txt");
|
|
|
|
/// if f.exists() {
|
|
|
|
/// let reader = f.open_reader(Open);
|
|
|
|
/// let mut mem = [0u8, 8*64000];
|
|
|
|
/// reader.read(mem);
|
|
|
|
/// // ...
|
|
|
|
/// }
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// * Is the given path a file?
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2013-09-20 15:52:37 -05:00
|
|
|
/// let f = get_file_path_from_wherever();
|
|
|
|
/// match f.is_file() {
|
|
|
|
/// true => doing_something_with_a_file(f),
|
|
|
|
/// _ => {}
|
|
|
|
/// }
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-09-15 14:23:53 -05:00
|
|
|
pub trait FileInfo : FileSystemInfo {
|
|
|
|
/// Whether the underlying implemention (be it a file path,
|
|
|
|
/// or something else) points at a "regular file" on the FS. Will return
|
2013-09-14 11:33:53 -05:00
|
|
|
/// false for paths to non-existent locations or directories or
|
|
|
|
/// other non-regular files (named pipes, etc).
|
2013-09-17 01:36:39 -05:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// Will not raise a condition
|
2013-09-14 11:33:53 -05:00
|
|
|
fn is_file(&self) -> bool {
|
2013-09-16 23:56:51 -05:00
|
|
|
match ignore_io_error(|| self.stat()) {
|
2013-08-26 09:24:10 -05:00
|
|
|
Some(s) => s.is_file,
|
2013-09-14 11:33:53 -05:00
|
|
|
None => false
|
2013-08-26 09:24:10 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-14 11:33:53 -05:00
|
|
|
|
|
|
|
/// Attempts to open a regular file for reading/writing based
|
|
|
|
/// on provided inputs
|
2013-09-17 01:10:03 -05:00
|
|
|
///
|
|
|
|
/// See `file::open` for more information on runtime semantics and error conditions
|
2013-09-14 11:33:53 -05:00
|
|
|
fn open_stream(&self, mode: FileMode, access: FileAccess) -> Option<FileStream> {
|
2013-09-16 23:56:51 -05:00
|
|
|
match ignore_io_error(|| self.stat()) {
|
2013-09-14 11:33:53 -05:00
|
|
|
Some(s) => match s.is_file {
|
2013-09-15 14:23:53 -05:00
|
|
|
true => open(self.get_path(), mode, access),
|
2013-09-16 15:25:10 -05:00
|
|
|
false => None
|
2013-08-26 09:24:10 -05:00
|
|
|
},
|
2013-09-15 14:23:53 -05:00
|
|
|
None => open(self.get_path(), mode, access)
|
2013-09-14 11:33:53 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-17 01:10:03 -05:00
|
|
|
|
|
|
|
/// Attempts to open a regular file in read-only mode, based
|
2013-09-14 11:33:53 -05:00
|
|
|
/// on provided inputs
|
2013-09-17 01:10:03 -05:00
|
|
|
///
|
|
|
|
/// See `file::open` for more information on runtime semantics and error conditions
|
2013-09-14 11:33:53 -05:00
|
|
|
fn open_reader(&self, mode: FileMode) -> Option<FileReader> {
|
|
|
|
match self.open_stream(mode, Read) {
|
|
|
|
Some(s) => Some(FileReader { stream: s}),
|
|
|
|
None => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-17 01:10:03 -05:00
|
|
|
/// Attempts to open a regular file in write-only mode, based
|
2013-09-14 11:33:53 -05:00
|
|
|
/// on provided inputs
|
2013-09-17 01:10:03 -05:00
|
|
|
///
|
|
|
|
/// See `file::open` for more information on runtime semantics and error conditions
|
2013-09-14 11:33:53 -05:00
|
|
|
fn open_writer(&self, mode: FileMode) -> Option<FileWriter> {
|
|
|
|
match self.open_stream(mode, Write) {
|
|
|
|
Some(s) => Some(FileWriter { stream: s}),
|
|
|
|
None => None
|
2013-08-26 09:24:10 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-14 11:33:53 -05:00
|
|
|
|
2013-09-17 01:10:03 -05:00
|
|
|
/// Attempt to remove a file from the filesystem
|
|
|
|
///
|
|
|
|
/// See `file::unlink` for more information on runtime semantics and error conditions
|
2013-09-14 11:33:53 -05:00
|
|
|
fn unlink(&self) {
|
2013-09-15 14:23:53 -05:00
|
|
|
unlink(self.get_path());
|
2013-09-14 11:33:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-16 15:25:10 -05:00
|
|
|
/// `FileSystemInfo` implementation for `Path`s
|
2013-09-15 14:23:53 -05:00
|
|
|
impl FileSystemInfo for Path {
|
|
|
|
fn get_path<'a>(&'a self) -> &'a Path { self }
|
2013-08-26 09:24:10 -05:00
|
|
|
}
|
2013-09-17 01:10:03 -05:00
|
|
|
|
2013-09-16 15:25:10 -05:00
|
|
|
/// `FileInfo` implementation for `Path`s
|
2013-09-15 14:23:53 -05:00
|
|
|
impl FileInfo for Path { }
|
2013-08-26 09:24:10 -05:00
|
|
|
|
2013-09-17 01:36:39 -05:00
|
|
|
/// Represents a directory, whose underlying path may or may not be valid
|
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
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// * Check if a directory exists, `mkdir`'ing it if not
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2013-09-20 15:52:37 -05:00
|
|
|
/// use std;
|
|
|
|
/// use std::path::Path;
|
|
|
|
/// use std::rt::io::file::{DirectoryInfo};
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-20 15:52:37 -05:00
|
|
|
/// let dir = &Path("/some/dir");
|
|
|
|
/// if !dir.exists() {
|
|
|
|
/// dir.mkdir();
|
|
|
|
/// }
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-17 01:36:39 -05:00
|
|
|
/// * Is the given path a directory? If so, iterate on its contents
|
2013-09-17 12:14:15 -05:00
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2013-09-20 15:52:37 -05:00
|
|
|
/// fn visit_dirs(dir: &Path, cb: &fn(&Path)) {
|
|
|
|
/// if dir.is_dir() {
|
|
|
|
/// let contents = dir.readdir();
|
|
|
|
/// for entry in contents.iter() {
|
|
|
|
/// if entry.is_dir() { visit_dirs(entry, cb); }
|
|
|
|
/// else { cb(entry); }
|
2013-09-17 01:36:39 -05:00
|
|
|
/// }
|
|
|
|
/// }
|
2013-10-21 15:08:31 -05:00
|
|
|
/// else { fail!("nope"); }
|
2013-09-20 15:52:37 -05:00
|
|
|
/// }
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-10-07 15:44:57 -05:00
|
|
|
pub trait DirectoryInfo : FileSystemInfo {
|
2013-09-15 14:23:53 -05:00
|
|
|
/// Whether the underlying implemention (be it a file path,
|
2013-09-17 01:10:03 -05:00
|
|
|
/// 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
|
2013-09-15 14:23:53 -05:00
|
|
|
/// not a directory (eg files, named pipes, links, etc)
|
2013-09-17 01:36:39 -05:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// Will not raise a condition
|
2013-09-15 14:23:53 -05:00
|
|
|
fn is_dir(&self) -> bool {
|
2013-09-16 23:56:51 -05:00
|
|
|
match ignore_io_error(|| self.stat()) {
|
2013-09-15 14:23:53 -05:00
|
|
|
Some(s) => s.is_dir,
|
|
|
|
None => false
|
|
|
|
}
|
2013-08-26 09:24:10 -05:00
|
|
|
}
|
2013-09-17 01:10:03 -05:00
|
|
|
|
2013-09-15 14:23:53 -05:00
|
|
|
/// Create a directory at the location pointed to by the
|
2013-09-17 01:10:03 -05:00
|
|
|
/// type underlying the given `DirectoryInfo`.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This method will raise a `PathAlreadyExists` kind of `io_error` condition
|
|
|
|
/// if the provided path exists
|
|
|
|
///
|
|
|
|
/// See `file::mkdir` for more information on runtime semantics and error conditions
|
2013-09-15 14:23:53 -05:00
|
|
|
fn mkdir(&self) {
|
2013-09-16 23:56:51 -05:00
|
|
|
match ignore_io_error(|| self.stat()) {
|
2013-09-15 14:23:53 -05:00
|
|
|
Some(_) => {
|
2013-09-26 19:21:59 -05:00
|
|
|
let path = self.get_path();
|
2013-09-15 14:23:53 -05:00
|
|
|
io_error::cond.raise(IoError {
|
|
|
|
kind: PathAlreadyExists,
|
2013-09-17 01:13:16 -05:00
|
|
|
desc: "Path already exists",
|
2013-09-15 14:23:53 -05:00
|
|
|
detail:
|
2013-09-27 19:02:31 -05:00
|
|
|
Some(format!("{} already exists; can't mkdir it",
|
2013-09-26 19:21:59 -05:00
|
|
|
path.display()))
|
2013-09-15 14:23:53 -05:00
|
|
|
})
|
|
|
|
},
|
|
|
|
None => mkdir(self.get_path())
|
|
|
|
}
|
2013-08-26 09:24:10 -05:00
|
|
|
}
|
2013-09-17 01:10:03 -05:00
|
|
|
|
|
|
|
/// Remove a directory at the given location.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This method will raise a `PathDoesntExist` kind of `io_error` condition
|
|
|
|
/// if the provided path exists. It will raise a `MismatchedFileTypeForOperation`
|
|
|
|
/// kind of `io_error` condition if the provided path points at any
|
|
|
|
/// non-directory file type
|
|
|
|
///
|
|
|
|
/// See `file::rmdir` for more information on runtime semantics and error conditions
|
2013-09-15 14:23:53 -05:00
|
|
|
fn rmdir(&self) {
|
2013-09-16 23:56:51 -05:00
|
|
|
match ignore_io_error(|| self.stat()) {
|
2013-09-15 14:23:53 -05:00
|
|
|
Some(s) => {
|
|
|
|
match s.is_dir {
|
|
|
|
true => rmdir(self.get_path()),
|
|
|
|
false => {
|
2013-09-26 19:21:59 -05:00
|
|
|
let path = self.get_path();
|
2013-09-15 14:23:53 -05:00
|
|
|
let ioerr = IoError {
|
|
|
|
kind: MismatchedFileTypeForOperation,
|
|
|
|
desc: "Cannot do rmdir() on a non-directory",
|
2013-09-27 19:02:31 -05:00
|
|
|
detail: Some(format!(
|
|
|
|
"{} is a non-directory; can't rmdir it",
|
2013-09-26 19:21:59 -05:00
|
|
|
path.display()))
|
2013-09-15 14:23:53 -05:00
|
|
|
};
|
|
|
|
io_error::cond.raise(ioerr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2013-09-26 19:21:59 -05:00
|
|
|
None => {
|
|
|
|
let path = self.get_path();
|
2013-09-15 14:23:53 -05:00
|
|
|
io_error::cond.raise(IoError {
|
|
|
|
kind: PathDoesntExist,
|
2013-09-17 01:13:16 -05:00
|
|
|
desc: "Path doesn't exist",
|
2013-09-27 19:02:31 -05:00
|
|
|
detail: Some(format!("{} doesn't exist; can't rmdir it",
|
2013-09-26 19:21:59 -05:00
|
|
|
path.display()))
|
2013-09-15 14:23:53 -05:00
|
|
|
})
|
2013-09-26 19:21:59 -05:00
|
|
|
}
|
2013-08-26 09:24:10 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-17 01:10:03 -05:00
|
|
|
|
|
|
|
// Get a collection of all entries at the given
|
|
|
|
// directory
|
2013-09-16 15:25:10 -05:00
|
|
|
fn readdir(&self) -> Option<~[Path]> {
|
|
|
|
readdir(self.get_path())
|
2013-08-26 09:24:10 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-15 14:23:53 -05:00
|
|
|
|
2013-09-17 01:10:03 -05:00
|
|
|
/// `DirectoryInfo` impl for `path::Path`
|
2013-09-15 14:23:53 -05:00
|
|
|
impl DirectoryInfo for Path { }
|
2013-08-26 09:24:10 -05:00
|
|
|
|
2013-09-21 16:15:26 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::super::{SeekSet, SeekCur, SeekEnd,
|
|
|
|
io_error, Read, Create, Open, ReadWrite};
|
|
|
|
use super::super::super::test::*;
|
|
|
|
use option::{Some, None};
|
|
|
|
use path::Path;
|
|
|
|
use super::*;
|
|
|
|
use iter::range;
|
|
|
|
#[test]
|
|
|
|
fn file_test_io_smoke_test() {
|
|
|
|
do run_in_mt_newsched_task {
|
|
|
|
let message = "it's alright. have a good time";
|
2013-10-05 21:49:32 -05:00
|
|
|
let filename = &Path::new("./tmp/file_rt_io_file_test.txt");
|
2013-09-21 16:15:26 -05:00
|
|
|
{
|
|
|
|
let mut write_stream = open(filename, Create, ReadWrite).unwrap();
|
|
|
|
write_stream.write(message.as_bytes());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
use str;
|
|
|
|
let mut read_stream = open(filename, Open, Read).unwrap();
|
|
|
|
let mut read_buf = [0, .. 1028];
|
|
|
|
let read_str = match read_stream.read(read_buf).unwrap() {
|
2013-10-21 15:08:31 -05:00
|
|
|
-1|0 => fail!("shouldn't happen"),
|
2013-09-21 16:15:26 -05:00
|
|
|
n => str::from_utf8(read_buf.slice_to(n))
|
|
|
|
};
|
|
|
|
assert!(read_str == message.to_owned());
|
|
|
|
}
|
|
|
|
unlink(filename);
|
2013-08-19 23:57:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-21 16:15:26 -05:00
|
|
|
#[test]
|
|
|
|
fn file_test_io_invalid_path_opened_without_create_should_raise_condition() {
|
|
|
|
do run_in_mt_newsched_task {
|
2013-10-05 21:49:32 -05:00
|
|
|
let filename = &Path::new("./tmp/file_that_does_not_exist.txt");
|
2013-09-21 16:15:26 -05:00
|
|
|
let mut called = false;
|
|
|
|
do io_error::cond.trap(|_| {
|
|
|
|
called = true;
|
|
|
|
}).inside {
|
|
|
|
let result = open(filename, Open, Read);
|
|
|
|
assert!(result.is_none());
|
|
|
|
}
|
|
|
|
assert!(called);
|
2013-08-20 02:34:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-21 16:15:26 -05:00
|
|
|
#[test]
|
|
|
|
fn file_test_iounlinking_invalid_path_should_raise_condition() {
|
|
|
|
do run_in_mt_newsched_task {
|
2013-10-05 21:49:32 -05:00
|
|
|
let filename = &Path::new("./tmp/file_another_file_that_does_not_exist.txt");
|
2013-09-21 16:15:26 -05:00
|
|
|
let mut called = false;
|
|
|
|
do io_error::cond.trap(|_| {
|
|
|
|
called = true;
|
|
|
|
}).inside {
|
|
|
|
unlink(filename);
|
|
|
|
}
|
|
|
|
assert!(called);
|
2013-08-20 02:34:50 -05:00
|
|
|
}
|
|
|
|
}
|
2013-08-20 17:38:41 -05:00
|
|
|
|
2013-09-21 16:15:26 -05:00
|
|
|
#[test]
|
|
|
|
fn file_test_io_non_positional_read() {
|
|
|
|
do run_in_mt_newsched_task {
|
|
|
|
use str;
|
|
|
|
let message = "ten-four";
|
|
|
|
let mut read_mem = [0, .. 8];
|
2013-10-05 21:49:32 -05:00
|
|
|
let filename = &Path::new("./tmp/file_rt_io_file_test_positional.txt");
|
2013-08-20 17:38:41 -05:00
|
|
|
{
|
2013-09-21 16:15:26 -05:00
|
|
|
let mut rw_stream = open(filename, Create, ReadWrite).unwrap();
|
|
|
|
rw_stream.write(message.as_bytes());
|
2013-08-20 17:38:41 -05:00
|
|
|
}
|
|
|
|
{
|
2013-09-21 16:15:26 -05:00
|
|
|
let mut read_stream = open(filename, Open, Read).unwrap();
|
|
|
|
{
|
|
|
|
let read_buf = read_mem.mut_slice(0, 4);
|
|
|
|
read_stream.read(read_buf);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let read_buf = read_mem.mut_slice(4, 8);
|
|
|
|
read_stream.read(read_buf);
|
|
|
|
}
|
2013-08-20 17:38:41 -05:00
|
|
|
}
|
2013-09-21 16:15:26 -05:00
|
|
|
unlink(filename);
|
|
|
|
let read_str = str::from_utf8(read_mem);
|
|
|
|
assert!(read_str == message.to_owned());
|
2013-08-20 17:38:41 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-15 09:10:56 -05:00
|
|
|
|
2013-09-21 16:15:26 -05:00
|
|
|
#[test]
|
|
|
|
fn file_test_io_seek_and_tell_smoke_test() {
|
|
|
|
do run_in_mt_newsched_task {
|
|
|
|
use str;
|
|
|
|
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-10-05 21:49:32 -05:00
|
|
|
let filename = &Path::new("./tmp/file_rt_io_file_test_seeking.txt");
|
2013-09-21 16:15:26 -05:00
|
|
|
{
|
|
|
|
let mut rw_stream = open(filename, Create, ReadWrite).unwrap();
|
|
|
|
rw_stream.write(message.as_bytes());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let mut read_stream = open(filename, Open, Read).unwrap();
|
|
|
|
read_stream.seek(set_cursor as i64, SeekSet);
|
|
|
|
tell_pos_pre_read = read_stream.tell();
|
|
|
|
read_stream.read(read_mem);
|
|
|
|
tell_pos_post_read = read_stream.tell();
|
|
|
|
}
|
|
|
|
unlink(filename);
|
2013-09-05 07:17:24 -05:00
|
|
|
let read_str = str::from_utf8(read_mem);
|
2013-09-21 16:15:26 -05:00
|
|
|
assert!(read_str == message.slice(4, 8).to_owned());
|
|
|
|
assert!(tell_pos_pre_read == set_cursor);
|
|
|
|
assert!(tell_pos_post_read == message.len() as u64);
|
|
|
|
}
|
|
|
|
}
|
2013-08-21 23:22:53 -05:00
|
|
|
|
2013-09-21 16:15:26 -05:00
|
|
|
#[test]
|
|
|
|
fn file_test_io_seek_and_write() {
|
|
|
|
do run_in_mt_newsched_task {
|
|
|
|
use str;
|
|
|
|
let initial_msg = "food-is-yummy";
|
|
|
|
let overwrite_msg = "-the-bar!!";
|
|
|
|
let final_msg = "foo-the-bar!!";
|
|
|
|
let seek_idx = 3;
|
|
|
|
let mut read_mem = [0, .. 13];
|
2013-10-05 21:49:32 -05:00
|
|
|
let filename = &Path::new("./tmp/file_rt_io_file_test_seek_and_write.txt");
|
2013-09-21 16:15:26 -05:00
|
|
|
{
|
|
|
|
let mut rw_stream = open(filename, Create, ReadWrite).unwrap();
|
|
|
|
rw_stream.write(initial_msg.as_bytes());
|
|
|
|
rw_stream.seek(seek_idx as i64, SeekSet);
|
|
|
|
rw_stream.write(overwrite_msg.as_bytes());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let mut read_stream = open(filename, Open, Read).unwrap();
|
|
|
|
read_stream.read(read_mem);
|
|
|
|
}
|
|
|
|
unlink(filename);
|
2013-09-05 07:17:24 -05:00
|
|
|
let read_str = str::from_utf8(read_mem);
|
2013-09-21 16:15:26 -05:00
|
|
|
assert!(read_str == final_msg.to_owned());
|
|
|
|
}
|
|
|
|
}
|
2013-08-21 23:22:53 -05:00
|
|
|
|
2013-09-21 16:15:26 -05:00
|
|
|
#[test]
|
|
|
|
fn file_test_io_seek_shakedown() {
|
|
|
|
do run_in_mt_newsched_task {
|
|
|
|
use str; // 01234567890123
|
|
|
|
let initial_msg = "qwer-asdf-zxcv";
|
|
|
|
let chunk_one = "qwer";
|
|
|
|
let chunk_two = "asdf";
|
|
|
|
let chunk_three = "zxcv";
|
|
|
|
let mut read_mem = [0, .. 4];
|
2013-10-05 21:49:32 -05:00
|
|
|
let filename = &Path::new("./tmp/file_rt_io_file_test_seek_shakedown.txt");
|
2013-09-21 16:15:26 -05:00
|
|
|
{
|
|
|
|
let mut rw_stream = open(filename, Create, ReadWrite).unwrap();
|
|
|
|
rw_stream.write(initial_msg.as_bytes());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let mut read_stream = open(filename, Open, Read).unwrap();
|
|
|
|
|
|
|
|
read_stream.seek(-4, SeekEnd);
|
|
|
|
read_stream.read(read_mem);
|
|
|
|
let read_str = str::from_utf8(read_mem);
|
|
|
|
assert!(read_str == chunk_three.to_owned());
|
|
|
|
|
|
|
|
read_stream.seek(-9, SeekCur);
|
|
|
|
read_stream.read(read_mem);
|
|
|
|
let read_str = str::from_utf8(read_mem);
|
|
|
|
assert!(read_str == chunk_two.to_owned());
|
|
|
|
|
|
|
|
read_stream.seek(0, SeekSet);
|
|
|
|
read_stream.read(read_mem);
|
|
|
|
let read_str = str::from_utf8(read_mem);
|
|
|
|
assert!(read_str == chunk_one.to_owned());
|
|
|
|
}
|
|
|
|
unlink(filename);
|
2013-08-21 23:22:53 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-15 09:10:56 -05:00
|
|
|
|
2013-09-21 16:15:26 -05:00
|
|
|
#[test]
|
|
|
|
fn file_test_stat_is_correct_on_is_file() {
|
|
|
|
do run_in_mt_newsched_task {
|
2013-10-05 21:49:32 -05:00
|
|
|
let filename = &Path::new("./tmp/file_stat_correct_on_is_file.txt");
|
2013-09-21 16:15:26 -05:00
|
|
|
{
|
|
|
|
let mut fs = open(filename, Create, ReadWrite).unwrap();
|
|
|
|
let msg = "hw";
|
|
|
|
fs.write(msg.as_bytes());
|
|
|
|
}
|
|
|
|
let stat_res = match stat(filename) {
|
|
|
|
Some(s) => s,
|
2013-10-21 15:08:31 -05:00
|
|
|
None => fail!("shouldn't happen")
|
2013-09-21 16:15:26 -05:00
|
|
|
};
|
|
|
|
assert!(stat_res.is_file);
|
|
|
|
unlink(filename);
|
|
|
|
}
|
2013-08-26 09:24:10 -05:00
|
|
|
}
|
|
|
|
|
2013-09-21 16:15:26 -05:00
|
|
|
#[test]
|
|
|
|
fn file_test_stat_is_correct_on_is_dir() {
|
|
|
|
do run_in_mt_newsched_task {
|
2013-10-05 21:49:32 -05:00
|
|
|
let filename = &Path::new("./tmp/file_stat_correct_on_is_dir");
|
2013-09-21 16:15:26 -05:00
|
|
|
mkdir(filename);
|
|
|
|
let stat_res = match stat(filename) {
|
|
|
|
Some(s) => s,
|
2013-10-21 15:08:31 -05:00
|
|
|
None => fail!("shouldn't happen")
|
2013-09-21 16:15:26 -05:00
|
|
|
};
|
|
|
|
assert!(stat_res.is_dir);
|
|
|
|
rmdir(filename);
|
|
|
|
}
|
2013-09-14 11:33:53 -05:00
|
|
|
}
|
2013-08-26 09:24:10 -05:00
|
|
|
|
2013-09-21 16:15:26 -05:00
|
|
|
#[test]
|
|
|
|
fn file_test_fileinfo_false_when_checking_is_file_on_a_directory() {
|
|
|
|
do run_in_mt_newsched_task {
|
2013-10-05 21:49:32 -05:00
|
|
|
let dir = &Path::new("./tmp/fileinfo_false_on_dir");
|
2013-09-21 16:15:26 -05:00
|
|
|
mkdir(dir);
|
|
|
|
assert!(dir.is_file() == false);
|
|
|
|
rmdir(dir);
|
|
|
|
}
|
2013-09-14 11:33:53 -05:00
|
|
|
}
|
2013-08-26 09:24:10 -05:00
|
|
|
|
2013-09-21 16:15:26 -05:00
|
|
|
#[test]
|
|
|
|
fn file_test_fileinfo_check_exists_before_and_after_file_creation() {
|
|
|
|
do run_in_mt_newsched_task {
|
2013-10-05 21:49:32 -05:00
|
|
|
let file = &Path::new("./tmp/fileinfo_check_exists_b_and_a.txt");
|
2013-09-21 16:15:26 -05:00
|
|
|
{
|
|
|
|
let msg = "foo".as_bytes();
|
|
|
|
let mut w = file.open_writer(Create);
|
|
|
|
w.write(msg);
|
|
|
|
}
|
|
|
|
assert!(file.exists());
|
|
|
|
file.unlink();
|
|
|
|
assert!(!file.exists());
|
2013-09-14 11:33:53 -05:00
|
|
|
}
|
2013-09-15 14:23:53 -05:00
|
|
|
}
|
|
|
|
|
2013-09-21 16:15:26 -05:00
|
|
|
#[test]
|
|
|
|
fn file_test_directoryinfo_check_exists_before_and_after_mkdir() {
|
|
|
|
do run_in_mt_newsched_task {
|
2013-10-05 21:49:32 -05:00
|
|
|
let dir = &Path::new("./tmp/before_and_after_dir");
|
2013-09-21 16:15:26 -05:00
|
|
|
assert!(!dir.exists());
|
|
|
|
dir.mkdir();
|
|
|
|
assert!(dir.exists());
|
|
|
|
assert!(dir.is_dir());
|
|
|
|
dir.rmdir();
|
|
|
|
assert!(!dir.exists());
|
|
|
|
}
|
2013-09-14 11:33:53 -05:00
|
|
|
}
|
2013-09-16 15:25:10 -05:00
|
|
|
|
2013-09-21 16:15:26 -05:00
|
|
|
#[test]
|
|
|
|
fn file_test_directoryinfo_readdir() {
|
|
|
|
use str;
|
|
|
|
do run_in_mt_newsched_task {
|
2013-10-05 21:49:32 -05:00
|
|
|
let dir = &Path::new("./tmp/di_readdir");
|
2013-09-21 16:15:26 -05:00
|
|
|
dir.mkdir();
|
|
|
|
let prefix = "foo";
|
|
|
|
for n in range(0,3) {
|
2013-10-05 21:49:32 -05:00
|
|
|
let f = dir.join(format!("{}.txt", n));
|
2013-09-21 16:15:26 -05:00
|
|
|
let mut w = f.open_writer(Create);
|
|
|
|
let msg_str = (prefix + n.to_str().to_owned()).to_owned();
|
|
|
|
let msg = msg_str.as_bytes();
|
|
|
|
w.write(msg);
|
|
|
|
}
|
|
|
|
match dir.readdir() {
|
|
|
|
Some(files) => {
|
|
|
|
let mut mem = [0u8, .. 4];
|
|
|
|
for f in files.iter() {
|
|
|
|
{
|
2013-09-26 19:21:59 -05:00
|
|
|
let n = f.filestem_str();
|
2013-09-21 16:15:26 -05:00
|
|
|
let mut r = f.open_reader(Open);
|
|
|
|
r.read(mem);
|
|
|
|
let read_str = str::from_utf8(mem);
|
|
|
|
let expected = match n {
|
2013-10-21 15:08:31 -05:00
|
|
|
None|Some("") => fail!("really shouldn't happen.."),
|
2013-09-26 19:21:59 -05:00
|
|
|
Some(n) => prefix+n
|
2013-09-21 16:15:26 -05:00
|
|
|
};
|
|
|
|
assert!(expected == read_str);
|
|
|
|
}
|
|
|
|
f.unlink();
|
2013-09-16 15:25:10 -05:00
|
|
|
}
|
2013-09-21 16:15:26 -05:00
|
|
|
},
|
2013-10-21 15:08:31 -05:00
|
|
|
None => fail!("shouldn't happen")
|
2013-09-21 16:15:26 -05:00
|
|
|
}
|
|
|
|
dir.rmdir();
|
2013-09-16 15:25:10 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-23 19:20:36 -05:00
|
|
|
}
|