From 534e7ef80c3e7c7bf97466613f1d7cb7f8f088ee Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jackpot51@gmail.com>
Date: Mon, 12 Feb 2024 15:09:13 -0700
Subject: [PATCH] Make redox fpath only return new format

---
 Cargo.lock                |  4 ++--
 Cargo.toml                |  2 +-
 src/platform/redox/mod.rs | 33 +++++++++++++++++++++++++++++++--
 3 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 0aa89491d..d6ce4d0a6 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -200,9 +200,9 @@ dependencies = [
 
 [[package]]
 name = "redox-path"
-version = "0.1.1"
+version = "0.2.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f45c7275fe1467ea17542c01766ae9872fa98aa7fe06ba5ea6595f7a9f0699c6"
+checksum = "64072665120942deff5fd5425d6c1811b854f4939e7f1c01ce755f64432bbea7"
 
 [[package]]
 name = "redox_syscall"
diff --git a/Cargo.toml b/Cargo.toml
index 37c9fb26c..3e8e98e16 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -53,7 +53,7 @@ sc = "0.2.3"
 [target.'cfg(target_os = "redox")'.dependencies]
 redox_syscall = "0.4"
 redox-exec = { path = "src/platform/redox/redox-exec" }
-redox-path = "0.1"
+redox-path = "0.2"
 
 [features]
 default = ["check_against_libc_crate"]
diff --git a/src/platform/redox/mod.rs b/src/platform/redox/mod.rs
index 20885fed4..f3f5b1132 100644
--- a/src/platform/redox/mod.rs
+++ b/src/platform/redox/mod.rs
@@ -11,7 +11,7 @@ use crate::{
     header::{
         dirent::dirent,
         errno::{EBADR, EINVAL, EIO, ENOENT, ENOMEM, ENOSYS, EPERM, ERANGE},
-        fcntl,
+        fcntl, limits,
         sys_mman::{MAP_ANONYMOUS, MAP_FAILED, PROT_READ, PROT_WRITE},
         sys_random,
         sys_resource::{rlimit, RLIM_INFINITY},
@@ -822,7 +822,36 @@ impl Pal for Sys {
     }
 
     fn fpath(fildes: c_int, out: &mut [u8]) -> ssize_t {
-        e(syscall::fpath(fildes as usize, out)) as ssize_t
+        // Since this is used by realpath, it converts from the old format to the new one for
+        // compatibility reasons
+        let mut buf = [0; limits::PATH_MAX];
+        let count = match syscall::fpath(fildes as usize, &mut buf) {
+            Ok(ok) => ok,
+            Err(err) => return e(Err(err)) as ssize_t,
+        };
+
+        let redox_path = match str::from_utf8(&buf[..count])
+            .ok()
+            .and_then(|x| redox_path::RedoxPath::from_absolute(x))
+        {
+            Some(some) => some,
+            None => return e(Err(syscall::Error::new(EINVAL))) as ssize_t,
+        };
+
+        let (scheme, reference) = match redox_path.as_parts() {
+            Some(some) => some,
+            None => return e(Err(syscall::Error::new(EINVAL))) as ssize_t,
+        };
+
+        let mut cursor = io::Cursor::new(out);
+        let res = match scheme.as_ref() {
+            "file" => write!(cursor, "/{}", reference.as_ref()),
+            _ => write!(cursor, "/scheme/{}/{}", scheme.as_ref(), reference.as_ref()),
+        };
+        match res {
+            Ok(()) => cursor.position() as ssize_t,
+            Err(_err) => e(Err(syscall::Error::new(syscall::ENAMETOOLONG))) as ssize_t,
+        }
     }
 
     fn readlink(pathname: CStr, out: &mut [u8]) -> ssize_t {
-- 
GitLab