Use UTF-8 for redox paths

This commit is contained in:
Jeremy Soller
2021-02-14 14:06:52 -07:00
parent caad589fd1
commit e24d27a0b3
4 changed files with 103 additions and 81 deletions
+5 -1
View File
@@ -24,7 +24,11 @@ unsafe fn access(path: *const c_char, mode: c_int) -> c_int {
// Wrapper over the systemcall, Do not use outside of ld_so
#[cfg(target_os = "redox")]
unsafe fn access(path: *const c_char, mode: c_int) -> c_int {
let path = CStr::from_ptr(path).to_bytes();
use core::str;
let path = match str::from_utf8(CStr::from_ptr(path).to_bytes()) {
Ok(ok) => ok,
Err(_) => return -1,
};
let fd = match syscall::open(path, syscall::O_CLOEXEC) {
Ok(fd) => fd,
_ => return -1,
+32 -7
View File
@@ -1,4 +1,4 @@
use core::{mem, ptr, result::Result as CoreResult, slice};
use core::{mem, ptr, result::Result as CoreResult, slice, str};
use syscall::{
self,
data::{Map, Stat as redox_stat, StatVfs as redox_statvfs, TimeSpec as redox_timespec},
@@ -37,6 +37,20 @@ mod ptrace;
mod signal;
mod socket;
macro_rules! path_from_c_str {
($c_str:expr) => {{
match $c_str.to_str() {
Ok(ok) => ok,
Err(err) => {
unsafe {
errno = EINVAL;
}
return -1;
}
}
}};
}
pub fn e(sys: Result<usize>) -> usize {
match sys {
Ok(ok) => ok,
@@ -138,7 +152,8 @@ impl Pal for Sys {
}
fn chdir(path: &CStr) -> c_int {
e(syscall::chdir(path.to_bytes())) as c_int
let path = path_from_c_str!(path);
e(syscall::chdir(path)) as c_int
}
fn chmod(path: &CStr, mode: mode_t) -> c_int {
@@ -330,7 +345,13 @@ impl Pal for Sys {
if res == !0 {
!0
} else {
e(syscall::chdir(&buf[..res])) as c_int
match str::from_utf8(&buf[..res]) {
Ok(path) => e(syscall::chdir(&path)) as c_int,
Err(_) => {
unsafe { errno = EINVAL };
return -1;
}
}
}
}
@@ -783,8 +804,9 @@ impl Pal for Sys {
}
fn open(path: &CStr, oflag: c_int, mode: mode_t) -> c_int {
let path = path_from_c_str!(path);
e(syscall::open(
path.to_bytes(),
path,
((oflag as usize) & 0xFFFF_0000) | ((mode as usize) & 0xFFFF),
)) as c_int
}
@@ -883,14 +905,16 @@ impl Pal for Sys {
}
fn rename(oldpath: &CStr, newpath: &CStr) -> c_int {
let newpath = path_from_c_str!(newpath);
match File::open(oldpath, fcntl::O_PATH | fcntl::O_CLOEXEC) {
Ok(file) => e(syscall::frename(*file as usize, newpath.to_bytes())) as c_int,
Ok(file) => e(syscall::frename(*file as usize, newpath)) as c_int,
Err(_) => -1,
}
}
fn rmdir(path: &CStr) -> c_int {
e(syscall::rmdir(path.to_bytes())) as c_int
let path = path_from_c_str!(path);
e(syscall::rmdir(path)) as c_int
}
fn sched_yield() -> c_int {
@@ -1016,7 +1040,8 @@ impl Pal for Sys {
}
fn unlink(path: &CStr) -> c_int {
e(syscall::unlink(path.to_bytes())) as c_int
let path = path_from_c_str!(path);
e(syscall::unlink(path)) as c_int
}
fn waitpid(mut pid: pid_t, stat_loc: *mut c_int, options: c_int) -> pid_t {