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-08-26 09:24:10 -05:00
|
|
|
use super::{SeekStyle,SeekSet, SeekCur, SeekEnd,
|
|
|
|
Open, Read, Create, ReadWrite};
|
2013-08-20 17:38:41 -05:00
|
|
|
use rt::rtio::{RtioFileStream, IoFactory, IoFactoryObject};
|
2013-08-22 18:31:23 -05:00
|
|
|
use rt::io::{io_error, read_error, EndOfFile,
|
2013-08-26 09:24:10 -05:00
|
|
|
FileMode, FileAccess, FileStat};
|
2013-08-19 23:57:47 -05:00
|
|
|
use rt::local::Local;
|
2013-08-26 09:24:10 -05:00
|
|
|
use option::{Some, None};
|
|
|
|
use path::Path;
|
|
|
|
use super::super::test::*;
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-08-22 18:31:23 -05:00
|
|
|
/// Open a file for reading/writing, as indicated by `path`.
|
|
|
|
pub fn open<P: PathLike>(path: &P,
|
|
|
|
mode: FileMode,
|
|
|
|
access: FileAccess
|
|
|
|
) -> Option<FileStream> {
|
|
|
|
let open_result = unsafe {
|
2013-08-27 20:45:13 -05:00
|
|
|
let io: *mut IoFactoryObject = Local::unsafe_borrow();
|
2013-08-22 18:31:23 -05:00
|
|
|
(*io).fs_open(path, mode, access)
|
|
|
|
};
|
|
|
|
match open_result {
|
|
|
|
Ok(fd) => Some(FileStream {
|
|
|
|
fd: fd,
|
|
|
|
last_nread: -1
|
|
|
|
}),
|
|
|
|
Err(ioerr) => {
|
|
|
|
io_error::cond.raise(ioerr);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
2013-04-19 14:04:19 -05:00
|
|
|
|
2013-08-22 18:31:23 -05:00
|
|
|
/// Unlink (remove) a file from the filesystem, as indicated
|
|
|
|
/// by `path`.
|
|
|
|
pub fn unlink<P: PathLike>(path: &P) {
|
|
|
|
let unlink_result = unsafe {
|
2013-08-27 20:45:13 -05:00
|
|
|
let io: *mut IoFactoryObject = Local::unsafe_borrow();
|
2013-08-22 18:31:23 -05:00
|
|
|
(*io).fs_unlink(path)
|
|
|
|
};
|
|
|
|
match unlink_result {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(ioerr) => {
|
|
|
|
io_error::cond.raise(ioerr);
|
|
|
|
}
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
2013-03-13 22:02:48 -05:00
|
|
|
|
2013-08-20 17:38:41 -05:00
|
|
|
/// Abstraction representing *positional* access to a file. In this case,
|
|
|
|
/// *positional* refers to it keeping an encounter *cursor* of where in the
|
|
|
|
/// file a subsequent `read` or `write` will begin from. Users of a `FileStream`
|
|
|
|
/// can `seek` to move the cursor to a given location *within the bounds of the
|
|
|
|
/// file* and can ask to have the `FileStream` `tell` them the location, in
|
|
|
|
/// bytes, of the cursor.
|
|
|
|
///
|
|
|
|
/// This abstraction is roughly modeled on the access workflow as represented
|
|
|
|
/// by `open(2)`, `read(2)`, `write(2)` and friends.
|
|
|
|
///
|
|
|
|
/// The `open` and `unlink` static methods are provided to manage creation/removal
|
|
|
|
/// of files. All other methods operatin on an instance of `FileStream`.
|
2013-08-19 23:57:47 -05:00
|
|
|
pub struct FileStream {
|
2013-08-20 17:38:41 -05:00
|
|
|
fd: ~RtioFileStream,
|
|
|
|
last_nread: int,
|
2013-08-19 23:57:47 -05:00
|
|
|
}
|
2013-03-13 22:02:48 -05:00
|
|
|
|
2013-08-29 16:20:48 -05:00
|
|
|
impl FileStream {
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
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]) {
|
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) => {
|
|
|
|
read_error::cond.raise(ioerr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
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) => {
|
|
|
|
read_error::cond.raise(ioerr);
|
|
|
|
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) => {
|
|
|
|
read_error::cond.raise(ioerr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-08-26 09:24:10 -05:00
|
|
|
pub struct FileInfo(Path);
|
|
|
|
|
|
|
|
/// FIXME: DOCS
|
|
|
|
impl<'self> FileInfo {
|
|
|
|
pub fn new<P: PathLike>(path: &P) -> FileInfo {
|
|
|
|
do path.path_as_str |p| {
|
|
|
|
FileInfo(Path(p))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// FIXME #8873 can't put this in FileSystemInfo
|
|
|
|
pub fn get_path(&'self self) -> &'self Path {
|
|
|
|
&(**self)
|
|
|
|
}
|
|
|
|
pub fn stat(&self) -> Option<FileStat> {
|
|
|
|
do io_error::cond.trap(|_| {
|
|
|
|
// FIXME: can we do something more useful here?
|
|
|
|
}).inside {
|
|
|
|
stat(self.get_path())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn exists(&self) -> bool {
|
|
|
|
match self.stat() {
|
|
|
|
Some(s) => {
|
|
|
|
match s.is_file {
|
|
|
|
true => {
|
|
|
|
true
|
|
|
|
},
|
|
|
|
false => {
|
|
|
|
// FIXME: raise condition?
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn is_file(&self) -> bool {
|
|
|
|
match self.stat() {
|
|
|
|
Some(s) => s.is_file,
|
|
|
|
None => {
|
|
|
|
// FIXME: raise condition
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn open(&self, mode: FileMode, access: FileAccess) -> Option<FileStream> {
|
|
|
|
match self.is_file() {
|
|
|
|
true => {
|
|
|
|
open(self.get_path(), mode, access)
|
|
|
|
},
|
|
|
|
false => {
|
|
|
|
// FIXME: raise condition
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//fn open_read(&self) -> FileStream;
|
|
|
|
//fn open_write(&self) -> FileStream;
|
|
|
|
//fn create(&self) -> FileStream;
|
|
|
|
//fn truncate(&self) -> FileStream;
|
|
|
|
//fn open_or_create(&self) -> FileStream;
|
|
|
|
//fn create_or_truncate(&self) -> FileStream;
|
|
|
|
//fn unlink(&self);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
/// FIXME: DOCS
|
|
|
|
impl DirectoryInfo<'self> {
|
|
|
|
fn new<P: PathLike>(path: &P) -> FileInfo {
|
|
|
|
FileInfo(Path(path.path_as_str()))
|
|
|
|
}
|
|
|
|
// FIXME #8873 can't put this in FileSystemInfo
|
|
|
|
fn get_path(&'self self) -> &'self Path {
|
|
|
|
&*self
|
|
|
|
}
|
|
|
|
fn stat(&self) -> Option<FileStat> {
|
|
|
|
file::stat(self.get_path())
|
|
|
|
}
|
|
|
|
fn exists(&self) -> bool {
|
|
|
|
do io_error::cond.trap(|_| {
|
|
|
|
}).inside {
|
|
|
|
match self.stat() {
|
|
|
|
Some(_) => true,
|
|
|
|
None => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn is_dir(&self) -> bool {
|
|
|
|
|
|
|
|
}
|
|
|
|
fn create(&self);
|
|
|
|
fn get_subdirs(&self, filter: &str) -> ~[Path];
|
|
|
|
fn get_files(&self, filter: &str) -> ~[Path];
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/// Given a `rt::io::support::PathLike`, query the file system to get
|
|
|
|
/// information about a file, directory, etc.
|
|
|
|
///
|
|
|
|
/// Returns a `Some(PathInfo)` on success, and raises a `rt::io::IoError` condition
|
|
|
|
/// on failure and returns `None`.
|
|
|
|
pub fn stat<P: PathLike>(path: &P) -> Option<FileStat> {
|
|
|
|
let open_result = unsafe {
|
|
|
|
let io: *mut IoFactoryObject = Local::unsafe_borrow();
|
|
|
|
(*io).fs_stat(path)
|
|
|
|
};
|
|
|
|
match open_result {
|
|
|
|
Ok(p) => {
|
|
|
|
Some(p)
|
|
|
|
},
|
|
|
|
Err(ioerr) => {
|
|
|
|
read_error::cond.raise(ioerr);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-19 23:57:47 -05:00
|
|
|
fn file_test_smoke_test_impl() {
|
2013-09-04 20:09:03 -05:00
|
|
|
do run_in_mt_newsched_task {
|
2013-08-19 23:57:47 -05:00
|
|
|
let message = "it's alright. have a good time";
|
2013-08-21 23:29:23 -05:00
|
|
|
let filename = &Path("./tmp/file_rt_io_file_test.txt");
|
2013-08-19 23:57:47 -05:00
|
|
|
{
|
2013-08-22 18:31:23 -05:00
|
|
|
let mut write_stream = open(filename, Create, ReadWrite).unwrap();
|
2013-08-19 23:57:47 -05:00
|
|
|
write_stream.write(message.as_bytes());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
use str;
|
2013-08-22 18:31:23 -05:00
|
|
|
let mut read_stream = open(filename, Open, Read).unwrap();
|
2013-08-19 23:57:47 -05:00
|
|
|
let mut read_buf = [0, .. 1028];
|
|
|
|
let read_str = match read_stream.read(read_buf).unwrap() {
|
|
|
|
-1|0 => fail!("shouldn't happen"),
|
2013-09-05 07:17:24 -05:00
|
|
|
n => str::from_utf8(read_buf.slice_to(n))
|
2013-08-19 23:57:47 -05:00
|
|
|
};
|
|
|
|
assert!(read_str == message.to_owned());
|
|
|
|
}
|
2013-08-22 18:31:23 -05:00
|
|
|
unlink(filename);
|
2013-08-19 23:57:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-13 22:02:48 -05:00
|
|
|
#[test]
|
2013-08-20 17:38:41 -05:00
|
|
|
fn file_test_io_smoke_test() {
|
2013-08-19 23:57:47 -05:00
|
|
|
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() {
|
2013-09-04 20:09:03 -05:00
|
|
|
do run_in_mt_newsched_task {
|
2013-08-21 23:29:23 -05:00
|
|
|
let filename = &Path("./tmp/file_that_does_not_exist.txt");
|
2013-08-20 02:34:50 -05:00
|
|
|
let mut called = false;
|
|
|
|
do io_error::cond.trap(|_| {
|
|
|
|
called = true;
|
|
|
|
}).inside {
|
2013-08-22 18:31:23 -05:00
|
|
|
let result = open(filename, Open, Read);
|
2013-08-20 02:34:50 -05:00
|
|
|
assert!(result.is_none());
|
|
|
|
}
|
|
|
|
assert!(called);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[test]
|
2013-08-20 17:38:41 -05:00
|
|
|
fn file_test_io_invalid_path_opened_without_create_should_raise_condition() {
|
2013-08-20 02:34:50 -05:00
|
|
|
file_test_invalid_path_opened_without_create_should_raise_condition_impl();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn file_test_unlinking_invalid_path_should_raise_condition_impl() {
|
2013-09-04 20:09:03 -05:00
|
|
|
do run_in_mt_newsched_task {
|
2013-08-21 23:29:23 -05:00
|
|
|
let filename = &Path("./tmp/file_another_file_that_does_not_exist.txt");
|
2013-08-20 02:34:50 -05:00
|
|
|
let mut called = false;
|
2013-08-20 19:54:37 -05:00
|
|
|
do io_error::cond.trap(|_| {
|
2013-08-20 02:34:50 -05:00
|
|
|
called = true;
|
|
|
|
}).inside {
|
2013-08-22 18:31:23 -05:00
|
|
|
unlink(filename);
|
2013-08-20 02:34:50 -05:00
|
|
|
}
|
|
|
|
assert!(called);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[test]
|
2013-08-20 17:38:41 -05:00
|
|
|
fn file_test_iounlinking_invalid_path_should_raise_condition() {
|
2013-08-20 02:34:50 -05:00
|
|
|
file_test_unlinking_invalid_path_should_raise_condition_impl();
|
|
|
|
}
|
2013-08-20 17:38:41 -05:00
|
|
|
|
|
|
|
fn file_test_io_non_positional_read_impl() {
|
2013-09-04 20:09:03 -05:00
|
|
|
do run_in_mt_newsched_task {
|
2013-08-20 17:38:41 -05:00
|
|
|
use str;
|
|
|
|
let message = "ten-four";
|
|
|
|
let mut read_mem = [0, .. 8];
|
2013-08-21 23:29:23 -05:00
|
|
|
let filename = &Path("./tmp/file_rt_io_file_test_positional.txt");
|
2013-08-20 17:38:41 -05:00
|
|
|
{
|
2013-08-22 18:31:23 -05:00
|
|
|
let mut rw_stream = open(filename, Create, ReadWrite).unwrap();
|
2013-08-20 17:38:41 -05:00
|
|
|
rw_stream.write(message.as_bytes());
|
|
|
|
}
|
|
|
|
{
|
2013-08-22 18:31:23 -05:00
|
|
|
let mut read_stream = open(filename, Open, Read).unwrap();
|
2013-08-20 17:38:41 -05:00
|
|
|
{
|
|
|
|
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-22 18:31:23 -05:00
|
|
|
unlink(filename);
|
2013-09-05 07:17:24 -05:00
|
|
|
let read_str = str::from_utf8(read_mem);
|
2013-08-20 17:38:41 -05:00
|
|
|
assert!(read_str == message.to_owned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file_test_io_non_positional_read() {
|
|
|
|
file_test_io_non_positional_read_impl();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn file_test_io_seeking_impl() {
|
2013-09-04 20:09:03 -05:00
|
|
|
do run_in_mt_newsched_task {
|
2013-08-20 17:38:41 -05:00
|
|
|
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-08-21 23:29:23 -05:00
|
|
|
let filename = &Path("./tmp/file_rt_io_file_test_seeking.txt");
|
2013-08-20 17:38:41 -05:00
|
|
|
{
|
2013-08-22 18:31:23 -05:00
|
|
|
let mut rw_stream = open(filename, Create, ReadWrite).unwrap();
|
2013-08-20 17:38:41 -05:00
|
|
|
rw_stream.write(message.as_bytes());
|
|
|
|
}
|
|
|
|
{
|
2013-08-22 18:31:23 -05:00
|
|
|
let mut read_stream = open(filename, Open, Read).unwrap();
|
2013-08-20 17:38:41 -05:00
|
|
|
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();
|
|
|
|
}
|
2013-08-22 18:31:23 -05:00
|
|
|
unlink(filename);
|
2013-09-05 07:17:24 -05:00
|
|
|
let read_str = str::from_utf8(read_mem);
|
2013-08-20 17:38:41 -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-09-15 09:10:56 -05:00
|
|
|
|
2013-08-20 17:38:41 -05:00
|
|
|
#[test]
|
|
|
|
fn file_test_io_seek_and_tell_smoke_test() {
|
|
|
|
file_test_io_seeking_impl();
|
|
|
|
}
|
2013-08-21 23:22:53 -05:00
|
|
|
|
|
|
|
fn file_test_io_seek_and_write_impl() {
|
2013-09-04 20:09:03 -05:00
|
|
|
do run_in_mt_newsched_task {
|
2013-08-21 23:22:53 -05:00
|
|
|
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-08-21 23:29:23 -05:00
|
|
|
let filename = &Path("./tmp/file_rt_io_file_test_seek_and_write.txt");
|
2013-08-21 23:22:53 -05:00
|
|
|
{
|
2013-08-22 18:31:23 -05:00
|
|
|
let mut rw_stream = open(filename, Create, ReadWrite).unwrap();
|
2013-08-21 23:22:53 -05:00
|
|
|
rw_stream.write(initial_msg.as_bytes());
|
|
|
|
rw_stream.seek(seek_idx as i64, SeekSet);
|
|
|
|
rw_stream.write(overwrite_msg.as_bytes());
|
|
|
|
}
|
|
|
|
{
|
2013-08-22 18:31:23 -05:00
|
|
|
let mut read_stream = open(filename, Open, Read).unwrap();
|
2013-08-21 23:22:53 -05:00
|
|
|
read_stream.read(read_mem);
|
|
|
|
}
|
2013-08-22 18:31:23 -05:00
|
|
|
unlink(filename);
|
2013-08-26 09:24:10 -05:00
|
|
|
let read_str = str::from_bytes(read_mem);
|
2013-08-21 23:22:53 -05:00
|
|
|
assert!(read_str == final_msg.to_owned());
|
|
|
|
}
|
|
|
|
}
|
2013-09-15 09:10:56 -05:00
|
|
|
|
2013-08-21 23:22:53 -05:00
|
|
|
#[test]
|
|
|
|
fn file_test_io_seek_and_write() {
|
|
|
|
file_test_io_seek_and_write_impl();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn file_test_io_seek_shakedown_impl() {
|
2013-09-04 20:09:03 -05:00
|
|
|
do run_in_mt_newsched_task {
|
2013-08-21 23:22:53 -05:00
|
|
|
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-08-21 23:29:23 -05:00
|
|
|
let filename = &Path("./tmp/file_rt_io_file_test_seek_shakedown.txt");
|
2013-08-21 23:22:53 -05:00
|
|
|
{
|
2013-08-22 18:31:23 -05:00
|
|
|
let mut rw_stream = open(filename, Create, ReadWrite).unwrap();
|
2013-08-21 23:22:53 -05:00
|
|
|
rw_stream.write(initial_msg.as_bytes());
|
|
|
|
}
|
|
|
|
{
|
2013-08-22 18:31:23 -05:00
|
|
|
let mut read_stream = open(filename, Open, Read).unwrap();
|
2013-08-21 23:22:53 -05:00
|
|
|
|
|
|
|
read_stream.seek(-4, SeekEnd);
|
|
|
|
read_stream.read(read_mem);
|
2013-09-05 07:17:24 -05:00
|
|
|
let read_str = str::from_utf8(read_mem);
|
2013-08-21 23:22:53 -05:00
|
|
|
assert!(read_str == chunk_three.to_owned());
|
|
|
|
|
|
|
|
read_stream.seek(-9, SeekCur);
|
|
|
|
read_stream.read(read_mem);
|
2013-09-05 07:17:24 -05:00
|
|
|
let read_str = str::from_utf8(read_mem);
|
2013-08-21 23:22:53 -05:00
|
|
|
assert!(read_str == chunk_two.to_owned());
|
|
|
|
|
|
|
|
read_stream.seek(0, SeekSet);
|
|
|
|
read_stream.read(read_mem);
|
2013-09-05 07:17:24 -05:00
|
|
|
let read_str = str::from_utf8(read_mem);
|
2013-08-21 23:22:53 -05:00
|
|
|
assert!(read_str == chunk_one.to_owned());
|
|
|
|
}
|
2013-08-22 18:31:23 -05:00
|
|
|
unlink(filename);
|
2013-08-21 23:22:53 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-15 09:10:56 -05:00
|
|
|
|
2013-08-21 23:22:53 -05:00
|
|
|
#[test]
|
|
|
|
fn file_test_io_seek_shakedown() {
|
|
|
|
file_test_io_seek_shakedown_impl();
|
|
|
|
}
|
2013-08-26 09:24:10 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file_test_stat_is_correct_on_is_file() {
|
|
|
|
do run_in_newsched_task {
|
|
|
|
let filename = &Path("./tmp/file_stat_correct_on_is_file.txt");
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
None => fail!("shouldn't happen")
|
|
|
|
};
|
|
|
|
assert!(stat_res.is_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file_test_stat_is_correct_on_is_dir() {
|
|
|
|
//assert!(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file_test_fileinfo_false_when_checking_is_file_on_a_directory() {
|
|
|
|
//assert!(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file_test_fileinfo_check_exists_before_and_after_file_creation() {
|
|
|
|
//assert!(false);
|
|
|
|
}
|