1. 添加处理rpc返回值的宏,将其转化为Result类型方便处理,将rpc过来ret值如实返还给sys

This commit is contained in:
Ekko.bao 2024-12-05 08:21:08 +08:00
parent b60feeb7ed
commit 94c9751c00

View File

@ -7,10 +7,9 @@ use lws_client::{
use serde_json::{self, Value};
use std::error::Error;
use std::ffi::OsStr;
use std::fs::File;
use std::io::{Error as IoError, ErrorKind, Read as IoRead};
use std::thread::{self, JoinHandle};
use std::{env, path, vec};
use std::thread::{self};
use std::{env, vec};
use tokio::runtime::{Builder, Runtime};
use tonic::transport::Channel as RpcChannel;
extern crate log;
@ -432,6 +431,14 @@ pub struct LwsVfsIns {
cache: FileAttrCacheManager,
}
macro_rules! ret2result {
($ret:expr) => {
match $ret {
0 => Ok(()),
_ => Err($ret),
}
};
}
fn mode_to_filetype(mode: libc::mode_t) -> FileType {
match mode & libc::S_IFMT {
libc::S_IFDIR => FileType::Directory,
@ -541,8 +548,8 @@ impl FilesystemMT for LwsVfsIns {
};
log::trace!("get resp: {:?}", resp);
if resp.ret != 0 {
self.cache.set(&path, Err(libc::ENOENT));
return Err(libc::ENOENT);
self.cache.set(&path, Err(resp.ret));
return Err(resp.ret);
}
let attr = file_attr(&resp.stat.unwrap(), &req);
self.cache.set(&path, Ok(attr.clone()));
@ -606,10 +613,7 @@ impl FilesystemMT for LwsVfsIns {
}
};
log::trace!("get resp: {:?}", resp);
if resp.ret != 0 {
return Err(libc::ENOMSG);
}
Ok(())
ret2result!(resp.ret)
}
/// Set timestamps of a filesystem entry.
@ -666,10 +670,7 @@ impl FilesystemMT for LwsVfsIns {
}
};
log::trace!("get resp: {:?}", resp);
if resp.ret != 0 {
return Err(libc::ENOMSG);
}
Ok(())
ret2result!(resp.ret)
}
// END OF SETATTR FUNCTIONS
@ -732,7 +733,7 @@ impl FilesystemMT for LwsVfsIns {
};
log::trace!("get resp: {:?}", resp);
if resp.ret != 0 {
return Err(libc::ENOMSG);
return Err(resp.ret)
}
self.getattr(req, Path::new(&path), None)
}
@ -768,10 +769,7 @@ impl FilesystemMT for LwsVfsIns {
};
self.cache.clr(vec![path]);
log::trace!("get resp: {:?}", resp);
if resp.ret != 0 {
return Err(libc::ENOMSG);
}
Ok(())
ret2result!(resp.ret)
}
/// Remove a directory.
@ -807,10 +805,7 @@ impl FilesystemMT for LwsVfsIns {
}
};
log::trace!("get resp: {:?}", resp);
if resp.ret != 0 {
return Err(libc::ENOMSG);
}
Ok(())
ret2result!(resp.ret)
}
/// Create a symbolic link.
@ -870,10 +865,7 @@ impl FilesystemMT for LwsVfsIns {
}
};
log::trace!("get resp: {:?}", resp);
if resp.ret != 0 {
return Err(libc::ENOMSG);
}
Ok(())
ret2result!(resp.ret)
}
/// Create a hard link.
@ -929,7 +921,7 @@ impl FilesystemMT for LwsVfsIns {
};
log::trace!("get resp: {:?}", resp);
if resp.ret != 0 {
return Err(libc::ENOMSG);
return Err(resp.ret)
}
let fi = resp.fi.unwrap();
Ok((fi.fh, fi.flags))
@ -989,7 +981,7 @@ impl FilesystemMT for LwsVfsIns {
};
log::trace!("get fread resp size is: {}", resp.size);
if resp.ret != 0 {
return callback(Err(libc::ENOMSG));
return callback(Err(resp.ret));
}
callback(Ok(&resp.buff))
}
@ -1044,7 +1036,7 @@ impl FilesystemMT for LwsVfsIns {
};
log::trace!("get resp: {:?}", resp);
if resp.ret != 0 {
return Err(libc::ENOMSG);
return Err(resp.ret)
}
Ok(resp.size as u32)
}
@ -1089,10 +1081,7 @@ impl FilesystemMT for LwsVfsIns {
}
};
log::trace!("get resp: {:?}", resp);
if resp.ret != 0 {
return Err(libc::ENOMSG);
}
Ok(())
ret2result!(resp.ret)
}
/// Called when an open file is closed.
@ -1145,11 +1134,8 @@ impl FilesystemMT for LwsVfsIns {
}
};
log::trace!("get resp: {:?}", resp);
if resp.ret != 0 {
return Err(libc::ENOMSG);
}
ret2result!(resp.ret)
// self.cache.clr(vec![path]);
Ok(())
}
/// Write out any pending changes of a file.
@ -1203,7 +1189,7 @@ impl FilesystemMT for LwsVfsIns {
};
log::trace!("get resp: {:?}", resp);
if resp.ret != 0 {
return Err(libc::ENOMSG);
return Err(resp.ret);
}
Ok((resp.fi.unwrap().fh, flags))
}
@ -1268,7 +1254,7 @@ impl FilesystemMT for LwsVfsIns {
};
log::trace!("get resp: {:?}", resp);
if resp.ret != 0 {
return Err(libc::ENOMSG);
return Err(resp.ret)
}
let mut dirs = vec![];
// let clrs: Vec<String> = resp.dirs.iter().map(|dir| dir.name.clone().into()).collect();
@ -1321,10 +1307,7 @@ impl FilesystemMT for LwsVfsIns {
}
};
log::trace!("get resp: {:?}", resp);
if resp.ret != 0 {
return Err(libc::ENOMSG);
}
Ok(())
ret2result!(resp.ret)
}
/// Write out any pending changes to a directory.
@ -1444,10 +1427,7 @@ impl FilesystemMT for LwsVfsIns {
}
};
log::trace!("get resp: {:?}", resp);
if resp.ret != 0 {
return Err(libc::EACCES);
}
Ok(())
ret2result!(resp.ret)
}
/// Create and open a new file.
@ -1501,15 +1481,17 @@ impl FilesystemMT for LwsVfsIns {
}
self.cache.clr(vec![path.to_string()]);
let fi = resp.fi.unwrap();
if let Ok((ttl, attr)) = self.getattr(req, Path::new(&path), Some(fi.fh)) {
return Ok(CreatedEntry {
fh: fi.fh,
flags,
attr,
ttl,
});
match self.getattr(req, Path::new(&path), Some(fi.fh)) {
Ok((ttl, attr)) => {
Ok(CreatedEntry {
fh: fi.fh,
flags,
attr,
ttl,
})
},
Err(e) => Err(e),
}
Err(libc::ENOMSG)
}
}
@ -1594,7 +1576,15 @@ impl LwsVfsIns {
where
F: FilesystemMT + Sync + Send + 'static,
{
let fuse_args = [OsStr::new("-o"), OsStr::new("fsname=lws_vfs")];
let user = match env::var("USER") {
Ok(user) => user,
Err(_) => {
log::warn!("Can not get user name, use 'unknown'");
"unknown".to_string()
},
};
let name = format!("fsname=lws_vfs@{}", user);
let fuse_args = [OsStr::new("-o"), OsStr::new(name.as_str())];
fuse_mt::mount(
fuse_mt::FuseMT::new(file_system, 10),
&mount_point,