Skip to main content

opendal_service_compfs/
core.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::future::Future;
19use std::path::Path;
20use std::path::PathBuf;
21
22use compio::buf::{IoBuf, IoVectoredBuf};
23use compio::dispatcher::Dispatcher;
24
25use opendal_core::raw::*;
26use opendal_core::*;
27
28// Wrapper type to avoid orphan rules
29#[derive(Debug, Clone)]
30pub struct CompfsBuffer(pub Vec<compio::bytes::Bytes>);
31
32impl IoBuf for CompfsBuffer {
33    fn as_init(&self) -> &[u8] {
34        self.0.first().map_or(&[], |b| b.as_ref())
35    }
36}
37
38impl From<CompfsBuffer> for opendal_core::Buffer {
39    fn from(buf: CompfsBuffer) -> Self {
40        buf.0.into()
41    }
42}
43
44impl From<opendal_core::Buffer> for CompfsBuffer {
45    fn from(mut buf: opendal_core::Buffer) -> Self {
46        Self(buf.by_ref().collect())
47    }
48}
49
50impl IoVectoredBuf for CompfsBuffer {
51    fn iter_slice(&self) -> impl Iterator<Item = &[u8]> {
52        self.0.iter().map(|b| b.as_ref())
53    }
54}
55
56#[derive(Debug)]
57pub(super) struct CompfsCore {
58    pub info: ServiceInfo,
59    pub capability: Capability,
60
61    pub root: PathBuf,
62    pub dispatcher: Dispatcher,
63    pub buf_pool: oio::PooledBuf,
64}
65
66impl CompfsCore {
67    /// Reject `..` traversal in a key so it cannot escape `root`, matching the
68    /// `fs` backend (#7684). Confinement lives here because `normalize_path`
69    /// deliberately leaves `.`/`..` unresolved (RFC 0112).
70    pub fn prepare_path(&self, path: &str) -> Result<PathBuf> {
71        use std::path::Component;
72        let trimmed = path.trim_end_matches('/');
73        if Path::new(trimmed)
74            .components()
75            .any(|c| matches!(c, Component::ParentDir))
76        {
77            return Err(Error::new(
78                ErrorKind::NotFound,
79                "path escapes the configured root via `..`",
80            )
81            .with_context("path", path));
82        }
83        Ok(self.root.join(trimmed))
84    }
85
86    pub async fn exec<Fn, Fut, R>(&self, f: Fn) -> opendal_core::Result<R>
87    where
88        Fn: FnOnce() -> Fut + Send + 'static,
89        Fut: Future<Output = std::io::Result<R>> + 'static,
90        R: Send + 'static,
91    {
92        self.dispatcher
93            .dispatch(f)
94            .map_err(|_| Error::new(ErrorKind::Unexpected, "compio spawn io task failed"))?
95            .await
96            .map_err(|_| Error::new(ErrorKind::Unexpected, "compio task cancelled"))?
97            .map_err(new_std_io_error)
98    }
99
100    pub async fn exec_blocking<Fn, R>(&self, f: Fn) -> Result<R>
101    where
102        Fn: FnOnce() -> R + Send + 'static,
103        R: Send + 'static,
104    {
105        self.dispatcher
106            .dispatch_blocking(f)
107            .map_err(|_| Error::new(ErrorKind::Unexpected, "compio spawn blocking task failed"))?
108            .await
109            .map_err(|_| Error::new(ErrorKind::Unexpected, "compio task cancelled"))
110    }
111}
112
113#[cfg(test)]
114mod tests {
115    use bytes::Bytes;
116    use rand::{RngExt, rng};
117
118    use super::*;
119
120    fn setup_buffer() -> (CompfsBuffer, usize, Bytes) {
121        let mut rng = rng();
122
123        let bs = (0..100)
124            .map(|_| {
125                let len = rng.random_range(1..100);
126                let mut buf = vec![0; len];
127                rng.fill(&mut buf[..]);
128                Bytes::from(buf)
129            })
130            .collect::<Vec<_>>();
131
132        let total_size = bs.iter().map(|b| b.len()).sum::<usize>();
133        let total_content = bs.iter().flatten().copied().collect::<Bytes>();
134        let buf = Buffer::from(bs);
135
136        (CompfsBuffer::from(buf), total_size, total_content)
137    }
138
139    #[test]
140    fn test_io_buf() {
141        let (buf, _len, _bytes) = setup_buffer();
142        let slice = IoBuf::as_init(&buf);
143
144        assert_eq!(slice, buf.0.first().unwrap().as_ref())
145    }
146
147    #[test]
148    fn test_io_vectored_buf() {
149        let (buf, len, bytes) = setup_buffer();
150        let collected = buf.iter_slice().flatten().copied().collect::<Bytes>();
151
152        assert_eq!(buf.total_len(), len);
153        assert_eq!(collected, bytes);
154    }
155
156    fn new_test_core() -> CompfsCore {
157        CompfsCore {
158            info: ServiceInfo::new("compfs", "", ""),
159            capability: Capability::default(),
160            root: PathBuf::from("/data/root"),
161            dispatcher: Dispatcher::new().unwrap(),
162            buf_pool: oio::PooledBuf::new(16),
163        }
164    }
165
166    #[test]
167    fn test_prepare_path_rejects_parent_dir() {
168        let core = new_test_core();
169        for key in ["../etc/passwd", "../../etc/passwd", "a/../../b", "a/.."] {
170            let err = core.prepare_path(key).unwrap_err();
171            assert_eq!(
172                err.kind(),
173                ErrorKind::NotFound,
174                "key should be rejected: {key}"
175            );
176        }
177    }
178
179    #[test]
180    fn test_prepare_path_allows_normal_keys() {
181        let core = new_test_core();
182        // Normal keys, `.` (CurDir), and trailing slashes resolve unchanged.
183        assert_eq!(
184            core.prepare_path("a/b.txt").unwrap(),
185            PathBuf::from("/data/root/a/b.txt")
186        );
187        assert_eq!(
188            core.prepare_path("a/b/").unwrap(),
189            PathBuf::from("/data/root/a/b")
190        );
191        assert_eq!(
192            core.prepare_path("./a/b").unwrap(),
193            PathBuf::from("/data/root/a/b")
194        );
195        // A key containing `..` only as a substring of a name is not a traversal.
196        assert_eq!(
197            core.prepare_path("a..b").unwrap(),
198            PathBuf::from("/data/root/a..b")
199        );
200    }
201}