From b52c8221503c3cf74e439942903854f4b55a2bdc Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Mon, 27 Aug 2018 08:44:42 -0600 Subject: [PATCH] Update Redox module to use CStr --- src/header/pwd/mod.rs | 2 +- src/platform/linux/mod.rs | 29 ++++++++++++++++------ src/platform/pal/mod.rs | 8 ++---- src/platform/rawfile.rs | 2 +- src/platform/redox/mod.rs | 52 +++++++++++++++++++-------------------- 5 files changed, 50 insertions(+), 43 deletions(-) diff --git a/src/header/pwd/mod.rs b/src/header/pwd/mod.rs index d719cecbae..97b912af91 100644 --- a/src/header/pwd/mod.rs +++ b/src/header/pwd/mod.rs @@ -48,7 +48,7 @@ where F: FnMut(&[&[u8]]) -> bool, { let file = match RawFile::open( - CStr::from_bytes_with_nul(b"/etc/passwd\0").unwrap(), + unsafe { CStr::from_bytes_with_nul_unchecked(b"/etc/passwd\0") }, fcntl::O_RDONLY, 0, ) { diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs index 1f9884aaa0..7bd671f821 100644 --- a/src/platform/linux/mod.rs +++ b/src/platform/linux/mod.rs @@ -1,9 +1,9 @@ use core::fmt::Write; use core::{mem, ptr}; -use c_str::CStr; use super::types::*; use super::{errno, FileWriter, Pal}; +use c_str::CStr; mod signal; mod socket; @@ -60,7 +60,15 @@ impl Pal for Sys { } fn chown(path: &CStr, owner: uid_t, group: gid_t) -> c_int { - e(unsafe { syscall!(FCHOWNAT, AT_FDCWD, path.as_ptr(), owner as u32, group as u32) }) as c_int + e(unsafe { + syscall!( + FCHOWNAT, + AT_FDCWD, + path.as_ptr(), + owner as u32, + group as u32 + ) + }) as c_int } fn clock_gettime(clk_id: clockid_t, tp: *mut timespec) -> c_int { @@ -79,11 +87,7 @@ impl Pal for Sys { e(unsafe { syscall!(DUP3, fildes, fildes2, 0) }) as c_int } - unsafe fn execve( - path: &CStr, - argv: *const *mut c_char, - envp: *const *mut c_char, - ) -> c_int { + unsafe fn execve(path: &CStr, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int { e(syscall!(EXECVE, path.as_ptr(), argv, envp)) as c_int } @@ -230,7 +234,16 @@ impl Pal for Sys { } fn link(path1: &CStr, path2: &CStr) -> c_int { - e(unsafe { syscall!(LINKAT, AT_FDCWD, path1.as_ptr(), AT_FDCWD, path2.as_ptr(), 0) }) as c_int + e(unsafe { + syscall!( + LINKAT, + AT_FDCWD, + path1.as_ptr(), + AT_FDCWD, + path2.as_ptr(), + 0 + ) + }) as c_int } fn lseek(fildes: c_int, offset: off_t, whence: c_int) -> off_t { diff --git a/src/platform/pal/mod.rs b/src/platform/pal/mod.rs index 3a1dec48a9..08981a5676 100644 --- a/src/platform/pal/mod.rs +++ b/src/platform/pal/mod.rs @@ -1,7 +1,7 @@ use core::ptr; -use c_str::CStr; use super::types::*; +use c_str::CStr; pub use self::signal::PalSignal; mod signal; @@ -46,11 +46,7 @@ pub trait Pal { Self::no_pal("dup2") } - unsafe fn execve( - path: &CStr, - argv: *const *mut c_char, - envp: *const *mut c_char, - ) -> c_int { + unsafe fn execve(path: &CStr, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int { Self::no_pal("execve") } diff --git a/src/platform/rawfile.rs b/src/platform/rawfile.rs index e111338aa5..1984217594 100644 --- a/src/platform/rawfile.rs +++ b/src/platform/rawfile.rs @@ -1,7 +1,7 @@ use core::ops::Deref; -use c_str::CStr; use super::{types::*, Pal, Sys}; +use c_str::CStr; pub struct RawFile(c_int); diff --git a/src/platform/redox/mod.rs b/src/platform/redox/mod.rs index 8fabf0617a..d156392318 100644 --- a/src/platform/redox/mod.rs +++ b/src/platform/redox/mod.rs @@ -10,7 +10,8 @@ use syscall::flag::*; use syscall::{self, Result}; use super::types::*; -use super::{c_str, errno, FileReader, FileWriter, Pal, RawFile, Read}; +use super::{errno, FileReader, FileWriter, Pal, RawFile, Read}; +use c_str::{CStr, CString}; mod signal; mod socket; @@ -101,13 +102,11 @@ impl Pal for Sys { } fn chdir(path: &CStr) -> c_int { - let path = unsafe { c_str(path) }; - e(syscall::chdir(path)) as c_int + e(syscall::chdir(path.to_bytes())) as c_int } fn chmod(path: &CStr, mode: mode_t) -> c_int { - let path = unsafe { c_str(path) }; - match syscall::open(path, O_WRONLY) { + match syscall::open(path.to_bytes(), O_WRONLY) { Err(err) => e(Err(err)) as c_int, Ok(fd) => { let res = syscall::fchmod(fd as usize, mode as u16); @@ -118,8 +117,7 @@ impl Pal for Sys { } fn chown(path: &CStr, owner: uid_t, group: gid_t) -> c_int { - let path = unsafe { c_str(path) }; - match syscall::open(path, O_WRONLY) { + match syscall::open(path.to_bytes(), O_WRONLY) { Err(err) => e(Err(err)) as c_int, Ok(fd) => { let res = syscall::fchown(fd as usize, owner as u32, group as u32); @@ -291,8 +289,7 @@ impl Pal for Sys { } fn utimens(path: &CStr, times: *const timespec) -> c_int { - let path = unsafe { c_str(path) }; - match syscall::open(path, O_STAT) { + match syscall::open(path.to_bytes(), O_STAT) { Err(err) => e(Err(err)) as c_int, Ok(fd) => { let res = Self::futimens(fd as c_int, times); @@ -444,9 +441,8 @@ impl Pal for Sys { } fn link(path1: &CStr, path2: &CStr) -> c_int { - let path1 = unsafe { c_str(path1) }; - let path2 = unsafe { c_str(path2) }; - e(unsafe { syscall::link(path1.as_ptr(), path2.as_ptr()) }) as c_int + e(unsafe { syscall::link(path1.as_ptr() as *const u8, path2.as_ptr() as *const u8) }) + as c_int } fn lseek(fd: c_int, offset: off_t, whence: c_int) -> off_t { @@ -459,8 +455,7 @@ impl Pal for Sys { fn mkdir(path: &CStr, mode: mode_t) -> c_int { let flags = O_CREAT | O_EXCL | O_CLOEXEC | O_DIRECTORY | mode as usize & 0o777; - let path = unsafe { c_str(path) }; - match syscall::open(path, flags) { + match syscall::open(path.to_bytes(), flags) { Ok(fd) => { let _ = syscall::close(fd); 0 @@ -471,8 +466,7 @@ impl Pal for Sys { fn mkfifo(path: &CStr, mode: mode_t) -> c_int { let flags = O_CREAT | MODE_FIFO as usize | mode as usize & 0o777; - let path = unsafe { c_str(path) }; - match syscall::open(path, flags) { + match syscall::open(path.to_bytes(), flags) { Ok(fd) => { let _ = syscall::close(fd); 0 @@ -541,8 +535,10 @@ impl Pal for Sys { } fn open(path: &CStr, oflag: c_int, mode: mode_t) -> c_int { - let path = unsafe { c_str(path) }; - e(syscall::open(path, (oflag as usize) | (mode as usize))) as c_int + e(syscall::open( + path.to_bytes(), + (oflag as usize) | (mode as usize), + )) as c_int } fn pipe(fds: &mut [c_int]) -> c_int { @@ -558,10 +554,9 @@ impl Pal for Sys { } fn rename(oldpath: &CStr, newpath: &CStr) -> c_int { - let (oldpath, newpath) = unsafe { (c_str(oldpath), c_str(newpath)) }; - match syscall::open(oldpath, O_WRONLY) { + match syscall::open(oldpath.to_bytes(), O_WRONLY) { Ok(fd) => { - let retval = syscall::frename(fd, newpath); + let retval = syscall::frename(fd, newpath.to_bytes()); let _ = syscall::close(fd); e(retval) as c_int } @@ -570,8 +565,7 @@ impl Pal for Sys { } fn rmdir(path: &CStr) -> c_int { - let path = unsafe { c_str(path) }; - e(syscall::rmdir(path)) as c_int + e(syscall::rmdir(path.to_bytes())) as c_int } fn select( @@ -590,7 +584,8 @@ impl Pal for Sys { unsafe { (*set).fds_bits[fd / (8 * mem::size_of::())] & mask == mask } } - let event_file = match RawFile::open("event:\0".as_ptr() as &CStr, 0, 0) { + let event_path = unsafe { CStr::from_bytes_with_nul_unchecked(b"event:\0") }; + let event_file = match RawFile::open(event_path, 0, 0) { Ok(file) => file, Err(_) => return -1, }; @@ -632,7 +627,11 @@ impl Pal for Sys { None } else { let timeout_file = match RawFile::open( - format!("time:{}\0", syscall::CLOCK_MONOTONIC).as_ptr() as &CStr, + &unsafe { + CString::from_vec_unchecked( + format!("time:{}", syscall::CLOCK_MONOTONIC).into_bytes(), + ) + }, 0, 0, ) { @@ -726,8 +725,7 @@ impl Pal for Sys { } fn unlink(path: &CStr) -> c_int { - let path = unsafe { c_str(path) }; - e(syscall::unlink(path)) as c_int + e(syscall::unlink(path.to_bytes())) as c_int } fn waitpid(mut pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t {