Files
turso/core/io/windows.rs
Pekka Enberg 351242561d Kill anyhow usage
Switch anyhow to explicit `LimboError` type using thiserror crate, which
lets us make error handling more structured.
2024-07-25 17:15:08 +03:00

68 lines
1.5 KiB
Rust

use crate::{Completion, File, Result, WriteCompletion, IO};
use log::trace;
use std::cell::RefCell;
use std::io::{Read, Seek, Write};
use std::rc::Rc;
pub struct WindowsIO {}
impl WindowsIO {
pub fn new() -> Result<Self> {
Ok(Self {})
}
}
impl IO for WindowsIO {
fn open_file(&self, path: &str) -> Result<Rc<dyn File>> {
trace!("open_file(path = {})", path);
let file = std::fs::File::open(path)?;
Ok(Rc::new(WindowsFile {
file: RefCell::new(file),
}))
}
fn run_once(&self) -> Result<()> {
Ok(())
}
}
pub struct WindowsFile {
file: RefCell<std::fs::File>,
}
impl File for WindowsFile {
fn lock_file(&self, exclusive: bool) -> Result<()> {
unimplemented!()
}
fn unlock_file(&self) -> Result<()> {
unimplemented!()
}
fn pread(&self, pos: usize, c: Rc<Completion>) -> Result<()> {
let mut file = self.file.borrow_mut();
file.seek(std::io::SeekFrom::Start(pos as u64))?;
{
let mut buf = c.buf_mut();
let buf = buf.as_mut_slice();
file.read_exact(buf)?;
}
c.complete();
Ok(())
}
fn pwrite(
&self,
pos: usize,
buffer: Rc<RefCell<crate::Buffer>>,
_c: Rc<WriteCompletion>,
) -> Result<()> {
let mut file = self.file.borrow_mut();
file.seek(std::io::SeekFrom::Start(pos as u64))?;
let buf = buffer.borrow();
let buf = buf.as_slice();
file.write_all(buf)?;
Ok(())
}
}