添加初始提交

1. 编译已经通过
2. 添加好了rpc客户端的基本实现
3. 添加好了fuser的架子,内容等待实现
This commit is contained in:
2024-07-16 07:55:44 +08:00
parent 71505636ec
commit 24573524a6
8 changed files with 1599 additions and 0 deletions

19
src/client.rs Executable file
View File

@ -0,0 +1,19 @@
use lws_client::LwsVfsIns;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut lws_ins = match LwsVfsIns::new("config.json").await{
Ok(ins) => ins,
Err(e) => {
println!("Error creating lws server instance: {:?}", e);
return Err(e);
}
};
match lws_ins.hello().await{
Err(e) => {
println!("lws client instance hello err {:?}", e);
return Err(e);
},
_ =>{},
}
LwsVfsIns::mount(lws_ins)
}

109
src/lib.rs Normal file
View File

@ -0,0 +1,109 @@
use lws_client::HelloRequest;
use std::{io::Read as _};
use std::fs::File;
use std::error::Error;
use std::collections::HashMap;
use serde_json::{self, Value};
use lws_client::lws_vfs_client::LwsVfsClient;
use std::os::raw::c_int;
use fuser::{
KernelConfig, FileAttr, FileType, Filesystem, MountOption, ReplyAttr, ReplyData, ReplyDirectory, ReplyEntry,
Request,
};
pub mod lws_client {
tonic::include_proto!("lws_vfs"); //导入lws vfs proto buffer
}
#[derive(Debug, Default)]
pub struct Config {
port: u16,
addr: String,
mount_map: HashMap<String, String>,
}
impl Config {
pub fn new(json: &str) -> Result<Config, Box<dyn Error>> {
let mut file = File::open(json)?;
let mut buffer = String::new();
file.read_to_string(&mut buffer)?;
let json: Value = serde_json::from_str(buffer.as_ref())?;
let port: u16 = match json.get("port") {
Some(port) => port.as_u64().expect("expect port is a number but its not") as u16,
None => 5001,
};
let addr: String = match json.get("addr") {
Some(addr) => addr.as_str().expect("expect addr is a String but its not").to_string(),
None => "localhost".to_string(),
};
let mounts = match json.get("mount") {
Some(mounts) => mounts.as_object().unwrap(),
None => {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
"no mount map",
)))
}
};
let mount_map = mounts
.iter()
.map(|(key, value)| {
(
key.as_str().to_string(),
value.as_str().unwrap().to_string(),
)
})
.collect();
Ok(Config { port, addr, mount_map })
}
pub fn get_port(&self) -> u16 {
self.port
}
pub fn get_addr(&self) -> &String {
&self.addr
}
}
pub struct LwsVfsIns {
pub config: Config,
rpc: LwsVfsClient<tonic::transport::Channel>
}
impl Filesystem for LwsVfsIns {
fn init(&mut self, _req: &Request, #[allow(unused_variables)] config: &mut KernelConfig,
) -> Result<(), c_int> {
Ok(())
}
}
impl LwsVfsIns {
pub async fn new(json: &str) -> Result<LwsVfsIns, Box<dyn Error>> {
let config = Config::new(json)?;
let addr = format!("http://{}:{}", config.get_addr(), config.get_port());
//connect to server
let rpc = LwsVfsClient::connect(addr).await?;
Ok(LwsVfsIns {
config,
rpc,
})
}
pub async fn hello(&mut self) -> Result<(), Box<dyn Error>> {
let request = tonic::Request::new(HelloRequest {
name: "Ekko lws hello".into(),
});
let response = &self.rpc.say_hello(request).await?;
println!("RESPONSE={:?}", response);
Ok(())
}
pub fn mount<FS:Filesystem>(file_system:FS) -> Result<(), Box<dyn Error>> {
let mountpoint = "/mnt";
let options = vec![MountOption::RO, MountOption::FSName("lws_fs".to_string())];
fuser::mount2(file_system, mountpoint, &options).unwrap();
Ok(())
}
}

3
src/main.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}