1. 添加处理rpc返回值的宏,将其转化为Result类型方便处理,将rpc过来ret值如实返还给sys
This commit is contained in:
		
							
								
								
									
										102
									
								
								src/lib.rs
									
									
									
									
									
								
							
							
						
						
									
										102
									
								
								src/lib.rs
									
									
									
									
									
								
							@@ -7,10 +7,9 @@ use lws_client::{
 | 
				
			|||||||
use serde_json::{self, Value};
 | 
					use serde_json::{self, Value};
 | 
				
			||||||
use std::error::Error;
 | 
					use std::error::Error;
 | 
				
			||||||
use std::ffi::OsStr;
 | 
					use std::ffi::OsStr;
 | 
				
			||||||
use std::fs::File;
 | 
					 | 
				
			||||||
use std::io::{Error as IoError, ErrorKind, Read as IoRead};
 | 
					use std::io::{Error as IoError, ErrorKind, Read as IoRead};
 | 
				
			||||||
use std::thread::{self, JoinHandle};
 | 
					use std::thread::{self};
 | 
				
			||||||
use std::{env, path, vec};
 | 
					use std::{env, vec};
 | 
				
			||||||
use tokio::runtime::{Builder, Runtime};
 | 
					use tokio::runtime::{Builder, Runtime};
 | 
				
			||||||
use tonic::transport::Channel as RpcChannel;
 | 
					use tonic::transport::Channel as RpcChannel;
 | 
				
			||||||
extern crate log;
 | 
					extern crate log;
 | 
				
			||||||
@@ -432,6 +431,14 @@ pub struct LwsVfsIns {
 | 
				
			|||||||
    cache: FileAttrCacheManager,
 | 
					    cache: FileAttrCacheManager,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					macro_rules! ret2result {
 | 
				
			||||||
 | 
					    ($ret:expr) => {
 | 
				
			||||||
 | 
					        match $ret {
 | 
				
			||||||
 | 
					            0 => Ok(()),
 | 
				
			||||||
 | 
					            _ => Err($ret),
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
fn mode_to_filetype(mode: libc::mode_t) -> FileType {
 | 
					fn mode_to_filetype(mode: libc::mode_t) -> FileType {
 | 
				
			||||||
    match mode & libc::S_IFMT {
 | 
					    match mode & libc::S_IFMT {
 | 
				
			||||||
        libc::S_IFDIR => FileType::Directory,
 | 
					        libc::S_IFDIR => FileType::Directory,
 | 
				
			||||||
@@ -541,8 +548,8 @@ impl FilesystemMT for LwsVfsIns {
 | 
				
			|||||||
        };
 | 
					        };
 | 
				
			||||||
        log::trace!("get resp: {:?}", resp);
 | 
					        log::trace!("get resp: {:?}", resp);
 | 
				
			||||||
        if resp.ret != 0 {
 | 
					        if resp.ret != 0 {
 | 
				
			||||||
            self.cache.set(&path, Err(libc::ENOENT));
 | 
					            self.cache.set(&path, Err(resp.ret));
 | 
				
			||||||
            return Err(libc::ENOENT);
 | 
					            return Err(resp.ret);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        let attr = file_attr(&resp.stat.unwrap(), &req);
 | 
					        let attr = file_attr(&resp.stat.unwrap(), &req);
 | 
				
			||||||
        self.cache.set(&path, Ok(attr.clone()));
 | 
					        self.cache.set(&path, Ok(attr.clone()));
 | 
				
			||||||
@@ -606,10 +613,7 @@ impl FilesystemMT for LwsVfsIns {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
        log::trace!("get resp: {:?}", resp);
 | 
					        log::trace!("get resp: {:?}", resp);
 | 
				
			||||||
        if resp.ret != 0 {
 | 
					        ret2result!(resp.ret)
 | 
				
			||||||
            return Err(libc::ENOMSG);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        Ok(())
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Set timestamps of a filesystem entry.
 | 
					    /// Set timestamps of a filesystem entry.
 | 
				
			||||||
@@ -666,10 +670,7 @@ impl FilesystemMT for LwsVfsIns {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
        log::trace!("get resp: {:?}", resp);
 | 
					        log::trace!("get resp: {:?}", resp);
 | 
				
			||||||
        if resp.ret != 0 {
 | 
					        ret2result!(resp.ret)
 | 
				
			||||||
            return Err(libc::ENOMSG);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        Ok(())
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // END OF SETATTR FUNCTIONS
 | 
					    // END OF SETATTR FUNCTIONS
 | 
				
			||||||
@@ -732,7 +733,7 @@ impl FilesystemMT for LwsVfsIns {
 | 
				
			|||||||
        };
 | 
					        };
 | 
				
			||||||
        log::trace!("get resp: {:?}", resp);
 | 
					        log::trace!("get resp: {:?}", resp);
 | 
				
			||||||
        if resp.ret != 0 {
 | 
					        if resp.ret != 0 {
 | 
				
			||||||
            return Err(libc::ENOMSG);
 | 
					            return Err(resp.ret)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        self.getattr(req, Path::new(&path), None)
 | 
					        self.getattr(req, Path::new(&path), None)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -768,10 +769,7 @@ impl FilesystemMT for LwsVfsIns {
 | 
				
			|||||||
        };
 | 
					        };
 | 
				
			||||||
        self.cache.clr(vec![path]);
 | 
					        self.cache.clr(vec![path]);
 | 
				
			||||||
        log::trace!("get resp: {:?}", resp);
 | 
					        log::trace!("get resp: {:?}", resp);
 | 
				
			||||||
        if resp.ret != 0 {
 | 
					        ret2result!(resp.ret)
 | 
				
			||||||
            return Err(libc::ENOMSG);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        Ok(())
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Remove a directory.
 | 
					    /// Remove a directory.
 | 
				
			||||||
@@ -807,10 +805,7 @@ impl FilesystemMT for LwsVfsIns {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
        log::trace!("get resp: {:?}", resp);
 | 
					        log::trace!("get resp: {:?}", resp);
 | 
				
			||||||
        if resp.ret != 0 {
 | 
					        ret2result!(resp.ret)
 | 
				
			||||||
            return Err(libc::ENOMSG);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        Ok(())
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Create a symbolic link.
 | 
					    /// Create a symbolic link.
 | 
				
			||||||
@@ -870,10 +865,7 @@ impl FilesystemMT for LwsVfsIns {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
        log::trace!("get resp: {:?}", resp);
 | 
					        log::trace!("get resp: {:?}", resp);
 | 
				
			||||||
        if resp.ret != 0 {
 | 
					        ret2result!(resp.ret)
 | 
				
			||||||
            return Err(libc::ENOMSG);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        Ok(())
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Create a hard link.
 | 
					    /// Create a hard link.
 | 
				
			||||||
@@ -929,7 +921,7 @@ impl FilesystemMT for LwsVfsIns {
 | 
				
			|||||||
        };
 | 
					        };
 | 
				
			||||||
        log::trace!("get resp: {:?}", resp);
 | 
					        log::trace!("get resp: {:?}", resp);
 | 
				
			||||||
        if resp.ret != 0 {
 | 
					        if resp.ret != 0 {
 | 
				
			||||||
            return Err(libc::ENOMSG);
 | 
					            return Err(resp.ret)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        let fi = resp.fi.unwrap();
 | 
					        let fi = resp.fi.unwrap();
 | 
				
			||||||
        Ok((fi.fh, fi.flags))
 | 
					        Ok((fi.fh, fi.flags))
 | 
				
			||||||
@@ -989,7 +981,7 @@ impl FilesystemMT for LwsVfsIns {
 | 
				
			|||||||
        };
 | 
					        };
 | 
				
			||||||
        log::trace!("get fread resp size is: {}", resp.size);
 | 
					        log::trace!("get fread resp size is: {}", resp.size);
 | 
				
			||||||
        if resp.ret != 0 {
 | 
					        if resp.ret != 0 {
 | 
				
			||||||
            return callback(Err(libc::ENOMSG));
 | 
					            return callback(Err(resp.ret));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        callback(Ok(&resp.buff))
 | 
					        callback(Ok(&resp.buff))
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -1044,7 +1036,7 @@ impl FilesystemMT for LwsVfsIns {
 | 
				
			|||||||
        };
 | 
					        };
 | 
				
			||||||
        log::trace!("get resp: {:?}", resp);
 | 
					        log::trace!("get resp: {:?}", resp);
 | 
				
			||||||
        if resp.ret != 0 {
 | 
					        if resp.ret != 0 {
 | 
				
			||||||
            return Err(libc::ENOMSG);
 | 
					            return Err(resp.ret)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        Ok(resp.size as u32)
 | 
					        Ok(resp.size as u32)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -1089,10 +1081,7 @@ impl FilesystemMT for LwsVfsIns {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
        log::trace!("get resp: {:?}", resp);
 | 
					        log::trace!("get resp: {:?}", resp);
 | 
				
			||||||
        if resp.ret != 0 {
 | 
					        ret2result!(resp.ret)
 | 
				
			||||||
            return Err(libc::ENOMSG);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        Ok(())
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Called when an open file is closed.
 | 
					    /// Called when an open file is closed.
 | 
				
			||||||
@@ -1145,11 +1134,8 @@ impl FilesystemMT for LwsVfsIns {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
        log::trace!("get resp: {:?}", resp);
 | 
					        log::trace!("get resp: {:?}", resp);
 | 
				
			||||||
        if resp.ret != 0 {
 | 
					        ret2result!(resp.ret)
 | 
				
			||||||
            return Err(libc::ENOMSG);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        // self.cache.clr(vec![path]);
 | 
					        // self.cache.clr(vec![path]);
 | 
				
			||||||
        Ok(())
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Write out any pending changes of a file.
 | 
					    /// Write out any pending changes of a file.
 | 
				
			||||||
@@ -1203,7 +1189,7 @@ impl FilesystemMT for LwsVfsIns {
 | 
				
			|||||||
        };
 | 
					        };
 | 
				
			||||||
        log::trace!("get resp: {:?}", resp);
 | 
					        log::trace!("get resp: {:?}", resp);
 | 
				
			||||||
        if resp.ret != 0 {
 | 
					        if resp.ret != 0 {
 | 
				
			||||||
            return Err(libc::ENOMSG);
 | 
					            return Err(resp.ret);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        Ok((resp.fi.unwrap().fh, flags))
 | 
					        Ok((resp.fi.unwrap().fh, flags))
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -1268,7 +1254,7 @@ impl FilesystemMT for LwsVfsIns {
 | 
				
			|||||||
        };
 | 
					        };
 | 
				
			||||||
        log::trace!("get resp: {:?}", resp);
 | 
					        log::trace!("get resp: {:?}", resp);
 | 
				
			||||||
        if resp.ret != 0 {
 | 
					        if resp.ret != 0 {
 | 
				
			||||||
            return Err(libc::ENOMSG);
 | 
					            return Err(resp.ret)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        let mut dirs = vec![];
 | 
					        let mut dirs = vec![];
 | 
				
			||||||
        // let clrs: Vec<String> = resp.dirs.iter().map(|dir| dir.name.clone().into()).collect();
 | 
					        // 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);
 | 
					        log::trace!("get resp: {:?}", resp);
 | 
				
			||||||
        if resp.ret != 0 {
 | 
					        ret2result!(resp.ret)
 | 
				
			||||||
            return Err(libc::ENOMSG);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        Ok(())
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Write out any pending changes to a directory.
 | 
					    /// Write out any pending changes to a directory.
 | 
				
			||||||
@@ -1444,10 +1427,7 @@ impl FilesystemMT for LwsVfsIns {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
        log::trace!("get resp: {:?}", resp);
 | 
					        log::trace!("get resp: {:?}", resp);
 | 
				
			||||||
        if resp.ret != 0 {
 | 
					        ret2result!(resp.ret)
 | 
				
			||||||
            return Err(libc::EACCES);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        Ok(())
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Create and open a new file.
 | 
					    /// Create and open a new file.
 | 
				
			||||||
@@ -1501,15 +1481,17 @@ impl FilesystemMT for LwsVfsIns {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        self.cache.clr(vec![path.to_string()]);
 | 
					        self.cache.clr(vec![path.to_string()]);
 | 
				
			||||||
        let fi = resp.fi.unwrap();
 | 
					        let fi = resp.fi.unwrap();
 | 
				
			||||||
        if let Ok((ttl, attr)) = self.getattr(req, Path::new(&path), Some(fi.fh)) {
 | 
					        match self.getattr(req, Path::new(&path), Some(fi.fh)) {
 | 
				
			||||||
            return Ok(CreatedEntry {
 | 
					            Ok((ttl, attr)) => {
 | 
				
			||||||
                fh: fi.fh,
 | 
					                Ok(CreatedEntry {
 | 
				
			||||||
                flags,
 | 
					                    fh: fi.fh,
 | 
				
			||||||
                attr,
 | 
					                    flags,
 | 
				
			||||||
                ttl,
 | 
					                    attr,
 | 
				
			||||||
            });
 | 
					                    ttl,
 | 
				
			||||||
 | 
					                })
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            Err(e) => Err(e),
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        Err(libc::ENOMSG)
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1594,7 +1576,15 @@ impl LwsVfsIns {
 | 
				
			|||||||
    where
 | 
					    where
 | 
				
			||||||
        F: FilesystemMT + Sync + Send + 'static,
 | 
					        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::mount(
 | 
				
			||||||
            fuse_mt::FuseMT::new(file_system, 10),
 | 
					            fuse_mt::FuseMT::new(file_system, 10),
 | 
				
			||||||
            &mount_point,
 | 
					            &mount_point,
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user