|
| 1 | +use std::io; |
| 2 | + |
| 3 | +use web_sys::{FileSystemFileHandle, FileSystemReadWriteOptions, FileSystemSyncAccessHandle}; |
| 4 | + |
| 5 | +use crate::{disk::opfs::promise, error::wasm_err, Error, IoBuf, IoBufMut, Read, Write}; |
| 6 | + |
| 7 | +/// OPFS based on [FileSystemWritableFileStream](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle) |
| 8 | +/// This file is only accessible inside dedicated Web Workers. |
| 9 | +pub struct OPFSSyncFile { |
| 10 | + file_handle: FileSystemFileHandle, |
| 11 | + access_handle: Option<FileSystemSyncAccessHandle>, |
| 12 | +} |
| 13 | + |
| 14 | +impl OPFSSyncFile { |
| 15 | + pub(crate) async fn new(file_handle: FileSystemFileHandle) -> Result<Self, Error> { |
| 16 | + let js_promise = file_handle.create_sync_access_handle(); |
| 17 | + let access_handle = Some(promise::<FileSystemSyncAccessHandle>(js_promise).await?); |
| 18 | + Ok(Self { |
| 19 | + file_handle, |
| 20 | + access_handle, |
| 21 | + }) |
| 22 | + } |
| 23 | + |
| 24 | + pub(crate) async fn access_handle(&mut self) -> &FileSystemSyncAccessHandle { |
| 25 | + if self.access_handle.is_none() { |
| 26 | + let js_promise = self.file_handle.create_sync_access_handle(); |
| 27 | + self.access_handle = Some( |
| 28 | + promise::<FileSystemSyncAccessHandle>(js_promise) |
| 29 | + .await |
| 30 | + .unwrap(), |
| 31 | + ); |
| 32 | + } |
| 33 | + self.access_handle.as_ref().unwrap() |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl Write for OPFSSyncFile { |
| 38 | + /// Attempts to write an entire buffer into the file. |
| 39 | + /// |
| 40 | + /// No changes are written to the actual file on disk until [`OPFSFile::close`] has been called. |
| 41 | + /// See more detail in [write](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/write) |
| 42 | + async fn write_all<B: IoBuf>(&mut self, buf: B) -> (Result<(), Error>, B) { |
| 43 | + match self |
| 44 | + .access_handle() |
| 45 | + .await |
| 46 | + .write_with_u8_array(buf.as_slice()) |
| 47 | + { |
| 48 | + Ok(_) => (Ok(()), buf), |
| 49 | + Err(err) => (Err(wasm_err(err)), buf), |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// Persists any changes made to the file. |
| 54 | + /// See more detail in [write](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/flush) |
| 55 | + async fn flush(&mut self) -> Result<(), Error> { |
| 56 | + self.access_handle().await.flush().map_err(wasm_err) |
| 57 | + } |
| 58 | + |
| 59 | + async fn close(&mut self) -> Result<(), Error> { |
| 60 | + if let Some(access_handle) = self.access_handle.take() { |
| 61 | + access_handle.close(); |
| 62 | + } |
| 63 | + Ok(()) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl Read for OPFSSyncFile { |
| 68 | + /// Reads the exact number of bytes required to fill `buf` at `pos`. |
| 69 | + /// |
| 70 | + /// # Errors |
| 71 | + /// |
| 72 | + /// If the operation encounters an "end of file" before completely |
| 73 | + /// filling the buffer, it returns an error of [`crate::Error`]. |
| 74 | + async fn read_exact_at<B: IoBufMut>(&mut self, mut buf: B, pos: u64) -> (Result<(), Error>, B) { |
| 75 | + let buf_len = buf.bytes_init() as i32; |
| 76 | + let options = FileSystemReadWriteOptions::new(); |
| 77 | + options.set_at(pos as f64); |
| 78 | + |
| 79 | + let access_handle = self.access_handle().await; |
| 80 | + let size = access_handle |
| 81 | + .get_size() |
| 82 | + .expect("InvalidStateError: file is already closed."); |
| 83 | + if (size.round() as u64) < pos + buf_len as u64 { |
| 84 | + return ( |
| 85 | + Err(Error::Io(io::Error::new( |
| 86 | + io::ErrorKind::UnexpectedEof, |
| 87 | + "Read unexpected eof", |
| 88 | + ))), |
| 89 | + buf, |
| 90 | + ); |
| 91 | + } |
| 92 | + match access_handle.read_with_u8_array_and_options(buf.as_slice_mut(), &options) { |
| 93 | + Ok(_) => (Ok(()), buf), |
| 94 | + Err(err) => (Err(wasm_err(err)), buf), |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /// Reads all bytes until EOF in this source, placing them into `buf`. |
| 99 | + /// |
| 100 | + /// # Errors |
| 101 | + /// |
| 102 | + /// If an error is encountered then the `read_to_end_at` operation |
| 103 | + /// immediately completes. |
| 104 | + async fn read_to_end_at(&mut self, mut buf: Vec<u8>, pos: u64) -> (Result<(), Error>, Vec<u8>) { |
| 105 | + let options = FileSystemReadWriteOptions::new(); |
| 106 | + options.set_at(pos as f64); |
| 107 | + |
| 108 | + let access_handle = self.access_handle().await; |
| 109 | + let size = access_handle |
| 110 | + .get_size() |
| 111 | + .expect("InvalidStateError: file is already closed."); |
| 112 | + let buf_len = size.round() as usize - pos as usize; |
| 113 | + if buf_len == 0 { |
| 114 | + return ( |
| 115 | + Err(Error::Io(io::Error::new( |
| 116 | + io::ErrorKind::UnexpectedEof, |
| 117 | + "Read unexpected eof", |
| 118 | + ))), |
| 119 | + buf, |
| 120 | + ); |
| 121 | + } |
| 122 | + buf.resize(buf_len, 0); |
| 123 | + |
| 124 | + match access_handle.read_with_u8_array_and_options(buf.as_slice_mut(), &options) { |
| 125 | + Ok(_) => (Ok(()), buf), |
| 126 | + Err(err) => (Err(wasm_err(err)), buf), |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /// Return the size of file in bytes. |
| 131 | + async fn size(&self) -> Result<u64, Error> { |
| 132 | + match self.access_handle.as_ref() { |
| 133 | + Some(access_handle) => access_handle |
| 134 | + .get_size() |
| 135 | + .map(|sz| sz.round() as u64) |
| 136 | + .map_err(wasm_err), |
| 137 | + None => { |
| 138 | + // FIXME: here should throw an error |
| 139 | + let js_promise = self.file_handle.create_sync_access_handle(); |
| 140 | + let access_handle = promise::<FileSystemSyncAccessHandle>(js_promise) |
| 141 | + .await |
| 142 | + .unwrap(); |
| 143 | + let result = access_handle |
| 144 | + .get_size() |
| 145 | + .map(|sz| sz.round() as u64) |
| 146 | + .map_err(wasm_err); |
| 147 | + |
| 148 | + access_handle.close(); |
| 149 | + |
| 150 | + result |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +impl Drop for OPFSSyncFile { |
| 157 | + fn drop(&mut self) { |
| 158 | + if let Some(access_handle) = self.access_handle.take() { |
| 159 | + access_handle.close(); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments