1. 修改日志库,使用log代替println宏。

2. 处理调试错误
3. 修复server的ip绑定错误。绑定0.0.0.0解决。
This commit is contained in:
Ekko.bao 2024-08-04 18:02:54 +08:00
parent f92393ae35
commit e044ac77b0
5 changed files with 130 additions and 58 deletions

61
Cargo.lock generated
View File

@ -65,6 +65,17 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi 0.1.19",
"libc",
"winapi",
]
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.3.0" version = "1.3.0"
@ -173,6 +184,19 @@ version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b"
[[package]]
name = "env_logger"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3"
dependencies = [
"atty",
"humantime",
"log",
"regex",
"termcolor",
]
[[package]] [[package]]
name = "equivalent" name = "equivalent"
version = "1.0.1" version = "1.0.1"
@ -300,6 +324,15 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.3.9" version = "0.3.9"
@ -340,6 +373,12 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]] [[package]]
name = "hyper" name = "hyper"
version = "0.14.29" version = "0.14.29"
@ -433,7 +472,9 @@ checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
name = "lws_vfs" name = "lws_vfs"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"env_logger",
"libc", "libc",
"log",
"prost", "prost",
"serde", "serde",
"serde_json", "serde_json",
@ -494,7 +535,7 @@ version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43"
dependencies = [ dependencies = [
"hermit-abi", "hermit-abi 0.3.9",
"libc", "libc",
] ]
@ -817,6 +858,15 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "termcolor"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755"
dependencies = [
"winapi-util",
]
[[package]] [[package]]
name = "tokio" name = "tokio"
version = "1.38.0" version = "1.38.0"
@ -1025,6 +1075,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [
"windows-sys 0.52.0",
]
[[package]] [[package]]
name = "winapi-x86_64-pc-windows-gnu" name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"

View File

@ -24,6 +24,8 @@ windows = "0.28"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
libc = "0.2" libc = "0.2"
log = "0.4"
env_logger = "0.8"
[build-dependencies] [build-dependencies]
tonic-build = "0.11" tonic-build = "0.11"

View File

