@@ -52,19 +52,22 @@ fs::unlink(&path);
52
52
use c_str:: ToCStr ;
53
53
use clone:: Clone ;
54
54
use container:: Container ;
55
+ use io;
55
56
use iter:: Iterator ;
56
57
use kinds:: Send ;
57
- use super :: { Reader , Writer , Seek } ;
58
- use super :: { SeekStyle , Read , Write , Open , IoError , Truncate } ;
59
- use super :: { FileMode , FileAccess , FileStat , IoResult , FilePermission } ;
60
- use rt:: rtio:: { RtioFileStream , IoFactory , LocalIo } ;
61
- use io;
58
+ use libc;
62
59
use option:: { Some , None , Option } ;
63
60
use owned:: Box ;
64
- use result:: { Ok , Err } ;
65
- use path;
66
61
use path:: { Path , GenericPath } ;
62
+ use path;
63
+ use result:: { Ok , Err } ;
64
+ use rt:: rtio:: { RtioFileStream , IoFactory , LocalIo } ;
65
+ use rt:: rtio;
67
66
use slice:: { OwnedVector , ImmutableVector } ;
67
+ use super :: UnstableFileStat ;
68
+ use super :: { FileMode , FileAccess , FileStat , IoResult , FilePermission } ;
69
+ use super :: { Reader , Writer , Seek , Append , SeekCur , SeekEnd , SeekSet } ;
70
+ use super :: { SeekStyle , Read , Write , ReadWrite , Open , IoError , Truncate } ;
68
71
use vec:: Vec ;
69
72
70
73
/// Unconstrained file access type that exposes read and write operations
@@ -126,6 +129,16 @@ impl File {
126
129
pub fn open_mode ( path : & Path ,
127
130
mode : FileMode ,
128
131
access : FileAccess ) -> IoResult < File > {
132
+ let mode = match mode {
133
+ Open => rtio:: Open ,
134
+ Append => rtio:: Append ,
135
+ Truncate => rtio:: Truncate ,
136
+ } ;
137
+ let access = match access {
138
+ Read => rtio:: Read ,
139
+ Write => rtio:: Write ,
140
+ ReadWrite => rtio:: ReadWrite ,
141
+ } ;
129
142
LocalIo :: maybe_raise ( |io| {
130
143
io. fs_open ( & path. to_c_str ( ) , mode, access) . map ( |fd| {
131
144
File {
@@ -134,7 +147,7 @@ impl File {
134
147
last_nread : -1
135
148
}
136
149
} )
137
- } )
150
+ } ) . map_err ( IoError :: from_rtio_error )
138
151
}
139
152
140
153
/// Attempts to open a file in read-only mode. This function is equivalent to
@@ -184,15 +197,15 @@ impl File {
184
197
/// device. This will flush any internal buffers necessary to perform this
185
198
/// operation.
186
199
pub fn fsync ( & mut self ) -> IoResult < ( ) > {
187
- self . fd . fsync ( )
200
+ self . fd . fsync ( ) . map_err ( IoError :: from_rtio_error )
188
201
}
189
202
190
203
/// This function is similar to `fsync`, except that it may not synchronize
191
204
/// file metadata to the filesystem. This is intended for use case which
192
205
/// must synchronize content, but don't need the metadata on disk. The goal
193
206
/// of this method is to reduce disk operations.
194
207
pub fn datasync ( & mut self ) -> IoResult < ( ) > {
195
- self . fd . datasync ( )
208
+ self . fd . datasync ( ) . map_err ( IoError :: from_rtio_error )
196
209
}
197
210
198
211
/// Either truncates or extends the underlying file, updating the size of
@@ -204,7 +217,7 @@ impl File {
204
217
/// will be extended to `size` and have all of the intermediate data filled
205
218
/// in with 0s.
206
219
pub fn truncate ( & mut self , size : i64 ) -> IoResult < ( ) > {
207
- self . fd . truncate ( size)
220
+ self . fd . truncate ( size) . map_err ( IoError :: from_rtio_error )
208
221
}
209
222
210
223
/// Tests whether this stream has reached EOF.
@@ -217,7 +230,10 @@ impl File {
217
230
218
231
/// Queries information about the underlying file.
219
232
pub fn stat ( & mut self ) -> IoResult < FileStat > {
220
- self . fd . fstat ( )
233
+ match self . fd . fstat ( ) {
234
+ Ok ( s) => Ok ( from_rtio ( s) ) ,
235
+ Err ( e) => Err ( IoError :: from_rtio_error ( e) ) ,
236
+ }
221
237
}
222
238
}
223
239
@@ -243,7 +259,9 @@ impl File {
243
259
/// user lacks permissions to remove the file, or if some other filesystem-level
244
260
/// error occurs.
245
261
pub fn unlink ( path : & Path ) -> IoResult < ( ) > {
246
- LocalIo :: maybe_raise ( |io| io. fs_unlink ( & path. to_c_str ( ) ) )
262
+ LocalIo :: maybe_raise ( |io| {
263
+ io. fs_unlink ( & path. to_c_str ( ) )
264
+ } ) . map_err ( IoError :: from_rtio_error)
247
265
}
248
266
249
267
/// Given a path, query the file system to get information about a file,
@@ -268,9 +286,10 @@ pub fn unlink(path: &Path) -> IoResult<()> {
268
286
/// to perform a `stat` call on the given path or if there is no entry in the
269
287
/// filesystem at the provided path.
270
288
pub fn stat ( path : & Path ) -> IoResult < FileStat > {
271
- LocalIo :: maybe_raise ( |io| {
272
- io. fs_stat ( & path. to_c_str ( ) )
273
- } )
289
+ match LocalIo :: maybe_raise ( |io| io. fs_stat ( & path. to_c_str ( ) ) ) {
290
+ Ok ( s) => Ok ( from_rtio ( s) ) ,
291
+ Err ( e) => Err ( IoError :: from_rtio_error ( e) ) ,
292
+ }
274
293
}
275
294
276
295
/// Perform the same operation as the `stat` function, except that this
@@ -282,9 +301,46 @@ pub fn stat(path: &Path) -> IoResult<FileStat> {
282
301
///
283
302
/// See `stat`
284
303
pub fn lstat ( path : & Path ) -> IoResult < FileStat > {
285
- LocalIo :: maybe_raise ( |io| {
286
- io. fs_lstat ( & path. to_c_str ( ) )
287
- } )
304
+ match LocalIo :: maybe_raise ( |io| io. fs_lstat ( & path. to_c_str ( ) ) ) {
305
+ Ok ( s) => Ok ( from_rtio ( s) ) ,
306
+ Err ( e) => Err ( IoError :: from_rtio_error ( e) ) ,
307
+ }
308
+ }
309
+
310
+ fn from_rtio ( s : rtio:: FileStat ) -> FileStat {
311
+ let rtio:: FileStat {
312
+ size, kind, perm, created, modified,
313
+ accessed, device, inode, rdev,
314
+ nlink, uid, gid, blksize, blocks, flags, gen
315
+ } = s;
316
+
317
+ FileStat {
318
+ size : size,
319
+ kind : match ( kind as libc:: c_int ) & libc:: S_IFMT {
320
+ libc:: S_IFREG => io:: TypeFile ,
321
+ libc:: S_IFDIR => io:: TypeDirectory ,
322
+ libc:: S_IFIFO => io:: TypeNamedPipe ,
323
+ libc:: S_IFBLK => io:: TypeBlockSpecial ,
324
+ libc:: S_IFLNK => io:: TypeSymlink ,
325
+ _ => io:: TypeUnknown ,
326
+ } ,
327
+ perm : FilePermission :: from_bits_truncate ( perm as u32 ) ,
328
+ created : created,
329
+ modified : modified,
330
+ accessed : accessed,
331
+ unstable : UnstableFileStat {
332
+ device : device,
333
+ inode : inode,
334
+ rdev : rdev,
335
+ nlink : nlink,
336
+ uid : uid,
337
+ gid : gid,
338
+ blksize : blksize,
339
+ blocks : blocks,
340
+ flags : flags,
341
+ gen : gen,
342
+ } ,
343
+ }
288
344
}
289
345
290
346
/// Rename a file or directory to a new name.
@@ -304,7 +360,9 @@ pub fn lstat(path: &Path) -> IoResult<FileStat> {
304
360
/// permissions to view the contents, or if some other intermittent I/O error
305
361
/// occurs.
306
362
pub fn rename ( from : & Path , to : & Path ) -> IoResult < ( ) > {
307
- LocalIo :: maybe_raise ( |io| io. fs_rename ( & from. to_c_str ( ) , & to. to_c_str ( ) ) )
363
+ LocalIo :: maybe_raise ( |io| {
364
+ io. fs_rename ( & from. to_c_str ( ) , & to. to_c_str ( ) )
365
+ } ) . map_err ( IoError :: from_rtio_error)
308
366
}
309
367
310
368
/// Copies the contents of one file to another. This function will also
@@ -382,25 +440,33 @@ pub fn copy(from: &Path, to: &Path) -> IoResult<()> {
382
440
/// Some possible error situations are not having the permission to
383
441
/// change the attributes of a file or the file not existing.
384
442
pub fn chmod ( path : & Path , mode : io:: FilePermission ) -> IoResult < ( ) > {
385
- LocalIo :: maybe_raise ( |io| io. fs_chmod ( & path. to_c_str ( ) , mode) )
443
+ LocalIo :: maybe_raise ( |io| {
444
+ io. fs_chmod ( & path. to_c_str ( ) , mode. bits ( ) as uint )
445
+ } ) . map_err ( IoError :: from_rtio_error)
386
446
}
387
447
388
448
/// Change the user and group owners of a file at the specified path.
389
449
pub fn chown ( path : & Path , uid : int , gid : int ) -> IoResult < ( ) > {
390
- LocalIo :: maybe_raise ( |io| io. fs_chown ( & path. to_c_str ( ) , uid, gid) )
450
+ LocalIo :: maybe_raise ( |io| {
451
+ io. fs_chown ( & path. to_c_str ( ) , uid, gid)
452
+ } ) . map_err ( IoError :: from_rtio_error)
391
453
}
392
454
393
455
/// Creates a new hard link on the filesystem. The `dst` path will be a
394
456
/// link pointing to the `src` path. Note that systems often require these
395
457
/// two paths to both be located on the same filesystem.
396
458
pub fn link ( src : & Path , dst : & Path ) -> IoResult < ( ) > {
397
- LocalIo :: maybe_raise ( |io| io. fs_link ( & src. to_c_str ( ) , & dst. to_c_str ( ) ) )
459
+ LocalIo :: maybe_raise ( |io| {
460
+ io. fs_link ( & src. to_c_str ( ) , & dst. to_c_str ( ) )
461
+ } ) . map_err ( IoError :: from_rtio_error)
398
462
}
399
463
400
464
/// Creates a new symbolic link on the filesystem. The `dst` path will be a
401
465
/// symlink pointing to the `src` path.
402
466
pub fn symlink ( src : & Path , dst : & Path ) -> IoResult < ( ) > {
403
- LocalIo :: maybe_raise ( |io| io. fs_symlink ( & src. to_c_str ( ) , & dst. to_c_str ( ) ) )
467
+ LocalIo :: maybe_raise ( |io| {
468
+ io. fs_symlink ( & src. to_c_str ( ) , & dst. to_c_str ( ) )
469
+ } ) . map_err ( IoError :: from_rtio_error)
404
470
}
405
471
406
472
/// Reads a symlink, returning the file that the symlink points to.
@@ -412,7 +478,7 @@ pub fn symlink(src: &Path, dst: &Path) -> IoResult<()> {
412
478
pub fn readlink ( path : & Path ) -> IoResult < Path > {
413
479
LocalIo :: maybe_raise ( |io| {
414
480
Ok ( Path :: new ( try!( io. fs_readlink ( & path. to_c_str ( ) ) ) ) )
415
- } )
481
+ } ) . map_err ( IoError :: from_rtio_error )
416
482
}
417
483
418
484
/// Create a new, empty directory at the provided path
@@ -433,7 +499,9 @@ pub fn readlink(path: &Path) -> IoResult<Path> {
433
499
/// This call will return an error if the user lacks permissions to make a new
434
500
/// directory at the provided path, or if the directory already exists.
435
501
pub fn mkdir ( path : & Path , mode : FilePermission ) -> IoResult < ( ) > {
436
- LocalIo :: maybe_raise ( |io| io. fs_mkdir ( & path. to_c_str ( ) , mode) )
502
+ LocalIo :: maybe_raise ( |io| {
503
+ io. fs_mkdir ( & path. to_c_str ( ) , mode. bits ( ) as uint )
504
+ } ) . map_err ( IoError :: from_rtio_error)
437
505
}
438
506
439
507
/// Remove an existing, empty directory
@@ -453,7 +521,9 @@ pub fn mkdir(path: &Path, mode: FilePermission) -> IoResult<()> {
453
521
/// This call will return an error if the user lacks permissions to remove the
454
522
/// directory at the provided path, or if the directory isn't empty.
455
523
pub fn rmdir ( path : & Path ) -> IoResult < ( ) > {
456
- LocalIo :: maybe_raise ( |io| io. fs_rmdir ( & path. to_c_str ( ) ) )
524
+ LocalIo :: maybe_raise ( |io| {
525
+ io. fs_rmdir ( & path. to_c_str ( ) )
526
+ } ) . map_err ( IoError :: from_rtio_error)
457
527
}
458
528
459
529
/// Retrieve a vector containing all entries within a provided directory
@@ -492,7 +562,7 @@ pub fn readdir(path: &Path) -> IoResult<Vec<Path>> {
492
562
Ok ( try!( io. fs_readdir ( & path. to_c_str ( ) , 0 ) ) . move_iter ( ) . map ( |a| {
493
563
Path :: new ( a)
494
564
} ) . collect ( ) )
495
- } )
565
+ } ) . map_err ( IoError :: from_rtio_error )
496
566
}
497
567
498
568
/// Returns an iterator which will recursively walk the directory structure
@@ -616,7 +686,9 @@ pub fn rmdir_recursive(path: &Path) -> IoResult<()> {
616
686
/// be in milliseconds.
617
687
// FIXME(#10301) these arguments should not be u64
618
688
pub fn change_file_times ( path : & Path , atime : u64 , mtime : u64 ) -> IoResult < ( ) > {
619
- LocalIo :: maybe_raise ( |io| io. fs_utime ( & path. to_c_str ( ) , atime, mtime) )
689
+ LocalIo :: maybe_raise ( |io| {
690
+ io. fs_utime ( & path. to_c_str ( ) , atime, mtime)
691
+ } ) . map_err ( IoError :: from_rtio_error)
620
692
}
621
693
622
694
impl Reader for File {
@@ -629,28 +701,35 @@ impl Reader for File {
629
701
_ => Ok ( read as uint )
630
702
}
631
703
} ,
632
- Err ( e) => Err ( e ) ,
704
+ Err ( e) => Err ( IoError :: from_rtio_error ( e ) ) ,
633
705
}
634
706
}
635
707
}
636
708
637
709
impl Writer for File {
638
- fn write ( & mut self , buf : & [ u8 ] ) -> IoResult < ( ) > { self . fd . write ( buf) }
710
+ fn write ( & mut self , buf : & [ u8 ] ) -> IoResult < ( ) > {
711
+ self . fd . write ( buf) . map_err ( IoError :: from_rtio_error)
712
+ }
639
713
}
640
714
641
715
impl Seek for File {
642
716
fn tell ( & self ) -> IoResult < u64 > {
643
- self . fd . tell ( )
717
+ self . fd . tell ( ) . map_err ( IoError :: from_rtio_error )
644
718
}
645
719
646
720
fn seek ( & mut self , pos : i64 , style : SeekStyle ) -> IoResult < ( ) > {
721
+ let style = match style {
722
+ SeekSet => rtio:: SeekSet ,
723
+ SeekCur => rtio:: SeekCur ,
724
+ SeekEnd => rtio:: SeekEnd ,
725
+ } ;
647
726
match self . fd . seek ( pos, style) {
648
727
Ok ( _) => {
649
728
// successful seek resets EOF indicator
650
729
self . last_nread = -1 ;
651
730
Ok ( ( ) )
652
731
}
653
- Err ( e) => Err ( e ) ,
732
+ Err ( e) => Err ( IoError :: from_rtio_error ( e ) ) ,
654
733
}
655
734
}
656
735
}
0 commit comments