Skip to content

Commit

Permalink
Merge branch 'gvfs-pr2' into gvfs-pr3
Browse files Browse the repository at this point in the history
  • Loading branch information
diqiu50 committed Dec 24, 2024
2 parents 37a46d6 + df48db4 commit 2bd05da
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 74 deletions.
1 change: 1 addition & 0 deletions clients/filesystem-fuse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ libc = "0.2.168"
log = "0.4.22"
tokio = { version = "1.38.0", features = ["full"] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
once_cell = "1.20.2"
123 changes: 59 additions & 64 deletions clients/filesystem-fuse/src/default_raw_filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/
use crate::filesystem::{
FileStat, PathFileSystem, RawFileSystem, INITIAL_FILE_ID, ROOT_DIR_FILE_ID,
FileStat, PathFileSystem, RawFileSystem, Result, INITIAL_FILE_ID, ROOT_DIR_FILE_ID,
ROOT_DIR_PARENT_FILE_ID, ROOT_DIR_PATH,
};
use crate::opened_file::{FileHandle, OpenFileFlags};
Expand Down Expand Up @@ -61,7 +61,7 @@ impl<T: PathFileSystem> DefaultRawFileSystem<T> {
.fetch_add(1, std::sync::atomic::Ordering::SeqCst)
}

async fn get_file_entry(&self, file_id: u64) -> crate::filesystem::Result<FileEntry> {
async fn get_file_entry(&self, file_id: u64) -> Result<FileEntry> {
self.file_entry_manager
.read()
.await
Expand Down Expand Up @@ -97,7 +97,7 @@ impl<T: PathFileSystem> DefaultRawFileSystem<T> {
file_id: u64,
flags: u32,
kind: FileType,
) -> crate::filesystem::Result<FileHandle> {
) -> Result<FileHandle> {
let file_entry = self.get_file_entry(file_id).await?;

let mut opened_file = {
Expand All @@ -121,28 +121,37 @@ impl<T: PathFileSystem> DefaultRawFileSystem<T> {
let file = file.lock().await;
Ok(file.file_handle())
}

async fn remove_file_entry_locked(&self, path: &Path) {
let mut file_manager = self.file_entry_manager.write().await;
file_manager.remove(path);
}

async fn insert_file_entry_locked(&self, parent_file_id: u64, file_id: u64, path: &Path) {
let mut file_manager = self.file_entry_manager.write().await;
file_manager.insert(parent_file_id, file_id, path);
}
}

#[async_trait]
impl<T: PathFileSystem> RawFileSystem for DefaultRawFileSystem<T> {
async fn init(&self) -> crate::filesystem::Result<()> {
async fn init(&self) -> Result<()> {
// init root directory
self.file_entry_manager.write().await.insert(
self.insert_file_entry_locked(
ROOT_DIR_PARENT_FILE_ID,
ROOT_DIR_FILE_ID,
Path::new(ROOT_DIR_PATH),
);
)
.await;
self.fs.init().await
}

async fn get_file_path(&self, file_id: u64) -> String {
let file_entry = self.get_file_entry(file_id).await;
file_entry
.map(|x| x.path.to_string_lossy().to_string())
.unwrap_or_else(|_| "".to_string())
async fn get_file_path(&self, file_id: u64) -> Result<String> {
let file_entry = self.get_file_entry(file_id).await?;
Ok(file_entry.path.to_string_lossy().to_string())
}

async fn valid_file_handle_id(&self, file_id: u64, fh: u64) -> crate::filesystem::Result<()> {
async fn valid_file_handle_id(&self, file_id: u64, fh: u64) -> Result<()> {
let fh_file_id = self
.opened_file_manager
.get(fh)
Expand All @@ -157,7 +166,7 @@ impl<T: PathFileSystem> RawFileSystem for DefaultRawFileSystem<T> {
.ok_or(Errno::from(libc::EBADF))
}

async fn stat(&self, file_id: u64) -> crate::filesystem::Result<FileStat> {
async fn stat(&self, file_id: u64) -> Result<FileStat> {
let file_entry = self.get_file_entry(file_id).await?;
let mut file_stat = self.fs.stat(&file_entry.path).await?;
file_stat.set_file_id(file_entry.parent_file_id, file_entry.file_id);
Expand All @@ -181,21 +190,22 @@ impl<T: PathFileSystem> RawFileSystem for DefaultRawFileSystem<T> {
Ok(file_stat)
}

async fn read_dir(&self, file_id: u64) -> crate::filesystem::Result<Vec<FileStat>> {
async fn read_dir(&self, file_id: u64) -> Result<Vec<FileStat>> {
let file_entry = self.get_file_entry(file_id).await?;
let mut child_filestats = self.fs.read_dir(&file_entry.path).await?;
for file in child_filestats.iter_mut() {
self.resolve_file_id_to_filestat(file, file_id).await;
for file_stat in child_filestats.iter_mut() {
self.resolve_file_id_to_filestat(file_stat, file_stat.file_id)
.await;
}
Ok(child_filestats)
}

async fn open_file(&self, file_id: u64, flags: u32) -> crate::filesystem::Result<FileHandle> {
async fn open_file(&self, file_id: u64, flags: u32) -> Result<FileHandle> {
self.open_file_internal(file_id, flags, FileType::RegularFile)
.await
}

async fn open_dir(&self, file_id: u64, flags: u32) -> crate::filesystem::Result<FileHandle> {
async fn open_dir(&self, file_id: u64, flags: u32) -> Result<FileHandle> {
self.open_file_internal(file_id, flags, FileType::Directory)
.await
}
Expand All @@ -205,84 +215,70 @@ impl<T: PathFileSystem> RawFileSystem for DefaultRawFileSystem<T> {
parent_file_id: u64,
name: &OsStr,
flags: u32,
) -> crate::filesystem::Result<FileHandle> {
) -> Result<FileHandle> {
let parent_file_entry = self.get_file_entry(parent_file_id).await?;
let path = parent_file_entry.path.join(name);
let mut opened_file = self.fs.create_file(&path, OpenFileFlags(flags)).await?;
let mut file_without_id = self
.fs
.create_file(&parent_file_entry.path.join(name), OpenFileFlags(flags))
.await?;

opened_file.set_file_id(parent_file_id, self.next_file_id());
file_without_id.set_file_id(parent_file_id, self.next_file_id());

// insert the new file to file entry manager
{
let mut file_manager = self.file_entry_manager.write().await;
file_manager.insert(
parent_file_id,
opened_file.file_stat.file_id,
&opened_file.file_stat.path,
);
}

// put the file to the opened file manager
let opened_file = self.opened_file_manager.put(opened_file);
let opened_file = opened_file.lock().await;
Ok(opened_file.file_handle())
self.insert_file_entry_locked(
parent_file_id,
file_without_id.file_stat.file_id,
&file_without_id.file_stat.path,
)
.await;

// put the openfile to the opened file manager and allocate a file handle id
let file_with_id = self.opened_file_manager.put(file_without_id);
let opened_file_with_file_handle_id = file_with_id.lock().await;
Ok(opened_file_with_file_handle_id.file_handle())
}

async fn create_dir(
&self,
parent_file_id: u64,
name: &OsStr,
) -> crate::filesystem::Result<u64> {
async fn create_dir(&self, parent_file_id: u64, name: &OsStr) -> Result<u64> {
let parent_file_entry = self.get_file_entry(parent_file_id).await?;
let path = parent_file_entry.path.join(name);
let mut filestat = self.fs.create_dir(&path).await?;

filestat.set_file_id(parent_file_id, self.next_file_id());

// insert the new file to file entry manager
{
let mut file_manager = self.file_entry_manager.write().await;
file_manager.insert(filestat.parent_file_id, filestat.file_id, &filestat.path);
}
self.insert_file_entry_locked(parent_file_id, filestat.file_id, &filestat.path)
.await;
Ok(filestat.file_id)
}

async fn set_attr(&self, file_id: u64, file_stat: &FileStat) -> crate::filesystem::Result<()> {
async fn set_attr(&self, file_id: u64, file_stat: &FileStat) -> Result<()> {
let file_entry = self.get_file_entry(file_id).await?;
self.fs.set_attr(&file_entry.path, file_stat, true).await
}

async fn remove_file(
&self,
parent_file_id: u64,
name: &OsStr,
) -> crate::filesystem::Result<()> {
async fn remove_file(&self, parent_file_id: u64, name: &OsStr) -> Result<()> {
let parent_file_entry = self.get_file_entry(parent_file_id).await?;
let path = parent_file_entry.path.join(name);
self.fs.remove_file(&path).await?;

// remove the file from file entry manager
{
let mut file_manager = self.file_entry_manager.write().await;
file_manager.remove(&path);
}
self.remove_file_entry_locked(&parent_file_entry.path.join(name))
.await;
Ok(())
}

async fn remove_dir(&self, parent_file_id: u64, name: &OsStr) -> crate::filesystem::Result<()> {
async fn remove_dir(&self, parent_file_id: u64, name: &OsStr) -> Result<()> {
let parent_file_entry = self.get_file_entry(parent_file_id).await?;
let path = parent_file_entry.path.join(name);
self.fs.remove_dir(&path).await?;

// remove the dir from file entry manager
{
let mut file_manager = self.file_entry_manager.write().await;
file_manager.remove(&path);
}
self.remove_file_entry_locked(&parent_file_entry.path.join(name))
.await;
Ok(())
}

async fn close_file(&self, _file_id: u64, fh: u64) -> crate::filesystem::Result<()> {
async fn close_file(&self, _file_id: u64, fh: u64) -> Result<()> {
let opened_file = self
.opened_file_manager
.remove(fh)
Expand All @@ -298,15 +294,14 @@ impl<T: PathFileSystem> RawFileSystem for DefaultRawFileSystem<T> {
offset: u64,
size: u32,
) -> crate::filesystem::Result<Bytes> {
let file_stat: FileStat;
let data = {
let (data, file_stat) = {
let opened_file = self
.opened_file_manager
.get(fh)
.ok_or(Errno::from(libc::EBADF))?;
let mut opened_file = opened_file.lock().await;
file_stat = opened_file.file_stat.clone();
opened_file.read(offset, size).await
let data = opened_file.read(offset, size).await;
(data, opened_file.file_stat.clone())
};

// update the file atime
Expand Down
2 changes: 1 addition & 1 deletion clients/filesystem-fuse/src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub(crate) trait RawFileSystem: Send + Sync {
async fn init(&self) -> Result<()>;

/// Get the file path by file id, if the file id is valid, return the file path
async fn get_file_path(&self, file_id: u64) -> String;
async fn get_file_path(&self, file_id: u64) -> Result<String>;

/// Validate the file id and file handle, if file id and file handle is valid and it associated, return Ok
async fn valid_file_handle_id(&self, file_id: u64, fh: u64) -> Result<()>;
Expand Down
4 changes: 0 additions & 4 deletions clients/filesystem-fuse/src/fuse_api_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ impl<T: RawFileSystem> FuseApiHandle<T> {
}
}

pub async fn get_file_path(&self, file_id: u64) -> String {
self.fs.get_file_path(file_id).await
}

async fn get_modified_file_stat(
&self,
file_id: u64,
Expand Down
5 changes: 4 additions & 1 deletion clients/filesystem-fuse/src/memory_filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ impl PathFileSystem for MemoryFileSystem {
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);
self.file_map
.write()
.unwrap()
.insert(meta_file_path, meta_file);
Ok(())
}

Expand Down
5 changes: 3 additions & 2 deletions clients/filesystem-fuse/src/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ use crate::fuse_server::FuseServer;
use crate::memory_filesystem::MemoryFileSystem;
use fuse3::raw::Filesystem;
use log::info;
use std::sync::{Arc, LazyLock};
use once_cell::sync::Lazy;
use std::sync::Arc;
use tokio::sync::Mutex;

static SERVER: LazyLock<Mutex<Option<Arc<FuseServer>>>> = LazyLock::new(|| Mutex::new(None));
static SERVER: Lazy<Mutex<Option<Arc<FuseServer>>>> = Lazy::new(|| Mutex::new(None));

pub async fn mount(mount_point: &str) -> fuse3::Result<()> {
info!("Starting gvfs-fuse server...");
Expand Down
1 change: 1 addition & 0 deletions clients/filesystem-fuse/src/opened_file_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl OpenedFileManager {
}

pub(crate) fn put(&self, mut file: OpenedFile) -> Arc<Mutex<OpenedFile>> {
// Put the file into the file handle map, and allocate a file handle id for the file.
let file_handle_id = self.next_handle_id();
file.handle_id = file_handle_id;
let file_handle = Arc::new(Mutex::new(file));
Expand Down
2 changes: 0 additions & 2 deletions clients/filesystem-fuse/tests/fuse_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ fn test_fuse_filesystem(mount_point: &str) {
assert!(!file_exists(&test_dir));

info!("Success test");

sleep(Duration::from_secs(15));
}

fn file_exists<P: AsRef<Path>>(path: P) -> bool {
Expand Down

0 comments on commit 2bd05da

Please sign in to comment.