Skip to content

Commit 8e65046

Browse files
committed
Add more necessary logic for bridging with rusqlite-vfs.
1 parent 201649e commit 8e65046

File tree

3 files changed

+36
-64
lines changed

3 files changed

+36
-64
lines changed

src/filesystem.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ pub trait FileSystem: Debug + Sync + Send + 'static {
2121
fn create_dir(&self, path: &str) -> VfsResult<()>;
2222
/// Opens the file at this path for reading
2323
fn open_file(&self, path: &str) -> VfsResult<Box<dyn SeekAndRead>>;
24-
/// Opens the file at this path for reading and writing
25-
fn update_file(&self, path: &str) -> VfsResult<Box<dyn SeekAndReadAndWrite>>;
2624
/// Creates a file at this path for writing
2725
fn create_file(&self, path: &str) -> VfsResult<Box<dyn Write>>;
2826
/// Opens the file at this path for appending
@@ -47,6 +45,32 @@ pub trait FileSystem: Debug + Sync + Send + 'static {
4745
fn move_dir(&self, _src: &str, _dest: &str) -> VfsResult<()> {
4846
Err(VfsError::NotSupported)
4947
}
48+
49+
/// Informs the filesystem to 'flush' its potentially cached information.
50+
///
51+
/// This is, by default, queries the current size.
52+
fn size_hint(&self, path: &str) -> VfsResult<u64> {
53+
self.metadata(path).map(|f| f.len)
54+
}
55+
56+
/// Informs the filesystem to 'flush' its potentially cached information.
57+
///
58+
/// This is, by default, a no-op.
59+
fn sync(&self, _path: &str) -> VfsResult<()> {
60+
Ok(())
61+
}
62+
63+
/// Set a size hint for the associated path.
64+
///
65+
/// This is, by default, a no-op.
66+
fn set_size_hint(&self, _hint: usize, _path: &str) -> VfsResult<()> {
67+
Ok(())
68+
}
69+
70+
/// Opens the file at this path for reading and writing
71+
fn update_file(&self, _path: &str) -> VfsResult<Box<dyn SeekAndReadAndWrite>> {
72+
Err(VfsError::NotSupported)
73+
}
5074
}
5175

5276
impl<T: FileSystem> From<T> for VfsPath {

src/impls/memory.rs

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -120,48 +120,6 @@ impl Seek for ReadableFile {
120120
}
121121
}
122122

123-
struct WriteableAndReadableFile {
124-
content: Cursor<Vec<u8>>,
125-
destination: String,
126-
fs: MemoryFsHandle,
127-
}
128-
129-
impl Write for WriteableAndReadableFile {
130-
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
131-
self.content.write(buf)
132-
}
133-
134-
fn flush(&mut self) -> std::io::Result<()> {
135-
self.content.flush()
136-
}
137-
}
138-
139-
impl Read for WriteableAndReadableFile {
140-
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
141-
self.content.read(buf)
142-
}
143-
}
144-
145-
impl Seek for WriteableAndReadableFile {
146-
fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
147-
self.content.seek(pos)
148-
}
149-
}
150-
151-
impl Drop for WriteableAndReadableFile {
152-
fn drop(&mut self) {
153-
let mut content = vec![];
154-
swap(&mut content, self.content.get_mut());
155-
self.fs.write().unwrap().files.insert(
156-
self.destination.clone(),
157-
MemoryFile {
158-
file_type: VfsFileType::File,
159-
content: Arc::new(content),
160-
},
161-
);
162-
}
163-
}
164-
165123
impl FileSystem for MemoryFS {
166124
fn read_dir(&self, path: &str) -> VfsResult<Box<dyn Iterator<Item = String>>> {
167125
let prefix = format!("{}/", path);
@@ -219,24 +177,6 @@ impl FileSystem for MemoryFS {
219177
}))
220178
}
221179

222-
fn update_file(&self, path: &str) -> VfsResult<Box<dyn crate::SeekAndReadAndWrite>> {
223-
let handle = self.handle.read().unwrap();
224-
let file = handle
225-
.files
226-
.get(path)
227-
.ok_or_else(|| VfsError::FileNotFound {
228-
path: path.to_string(),
229-
})?;
230-
ensure_file(file)?;
231-
let mut content = Cursor::new(file.content.as_ref().clone());
232-
content.seek(SeekFrom::Start(0))?;
233-
Ok(Box::new(WriteableAndReadableFile {
234-
content,
235-
destination: path.to_string(),
236-
fs: self.handle.clone(),
237-
}))
238-
}
239-
240180
fn create_file(&self, path: &str) -> VfsResult<Box<dyn Write>> {
241181
self.ensure_has_parent(path)?;
242182
let content = Arc::new(Vec::<u8>::new());

src/path.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,9 @@ impl VfsPath {
317317
///
318318
/// assert_eq!(&result, "Hello, world!");
319319
///
320-
/// handle.rewind()?;
320+
/// handle.seek(SeekFrom::Start(0))?;
321321
/// handle.write(b"Goodnight, world.")?;
322-
/// handle.rewind()?;
322+
/// handle.seek(SeekFrom::Start(0))?;
323323
///
324324
/// let mut result2 = String::new();
325325
/// handle.read_to_string(&mut result2)?;
@@ -873,6 +873,14 @@ impl VfsPath {
873873
})?;
874874
Ok(())
875875
}
876+
877+
pub fn sync(&mut self) -> VfsResult<()> {
878+
self.fs.fs.sync(&self.path)
879+
}
880+
881+
pub fn set_size_hint(&mut self, size_hint: usize) -> VfsResult<()> {
882+
self.fs.fs.set_size_hint(size_hint, &self.path)
883+
}
876884
}
877885

878886
/// An iterator for recursively walking a file hierarchy

0 commit comments

Comments
 (0)