Implement realpath

This commit is contained in:
jD91mZM2
2018-10-07 10:32:36 +02:00
parent 26d629674a
commit 418a960f3b
11 changed files with 120 additions and 11 deletions
+38 -1
View File
@@ -1,11 +1,15 @@
use core::fmt::Write;
use alloc::vec::Vec;
use core::fmt::Write as _WriteFmt;
use core::{mem, ptr};
use core_io::Write;
use super::types::*;
use super::{errno, FileWriter, Pal};
use c_str::CStr;
use fs::File;
use header::dirent::dirent;
use header::errno::{EINVAL, ENOSYS};
use header::fcntl;
use header::signal::SIGCHLD;
use header::sys_ioctl::{winsize, TCGETS, TCSETS, TIOCGWINSZ};
// use header::sys_resource::rusage;
@@ -302,6 +306,39 @@ impl Pal for Sys {
e(unsafe { syscall!(READ, fildes, buf.as_mut_ptr(), buf.len()) }) as ssize_t
}
fn realpath(pathname: &CStr, out: &mut [u8]) -> c_int {
fn readlink(pathname: &CStr, out: &mut [u8]) -> ssize_t {
e(unsafe { syscall!(READLINKAT, AT_FDCWD, pathname.as_ptr(), out.as_mut_ptr(), out.len()) }) as ssize_t
}
let file = match File::open(pathname, fcntl::O_PATH) {
Ok(file) => file,
Err(_) => return -1
};
if out.is_empty() {
return 0;
}
let mut proc_path = b"/proc/self/fd/".to_vec();
write!(proc_path, "{}", *file).unwrap();
proc_path.push(0);
let len = out.len() - 1;
let read = readlink(CStr::from_bytes_with_nul(&proc_path).unwrap(), &mut out[..len]);
if read < 0 {
return -1;
}
out[read as usize] = 0;
// TODO: Should these checks from musl be ported?
// https://gitlab.com/bminor/musl/blob/master/src/misc/realpath.c#L33-38
// I'm not exactly sure what they're checking...
// Seems to be a sanity check whether or not it's still the same file?
0
}
fn rename(old: &CStr, new: &CStr) -> c_int {
e(unsafe { syscall!(RENAMEAT, AT_FDCWD, old.as_ptr(), AT_FDCWD, new.as_ptr()) }) as c_int
}