This is an automated email from the ASF dual-hosted git repository.

Xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git


The following commit(s) were added to refs/heads/main by this push:
     new 50193a1090 fix(services/compfs,monoiofs): make URI roots absolute 
(#9044)
50193a1090 is described below

commit 50193a1090caed3fe9b2622916dde4d775c1331e
Author: mola <[email protected]>
AuthorDate: Wed Nov 25 12:53:47 2026 +0800

    fix(services/compfs,monoiofs): make URI roots absolute (#9044)
    
    `compfs:///data` and `monoiofs:///data` set the root to `data`, which
    resolves against the process working directory instead of `/data`. fs
    and hf3fs already keep the leading `/`. Keep it in compfs and monoiofs
    too, and keep Windows drive paths such as `compfs:///C:/data` as is.
    
    Co-authored-by: Mola-maker <[email protected]>
---
 core/services/compfs/src/config.rs   | 41 ++++++++++++++++++++++++++++++++++--
 core/services/monoiofs/src/config.rs |  4 ++--
 2 files changed, 41 insertions(+), 4 deletions(-)

diff --git a/core/services/compfs/src/config.rs 
b/core/services/compfs/src/config.rs
index b54145dcae..275048e50d 100644
--- a/core/services/compfs/src/config.rs
+++ b/core/services/compfs/src/config.rs
@@ -38,7 +38,7 @@ impl opendal_core::Configurator for CompfsConfig {
         if let Some(root) = uri.root()
             && !root.is_empty()
         {
-            map.insert("root".to_string(), root.to_string());
+            map.insert("root".to_string(), uri_path_to_root(root));
         }
 
         Self::from_iter(map)
@@ -49,6 +49,29 @@ impl opendal_core::Configurator for CompfsConfig {
     }
 }
 
+/// Turn the path of a `compfs:///` uri, without its leading `/`, into an
+/// absolute root.
+///
+/// On Windows, `compfs:///C:/data` carries the drive in its path, and
+/// `/C:/data` is not a valid path, so keep `C:/data` as is.
+fn uri_path_to_root(path: &str) -> String {
+    let bytes = path.as_bytes();
+    let has_drive = bytes.len() >= 2
+        && bytes[0].is_ascii_alphabetic()
+        && bytes[1] == b':'
+        && (bytes.len() == 2 || bytes[2] == b'/');
+    if cfg!(windows) && has_drive {
+        // A bare `C:` means the current directory of drive C, not its root.
+        if bytes.len() == 2 {
+            format!("{path}/")
+        } else {
+            path.to_string()
+        }
+    } else {
+        format!("/{path}")
+    }
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;
@@ -60,6 +83,20 @@ mod tests {
         let uri = OperatorUri::new("compfs:///workdir", Vec::<(String, 
String)>::new()).unwrap();
 
         let cfg = CompfsConfig::from_uri(&uri).unwrap();
-        assert_eq!(cfg.root.as_deref(), Some("workdir"));
+        assert_eq!(cfg.root.as_deref(), Some("/workdir"));
+    }
+
+    #[test]
+    fn from_uri_with_windows_drive() {
+        let uri = OperatorUri::new("compfs:///C:/data", Vec::<(String, 
String)>::new()).unwrap();
+
+        let cfg = CompfsConfig::from_uri(&uri).unwrap();
+        let expected = if cfg!(windows) { "C:/data" } else { "/C:/data" };
+        assert_eq!(cfg.root.as_deref(), Some(expected));
+
+        let uri = OperatorUri::new("compfs:///C:/", Vec::<(String, 
String)>::new()).unwrap();
+        let cfg = CompfsConfig::from_uri(&uri).unwrap();
+        let expected = if cfg!(windows) { "C:/" } else { "/C:" };
+        assert_eq!(cfg.root.as_deref(), Some(expected));
     }
 }
diff --git a/core/services/monoiofs/src/config.rs 
b/core/services/monoiofs/src/config.rs
index 592aa4e8f6..6cb6765d12 100644
--- a/core/services/monoiofs/src/config.rs
+++ b/core/services/monoiofs/src/config.rs
@@ -43,7 +43,7 @@ impl Configurator for MonoiofsConfig {
         if let Some(root) = uri.root()
             && !root.is_empty()
         {
-            map.insert("root".to_string(), root.to_string());
+            map.insert("root".to_string(), format!("/{root}"));
         }
 
         Self::from_iter(map)
@@ -63,7 +63,7 @@ mod tests {
         let uri = OperatorUri::new("monoiofs:///tmp", Vec::<(String, 
String)>::new())?;
 
         let cfg = MonoiofsConfig::from_uri(&uri)?;
-        assert_eq!(cfg.root.as_deref(), Some("tmp"));
+        assert_eq!(cfg.root.as_deref(), Some("/tmp"));
         Ok(())
     }
 }

Reply via email to