日常同步代码。更新测试项
This commit is contained in:
parent
6586dab3f0
commit
171a1fa2af
|
@ -1,15 +1,15 @@
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
use std::io::{self, Seek as ioSeek};
|
|
||||||
use std::error::Error;
|
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;
|
extern crate winapi;
|
||||||
use io::Write;
|
use io::Write;
|
||||||
use winapi::um::winnt::{FILE_ATTRIBUTE_READONLY};
|
|
||||||
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;
|
||||||
|
|
||||||
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>> {
|
||||||
sta.fst_size = metadata.len();
|
sta.fst_size = metadata.len();
|
||||||
|
@ -33,10 +33,16 @@ pub fn fopen(path: &String, _fi: &mut FileInfo) -> Result<fs::File, Box<dyn Erro
|
||||||
return Ok(fs::File::open(path)?);
|
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)?;
|
let mut file = fs::File::open(path)?;
|
||||||
match file.seek_read(buffer, offsize as u64) {
|
match file.seek_read(buffer, offsize as u64) {
|
||||||
Ok(_) =>{},
|
Ok(_) => {}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
buffer.resize(file.stream_position()? as usize - offsize, 0);
|
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)
|
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>> {
|
) -> Result<i32, Box<dyn Error>> {
|
||||||
let mut file = fs::File::open(path)?;
|
let mut file = fs::File::open(path)?;
|
||||||
match file.seek_write(buffer, offsize as u64) {
|
match file.seek_write(buffer, offsize as u64) {
|
||||||
Ok(_) =>{},
|
Ok(_) => {}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
*size = file.stream_position()? as usize - offsize;
|
*size = file.stream_position()? as usize - offsize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(0)
|
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>> {
|
) -> Result<i32, Box<dyn Error>> {
|
||||||
let mut readed = fs::read_dir(path)?;
|
let mut readed = fs::read_dir(path)?;
|
||||||
loop {
|
loop {
|
||||||
|
@ -82,16 +98,19 @@ pub fn fchown(_path: &String, _uid: u32, _gid: u32) -> Result<i32, Box<dyn Error
|
||||||
Ok(-1)
|
Ok(-1)
|
||||||
}
|
}
|
||||||
pub fn ftruncate(path: &String, _size: usize) -> Result<i32, Box<dyn Error>> {
|
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)
|
Ok(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
use std::fs::OpenOptions;
|
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 std::ptr::null_mut;
|
||||||
use winapi::ctypes::c_void;
|
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 {
|
fn system_time_to_file_time(seconds: u64, nanoseconds: u32) -> FILETIME {
|
||||||
let total_nanoseconds = (seconds * 1_000_000_000) + nanoseconds as u64;
|
let total_nanoseconds = (seconds * 1_000_000_000) + nanoseconds as u64;
|
||||||
let total_100_nanoseconds = total_nanoseconds / 100;
|
let total_100_nanoseconds = total_nanoseconds / 100;
|
||||||
|
@ -107,9 +126,7 @@ pub fn futimens(path: &String, a:& Vec<u64>, m: & Vec<u64>) -> Result<i32, Box<d
|
||||||
let atime = system_time_to_file_time(a[0], a[1] as u32);
|
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 mtime = system_time_to_file_time(m[0], m[1] as u32);
|
||||||
|
|
||||||
let result = unsafe {
|
let result = unsafe { SetFileTime(handle as *mut c_void, null_mut(), &atime, &mtime) };
|
||||||
SetFileTime(handle as *mut c_void, null_mut(), &atime, &mtime)
|
|
||||||
};
|
|
||||||
|
|
||||||
if result != 0 {
|
if result != 0 {
|
||||||
Ok(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>> {
|
pub fn frelease(_path: &String, _fi: &mut FileInfo) -> Result<i32, Box<dyn Error>> {
|
||||||
Ok(0)
|
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>> {
|
) -> Result<i32, Box<dyn Error>> {
|
||||||
Ok(-1)
|
Ok(-1)
|
||||||
}
|
}
|
||||||
|
@ -159,38 +181,38 @@ fn test() {
|
||||||
use crate::fs_impl::*;
|
use crate::fs_impl::*;
|
||||||
let path = String::from("test.txt");
|
let path = String::from("test.txt");
|
||||||
let mut file: fs::File = fs::File::open("test.txt").unwrap();
|
let mut file: fs::File = fs::File::open("test.txt").unwrap();
|
||||||
let _ = file.write(b"This is a test file.\n");
|
let _ = file.seek_write(b"This is a test file.\n", 0);
|
||||||
let mut buffer = vec![0; 100];
|
drop(file);
|
||||||
let mut size = 0;
|
let mut size = 0;
|
||||||
let mut offsize: usize = 0;
|
let mut offsize: usize = 0;
|
||||||
let mut fi = FileInfo::default();
|
let mut fi = FileInfo::default();
|
||||||
size = 4;
|
size = 4;
|
||||||
let mut buffer = vec![0; size];
|
|
||||||
offsize = 5;
|
offsize = 5;
|
||||||
|
let mut buffer = vec![0; size];
|
||||||
let _ = fread(&path, &mut buffer, &mut size, offsize, &mut fi);
|
let _ = fread(&path, &mut buffer, &mut size, offsize, &mut fi);
|
||||||
assert_eq!(buffer.len(), 4);
|
assert_eq!(buffer.len(), 4);
|
||||||
assert_eq!(buffer, b"is a");
|
assert_eq!(buffer, b"is a");
|
||||||
let result = fwrite(&path, &buffer, &mut size, offsize, &mut fi);
|
// let result = fwrite(&path, &buffer, &mut size, offsize, &mut fi);
|
||||||
println!("result: {:?}", result);
|
// println!("result: {:?}", result);
|
||||||
println!("size: {:?}", size);
|
// println!("size: {:?}", size);
|
||||||
println!("buffer: {:?}", buffer);
|
// println!("buffer: {:?}", buffer);
|
||||||
let result = freaddir(&path, &mut vec![], 0, 0, &mut fi);
|
// let result = freaddir(&path, &mut vec![], 0, 0, &mut fi);
|
||||||
println!("result: {:?}", result);
|
// println!("result: {:?}", result);
|
||||||
println!("buffer: {:?}", buffer);
|
// println!("buffer: {:?}", buffer);
|
||||||
let result = fmkdir(&path, 0);
|
// let result = fmkdir(&path, 0);
|
||||||
println!("result: {:?}", result);
|
// println!("result: {:?}", result);
|
||||||
let result = fchown(&path, 0, 0);
|
// let result = fchown(&path, 0, 0);
|
||||||
println!("result: {:?}", result);
|
// println!("result: {:?}", result);
|
||||||
let result = ftruncate(&path, 100);
|
// let result = ftruncate(&path, 100);
|
||||||
println!("result: {:?}", result);
|
// println!("result: {:?}", result);
|
||||||
let result = futimens(&path, &vec![100, 0], &vec![100, 0]);
|
// let result = futimens(&path, &vec![100, 0], &vec![100, 0]);
|
||||||
println!("result: {:?}", result);
|
// println!("result: {:?}", result);
|
||||||
let result = frelease(&path, &mut fi);
|
// let result = frelease(&path, &mut fi);
|
||||||
println!("result: {:?}", result);
|
// println!("result: {:?}", result);
|
||||||
let result = fsetxattr(&path, "test", &vec![1,2,3], 3, 0);
|
// let result = fsetxattr(&path, "test", &vec![1,2,3], 3, 0);
|
||||||
println!("result: {:?}", result);
|
// println!("result: {:?}", result);
|
||||||
let result = fgetxattr(&path, "test", 100);
|
// let result = fgetxattr(&path, "test", 100);
|
||||||
println!("result: {:?}", result);
|
// println!("result: {:?}", result);
|
||||||
let result = faccess(&path, 0o777);
|
// let result = faccess(&path, 0o777);
|
||||||
println!("result: {:?}", result);
|
// println!("result: {:?}", result);
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user