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.
|
|
|
|
|
|
|
|
use prelude::*;
|
2013-04-19 17:18:38 -05:00
|
|
|
use super::support::PathLike;
|
2013-04-24 20:26:49 -05:00
|
|
|
use super::{Reader, Writer, Seek};
|
2013-04-24 14:11:33 -05:00
|
|
|
use super::SeekStyle;
|
2013-08-19 23:57:47 -05:00
|
|
|
use rt::rtio::{RtioFileDescriptor, IoFactory, IoFactoryObject};
|
|
|
|
use rt::io::{io_error, read_error, EndOfFile};
|
|
|
|
use rt::local::Local;
|
2013-08-20 02:34:50 -05:00
|
|
|
use rt::test::*;
|
2013-08-19 23:57:47 -05:00
|
|
|
use libc::{O_RDWR, O_RDONLY, O_WRONLY, S_IWUSR, S_IRUSR,
|
|
|
|
O_CREAT, O_TRUNC, O_APPEND};
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-07-12 16:43:57 -05:00
|
|
|
/// # FIXME #7785
|
2013-04-17 19:55:21 -05:00
|
|
|
/// * Ugh, this is ridiculous. What is the best way to represent these options?
|
|
|
|
enum FileMode {
|
|
|
|
/// Opens an existing file. IoError if file does not exist.
|
|
|
|
Open,
|
|
|
|
/// Creates a file. IoError if file exists.
|
|
|
|
Create,
|
|
|
|
/// Opens an existing file or creates a new one.
|
|
|
|
OpenOrCreate,
|
|
|
|
/// Opens an existing file or creates a new one, positioned at EOF.
|
|
|
|
Append,
|
|
|
|
/// Opens an existing file, truncating it to 0 bytes.
|
|
|
|
Truncate,
|
|
|
|
/// Opens an existing file or creates a new one, truncating it to 0 bytes.
|
|
|
|
CreateOrTruncate,
|
|
|
|
}
|
2013-04-19 14:04:19 -05:00
|
|
|
|
2013-04-17 19:55:21 -05:00
|
|
|
enum FileAccess {
|
|
|
|
Read,
|
|
|
|
Write,
|
|
|
|
ReadWrite
|
|
|
|
}
|
2013-03-13 22:02:48 -05:00
|
|
|
|
2013-08-19 23:57:47 -05:00
|
|
|
pub struct FileStream {
|
|
|
|
fd: ~RtioFileDescriptor,
|
|
|
|
last_nread: int
|
|
|
|
}
|
2013-03-13 22:02:48 -05:00
|
|
|
|
2013-04-17 19:55:21 -05:00
|
|
|
impl FileStream {
|
2013-08-19 23:57:47 -05:00
|
|
|
pub fn open<P: PathLike>(path: &P,
|
|
|
|
mode: FileMode,
|
|
|
|
access: FileAccess
|
2013-04-22 15:10:59 -05:00
|
|
|
) -> Option<FileStream> {
|
2013-08-19 23:57:47 -05:00
|
|
|
let open_result = unsafe {
|
|
|
|
let io = Local::unsafe_borrow::<IoFactoryObject>();
|
|
|
|
let mut flags = match mode {
|
|
|
|
Open => 0,
|
|
|
|
Create => O_CREAT,
|
|
|
|
OpenOrCreate => O_CREAT,
|
|
|
|
Append => O_APPEND,
|
|
|
|
Truncate => O_TRUNC,
|
|
|
|
CreateOrTruncate => O_TRUNC | O_CREAT
|
|
|
|
};
|
|
|
|
flags = match access {
|
|
|
|
Read => flags | O_RDONLY,
|
|
|
|
Write => flags | O_WRONLY,
|
|
|
|
ReadWrite => flags | O_RDWR
|
|
|
|
};
|
|
|
|
let create_mode = match mode {
|
|
|
|
Create|OpenOrCreate|CreateOrTruncate =>
|
|
|
|
S_IRUSR | S_IWUSR,
|
|
|
|
_ => 0
|
|
|
|
};
|
|
|
|
(*io).fs_open(path, flags as int, create_mode as int)
|
|
|
|
};
|
|
|
|
match open_result {
|
|
|
|
Ok(fd) => Some(FileStream {
|
|
|
|
fd: fd,
|
|
|
|
last_nread: -1
|
|
|
|
}),
|
|
|
|
Err(ioerr) => {
|
|
|
|
io_error::cond.raise(ioerr);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2013-03-13 22:02:48 -05:00
|
|
|
}
|
2013-08-20 02:34:50 -05:00
|
|
|
fn unlink<P: PathLike>(path: &P) {
|
|
|
|
let unlink_result = unsafe {
|
|
|
|
let io = Local::unsafe_borrow::<IoFactoryObject>();
|
|
|
|
(*io).fs_unlink(path)
|
|
|
|
};
|
|
|
|
match unlink_result {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(ioerr) => {
|
|
|
|
io_error::cond.raise(ioerr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-13 22:02:48 -05:00
|
|
|
}
|
|
|
|
|
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> {
|
|
|
|
match self.fd.read(buf, 0) {
|
|
|
|
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 {
|
|
|
|
read_error::cond.raise(ioerr);
|
|
|
|
}
|
|
|
|
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-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]) {
|
|
|
|
match self.fd.write(buf, 0) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(ioerr) => {
|
|
|
|
io_error::cond.raise(ioerr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
|
|
|
|
fn flush(&mut self) { fail!() }
|
|
|
|
}
|
|
|
|
|
2013-04-19 16:58:21 -05:00
|
|
|
impl Seek for FileStream {
|
2013-04-17 19:55:21 -05:00
|
|
|
fn tell(&self) -> u64 { fail!() }
|
|
|
|
|
|
|
|
fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail!() }
|
|
|
|
}
|
|
|
|
|
2013-08-19 23:57:47 -05:00
|
|
|
fn file_test_smoke_test_impl() {
|
|
|
|
do run_in_newsched_task {
|
|
|
|
let message = "it's alright. have a good time";
|
2013-08-20 02:34:50 -05:00
|
|
|
let filename = &Path("./rt_io_file_test.txt");
|
2013-08-19 23:57:47 -05:00
|
|
|
{
|
|
|
|
let mut write_stream = FileStream::open(filename, Create, ReadWrite).unwrap();
|
|
|
|
write_stream.write(message.as_bytes());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
use str;
|
|
|
|
let mut read_stream = FileStream::open(filename, Open, Read).unwrap();
|
|
|
|
let mut read_buf = [0, .. 1028];
|
|
|
|
let read_str = match read_stream.read(read_buf).unwrap() {
|
|
|
|
-1|0 => fail!("shouldn't happen"),
|
|
|
|
n => str::from_bytes(read_buf.slice_to(n))
|
|
|
|
};
|
|
|
|
assert!(read_str == message.to_owned());
|
|
|
|
}
|
2013-08-20 02:34:50 -05:00
|
|
|
FileStream::unlink(filename);
|
2013-08-19 23:57:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-13 22:02:48 -05:00
|
|
|
#[test]
|
2013-08-19 23:57:47 -05:00
|
|
|
fn file_test_smoke_test() {
|
|
|
|
file_test_smoke_test_impl();
|
2013-03-13 22:02:48 -05:00
|
|
|
}
|
2013-08-20 02:34:50 -05:00
|
|
|
|
|
|
|
fn file_test_invalid_path_opened_without_create_should_raise_condition_impl() {
|
|
|
|
do run_in_newsched_task {
|
|
|
|
let filename = &Path("./file_that_does_not_exist.txt");
|
|
|
|
let mut called = false;
|
|
|
|
do io_error::cond.trap(|_| {
|
|
|
|
called = true;
|
|
|
|
}).inside {
|
|
|
|
let result = FileStream::open(filename, Open, Read);
|
|
|
|
assert!(result.is_none());
|
|
|
|
}
|
|
|
|
assert!(called);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn file_test_invalid_path_opened_without_create_should_raise_condition() {
|
|
|
|
file_test_invalid_path_opened_without_create_should_raise_condition_impl();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn file_test_unlinking_invalid_path_should_raise_condition_impl() {
|
|
|
|
use io;
|
|
|
|
do run_in_newsched_task {
|
|
|
|
let filename = &Path("./another_file_that_does_not_exist.txt");
|
|
|
|
let mut called = false;
|
|
|
|
do io_error::cond.trap(|e| {
|
|
|
|
io::println(fmt!("condition kind: %?", e.kind));
|
|
|
|
called = true;
|
|
|
|
}).inside {
|
|
|
|
FileStream::unlink(filename);
|
|
|
|
}
|
|
|
|
assert!(called);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn file_test_unlinking_invalid_path_should_raise_condition() {
|
|
|
|
file_test_unlinking_invalid_path_should_raise_condition_impl();
|
|
|
|
}
|