修改fd管理的层级,将其放到fs impl里面去。尚未改完,同步code

This commit is contained in:
Ekko.bao 2024-07-07 14:35:53 +08:00
parent 77ae3e7bac
commit 82960cfe31
3 changed files with 304 additions and 184 deletions

View File

@ -33,6 +33,12 @@ service LwsVfs {
rpc fchown(chown) returns (chown) {}
rpc frelease(release) returns (release) {}
rpc fmkdir(mkdir) returns (mkdir) {}
rpc frmdir(rmdir) returns (rmdir) {}
rpc fflush(flush) returns (flush) {}
rpc fopendir(opendir) returns (opendir) {}
rpc freleasedir(releasedir) returns (releasedir) {}
rpc fcreate (create) returns (create) {}
rpc funlink(unlink) returns (unlink) {}
}
// The request message containing the user's name.
@ -161,3 +167,38 @@ message mkdir {
uint32 mode = 3;
int32 ret = 15;
}
message rmdir {
string path = 1;
int32 ret = 15;
}
message flush {
string path = 1;
file_info fi = 2;
int32 ret = 15;
}
message opendir{
string path = 1;
file_info fi = 2;
int32 ret = 15;
}
message releasedir{
string path = 1;
file_info fi = 2;
int32 ret = 15;
}
message create{
string path = 1;
uint32 mode = 2;
file_info fi = 3;
int32 ret = 15;
}
message unlink{
string path = 1;
int32 ret = 15;
}

View File

