opendal/services/fs/
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::path::Path;
19use std::path::PathBuf;
20use std::sync::Arc;
21
22use uuid::Uuid;
23
24use crate::raw::*;
25use crate::*;
26
27#[derive(Debug)]
28pub struct FsCore {
29    pub info: Arc<AccessorInfo>,
30    pub root: PathBuf,
31    pub atomic_write_dir: Option<PathBuf>,
32    pub buf_pool: oio::PooledBuf,
33}
34
35impl FsCore {
36    // Build write path and ensure the parent dirs created
37    pub async fn ensure_write_abs_path(&self, parent: &Path, path: &str) -> Result<PathBuf> {
38        let p = parent.join(path);
39
40        // Create dir before write path.
41        //
42        // TODO(xuanwo): There are many works to do here:
43        //   - Is it safe to create dir concurrently?
44        //   - Do we need to extract this logic as new util functions?
45        //   - Is it better to check the parent dir exists before call mkdir?
46        let parent = PathBuf::from(&p)
47            .parent()
48            .ok_or_else(|| {
49                Error::new(
50                    ErrorKind::Unexpected,
51                    "path should have parent but not, it must be malformed",
52                )
53                .with_context("input", p.to_string_lossy())
54            })?
55            .to_path_buf();
56
57        tokio::fs::create_dir_all(&parent)
58            .await
59            .map_err(new_std_io_error)?;
60
61        Ok(p)
62    }
63}
64
65#[inline]
66pub fn tmp_file_of(path: &str) -> String {
67    let name = get_basename(path);
68    let uuid = Uuid::new_v4().to_string();
69
70    format!("{name}.{uuid}")
71}