@ -11,7 +11,7 @@ use std::collections::HashMap;
extern crate winapi; extern crate winapi;
use std::os::windows::fs::{MetadataExt}; use std::os::windows::fs::{MetadataExt};
use std::os::windows::prelude::*; use std::os::windows::prelude::*;
use winapi::um::winnt::FILE_ATTRIBUTE_READONLY; use winapi::um::winnt::{FILE_ATTRIBUTE_READONLY, FILE_SHARE_READ, FILE_SHARE_WRITE};
use libc; use libc;
fn from_metadata(sta: &mut Fstat, metadata: &fs::Metadata) -> Result<(), Box<dyn Error>> { fn from_metadata(sta: &mut Fstat, metadata: &fs::Metadata) -> Result<(), Box<dyn Error>> {
@ -33,11 +33,13 @@ enum FileHandle {
DIR(ReadDir) DIR(ReadDir)
} }
type FD = u16;
#[derive(Debug, Default)] #[derive(Debug, Default)]
struct FileFdMgnt { struct FileFdMgnt {
// key is fd, value is FileHandle // key is fd, value is FileHandle
files: HashMap<u64, FileHandle>, files: HashMap<FD, FileHandle>,
curr_fd: u64, curr_fd: FD,
} }
impl FileFdMgnt { impl FileFdMgnt {
@ -51,6 +53,9 @@ impl FileFdMgnt {
let refd = self.curr_fd; let refd = self.curr_fd;
loop { loop {
self.curr_fd += 1; self.curr_fd += 1;
if self.curr_fd < 3 {
self.curr_fd = 3;
}
match self.files.get(&self.curr_fd) { match self.files.get(&self.curr_fd) {
Some(_) => {} Some(_) => {}
None => { None => {
@ -64,24 +69,24 @@ impl FileFdMgnt {
))); )));
} }
} }
Ok(self.curr_fd) Ok(self.curr_fd as u64)
} }
pub fn push(&mut self, handle: FileHandle) -> Result<u64, Box<dyn Error>> { pub fn push(&mut self, handle: FileHandle) -> Result<u64, Box<dyn Error>> {
let fd = self.gen_fd()?; let fd = self.gen_fd()?;
println!("push a fd: {}", fd); println!("push a fd: {}", fd);
self.files.insert(fd, handle); self.files.insert(fd as u16, handle);
Ok(fd) Ok(fd)
} }
pub fn pop(&mut self, fd: u64) -> Option<FileHandle> { pub fn pop(&mut self, fd: u64) -> Option<FileHandle> {
println!("pop a fd: {}", fd); println!("pop a fd: {}", fd);
self.files.remove(&fd) self.files.remove(&(fd as u16))
} }
// pub fn get(&mut self, fd: u64) -> Option<&FileHandle> { // pub fn get(&mut self, fd: u64) -> Option<&FileHandle> {
// self.files.get(&fd) // self.files.get(&fd)
// } // }
pub fn get_mut(&mut self, fd: u64) -> Option<&mut FileHandle> { pub fn get_mut(&mut self, fd: u64) -> Option<&mut FileHandle> {
println!("get a fd: {}", fd); println!("get a fd: {}", fd);
self.files.get_mut(&fd) self.files.get_mut(&(fd as u16))
} }
} }
@ -137,7 +142,7 @@ impl FSImpl {
let write = flag == libc::O_WRONLY as u32 || flag == libc::O_RDWR as u32; let write = flag == libc::O_WRONLY as u32 || flag == libc::O_RDWR as u32;
let f = File::options() let f = File::options()
.read(read) .read(read)
.write(write).open(path)?; .write(write).create(write).open(path)?;
let fd = self.file_fds.lock().unwrap().push(FileHandle::FILE(f))?; let fd = self.file_fds.lock().unwrap().push(FileHandle::FILE(f))?;
fi.fh = fd; fi.fh = fd;
return Ok(0); return Ok(0);
@ -317,10 +322,10 @@ impl FSImpl {
let _unused = self.fflush(path, fi); let _unused = self.fflush(path, fi);
} }
drop(fh); drop(fh);
Ok(0)
} else { } else {
Ok(-1) fs::remove_file(path)?;
} }
Ok(0)
} }
pub fn fsetxattr( pub fn fsetxattr(
&self, _path: &String, &self, _path: &String,
@ -398,7 +403,7 @@ impl FSImpl {
* Introduced in version 2.5 * Introduced in version 2.5
*/ */
pub fn fcreate(&self, path:&String, mode:u32, fi:&mut FileInfo) -> Result<i32, Box<dyn Error>> { pub fn fcreate(&self, path:&String, mode:u32, fi:&mut FileInfo) -> Result<i32, Box<dyn Error>> {
let file = File::options().write(true).access_mode(mode).create(true).open(path)?; let file = File::options().write(true).share_mode(FILE_SHARE_READ|FILE_SHARE_WRITE).access_mode(mode).create(true).open(path)?;
let fd = self.file_fds.lock().unwrap().push(FileHandle::FILE(file))?; let fd = self.file_fds.lock().unwrap().push(FileHandle::FILE(file))?;
fi.fh = fd; fi.fh = fd;
Ok(0) Ok(0)

View File

@ -11,6 +11,8 @@ use std::fs::File;
use std::io::Read as _; use std::io::Read as _;
use tonic::{Request, Response, Status}; use tonic::{Request, Response, Status};
use std::collections::HashMap; use std::collections::HashMap;
extern crate log;
mod fs_impl; mod fs_impl;
pub mod lws_vfs { pub mod lws_vfs {
tonic::include_proto!("lws_vfs"); tonic::include_proto!("lws_vfs");
@ -84,7 +86,7 @@ impl LwsVfsIns {
} }
fn lpath(&self, path: &String) -> String { fn lpath(&self, path: &String) -> String {
let mut ret = String::new(); let mut ret = String::new();
// println!("try to convert {}", path); // log::trace!("try to convert {}", path);
// /l0e -> w: // /l0e -> w:
for (k, v) in &self.config.mount_map { for (k, v) in &self.config.mount_map {
if path.starts_with(v) { if path.starts_with(v) {
@ -92,7 +94,7 @@ impl LwsVfsIns {
break; break;
} }
} }
// println!("path convert ret is {}", ret); // log::trace!("path convert ret is {}", ret);
ret ret
} }
} }
@ -104,7 +106,7 @@ impl LwsVfs for LwsVfsIns {
request: Request<HelloRequest>, request: Request<HelloRequest>,
) -> Result<Response<HelloReply>, Status> { ) -> Result<Response<HelloReply>, Status> {
let request = request.into_inner(); let request = request.into_inner();
println!("Got a request: {:?}", request); log::trace!("Got a request: {:?}", request);
let reply = HelloReply { let reply = HelloReply {
message: format!("Hello {}!", request.name), message: format!("Hello {}!", request.name),
@ -114,14 +116,14 @@ impl LwsVfs for LwsVfsIns {
} }
async fn fgetattr(&self, request: Request<Getattr>) -> Result<Response<Getattr>, Status> { async fn fgetattr(&self, request: Request<Getattr>) -> Result<Response<Getattr>, Status> {
let request = request.into_inner(); let request = request.into_inner();
println!("Got a request: {:?}", request); log::trace!("Got a request: {:?}", request);
let path = &self.lpath(&request.path); let path = &self.lpath(&request.path);
let mut fstat = Fstat::default(); let mut fstat = Fstat::default();
let mut fi = FileInfo::default(); let mut fi = FileInfo::default();
let ret = match self.fs.fgetattr(path, &mut fstat, &mut fi) { let ret = match self.fs.fgetattr(path, &mut fstat, &mut fi) {
Ok(ret) => ret, Ok(ret) => ret,
Err(e) => { Err(e) => {
println!("Error getting file metadata: {:?}", e); log::error!("Error getting file metadata: {:?}", e);
-1 -1
} }
}; };
@ -135,7 +137,7 @@ impl LwsVfs for LwsVfsIns {
} }
async fn fopen(&self, request: Request<Open>) -> Result<Response<Open>, Status> { async fn fopen(&self, request: Request<Open>) -> Result<Response<Open>, Status> {
let request = request.into_inner(); let request = request.into_inner();
println!("Got a request: {:?}", request); log::trace!("Got a request: {:?}", request);
let path = &self.lpath(&request.path); let path = &self.lpath(&request.path);
let mut fi = match request.fi{ let mut fi = match request.fi{
Some(fi) => fi, Some(fi) => fi,
@ -146,7 +148,7 @@ impl LwsVfs for LwsVfsIns {
0 0
} }
Err(e) => { Err(e) => {
println!("Error open file[{}]: {:?}", path, e); log::error!("Error open file[{}]: {:?}", path, e);
-1 -1
} }
}; };
@ -159,7 +161,6 @@ impl LwsVfs for LwsVfsIns {
} }
async fn fread(&self, request: Request<Read>) -> Result<Response<Read>, Status> { async fn fread(&self, request: Request<Read>) -> Result<Response<Read>, Status> {
let request = request.into_inner(); let request = request.into_inner();
println!("Got a request: {:?}", request);
let mut fi = match request.fi{ let mut fi = match request.fi{
Some(fi) => fi, Some(fi) => fi,
None => FileInfo::default(), None => FileInfo::default(),
@ -171,6 +172,7 @@ impl LwsVfs for LwsVfsIns {
unsafe { unsafe {
buff.set_len(size); buff.set_len(size);
} }
log::trace!("Got a request: fread {}, size {} offset {}", path, request.size, offset);
match self.fs.fread(path, &mut buff, &mut size, offset, &mut fi) { match self.fs.fread(path, &mut buff, &mut size, offset, &mut fi) {
Ok(ret) => { Ok(ret) => {
let reply = Read { let reply = Read {
@ -184,7 +186,7 @@ impl LwsVfs for LwsVfsIns {
Ok(Response::new(reply)) Ok(Response::new(reply))
} }
Err(e) => { Err(e) => {
println!("Error reading file[{}]: {:?}", path, e); log::error!("Error reading file[{}]: {:?}", path, e);
let reply = Read { let reply = Read {
path: request.path, path: request.path,
buff: Vec::new(), buff: Vec::new(),
@ -197,6 +199,7 @@ impl LwsVfs for LwsVfsIns {
} }
} }
} }
async fn fwrite(&self, request: Request<Write>) -> Result<Response<Write>, Status> { async fn fwrite(&self, request: Request<Write>) -> Result<Response<Write>, Status> {
let request = request.into_inner(); let request = request.into_inner();
let mut fi = match request.fi{ let mut fi = match request.fi{
@ -207,16 +210,17 @@ impl LwsVfs for LwsVfsIns {
let buff = request.buff; let buff = request.buff;
let mut size = request.size as usize; let mut size = request.size as usize;
let offset = request.offset as usize; let offset = request.offset as usize;
println!("Got a fwrite request: size {} offset {}", request.size, offset); log::trace!("Got a request: Write {}, size {} offset {}", path, request.size, offset);
let ret = match self.fs.fwrite(path, &buff, &mut size, offset, &mut fi) { let ret = match self.fs.fwrite(path, &buff, &mut size, offset, &mut fi) {
Ok(ret) => ret, Ok(ret) => ret,
Err(e) => { Err(e) => {
println!("Error fwrite file[{}]: {:?}", path, e); log::error!("Error fwrite file[{}]: {:?}", path, e);
-1 -1
} }
}; };
let reply = Write { let reply = Write {
ret, ret,
path: path.to_string(),
size: size as u64, size: size as u64,
..Default::default() ..Default::default()
}; };
@ -224,13 +228,13 @@ impl LwsVfs for LwsVfsIns {
} }
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 request = request.into_inner();
println!("Got a request: {:?}", request); log::trace!("Got a request: {:?}", request);
let path = &self.lpath(&request.path); let path = &self.lpath(&request.path);
let mask = request.mask; let mask = request.mask;
let ret = match self.fs.faccess(path, mask) { let ret = match self.fs.faccess(path, mask) {
Ok(ret) => ret, Ok(ret) => ret,
Err(e) => { Err(e) => {
println!("Error access file[{}]: {:?}", path, e); log::error!("Error access file[{}]: {:?}", path, e);
-1 -1
} }
}; };
@ -244,7 +248,7 @@ impl LwsVfs for LwsVfsIns {
async fn fsetxattr(&self, request: Request<Setxattr>) -> Result<Response<Setxattr>, Status> { async fn fsetxattr(&self, request: Request<Setxattr>) -> Result<Response<Setxattr>, Status> {
let request = request.into_inner(); let request = request.into_inner();
println!("Got a request: {:?}", request); log::trace!("Got a request: {:?}", request);
let path = &self.lpath(&request.path); let path = &self.lpath(&request.path);
let name = request.name.as_ref(); let name = request.name.as_ref();
let value = request.value; let value = request.value;
@ -253,7 +257,7 @@ impl LwsVfs for LwsVfsIns {
let ret = match self.fs.fsetxattr(path, name, &value, size, flags) { let ret = match self.fs.fsetxattr(path, name, &value, size, flags) {
Ok(ret) => ret, Ok(ret) => ret,
Err(e) => { Err(e) => {
println!("Error: setxattr file[{}]: {:?}", path, e); log::error!("Error: setxattr file[{}]: {:?}", path, e);
-1 -1
} }
}; };
@ -265,14 +269,14 @@ impl LwsVfs for LwsVfsIns {
} }
async fn fgetxattr(&self, request: Request<Getxattr>) -> Result<Response<Getxattr>, Status> { async fn fgetxattr(&self, request: Request<Getxattr>) -> Result<Response<Getxattr>, Status> {
let request = request.into_inner(); let request = request.into_inner();
println!("Got a request: {:?}", request); log::trace!("Got a request: {:?}", request);
let path = &self.lpath(&request.path); let path = &self.lpath(&request.path);
let name = request.name.as_ref(); let name = request.name.as_ref();
let size = request.size as usize; let size = request.size as usize;
let ret = match self.fs.fgetxattr(path, name, size) { let ret = match self.fs.fgetxattr(path, name, size) {
Ok(ret) => ret, Ok(ret) => ret,
Err(e) => { Err(e) => {
println!("Error: getxattr file[{}]: {:?}", path, e); log::error!("Error: getxattr file[{}]: {:?}", path, e);
-1 -1
} }
}; };
@ -285,7 +289,7 @@ impl LwsVfs for LwsVfsIns {
async fn freaddir(&self, request: Request<Readdir>) -> Result<Response<Readdir>, Status> { async fn freaddir(&self, request: Request<Readdir>) -> Result<Response<Readdir>, Status> {
let request = request.into_inner(); let request = request.into_inner();
println!("Got a request: {:?}", request); log::trace!("Got a request: {:?}", request);
let mut fi = match request.fi{ let mut fi = match request.fi{
Some(fi) => fi, Some(fi) => fi,
None => FileInfo::default(), None => FileInfo::default(),
@ -297,7 +301,7 @@ impl LwsVfs for LwsVfsIns {
let ret = match self.fs.freaddir(path, &mut dirs, size, offset, &mut fi) { let ret = match self.fs.freaddir(path, &mut dirs, size, offset, &mut fi) {
Ok(ret) => ret, Ok(ret) => ret,
Err(e) => { Err(e) => {
println!("Error: readdir file[{}]: {:?}", path, e); log::error!("Error: readdir file[{}]: {:?}", path, e);
-1 -1
} }
}; };
@ -310,13 +314,13 @@ impl LwsVfs for LwsVfsIns {
} }
async fn fmkdir(&self, request: Request<Mkdir>) -> Result<Response<Mkdir>, Status> { async fn fmkdir(&self, request: Request<Mkdir>) -> Result<Response<Mkdir>, Status> {
let request = request.into_inner(); let request = request.into_inner();
println!("Got a request: {:?}", request); log::trace!("Got a request: {:?}", request);
let path = &self.lpath(&request.path); let path = &self.lpath(&request.path);
let mode = request.mode; let mode = request.mode;
let ret = match self.fs.fmkdir(&path, mode) { let ret = match self.fs.fmkdir(&path, mode) {
Ok(ret) => ret, Ok(ret) => ret,
Err(e) => { Err(e) => {
println!("Error: mkdir [{}]: {:?}", path, e); log::error!("Error: mkdir [{}]: {:?}", path, e);
-1 -1
} }
}; };
@ -328,13 +332,13 @@ impl LwsVfs for LwsVfsIns {
} }
async fn ftruncate(&self, request: Request<Truncate>) -> Result<Response<Truncate>, Status> { async fn ftruncate(&self, request: Request<Truncate>) -> Result<Response<Truncate>, Status> {
let request = request.into_inner(); let request = request.into_inner();
println!("Got a request: {:?}", request); log::trace!("Got a request: {:?}", request);
let path = &self.lpath(&request.path); let path = &self.lpath(&request.path);
let size = request.size as u64; let size = request.size as u64;
let ret = match self.fs.ftruncate(path, size) { let ret = match self.fs.ftruncate(path, size) {
Ok(ret) => ret, Ok(ret) => ret,
Err(e) => { Err(e) => {
println!("Error: truncate file[{}]: {:?}", path, e); log::error!("Error: truncate file[{}]: {:?}", path, e);
-1 -1
} }
}; };
@ -347,14 +351,14 @@ impl LwsVfs for LwsVfsIns {
async fn futimens(&self, request: Request<Utimens>) -> Result<Response<Utimens>, Status> { async fn futimens(&self, request: Request<Utimens>) -> Result<Response<Utimens>, Status> {
let request = request.into_inner(); let request = request.into_inner();
println!("Got a request: {:?}", request); log::trace!("Got a request: {:?}", request);
let path = &self.lpath(&request.path); let path = &self.lpath(&request.path);
let a_time = vec![request.ts[0].tv_sec as u64, request.ts[0].tv_nsec as u64]; let a_time = vec![request.ts[0].tv_sec as u64, request.ts[0].tv_nsec as u64];
let m_time = vec![request.ts[1].tv_sec as u64, request.ts[1].tv_nsec as u64]; let m_time = vec![request.ts[1].tv_sec as u64, request.ts[1].tv_nsec as u64];
let ret = match self.fs.futimens(path, &a_time, &m_time) { let ret = match self.fs.futimens(path, &a_time, &m_time) {
Ok(ret) => ret, Ok(ret) => ret,
Err(e) => { Err(e) => {
println!("Error: futimens file[{}]: {:?}", path, e); log::error!("Error: futimens file[{}]: {:?}", path, e);
-1 -1
} }
}; };
@ -367,14 +371,14 @@ impl LwsVfs for LwsVfsIns {
async fn fchown(&self, request: Request<Chown>) -> Result<Response<Chown>, Status> { async fn fchown(&self, request: Request<Chown>) -> Result<Response<Chown>, Status> {
let request = request.into_inner(); let request = request.into_inner();
println!("Got a request: {:?}", request); log::trace!("Got a request: {:?}", request);
let path = &self.lpath(&request.path); let path = &self.lpath(&request.path);
let uid = request.uid as u32; let uid = request.uid as u32;
let gid = request.gid as u32; let gid = request.gid as u32;
let ret = match self.fs.fchown(path, uid, gid) { let ret = match self.fs.fchown(path, uid, gid) {
Ok(ret) => ret, Ok(ret) => ret,
Err(e) => { Err(e) => {
println!("Error: chown file[{}]: {:?}", path, e); log::error!("Error: chown file[{}]: {:?}", path, e);
-1 -1
} }
}; };
@ -386,7 +390,7 @@ impl LwsVfs for LwsVfsIns {
} }
async fn frelease(&self, request: Request<Release>) -> Result<Response<Release>, Status> { async fn frelease(&self, request: Request<Release>) -> Result<Response<Release>, Status> {
let request = request.into_inner(); let request = request.into_inner();
println!("Got a request: {:?}", request); log::trace!("Got a request: {:?}", request);
let path = &self.lpath(&request.path); let path = &self.lpath(&request.path);
let mut fi = match request.fi{ let mut fi = match request.fi{
Some(fi) => fi, Some(fi) => fi,
@ -395,7 +399,7 @@ impl LwsVfs for LwsVfsIns {
let ret = match self.fs.frelease(path, &mut fi, request.flush != 0) { let ret = match self.fs.frelease(path, &mut fi, request.flush != 0) {
Ok(ret) => ret, Ok(ret) => ret,
Err(e) => { Err(e) => {
println!("Error: release file[{}]: {:?}", path, e); log::error!("Error: release file[{}]: {:?}", path, e);
-1 -1
} }
}; };
@ -407,12 +411,12 @@ impl LwsVfs for LwsVfsIns {
} }
async fn frmdir(&self, request: Request<Rmdir>) -> Result<Response<Rmdir>, Status> { async fn frmdir(&self, request: Request<Rmdir>) -> Result<Response<Rmdir>, Status> {
let request = request.into_inner(); let request = request.into_inner();
println!("Got a request: {:?}", request); log::trace!("Got a request: {:?}", request);
let path = &self.lpath(&request.path); let path = &self.lpath(&request.path);
let ret = match self.fs.frmdir(path) { let ret = match self.fs.frmdir(path) {
Ok(ret) => ret, Ok(ret) => ret,
Err(e) => { Err(e) => {
println!("Error: rmdir [{}]: {:?}", path, e); log::error!("Error: rmdir [{}]: {:?}", path, e);
-1 -1
} }
}; };
@ -425,7 +429,7 @@ impl LwsVfs for LwsVfsIns {
async fn fflush(&self, request: Request<Flush>) -> Result<Response<Flush>, Status> { async fn fflush(&self, request: Request<Flush>) -> Result<Response<Flush>, Status> {
let request = request.into_inner(); let request = request.into_inner();
println!("Got a request: {:?}", request); log::trace!("Got a request: {:?}", request);
let path = &self.lpath(&request.path); let path = &self.lpath(&request.path);
let mut fi = match request.fi{ let mut fi = match request.fi{
Some(fi) => fi, Some(fi) => fi,
@ -434,7 +438,7 @@ impl LwsVfs for LwsVfsIns {
let ret = match self.fs.fflush(path, &mut fi) { let ret = match self.fs.fflush(path, &mut fi) {
Ok(ret) => ret, Ok(ret) => ret,
Err(e) => { Err(e) => {
println!("Error: flush file[{}]: {:?}", path, e); log::error!("Error: flush file[{}]: {:?}", path, e);
-1 -1
} }
}; };
@ -447,7 +451,7 @@ impl LwsVfs for LwsVfsIns {
async fn fcreate(&self, request: Request<Create>) -> Result<Response<Create>, Status> { async fn fcreate(&self, request: Request<Create>) -> Result<Response<Create>, Status> {
let request = request.into_inner(); let request = request.into_inner();
println!("Got a request: {:?}", request); log::trace!("Got a request: {:?}", request);
let path = &self.lpath(&request.path); let path = &self.lpath(&request.path);
let mode = request.mode as u32; let mode = request.mode as u32;
let mut fi = match request.fi{ let mut fi = match request.fi{
@ -457,12 +461,13 @@ impl LwsVfs for LwsVfsIns {
let ret = match self.fs.fcreate(path, mode, &mut fi) { let ret = match self.fs.fcreate(path, mode, &mut fi) {
Ok(ret) => ret, Ok(ret) => ret,
Err(e) => { Err(e) => {
println!("Error: create file[{}]: {:?}", path, e); log::error!("Error: create file[{}]: {:?}", path, e);
-1 -1
} }
}; };
let reply = Create { let reply = Create {
ret, ret,
fi:Some(fi),
..Create::default() ..Create::default()
}; };
Ok(Response::new(reply)) Ok(Response::new(reply))
@ -470,12 +475,12 @@ impl LwsVfs for LwsVfsIns {
async fn funlink(&self, request: Request<Unlink>) -> Result<Response<Unlink>, Status> { async fn funlink(&self, request: Request<Unlink>) -> Result<Response<Unlink>, Status> {
let request = request.into_inner(); let request = request.into_inner();
println!("Got a request: {:?}", request); log::trace!("Got a request: {:?}", request);
let path = &self.lpath(&request.path); let path = &self.lpath(&request.path);
let ret = match self.fs.funlink(path) { let ret = match self.fs.funlink(path) {
Ok(ret) => ret, Ok(ret) => ret,
Err(e) => { Err(e) => {
println!("Error: unlink file[{}]: {:?}", path, e); log::error!("Error: unlink file[{}]: {:?}", path, e);
-1 -1
} }
}; };
@ -488,7 +493,7 @@ impl LwsVfs for LwsVfsIns {
async fn fopendir(&self,request: Request<Opendir>) -> Result<Response<Opendir>, Status> { async fn fopendir(&self,request: Request<Opendir>) -> Result<Response<Opendir>, Status> {
let request = request.into_inner(); let request = request.into_inner();
println!("Got a request: {:?}", request); log::trace!("Got a request: {:?}", request);
let path = &self.lpath(&request.path); let path = &self.lpath(&request.path);
let mut fi = match request.fi{ let mut fi = match request.fi{
Some(fi) => fi, Some(fi) => fi,
@ -497,7 +502,7 @@ impl LwsVfs for LwsVfsIns {
let ret = match self.fs.fopendir(path, &mut fi) { let ret = match self.fs.fopendir(path, &mut fi) {
Ok(ret) => ret, Ok(ret) => ret,
Err(e) => { Err(e) => {
println!("Error: opendir dir[{}]: {:?}", path, e); log::error!("Error: opendir dir[{}]: {:?}", path, e);
-1 -1
} }
}; };
@ -511,7 +516,7 @@ impl LwsVfs for LwsVfsIns {
async fn freleasedir(&self, request: Request<Releasedir>) -> Result<Response<Releasedir>, Status> { async fn freleasedir(&self, request: Request<Releasedir>) -> Result<Response<Releasedir>, Status> {
let request = request.into_inner(); let request = request.into_inner();
println!("Got a request: {:?}", request); log::trace!("Got a request: {:?}", request);
let path = &self.lpath(&request.path); let path = &self.lpath(&request.path);
let mut fi = match request.fi{ let mut fi = match request.fi{
Some(fi) => fi, Some(fi) => fi,
@ -520,7 +525,7 @@ impl LwsVfs for LwsVfsIns {
let ret = match self.fs.freleasedir(path, &mut fi) { let ret = match self.fs.freleasedir(path, &mut fi) {
Ok(ret) => ret, Ok(ret) => ret,
Err(e) => { Err(e) => {
println!("Error: releasedir file[{}]: {:?}", path, e); log::error!("Error: releasedir file[{}]: {:?}", path, e);
-1 -1
} }
}; };
@ -533,13 +538,13 @@ impl LwsVfs for LwsVfsIns {
async fn frename(&self, request: Request<Rename>) -> Result<Response<Rename>, Status> { async fn frename(&self, request: Request<Rename>) -> Result<Response<Rename>, Status> {
let request = request.into_inner(); let request = request.into_inner();
println!("Got a request: {:?}", request); log::trace!("Got a request: {:?}", request);
let from = &self.lpath(&request.path); let from = &self.lpath(&request.path);
let to = &self.lpath(&request.new); let to = &self.lpath(&request.new);
let ret = match self.fs.frename(from, to) { let ret = match self.fs.frename(from, to) {
Ok(ret) => ret, Ok(ret) => ret,
Err(e) => { Err(e) => {
println!("Error: rename file[{}]: {:?}", from, e); log::error!("Error: rename file[{}]: {:?}", from, e);
-1 -1
} }
}; };

View File

@ -2,7 +2,7 @@ use tonic::transport;
use lws_vfs::LwsVfsIns; use lws_vfs::LwsVfsIns;
use lws_vfs::lws_vfs::lws_vfs_server::LwsVfsServer; use lws_vfs::lws_vfs::lws_vfs_server::LwsVfsServer;
extern crate log;
// fn main() { // fn main() {
// println!("Hello, world!"); // println!("Hello, world!");
// let file = "test.txt"; // let file = "test.txt";
@ -16,15 +16,16 @@ use lws_vfs::lws_vfs::lws_vfs_server::LwsVfsServer;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let instance = match LwsVfsIns::new("config.json"){ let instance = match LwsVfsIns::new("config.json"){
Ok(greeter) => greeter, Ok(greeter) => greeter,
Err(e) => { Err(e) => {
println!("Error creating lws server instance: {:?}", e); log::error!("Error creating lws server instance: {:?}", e);
return Err(e); return Err(e);
} }
}; };
let addr = format!("0.0.0.0:{}", instance.config.get_port()); let addr = format!("0.0.0.0:{}", instance.config.get_port());
println!("Listening on {}", addr); log::info!("Listening on {}", addr);
transport::Server::builder() transport::Server::builder()
.add_service(LwsVfsServer::new(instance)) .add_service(LwsVfsServer::new(instance))
.serve(addr.parse()?) .serve(addr.parse()?)