fuse3_opendal/file.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::ffi::OsString;
19use std::sync::Arc;
20
21use fuse3::Errno;
22use opendal::Writer;
23use tokio::sync::Mutex;
24
25/// Opened file represents file that opened in memory.
26///
27/// # FIXME
28///
29/// We should remove the `pub` filed to avoid unexpected changes.
30pub struct OpenedFile {
31 pub path: OsString,
32 pub is_read: bool,
33 pub inner_writer: Option<Arc<Mutex<InnerWriter>>>,
34}
35
36/// # FIXME
37///
38/// We need better naming and API for this struct.
39pub struct InnerWriter {
40 pub writer: Writer,
41 pub written: u64,
42}
43
44/// File key is the key of opened file.
45///
46/// # FIXME
47///
48/// We should remove the `pub` filed to avoid unexpected changes.
49#[derive(Debug, Clone, Copy)]
50pub struct FileKey(pub usize);
51
52impl TryFrom<u64> for FileKey {
53 type Error = Errno;
54
55 fn try_from(value: u64) -> std::result::Result<Self, Self::Error> {
56 match value {
57 0 => Err(Errno::from(libc::EBADF)),
58 _ => Ok(FileKey(value as usize - 1)),
59 }
60 }
61}
62
63impl FileKey {
64 pub fn to_fh(self) -> u64 {
65 self.0 as u64 + 1 // ensure fh is not 0
66 }
67}