-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #21936 - alexcrichton:fsv2, r=aturon
This commit is an implementation of [RFC 739][rfc] which adds a new `std::fs` module to the standard library. This module provides much of the same functionality as `std::old_io::fs` but it has many tweaked APIs as well as uses the new `std::path` module. [rfc]: rust-lang/rfcs#739
- Loading branch information
Showing
15 changed files
with
2,564 additions
and
13 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2015 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 core::prelude::*; | ||
use io::prelude::*; | ||
|
||
use io; | ||
use libc::{self, c_int, size_t, c_void}; | ||
use mem; | ||
use sys::cvt; | ||
|
||
pub type fd_t = c_int; | ||
|
||
pub struct FileDesc { | ||
fd: c_int, | ||
} | ||
|
||
impl FileDesc { | ||
pub fn new(fd: c_int) -> FileDesc { | ||
FileDesc { fd: fd } | ||
} | ||
|
||
pub fn raw(&self) -> c_int { self.fd } | ||
|
||
/// Extract the actual filedescriptor without closing it. | ||
pub fn into_raw(self) -> c_int { | ||
let fd = self.fd; | ||
unsafe { mem::forget(self) }; | ||
fd | ||
} | ||
|
||
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> { | ||
let ret = try!(cvt(unsafe { | ||
libc::read(self.fd, | ||
buf.as_mut_ptr() as *mut c_void, | ||
buf.len() as size_t) | ||
})); | ||
Ok(ret as usize) | ||
} | ||
|
||
pub fn write(&self, buf: &[u8]) -> io::Result<usize> { | ||
let ret = try!(cvt(unsafe { | ||
libc::write(self.fd, | ||
buf.as_ptr() as *const c_void, | ||
buf.len() as size_t) | ||
})); | ||
Ok(ret as usize) | ||
} | ||
} | ||
|
||
impl Drop for FileDesc { | ||
fn drop(&mut self) { | ||
// closing stdio file handles makes no sense, so never do it. Also, note | ||
// that errors are ignored when closing a file descriptor. The reason | ||
// for this is that if an error occurs we don't actually know if the | ||
// file descriptor was closed or not, and if we retried (for something | ||
// like EINTR), we might close another valid file descriptor (opened | ||
// after we closed ours. | ||
if self.fd > libc::STDERR_FILENO { | ||
let _ = unsafe { libc::close(self.fd) }; | ||
} | ||
} | ||
} |
Oops, something went wrong.