1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
// 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::*;
use super::support::PathLike;
use super::{Reader, Writer, Seek};
use super::{SeekSet, SeekCur, SeekEnd, SeekStyle};
use rt::rtio::{RtioFileStream, IoFactory, IoFactoryObject};
use rt::io::{io_error, read_error, EndOfFile,
FileMode, FileAccess, Open, Read, Create, ReadWrite};
use rt::local::Local;
use rt::test::*;
/// 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 {
let io: *mut IoFactoryObject = Local::unsafe_borrow();
(*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
}
}
}
/// Unlink (remove) a file from the filesystem, as indicated
/// by `path`.
pub fn unlink<P: PathLike>(path: &P) {
let unlink_result = unsafe {
let io: *mut IoFactoryObject = Local::unsafe_borrow();
(*io).fs_unlink(path)
};
match unlink_result {
Ok(_) => (),
Err(ioerr) => {
io_error::cond.raise(ioerr);
}
}
}
/// 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`.
pub struct FileStream {
fd: ~RtioFileStream,
last_nread: int,
}
impl FileStream {
}
impl Reader for FileStream {
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
match self.fd.read(buf) {
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;
}
}
}
fn eof(&mut self) -> bool {
self.last_nread == 0
}
}
impl Writer for FileStream {
fn write(&mut self, buf: &[u8]) {
match self.fd.write(buf) {
Ok(_) => (),
Err(ioerr) => {
io_error::cond.raise(ioerr);
}
}
}
fn flush(&mut self) {
match self.fd.flush() {
Ok(_) => (),
Err(ioerr) => {
read_error::cond.raise(ioerr);
}
}
}
}
impl Seek for FileStream {
fn tell(&self) -> u64 {
let res = self.fd.tell();
match res {
Ok(cursor) => cursor,
Err(ioerr) => {
read_error::cond.raise(ioerr);
return -1;
}
}
}
fn seek(&mut self, pos: i64, style: SeekStyle) {
match self.fd.seek(pos, style) {
Ok(_) => {
// successful seek resets EOF indicator
self.last_nread = -1;
()
},
Err(ioerr) => {
read_error::cond.raise(ioerr);
}
}
}
}
fn file_test_smoke_test_impl() {
do run_in_newsched_task {
let message = "it's alright. have a good time";
let filename = &Path("./tmp/file_rt_io_file_test.txt");
{
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() {
-1|0 => fail!("shouldn't happen"),
n => str::from_bytes(read_buf.slice_to(n))
};
assert!(read_str == message.to_owned());
}
unlink(filename);
}
}
#[test]
#[ignore(cfg(windows))] // FIXME #8810
fn file_test_io_smoke_test() {
file_test_smoke_test_impl();
}
fn file_test_invalid_path_opened_without_create_should_raise_condition_impl() {
do run_in_newsched_task {
let filename = &Path("./tmp/file_that_does_not_exist.txt");
let mut called = false;
do io_error::cond.trap(|_| {
called = true;
}).inside {
let result = open(filename, Open, Read);
assert!(result.is_none());
}
assert!(called);
}
}
#[test]
fn file_test_io_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() {
do run_in_newsched_task {
let filename = &Path("./tmp/file_another_file_that_does_not_exist.txt");
let mut called = false;
do io_error::cond.trap(|_| {
called = true;
}).inside {
unlink(filename);
}
assert!(called);
}
}
#[test]
fn file_test_iounlinking_invalid_path_should_raise_condition() {
file_test_unlinking_invalid_path_should_raise_condition_impl();
}
fn file_test_io_non_positional_read_impl() {
do run_in_newsched_task {
use str;
let message = "ten-four";
let mut read_mem = [0, .. 8];
let filename = &Path("./tmp/file_rt_io_file_test_positional.txt");
{
let mut rw_stream = open(filename, Create, ReadWrite).unwrap();
rw_stream.write(message.as_bytes());
}
{
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);
}
}
unlink(filename);
let read_str = str::from_bytes(read_mem);
assert!(read_str == message.to_owned());
}
}
#[test]
#[ignore(cfg(windows))] // FIXME #8810
fn file_test_io_non_positional_read() {
file_test_io_non_positional_read_impl();
}
fn file_test_io_seeking_impl() {
do run_in_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;
let filename = &Path("./tmp/file_rt_io_file_test_seeking.txt");
{
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);
let read_str = str::from_bytes(read_mem);
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);
}
}
#[test]
#[ignore(cfg(windows))] // FIXME #8810
fn file_test_io_seek_and_tell_smoke_test() {
file_test_io_seeking_impl();
}
fn file_test_io_seek_and_write_impl() {
use io;
do run_in_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];
let filename = &Path("./tmp/file_rt_io_file_test_seek_and_write.txt");
{
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);
let read_str = str::from_bytes(read_mem);
io::println(fmt!("read_str: '%?' final_msg: '%?'", read_str, final_msg));
assert!(read_str == final_msg.to_owned());
}
}
#[test]
#[ignore(cfg(windows))] // FIXME #8810
fn file_test_io_seek_and_write() {
file_test_io_seek_and_write_impl();
}
fn file_test_io_seek_shakedown_impl() {
do run_in_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];
let filename = &Path("./tmp/file_rt_io_file_test_seek_shakedown.txt");
{
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_bytes(read_mem);
assert!(read_str == chunk_three.to_owned());
read_stream.seek(-9, SeekCur);
read_stream.read(read_mem);
let read_str = str::from_bytes(read_mem);
assert!(read_str == chunk_two.to_owned());
read_stream.seek(0, SeekSet);
read_stream.read(read_mem);
let read_str = str::from_bytes(read_mem);
assert!(read_str == chunk_one.to_owned());
}
unlink(filename);
}
}
#[test]
#[ignore(cfg(windows))] // FIXME #8810
fn file_test_io_seek_shakedown() {
file_test_io_seek_shakedown_impl();
}
|