日常同步代码。更新测试项

This commit is contained in:
Ekko.bao 2024-07-04 20:43:28 +08:00
parent 6586dab3f0
commit 171a1fa2af
2 changed files with 74 additions and 51 deletions

View File

@ -1,15 +1,15 @@
use std::fs;
use std::io::{self, Seek as ioSeek};
use std::error::Error;
use std::io::{self, Seek as ioSeek};
use crate::lws_vfs::{Fstat, FileInfo};
use crate::lws_vfs::{FileInfo, Fstat};
extern crate winapi;
use io::Write;
use winapi::um::winnt::{FILE_ATTRIBUTE_READONLY};
use std::os::windows::fs::MetadataExt;
use std::os::windows::prelude::*;
use winapi::um::winnt::FILE_ATTRIBUTE_READONLY;
fn from_metadata(sta: &mut Fstat, metadata: &fs::Metadata) -> Result<(), Box<dyn Error>> {
sta.fst_size = metadata.len();
@ -29,14 +29,20 @@ 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(path: &String, _fi: &mut FileInfo) -> Result<fs::File, Box<dyn Error>> {
return Ok(fs::File::open(path)?);
}
pub fn fread(path: &String, buffer:&mut Vec<u8>, _size:&mut usize, offsize: usize, _fi: &mut FileInfo) -> Result<i32, Box<dyn Error>> {
pub fn fread(
path: &String,
buffer: &mut Vec<u8>,
_size: &mut usize,
offsize: usize,
_fi: &mut FileInfo,
) -> Result<i32, Box<dyn Error>> {
let mut file = fs::File::open(path)?;
match file.seek_read(buffer,offsize as u64){
Ok(_) =>{},
match file.seek_read(buffer, offsize as u64) {
Ok(_) => {}
Err(_) => {
buffer.resize(file.stream_position()? as usize - offsize, 0);
}
@ -44,18 +50,28 @@ pub fn fread(path: &String, buffer:&mut Vec<u8>, _size:&mut usize, offsize: usiz
Ok(0)
}
pub fn fwrite(path: &String, buffer: &Vec<u8>, size: &mut usize, offsize: usize, _fi: &mut FileInfo,
pub fn fwrite(
path: &String,
buffer: &Vec<u8>,
size: &mut usize,
offsize: usize,
_fi: &mut FileInfo,
) -> Result<i32, Box<dyn Error>> {
let mut file = fs::File::open(path)?;
match file.seek_write(buffer,offsize as u64){
Ok(_) =>{},
match file.seek_write(buffer, offsize as u64) {
Ok(_) => {}
Err(_) => {
*size = file.stream_position()? as usize - offsize;
}
}
Ok(0)
}
pub fn freaddir(path: &String, buffer: &mut Vec<String>, _size: usize, _offset: usize, _fi: &mut FileInfo,
pub fn freaddir(
path: &String,
buffer: &mut Vec<String>,
_size: usize,
_offset: usize,
_fi: &mut FileInfo,
) -> Result<i32, Box<dyn Error>> {
let mut readed = fs::read_dir(path)?;
loop {
@ -82,16 +98,19 @@ pub fn fchown(_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).truncate(true).open(path)?;
fs::OpenOptions::new()
.write(true)
.truncate(true)
.open(path)?;
Ok(0)
}
use std::fs::OpenOptions;
use winapi::um::fileapi::SetFileTime;
use winapi::shared::minwindef::FILETIME;
use winapi::shared::minwindef::DWORD;
use std::ptr::null_mut;
use winapi::ctypes::c_void;
use winapi::shared::minwindef::DWORD;
use winapi::shared::minwindef::FILETIME;
use winapi::um::fileapi::SetFileTime;
fn system_time_to_file_time(seconds: u64, nanoseconds: u32) -> FILETIME {
let total_nanoseconds = (seconds * 1_000_000_000) + nanoseconds as u64;
let total_100_nanoseconds = total_nanoseconds / 100;
@ -100,16 +119,14 @@ 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>> {
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 handle = file.as_raw_handle();
let atime = system_time_to_file_time(a[0], a[1] as u32);
let mtime = system_time_to_file_time(m[0], m[1] as u32);
let result = unsafe {
SetFileTime(handle as *mut c_void, null_mut(), &atime, &mtime)
};
let result = unsafe { SetFileTime(handle as *mut c_void, null_mut(), &atime, &mtime) };
if result != 0 {
Ok(0)
@ -120,7 +137,12 @@ pub fn futimens(path: &String, a:& Vec<u64>, m: & Vec<u64>) -> Result<i32, Box<d
pub fn frelease(_path: &String, _fi: &mut FileInfo) -> Result<i32, Box<dyn Error>> {
Ok(0)
}
pub fn fsetxattr(_path: &String,_name: &str,_value:&Vec<u8>,_size: usize,_flags: u32
pub fn fsetxattr(
_path: &String,
_name: &str,
_value: &Vec<u8>,
_size: usize,
_flags: u32,
) -> Result<i32, Box<dyn Error>> {
Ok(-1)
}
@ -128,9 +150,9 @@ pub fn fgetxattr(_path: &String, _name: &str, _size: usize) -> Result<i32, Box<d
Ok(-1)
}
fn get_file_mode_mask(path:&str) -> u32{
fn get_file_mode_mask(path: &str) -> u32 {
let mut permissions = 0;
let metadata = match fs::metadata(path){
let metadata = match fs::metadata(path) {
Ok(metadata) => metadata,
Err(_) => {
println!("get {} metadata fail", path);
@ -151,7 +173,7 @@ fn get_file_mode_mask(path:&str) -> u32{
}
pub fn faccess(path: &String, mask: u32) -> Result<i32, Box<dyn Error>> {
let permissions = get_file_mode_mask(path);
Ok(if permissions & mask != 0 {0} else {-1})
Ok(if permissions & mask != 0 { 0 } else { -1 })
}
#[test]
@ -159,38 +181,38 @@ fn test() {
use crate::fs_impl::*;
let path = String::from("test.txt");
let mut file: fs::File = fs::File::open("test.txt").unwrap();
let _ = file.write(b"This is a test file.\n");
let mut buffer = vec![0; 100];
let _ = file.seek_write(b"This is a test file.\n", 0);
drop(file);
let mut size = 0;
let mut offsize: usize = 0;
let mut fi = FileInfo::default();
size = 4;
size = 4;
offsize = 5;
let mut buffer = vec![0; size];
offsize = 5;
let _ = fread(&path, &mut buffer, &mut size, offsize, &mut fi);
assert_eq!(buffer.len(), 4);
assert_eq!(buffer, b"is a");
let result = fwrite(&path, &buffer, &mut size, offsize, &mut fi);
println!("result: {:?}", result);
println!("size: {:?}", size);
println!("buffer: {:?}", buffer);
let result = freaddir(&path, &mut vec![], 0, 0, &mut fi);
println!("result: {:?}", result);
println!("buffer: {:?}", buffer);
let result = fmkdir(&path, 0);
println!("result: {:?}", result);
let result = fchown(&path, 0, 0);
println!("result: {:?}", result);
let result = ftruncate(&path, 100);
println!("result: {:?}", result);
let result = futimens(&path, &vec![100, 0], &vec![100, 0]);
println!("result: {:?}", result);
let result = frelease(&path, &mut fi);
println!("result: {:?}", result);
let result = fsetxattr(&path, "test", &vec![1,2,3], 3, 0);
println!("result: {:?}", result);
let result = fgetxattr(&path, "test", 100);
println!("result: {:?}", result);
let result = faccess(&path, 0o777);
println!("result: {:?}", result);
// let result = fwrite(&path, &buffer, &mut size, offsize, &mut fi);
// println!("result: {:?}", result);
// println!("size: {:?}", size);
// println!("buffer: {:?}", buffer);
// let result = freaddir(&path, &mut vec![], 0, 0, &mut fi);
// println!("result: {:?}", result);
// println!("buffer: {:?}", buffer);
// let result = fmkdir(&path, 0);
// println!("result: {:?}", result);
// let result = fchown(&path, 0, 0);
// println!("result: {:?}", result);
// let result = ftruncate(&path, 100);
// println!("result: {:?}", result);
// let result = futimens(&path, &vec![100, 0], &vec![100, 0]);
// println!("result: {:?}", result);
// let result = frelease(&path, &mut fi);
// println!("result: {:?}", result);
// let result = fsetxattr(&path, "test", &vec![1,2,3], 3, 0);
// println!("result: {:?}", result);
// let result = fgetxattr(&path, "test", 100);
// println!("result: {:?}", result);
// let result = faccess(&path, 0o777);
// println!("result: {:?}", result);
}

1
test.txt Normal file
View File

@ -0,0 +1 @@
This is a test file.