@ -1,13 +1,14 @@
use std::fs;
use std::error::Error;
use std::io::{self, Seek as ioSeek};
use std::io::{self, ErrorKind, Seek as ioSeek, SeekFrom};
use crate::lws_vfs::{FileInfo, Fstat};
use std::sync::{Arc, Mutex};
use std::collections::HashMap;
extern crate winapi;
use fs::remove_file;
use io::Write;
use std::os::windows::fs::MetadataExt;
use std::os::windows::prelude::*;
use winapi::um::winnt::FILE_ATTRIBUTE_READONLY;
@ -22,7 +23,56 @@ fn from_metadata(sta: &mut Fstat, metadata: &fs::Metadata) -> Result<(), Box<dyn
sta.fst_atime = metadata.last_access_time();
Ok(())
}
pub fn fgetattr(path: &String, sta: &mut Fstat, _fi: &mut FileInfo) -> Result<i32, Box<dyn Error>> {
#[derive(Debug, Default)]
struct FileFdMgnt {
// key is fd, value is FileHandle
files: HashMap<u64, File>,
curr_fd: u64,
}
impl FileFdMgnt {
pub fn new() -> FileFdMgnt {
FileFdMgnt {
curr_fd: 3,
files: HashMap::new(),
}
}
fn gen_fd(&mut self) -> Result<u64, Box<dyn Error>> {
let refd = self.curr_fd;
loop {
self.curr_fd += 1;
match self.files.get(&self.curr_fd) {
Some(_) => {}
None => {
break;
}
}
if refd == self.curr_fd {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
"no fd",
)));
}
}
Ok(self.curr_fd)
}
pub fn push(&mut self, handle: File) -> Result<u64, Box<dyn Error>> {
let fd = self.gen_fd()?;
self.files.insert(fd, handle);
Ok(fd)
}
pub fn pop(&mut self, fd: u64) -> Option<File> {
self.files.remove(&fd)
}
}
pub struct FSImpl{
file_fds: Arc<Mutex<FileFdMgnt>>,
}
impl FSImpl {
pub fn fgetattr(&self, path: &String, sta: &mut Fstat, _fi: &mut FileInfo) -> Result<i32, Box<dyn Error>> {
let metadata = fs::metadata(path)?;
from_metadata(sta, &metadata)?;
let perm = get_file_mode_mask(path);
@ -30,12 +80,12 @@ pub fn fgetattr(path: &String, sta: &mut Fstat, _fi: &mut FileInfo) -> Result<i3
Ok(0)
}
pub fn fopen(path: &String, _fi: &mut FileInfo) -> Result<fs::File, Box<dyn Error>> {
pub fn fopen(&self, path: &String, _fi: &mut FileInfo) -> Result<fs::File, Box<dyn Error>> {
return Ok(fs::File::open(path)?);
}
pub fn fread(
path: &String,
&self, path: &String,
buffer: &mut Vec<u8>,
_size: &mut usize,
offsize: usize,
@ -44,7 +94,11 @@ pub fn fread(
let mut file = fs::File::open(path)?;
match file.seek_read(buffer, offsize as u64) {
Ok(size) => {
println!("size is:{}, buffer is {}", size, String::from_utf8(buffer.to_vec()).unwrap());
println!(
"size is:{}, buffer is {}",
size,
String::from_utf8(buffer.to_vec()).unwrap()
);
buffer.resize(size, 0)
}
Err(_) => {
@ -54,7 +108,12 @@ pub fn fread(
Ok(0)
}
pub fn fwrite(path: &String, buffer: &Vec<u8>, size: &mut usize, offsize: usize, _fi: &mut FileInfo,
pub fn fwrite(
&self, path: &String,
buffer: &Vec<u8>,
size: &mut usize,
offsize: usize,
_fi: &mut FileInfo,
) -> Result<i32, Box<dyn Error>> {
let mut file = fs::File::options().write(true).create(true).open(path)?;
match file.seek_write(buffer, offsize as u64) {
@ -68,7 +127,7 @@ pub fn fwrite(path: &String, buffer: &Vec<u8>, size: &mut usize, offsize: usize,
Ok(0)
}
pub fn freaddir(
path: &String,
&self, path: &String,
buffer: &mut Vec<String>,
_size: usize,
_offset: usize,
@ -91,22 +150,23 @@ pub fn freaddir(
}
Ok(0)
}
pub fn fmkdir(path: &String, _mode: u32) -> Result<i32, Box<dyn Error>> {
pub fn fmkdir(&self, path: &String, _mode: u32) -> Result<i32, Box<dyn Error>> {
fs::create_dir(path)?;
Ok(0)
}
pub fn fchown(_path: &String, _uid: u32, _gid: u32) -> Result<i32, Box<dyn Error>> {
pub fn fchown(&self, _path: &String, _uid: u32, _gid: u32) -> Result<i32, Box<dyn Error>> {
Ok(-1)
}
pub fn ftruncate(path: &String, _size: usize) -> Result<i32, Box<dyn Error>> {
fs::OpenOptions::new()
.write(true)
pub fn ftruncate(&self, path: &String, size: u64) -> Result<i32, Box<dyn Error>> {
let mut file = fs::File::options().write(true)
.truncate(true)
.open(path)?;
file.seek(SeekFrom::Start(size))?;
file.set_len(size)?;
Ok(0)
}
use std::fs::OpenOptions;
pub fn futimens(&self, path: &String, a: &Vec<u64>, m: &Vec<u64>) -> Result<i32, Box<dyn Error>> {
use std::ptr::null_mut;
use winapi::ctypes::c_void;
use winapi::shared::minwindef::DWORD;
@ -120,8 +180,7 @@ fn system_time_to_file_time(seconds: u64, nanoseconds: u32) -> FILETIME {
dwHighDateTime: (total_100_nanoseconds >> 32) as DWORD,
}
}
pub fn futimens(path: &String, a: &Vec<u64>, m: &Vec<u64>) -> Result<i32, Box<dyn Error>> {
let file = OpenOptions::new().write(true).open(path)?;
let file = fs::File::options().write(true).open(path)?;
let handle = file.as_raw_handle();
let atime = system_time_to_file_time(a[0], a[1] as u32);
@ -135,11 +194,16 @@ pub fn futimens(path: &String, a: &Vec<u64>, m: &Vec<u64>) -> Result<i32, Box<dy
Err(Box::new(io::Error::last_os_error()))
}
}
pub fn frelease(_path: &String, _fi: &mut FileInfo) -> Result<i32, Box<dyn Error>> {
pub fn frelease(&self, _path: &String, _fi: &mut FileInfo) -> Result<i32, Box<dyn Error>> {
if let Some(fh) == self.file_fds.lock().unwrap().pop(fi.fh){
drop(fh);
Ok(0)
} else {
Ok(-1)
}
}
pub fn fsetxattr(
_path: &String,
&self, _path: &String,
_name: &str,
_value: &Vec<u8>,
_size: usize,
@ -147,7 +211,7 @@ pub fn fsetxattr(
) -> Result<i32, Box<dyn Error>> {
Ok(-1)
}
pub fn fgetxattr(_path: &String, _name: &str, _size: usize) -> Result<i32, Box<dyn Error>> {
pub fn fgetxattr(&self, _path: &String, _name: &str, _size: usize) -> Result<i32, Box<dyn Error>> {
Ok(-1)
}
@ -172,16 +236,26 @@ fn get_file_mode_mask(path: &str) -> u32 {
}
permissions
}
pub fn faccess(path: &String, mask: u32) -> Result<i32, Box<dyn Error>> {
pub fn faccess(&self, path: &String, mask: u32) -> Result<i32, Box<dyn Error>> {
let permissions = get_file_mode_mask(path);
Ok(if permissions & mask != 0 { 0 } else { -1 })
}
pub fn frmdir(&self, path: &String) -> Result<i32, Box<dyn Error>> {
fs::remove_dir(path)?;
Ok(0)
}
}
#[test]
fn test_fread() {
use crate::fs_impl::*;
let path = String::from("test_fread.txt");
let file: fs::File = fs::File::options().write(true).create(true).open(&path).unwrap();
let file: fs::File = fs::File::options()
.write(true)
.create(true)
.open(&path)
.unwrap();
let ref_buffer = b"This is a test file.";
let _ = file.seek_write(ref_buffer, 0);
@ -244,6 +318,28 @@ fn test_fwrite() {
// cleanup
//let _ = fs::remove_file(path);
}
#[test]
fn test_dir() {
use std::path::Path;
let path = String::from("test_dir");
let sub_path = String::from("test_dir/test_sub_dir");
//创建父目录
let _ = fmkdir(&path, 0);
//创建子目录
let _ = fmkdir(&sub_path, 0);
assert_eq!(true, Path::new(&sub_path).is_dir());
assert_eq!(true, Path::new(&path).is_dir());
//一次性创建多级目录
let ret = fmkdir(&String::from("patent/child"), 0);
assert_ne!(
match ret {
Ok(ret) => ret,
Err(_) => -1,
},
0
);
fs::remove_dir_all(path);
}
// let result = fwrite(&path, &buffer, &mut size, offsize, &mut fi);
// println!("result: {:?}", result);
// println!("size: {:?}", size);

View File

@ -1,12 +1,13 @@
use lws_vfs::lws_vfs_server::LwsVfs;
use lws_vfs::{HelloReply, HelloRequest, Getattr, Setxattr, Access, Readdir, Read, Open,Write,Getxattr,Truncate,Utimens,Chown, Release, Fstat, FileInfo, Mkdir};
use tonic::{Request, Response, Status};
use lws_vfs::{
Access, Chown, FileInfo, Flush, Fstat, Getattr, Getxattr, HelloReply, HelloRequest, Mkdir,
Open, Read, Readdir, Release, Rmdir, Setxattr, Truncate, Utimens, Write, Flush,
};
use serde_json::{self, Value};
use std::error::Error;
use std::fs::File;
use std::io::{BufReader, Read as _};
use std::error::Error;
use std::collections::HashMap;
use std::sync::{Arc,Mutex};
use tonic::{Request, Response, Status};
mod fs_impl;
pub mod lws_vfs {
tonic::include_proto!("lws_vfs");
@ -36,23 +37,27 @@ impl Config{
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
}
None => 5001,
};
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")))
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,
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, mount_map })
}
pub fn get_port(&self) -> u16 {
@ -60,51 +65,6 @@ impl Config{
}
}
#[derive(Debug, Default)]
struct FileFdMgnt {
// key is fd, value is FileHandle
files:HashMap<u64,File>,
curr_fd:u64,
}
impl FileFdMgnt {
pub fn new() -> FileFdMgnt {
FileFdMgnt{
curr_fd:3,
files:HashMap::new()
}
}
fn gen_fd(&mut self) -> Result<u64, Box<dyn Error>> {
let refd = self.curr_fd;
loop{
self.curr_fd += 1;
match self.files.get(&self.curr_fd){
Some(_) => {}
None => {
break;
}
}
if refd == self.curr_fd {
return Err(Box::new(std::io::Error::new(std::io::ErrorKind::Other, "no fd")))
}
}
Ok(self.curr_fd)
}
pub fn push(&mut self, handle:File) -> Result<u64, Box<dyn Error>> {
let fd = self.gen_fd()?;
self.files.insert(fd, handle);
Ok(fd)
}
pub fn pop(&mut self, fd:u64){
if let Some(handle) = self.files.remove(&fd){
drop(handle);
}
else{
println!("not found fd:{}", fd);
}
}
}
/// server 的基础结构
#[derive(Debug, Default)]
pub struct LwsVfsIns {
@ -133,7 +93,10 @@ impl LwsVfsIns {
#[tonic::async_trait]
impl LwsVfs for LwsVfsIns {
async fn say_hello(&self, request: Request<HelloRequest>) -> Result<Response<HelloReply>, Status> {
async fn say_hello(
&self,
request: Request<HelloRequest>,
) -> Result<Response<HelloReply>, Status> {
println!("Got a request: {:?}", request);
let reply = HelloReply {
@ -198,7 +161,9 @@ impl LwsVfs for LwsVfsIns {
let mut size: usize = request.size.try_into().unwrap();
let offset: usize = request.offset.try_into().unwrap();
let mut buff: Vec<u8> = Vec::with_capacity(size);
unsafe { buff.set_len(size); }
unsafe {
buff.set_len(size);
}
match fs_impl::fread(path, &mut buff, &mut size, offset, &mut fi) {
Ok(ret) => {
let reply = Read {
@ -210,7 +175,7 @@ impl LwsVfs for LwsVfsIns {
ret,
};
Ok(Response::new(reply))
},
}
Err(e) => {
println!("Error reading file[{}]: {:?}", path, e);
let reply = Read {
@ -233,9 +198,7 @@ impl LwsVfs for LwsVfsIns {
let mut size = request.size as usize;
let offset = request.offset as usize;
let ret = match fs_impl::fwrite(path, &buff, &mut size, offset, &mut fi) {
Ok(ret) => {
ret
},
Ok(ret) => ret,
Err(e) => {
println!("Error fwrite file[{}]: {:?}", path, e);
-1
@ -247,16 +210,13 @@ impl LwsVfs for LwsVfsIns {
..Default::default()
};
Ok(Response::new(reply))
}
async fn faccess( &self, request: Request<Access>,) -> Result<Response<Access>, Status> {
async fn faccess(&self, request: Request<Access>) -> Result<Response<Access>, Status> {
let request = request.into_inner();
let path = &self.lpath(&request.path);
let mask = request.mask;
let ret = match fs_impl::faccess(path, mask) {
Ok(ret) => {
ret
},
Ok(ret) => ret,
Err(e) => {
println!("Error access file[{}]: {:?}", path, e);
-1
@ -282,7 +242,7 @@ impl LwsVfs for LwsVfsIns {
Err(e) => {
println!("Error: setxattr file[{}]: {:?}", path, e);
-1
},
}
};
let reply = Setxattr {
ret: ret,
@ -350,7 +310,7 @@ impl LwsVfs for LwsVfsIns {
async fn ftruncate(&self, request: Request<Truncate>) -> Result<Response<Truncate>, Status> {
let request = request.into_inner();
let path = &self.lpath(&request.path);
let size = request.size as usize;
let size = request.size as u64;
let ret = match fs_impl::ftruncate(path, size) {
Ok(ret) => ret,
Err(e) => {
@ -420,4 +380,27 @@ impl LwsVfs for LwsVfsIns {
};
Ok(Response::new(reply))
}
async fn frmdir(&self, request: Request<Rmdir>) -> Result<Response<Rmdir>, Status> {
let request = request.into_inner();
let path = &self.lpath(&request.path);
let ret = match fs_impl::frmdir(path) {
Ok(ret) => ret,
Err(e) => {
println!("Error: rmdir [{}]: {:?}", path, e);
-1
}
};
let reply = Rmdir {
ret,
path: request.path,
};
Ok(Response::new(reply))
}
async fn fflush(&self, request: Request<Flush>) -> Result<Response<Flush>, Status> {
let request = request.into_inner();
let path = &self.lpath(&request.path);
let mut fi = request.fi.unwrap();
self.file_fds.lock().unwrap().pop(fi.fh);
}
}