Skip to content

Commit a5de9bb

Browse files
committed
Remove unsafe libc layer
1 parent 8b09e01 commit a5de9bb

File tree

10 files changed

+193
-637
lines changed

10 files changed

+193
-637
lines changed

src/libstd/fs.rs

+10
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,16 @@ impl File {
348348
inner: self.inner.duplicate()?
349349
})
350350
}
351+
352+
/// Get the path that this file points to.
353+
///
354+
/// This function is only implemented on Redox, but could be
355+
/// implemented on other operating systems using readlink
356+
#[cfg(redox)]
357+
#[stable(feature = "rust1", since = "1.14.0")]
358+
pub fn path(&self) -> io::Result<PathBuf> {
359+
self.inner.path()
360+
}
351361
}
352362

353363
impl AsInner<fs_imp::File> for File {

src/libstd/sys/redox/ext/io.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
#![stable(feature = "rust1", since = "1.0.0")]
1414

1515
use fs;
16-
use os::raw;
1716
use sys;
1817
use sys_common::{AsInner, FromInner, IntoInner};
1918

2019
/// Raw file descriptors.
2120
#[stable(feature = "rust1", since = "1.0.0")]
22-
pub type RawFd = raw::c_int;
21+
pub type RawFd = usize;
2322

2423
/// A trait to extract the raw unix file descriptor from an underlying
2524
/// object.

src/libstd/sys/redox/fd.rs

+11-21
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,32 @@
1111
#![unstable(reason = "not public", issue = "0", feature = "fd")]
1212

1313
use io::{self, Read};
14-
use libc::{self, c_int, c_void};
14+
use libc;
1515
use mem;
1616
use sys::cvt;
1717
use sys_common::AsInner;
1818
use sys_common::io::read_to_end_uninitialized;
1919

2020
pub struct FileDesc {
21-
fd: c_int,
21+
fd: usize,
2222
}
2323

2424
impl FileDesc {
25-
pub fn new(fd: c_int) -> FileDesc {
25+
pub fn new(fd: usize) -> FileDesc {
2626
FileDesc { fd: fd }
2727
}
2828

29-
pub fn raw(&self) -> c_int { self.fd }
29+
pub fn raw(&self) -> usize { self.fd }
3030

3131
/// Extracts the actual filedescriptor without closing it.
32-
pub fn into_raw(self) -> c_int {
32+
pub fn into_raw(self) -> usize {
3333
let fd = self.fd;
3434
mem::forget(self);
3535
fd
3636
}
3737

3838
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
39-
let ret = cvt(unsafe {
40-
libc::read(self.fd,
41-
buf.as_mut_ptr() as *mut c_void,
42-
buf.len())
43-
})?;
44-
Ok(ret as usize)
39+
cvt(libc::read(self.fd, buf))
4540
}
4641

4742
pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
@@ -50,12 +45,7 @@ impl FileDesc {
5045
}
5146

5247
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
53-
let ret = cvt(unsafe {
54-
libc::write(self.fd,
55-
buf.as_ptr() as *const c_void,
56-
buf.len())
57-
})?;
58-
Ok(ret as usize)
48+
cvt(libc::write(self.fd, buf))
5949
}
6050

6151
pub fn set_cloexec(&self) -> io::Result<()> {
@@ -86,7 +76,7 @@ impl FileDesc {
8676
}
8777

8878
pub fn duplicate(&self) -> io::Result<FileDesc> {
89-
let new_fd = cvt(unsafe { libc::dup(self.fd) })?;
79+
let new_fd = cvt(libc::dup(self.fd, &[]))?;
9080
Ok(FileDesc::new(new_fd))
9181
}
9282
}
@@ -101,8 +91,8 @@ impl<'a> Read for &'a FileDesc {
10191
}
10292
}
10393

104-
impl AsInner<c_int> for FileDesc {
105-
fn as_inner(&self) -> &c_int { &self.fd }
94+
impl AsInner<usize> for FileDesc {
95+
fn as_inner(&self) -> &usize { &self.fd }
10696
}
10797

10898
impl Drop for FileDesc {
@@ -112,6 +102,6 @@ impl Drop for FileDesc {
112102
// the file descriptor was closed or not, and if we retried (for
113103
// something like EINTR), we might close another valid file descriptor
114104
// (opened after we closed ours.
115-
let _ = unsafe { libc::close(self.fd) };
105+
let _ = libc::close(self.fd);
116106
}
117107
}

0 commit comments

Comments
 (0)