FANNG1 commented on code in PR #5915: URL: https://github.com/apache/gravitino/pull/5915#discussion_r1897084862
########## clients/filesystem-fuse/src/memory_filesystem.rs: ########## @@ -0,0 +1,250 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +use crate::filesystem::{FileReader, FileStat, FileWriter, PathFileSystem, Result}; +use crate::opened_file::{OpenFileFlags, OpenedFile}; +use async_trait::async_trait; +use bytes::Bytes; +use fuse3::FileType::{Directory, RegularFile}; +use fuse3::{Errno, FileType}; +use std::collections::BTreeMap; +use std::path::{Path, PathBuf}; +use std::sync::{Arc, Mutex, RwLock}; + +// Simple in-memory file implementation of MemoryFileSystem +struct MemoryFile { + kind: FileType, + data: Arc<Mutex<Vec<u8>>>, +} + +// MemoryFileSystem is a simple in-memory filesystem implementation +// It is used for testing purposes +pub struct MemoryFileSystem { + // file_map is a map of file name to file size + file_map: RwLock<BTreeMap<PathBuf, MemoryFile>>, +} + +impl MemoryFileSystem { + const FS_META_FILE_NAME: &'static str = "/.gvfs_meta"; + + pub(crate) async fn new() -> Self { + Self { + file_map: RwLock::new(Default::default()), + } + } + + fn create_file_stat(&self, path: &Path, file: &MemoryFile) -> FileStat { + match file.kind { + Directory => FileStat::new_dir_filestat_with_path(path), + _ => { + FileStat::new_file_filestat_with_path(path, file.data.lock().unwrap().len() as u64) + } + } + } +} + +#[async_trait] +impl PathFileSystem for MemoryFileSystem { + async fn init(&self) -> Result<()> { + let root_file = MemoryFile { + kind: Directory, + data: Arc::new(Mutex::new(Vec::new())), + }; + let root_path = PathBuf::from("/"); + self.file_map.write().unwrap().insert(root_path, root_file); + + let meta_file = MemoryFile { + kind: RegularFile, + data: Arc::new(Mutex::new(Vec::new())), + }; + let meta_file_path = Path::new(Self::FS_META_FILE_NAME).to_path_buf(); + self.file_map + .write() + .unwrap() + .insert(meta_file_path, meta_file); + Ok(()) + } + + async fn stat(&self, path: &Path) -> Result<FileStat> { + self.file_map + .read() + .unwrap() + .get(path) + .map(|x| self.create_file_stat(path, x)) + .ok_or(Errno::from(libc::ENOENT)) + } + + async fn read_dir(&self, path: &Path) -> Result<Vec<FileStat>> { + let file_map = self.file_map.read().unwrap(); + + let results: Vec<FileStat> = file_map + .iter() + .filter(|x| path_in_dir(path, x.0)) Review Comment: please filter out the files in sub directory ########## clients/filesystem-fuse/src/main.rs: ########## @@ -16,69 +16,18 @@ * specific language governing permissions and limitations * under the License. */ -mod default_raw_filesystem; -mod filesystem; -mod fuse_api_handle; -mod opened_file; -mod opened_file_manager; -mod utils; - -use log::debug; +use gvfs_fuse::{gvfs_mount, gvfs_unmount}; use log::info; -use std::process::exit; +use tokio::signal; #[tokio::main] -async fn main() { +async fn main() -> fuse3::Result<()> { tracing_subscriber::fmt().init(); - info!("Starting filesystem..."); - debug!("Shutdown filesystem..."); - exit(0); -} + tokio::spawn(async { gvfs_mount("gvfs").await }); -async fn create_gvfs_fuse_filesystem() { - // Gvfs-fuse filesystem structure: - // FuseApiHandle - // ├─ DefaultRawFileSystem (RawFileSystem) - // │ └─ FileSystemLog (PathFileSystem) - // │ ├─ GravitinoComposedFileSystem (PathFileSystem) - // │ │ ├─ GravitinoFilesetFileSystem (PathFileSystem) - // │ │ │ └─ S3FileSystem (PathFileSystem) - // │ │ │ └─ OpenDALFileSystem (PathFileSystem) - // │ │ ├─ GravitinoFilesetFileSystem (PathFileSystem) - // │ │ │ └─ HDFSFileSystem (PathFileSystem) - // │ │ │ └─ OpenDALFileSystem (PathFileSystem) - // │ │ ├─ GravitinoFilesetFileSystem (PathFileSystem) - // │ │ │ └─ JuiceFileSystem (PathFileSystem) - // │ │ │ └─ NasFileSystem (PathFileSystem) - // │ │ ├─ GravitinoFilesetFileSystem (PathFileSystem) - // │ │ │ └─ XXXFileSystem (PathFileSystem) - // - // `SimpleFileSystem` is a low-level filesystem designed to communicate with FUSE APIs. - // It manages file and directory relationships, as well as file mappings. - // It delegates file operations to the PathFileSystem - // - // `FileSystemLog` is a decorator that adds extra debug logging functionality to file system APIs. - // Similar implementations include permissions, caching, and metrics. - // - // `GravitinoComposeFileSystem` is a composite file system that can combine multiple `GravitinoFilesetFileSystem`. - // It use the part of catalog and schema of fileset path to a find actual GravitinoFilesetFileSystem. delegate the operation to the real storage. - // If the user only mounts a fileset, this layer is not present. There will only be one below layer. - // - // `GravitinoFilesetFileSystem` is a file system that can access a fileset.It translates the fileset path to the real storage path. - // and delegate the operation to the real storage. - // - // `OpenDALFileSystem` is a file system that use the OpenDAL to access real storage. - // it can assess the S3, HDFS, gcs, azblob and other storage. - // - // `S3FileSystem` is a file system that use `OpenDALFileSystem` to access S3 storage. - // - // `HDFSFileSystem` is a file system that use `OpenDALFileSystem` to access HDFS storage. - // - // `NasFileSystem` is a filesystem that uses a locally accessible path mounted by NAS tools, such as JuiceFS. - // - // `JuiceFileSystem` is a file that use `NasFileSystem` to access JuiceFS storage. - // - // `XXXFileSystem is a filesystem that allows you to implement file access through your own extensions. + let _ = signal::ctrl_c().await; + info!("Received Ctrl+C, Unmounting gvfs..."); + gvfs_unmount().await; Review Comment: what will happen if the process crashed without invoking `gvfs_unmount`? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@gravitino.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org