Skip to content

Commit a9db9a6

Browse files
committed
fix error in the SimpleFilesystem
1 parent 8f76564 commit a9db9a6

8 files changed

+287
-153
lines changed

clients/filesystem-fuse/src/filesystem.rs

+190-97
Large diffs are not rendered by default.

clients/filesystem-fuse/src/filesystem_metadata.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* under the License.
1818
*/
1919
use crate::filesystem::{FileStat, Result};
20-
use std::collections::HashMap;
2120
use crate::utils::join_file_path;
21+
use std::collections::HashMap;
2222

2323
/// DefaultFileSystemMetadata is a simple implementation of FileSystemMetadata
2424
/// that stores file metadata in memory.
@@ -35,7 +35,7 @@ pub struct DefaultFileSystemMetadata {
3535

3636
impl DefaultFileSystemMetadata {
3737
pub const ROOT_DIR_PARENT_FILE_ID: u64 = 0;
38-
pub const ROOT_DIR_PARENT_FILE_NAME: &'static str = "";
38+
pub const ROOT_DIR_NAME: &'static str = "";
3939
pub const ROOT_DIR_FILE_ID: u64 = 1;
4040
pub const FS_META_FILE_NAME: &'static str = ".gvfs_meta";
4141

clients/filesystem-fuse/src/fuse_api_handle.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,7 @@ impl<T: RawFileSystem> Filesystem for FuseApiHandle<T> {
206206
flags: u32,
207207
) -> fuse3::Result<ReplyWrite> {
208208
let mut written = self.local_fs.write(inode, fh, offset, data).await?;
209-
Ok(ReplyWrite {
210-
written: written,
211-
})
209+
Ok(ReplyWrite { written: written })
212210
}
213211

214212
async fn statfs(&self, req: Request, inode: Inode) -> fuse3::Result<ReplyStatFs> {

clients/filesystem-fuse/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
mod opened_file_manager;
2019
mod filesystem;
2120
mod filesystem_metadata;
2221
mod fuse_api_handle;
2322
pub mod fuse_server;
2423
mod memory_filesystem;
24+
mod opened_file_manager;
2525
mod utils;

clients/filesystem-fuse/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
mod opened_file_manager;
2019
mod filesystem;
2120
mod filesystem_metadata;
2221
mod fuse_api_handle;
2322
mod fuse_server;
2423
mod memory_filesystem;
24+
mod opened_file_manager;
2525
mod utils;
2626

2727
use crate::fuse_server::FuseServer;

clients/filesystem-fuse/src/memory_filesystem.rs

+87-45
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,74 @@
1717
* under the License.
1818
*/
1919
use crate::filesystem::{
20-
FileReader, FileStat, FileWriter, OpenedFile, PathFileSystem, Result,
20+
FileReader, FileStat, FileWriter, OpenFileFlags, OpenedFile, PathFileSystem, Result,
2121
};
22+
use crate::filesystem_metadata::DefaultFileSystemMetadata;
23+
use crate::utils::join_file_path;
2224
use async_trait::async_trait;
25+
use bytes::Bytes;
2326
use dashmap::DashMap;
27+
use fuse3::FileType::{Directory, RegularFile};
2428
use fuse3::{Errno, FileType};
2529
use regex::Regex;
2630
use std::collections::BTreeMap;
2731
use std::sync::{Arc, Mutex, RwLock};
28-
use bytes::Bytes;
29-
use crate::utils::join_file_path;
3032

3133
// MemoryFileSystem is a simple in-memory filesystem implementation
3234
// It is used for testing purposes
33-
pub(crate) struct MemoryFileSystem {
34-
// file_map is a map of file stats
35-
file_map: RwLock<BTreeMap<String, FileStat>>,
3635

37-
// file_data_map is a map of file data
38-
file_data_map: DashMap<String, Arc<Mutex<Vec<u8>>>>,
36+
struct MemoryFile {
37+
kind: FileType,
38+
data: Arc<Mutex<Vec<u8>>>,
39+
}
40+
41+
pub(crate) struct MemoryFileSystem {
42+
// file_map is a map of file name to file size
43+
file_map: RwLock<BTreeMap<String, MemoryFile>>,
3944
}
4045

4146
impl MemoryFileSystem {
4247
pub fn new() -> Self {
4348
Self {
4449
file_map: RwLock::new(Default::default()),
45-
file_data_map: Default::default(),
4650
}
4751
}
4852

4953
pub fn init(&self) {}
54+
55+
fn create_file_stat(&self, path: &str, file: &MemoryFile) -> FileStat {
56+
match file.kind {
57+
Directory => FileStat::new_dir_with_path(path),
58+
_ => FileStat::new_file_with_path(path, file.data.lock().unwrap().len() as u64),
59+
}
60+
}
5061
}
5162

5263
#[async_trait]
5364
impl PathFileSystem for MemoryFileSystem {
54-
async fn init(&self) {}
65+
async fn init(&self) {
66+
let root = MemoryFile {
67+
kind: Directory,
68+
data: Arc::new(Mutex::new(Vec::new())),
69+
};
70+
self.file_map.write().unwrap().insert("".to_string(), root);
71+
72+
let meta = MemoryFile {
73+
kind: RegularFile,
74+
data: Arc::new(Mutex::new(Vec::new())),
75+
};
76+
self.file_map.write().unwrap().insert(
77+
DefaultFileSystemMetadata::FS_META_FILE_NAME.to_string(),
78+
meta,
79+
);
80+
}
5581

5682
async fn stat(&self, name: &str) -> Result<FileStat> {
5783
self.file_map
5884
.read()
5985
.unwrap()
6086
.get(name)
61-
.map(|x| x.clone())
87+
.map(|x| self.create_file_stat(name, x))
6288
.ok_or(Errno::from(libc::ENOENT))
6389
}
6490

@@ -72,66 +98,84 @@ impl PathFileSystem for MemoryFileSystem {
7298
let results: Vec<FileStat> = file_map
7399
.iter()
74100
.filter(|x| dir_child_reg_expr(name).is_match(x.0))
75-
.map(|(_, v)| v.clone())
101+
.map(|(k, v)| self.create_file_stat(k, v))
76102
.collect();
77103

78104
Ok(results)
79105
}
80106

81-
async fn open_file(&self, name: &str, flags : u32) -> Result<OpenedFile> {
107+
async fn open_file(&self, name: &str, flags: OpenFileFlags) -> Result<OpenedFile> {
82108
let file_stat = self.stat(name).await?;
83109
let mut file = OpenedFile::new(file_stat.clone());
84110
match file.file_stat.kind {
85-
FileType::Directory => {
86-
Ok(file)
87-
}
88-
FileType::RegularFile => {
89-
let data = self.file_data_map.get(&file.file_stat.path).unwrap().value().clone();
90-
file.reader = Some(Box::new(MemoryFileReader {
91-
data: data.clone(),
92-
}));
93-
file.writer = Some(Box::new(MemoryFileWriter {
94-
data: data,
95-
}));
111+
Directory => Ok(file),
112+
RegularFile => {
113+
let data = self
114+
.file_map
115+
.read()
116+
.unwrap()
117+
.get(&file.file_stat.path)
118+
.unwrap()
119+
.data
120+
.clone();
121+
file.reader = Some(Box::new(MemoryFileReader { data: data.clone() }));
122+
file.writer = Some(Box::new(MemoryFileWriter { data: data }));
96123
Ok(file)
97124
}
98125
_ => Err(Errno::from(libc::EBADF)),
99126
}
100127
}
101128

102-
async fn create_file(&self, parent: &str, name: &str) -> Result<OpenedFile> {
103-
let mut file_map = self.file_map.read().unwrap();
104-
if file_map.contains_key(&join_file_path(parent, name)) {
105-
return Err(Errno::from(libc::EEXIST));
106-
}
129+
async fn create_file(
130+
&self,
131+
parent: &str,
132+
name: &str,
133+
flags: OpenFileFlags,
134+
) -> Result<OpenedFile> {
135+
{
136+
let file_map = self.file_map.read().unwrap();
137+
if file_map.contains_key(&join_file_path(parent, name)) {
138+
return Err(Errno::from(libc::EEXIST));
139+
}
140+
};
107141

108142
let mut file = OpenedFile::new(FileStat::new_file(parent, name, 0));
109143

110-
self.file_data_map
111-
.insert(file.file_stat.path.clone(), Arc::new(Mutex::new(Vec::new())));
112-
let data = self.file_data_map.get(&file.file_stat.path).unwrap().value().clone();
113-
file.reader = Some(Box::new(MemoryFileReader {
114-
data: data.clone(),
115-
}));
116-
file.writer = Some(Box::new(MemoryFileWriter {
117-
data: data,
118-
}));
144+
let data = Arc::new(Mutex::new(Vec::new()));
145+
self.file_map.write().unwrap().insert(
146+
file.file_stat.path.clone(),
147+
MemoryFile {
148+
kind: RegularFile,
149+
data: data.clone(),
150+
},
151+
);
152+
file.reader = Some(Box::new(MemoryFileReader { data: data.clone() }));
153+
file.writer = Some(Box::new(MemoryFileWriter { data: data }));
154+
119155
Ok(file)
120156
}
121157

122158
async fn create_dir(&self, parent: &str, name: &str) -> Result<OpenedFile> {
123-
let mut file_map = self.file_map.read().unwrap();
124-
if file_map.contains_key(&join_file_path(parent, name)) {
125-
return Err(Errno::from(libc::EEXIST));
159+
{
160+
let mut file_map = self.file_map.read().unwrap();
161+
if file_map.contains_key(&join_file_path(parent, name)) {
162+
return Err(Errno::from(libc::EEXIST));
163+
}
126164
}
127165

128166
let file = OpenedFile::new(FileStat::new_dir(parent, name));
167+
self.file_map.write().unwrap().insert(
168+
file.file_stat.path.clone(),
169+
MemoryFile {
170+
kind: Directory,
171+
data: Arc::new(Mutex::new(Vec::new())),
172+
},
173+
);
174+
129175
Ok(file)
130176
}
131177

132178
async fn set_attr(&self, name: &str, file_stat: &FileStat, flush: bool) -> Result<()> {
133-
let mut file_map = self.file_map.write().unwrap();
134-
file_map.insert(name.to_string(), file_stat.clone());
135179
Ok(())
136180
}
137181

@@ -165,7 +209,6 @@ pub(crate) struct MemoryFileReader {
165209

166210
#[async_trait]
167211
impl FileReader for MemoryFileReader {
168-
169212
async fn read(&mut self, offset: u64, size: u32) -> Result<Bytes> {
170213
let v = self.data.lock().unwrap();
171214
let start = offset as usize;
@@ -183,7 +226,6 @@ pub(crate) struct MemoryFileWriter {
183226

184227
#[async_trait]
185228
impl FileWriter for MemoryFileWriter {
186-
187229
async fn write(&mut self, offset: u64, data: &[u8]) -> Result<u32> {
188230
let mut v = self.data.lock().unwrap();
189231
let start = offset as usize;

clients/filesystem-fuse/src/opened_file_manager.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use crate::filesystem::OpenedFile;
2020
use dashmap::DashMap;
2121
use std::sync::atomic::AtomicU64;
22-
use std::sync::{Arc};
22+
use std::sync::Arc;
2323
use tokio::sync::Mutex;
2424

2525
// OpenedFileManager is a manager for opened files.
@@ -54,7 +54,9 @@ impl OpenedFileManager {
5454
}
5555

5656
pub(crate) fn get_file(&self, handle_id: u64) -> Option<Arc<Mutex<OpenedFile>>> {
57-
self.file_handle_map.get(&handle_id).map(|x| x.value().clone())
57+
self.file_handle_map
58+
.get(&handle_id)
59+
.map(|x| x.value().clone())
5860
}
5961

6062
pub(crate) fn remove_file(&self, handle_id: u64) {

clients/filesystem-fuse/src/utils.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
* under the License.
1818
*/
1919

20-
2120
pub fn join_file_path(parent: &str, name: &str) -> String {
2221
if parent.is_empty() {
2322
name.to_string()
2423
} else {
2524
format!("{}/{}", parent, name)
2625
}
27-
}
26+
}

0 commit comments

Comments
 (0)