Skip to content

Commit 05c8cc7

Browse files
committed
std: rework file io.. support [p]read,[p]write, impl seek/tell + more tests
1 parent 48d6761 commit 05c8cc7

File tree

4 files changed

+214
-53
lines changed

4 files changed

+214
-53
lines changed

src/libstd/rt/io/file.rs

Lines changed: 124 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@
1111
use prelude::*;
1212
use super::support::PathLike;
1313
use super::{Reader, Writer, Seek};
14-
use super::SeekStyle;
15-
use rt::rtio::{RtioFileDescriptor, IoFactory, IoFactoryObject};
14+
use super::{SeekSet, SeekStyle};
15+
use rt::rtio::{RtioFileStream, IoFactory, IoFactoryObject};
1616
use rt::io::{io_error, read_error, EndOfFile};
1717
use rt::local::Local;
1818
use rt::test::*;
1919
use libc::{O_RDWR, O_RDONLY, O_WRONLY, S_IWUSR, S_IRUSR,
2020
O_CREAT, O_TRUNC, O_APPEND};
2121

22-
/// # FIXME #7785
23-
/// * Ugh, this is ridiculous. What is the best way to represent these options?
22+
/// Instructions on how to open a file and return a `FileStream`.
2423
enum FileMode {
2524
/// Opens an existing file. IoError if file does not exist.
2625
Open,
@@ -36,15 +35,29 @@ enum FileMode {
3635
CreateOrTruncate,
3736
}
3837

38+
/// How should the file be opened? `FileStream`s opened with `Read` will
39+
/// raise an `io_error` condition if written to.
3940
enum FileAccess {
4041
Read,
4142
Write,
4243
ReadWrite
4344
}
4445

46+
/// Abstraction representing *positional* access to a file. In this case,
47+
/// *positional* refers to it keeping an encounter *cursor* of where in the
48+
/// file a subsequent `read` or `write` will begin from. Users of a `FileStream`
49+
/// can `seek` to move the cursor to a given location *within the bounds of the
50+
/// file* and can ask to have the `FileStream` `tell` them the location, in
51+
/// bytes, of the cursor.
52+
///
53+
/// This abstraction is roughly modeled on the access workflow as represented
54+
/// by `open(2)`, `read(2)`, `write(2)` and friends.
55+
///
56+
/// The `open` and `unlink` static methods are provided to manage creation/removal
57+
/// of files. All other methods operatin on an instance of `FileStream`.
4558
pub struct FileStream {
46-
fd: ~RtioFileDescriptor,
47-
last_nread: int
59+
fd: ~RtioFileStream,
60+
last_nread: int,
4861
}
4962

5063
impl FileStream {
@@ -101,7 +114,7 @@ impl FileStream {
101114

102115
impl Reader for FileStream {
103116
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
104-
match self.fd.read(buf, 0) {
117+
match self.fd.read(buf) {
105118
Ok(read) => {
106119
self.last_nread = read;
107120
match read {
@@ -126,21 +139,54 @@ impl Reader for FileStream {
126139

127140
impl Writer for FileStream {
128141
fn write(&mut self, buf: &[u8]) {
129-
match self.fd.write(buf, 0) {
142+
match self.fd.write(buf) {
130143
Ok(_) => (),
131144
Err(ioerr) => {
132145
io_error::cond.raise(ioerr);
133146
}
134147
}
135148
}
136149

137-
fn flush(&mut self) { fail!() }
150+
fn flush(&mut self) {
151+
match self.fd.flush() {
152+
Ok(_) => (),
153+
Err(ioerr) => {
154+
read_error::cond.raise(ioerr);
155+
}
156+
}
157+
}
138158
}
139159

140160
impl Seek for FileStream {
141-
fn tell(&self) -> u64 { fail!() }
161+
fn tell(&self) -> u64 {
162+
let res = self.fd.tell();
163+
match res {
164+
Ok(cursor) => cursor,
165+
Err(ioerr) => {
166+
read_error::cond.raise(ioerr);
167+
return -1;
168+
}
169+
}
170+
}
142171

143-
fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail!() }
172+
fn seek(&mut self, pos: i64, style: SeekStyle) {
173+
use libc::{SEEK_SET, SEEK_CUR, SEEK_END};
174+
let whence = match style {
175+
SeekSet => SEEK_SET,
176+
SeekCur => SEEK_CUR,
177+
SeekEnd => SEEK_END
178+
} as i64;
179+
match self.fd.seek(pos, whence) {
180+
Ok(_) => {
181+
// successful seek resets EOF indocator
182+
self.last_nread = -1;
183+
()
184+
},
185+
Err(ioerr) => {
186+
read_error::cond.raise(ioerr);
187+
}
188+
}
189+
}
144190
}
145191

146192
fn file_test_smoke_test_impl() {
@@ -166,7 +212,7 @@ fn file_test_smoke_test_impl() {
166212
}
167213

168214
#[test]
169-
fn file_test_smoke_test() {
215+
fn file_test_io_smoke_test() {
170216
file_test_smoke_test_impl();
171217
}
172218

@@ -184,17 +230,15 @@ fn file_test_invalid_path_opened_without_create_should_raise_condition_impl() {
184230
}
185231
}
186232
#[test]
187-
fn file_test_invalid_path_opened_without_create_should_raise_condition() {
233+
fn file_test_io_invalid_path_opened_without_create_should_raise_condition() {
188234
file_test_invalid_path_opened_without_create_should_raise_condition_impl();
189235
}
190236

191237
fn file_test_unlinking_invalid_path_should_raise_condition_impl() {
192-
use io;
193238
do run_in_newsched_task {
194239
let filename = &Path("./another_file_that_does_not_exist.txt");
195240
let mut called = false;
196241
do io_error::cond.trap(|e| {
197-
io::println(fmt!("condition kind: %?", e.kind));
198242
called = true;
199243
}).inside {
200244
FileStream::unlink(filename);
@@ -203,6 +247,70 @@ fn file_test_unlinking_invalid_path_should_raise_condition_impl() {
203247
}
204248
}
205249
#[test]
206-
fn file_test_unlinking_invalid_path_should_raise_condition() {
250+
fn file_test_iounlinking_invalid_path_should_raise_condition() {
207251
file_test_unlinking_invalid_path_should_raise_condition_impl();
208252
}
253+
254+
fn file_test_io_non_positional_read_impl() {
255+
do run_in_newsched_task {
256+
use str;
257+
let message = "ten-four";
258+
let mut read_mem = [0, .. 8];
259+
let filename = &Path("./rt_io_file_test_positional.txt");
260+
{
261+
let mut rw_stream = FileStream::open(filename, Create, ReadWrite).unwrap();
262+
rw_stream.write(message.as_bytes());
263+
}
264+
{
265+
let mut read_stream = FileStream::open(filename, Open, Read).unwrap();
266+
{
267+
let read_buf = read_mem.mut_slice(0, 4);
268+
read_stream.read(read_buf);
269+
}
270+
{
271+
let read_buf = read_mem.mut_slice(4, 8);
272+
read_stream.read(read_buf);
273+
}
274+
}
275+
FileStream::unlink(filename);
276+
let read_str = str::from_bytes(read_mem);
277+
assert!(read_str == message.to_owned());
278+
}
279+
}
280+
281+
#[test]
282+
fn file_test_io_non_positional_read() {
283+
file_test_io_non_positional_read_impl();
284+
}
285+
286+
fn file_test_io_seeking_impl() {
287+
do run_in_newsched_task {
288+
use str;
289+
let message = "ten-four";
290+
let mut read_mem = [0, .. 4];
291+
let set_cursor = 4 as u64;
292+
let mut tell_pos_pre_read;
293+
let mut tell_pos_post_read;
294+
let filename = &Path("./rt_io_file_test_seeking.txt");
295+
{
296+
let mut rw_stream = FileStream::open(filename, Create, ReadWrite).unwrap();
297+
rw_stream.write(message.as_bytes());
298+
}
299+
{
300+
let mut read_stream = FileStream::open(filename, Open, Read).unwrap();
301+
read_stream.seek(set_cursor as i64, SeekSet);
302+
tell_pos_pre_read = read_stream.tell();
303+
read_stream.read(read_mem);
304+
tell_pos_post_read = read_stream.tell();
305+
}
306+
FileStream::unlink(filename);
307+
let read_str = str::from_bytes(read_mem);
308+
assert!(read_str == message.slice(4, 8).to_owned());
309+
assert!(tell_pos_pre_read == set_cursor);
310+
assert!(tell_pos_post_read == message.len() as u64);
311+
}
312+
}
313+
#[test]
314+
fn file_test_io_seek_and_tell_smoke_test() {
315+
file_test_io_seeking_impl();
316+
}

src/libstd/rt/io/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ pub enum SeekStyle {
461461
/// # XXX
462462
/// * Are `u64` and `i64` the right choices?
463463
pub trait Seek {
464+
/// Return position of file cursor in the stream
464465
fn tell(&self) -> u64;
465466

466467
/// Seek to an offset in a stream

src/libstd/rt/rtio.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ pub trait IoFactory {
6666
fn tcp_bind(&mut self, addr: SocketAddr) -> Result<~RtioTcpListenerObject, IoError>;
6767
fn udp_bind(&mut self, addr: SocketAddr) -> Result<~RtioUdpSocketObject, IoError>;
6868
fn timer_init(&mut self) -> Result<~RtioTimerObject, IoError>;
69-
fn fs_from_raw_fd(&mut self, fd: c_int, close_on_drop: bool) -> ~RtioFileDescriptor;
69+
fn fs_from_raw_fd(&mut self, fd: c_int, close_on_drop: bool) -> ~RtioFileStream;
7070
fn fs_open<P: PathLike>(&mut self, path: &P, flags: int, mode:int)
71-
-> Result<~RtioFileDescriptor, IoError>;
71+
-> Result<~RtioFileStream, IoError>;
7272
fn fs_unlink<P: PathLike>(&mut self, path: &P) -> Result<(), IoError>;
7373
}
7474

@@ -113,7 +113,12 @@ pub trait RtioTimer {
113113
fn sleep(&mut self, msecs: u64);
114114
}
115115

116-
pub trait RtioFileDescriptor {
117-
fn read(&mut self, buf: &mut [u8], offset: i64) -> Result<int, IoError>;
118-
fn write(&mut self, buf: &[u8], offset: i64) -> Result<(), IoError>;
116+
pub trait RtioFileStream {
117+
fn read(&mut self, buf: &mut [u8]) -> Result<int, IoError>;
118+
fn write(&mut self, buf: &[u8]) -> Result<(), IoError>;
119+
fn pread(&mut self, buf: &mut [u8], offset: u64) -> Result<int, IoError>;
120+
fn pwrite(&mut self, buf: &[u8], offset: u64) -> Result<(), IoError>;
121+
fn seek(&mut self, pos: i64, whence: i64) -> Result<(), IoError>;
122+
fn tell(&self) -> Result<u64, IoError>;
123+
fn flush(&mut self) -> Result<(), IoError>;
119124
}

0 commit comments

Comments
 (0)