|
| 1 | +use crate::{DatabaseError, Result, StorageBackend}; |
| 2 | +use std::fs::File; |
| 3 | +use std::io; |
| 4 | + |
| 5 | +#[cfg(feature = "logging")] |
| 6 | +use log::warn; |
| 7 | + |
| 8 | +#[cfg(unix)] |
| 9 | +use std::os::unix::fs::FileExt; |
| 10 | + |
| 11 | +#[cfg(windows)] |
| 12 | +use std::os::windows::fs::FileExt; |
| 13 | + |
| 14 | +#[cfg(target_os = "wasi")] |
| 15 | +use std::os::wasi::fs::FileExt; |
| 16 | + |
| 17 | +#[cfg(target_os = "macos")] |
| 18 | +use std::os::unix::io::AsRawFd; |
| 19 | + |
| 20 | +/// Stores a database as a file on-disk. |
| 21 | +#[derive(Debug)] |
| 22 | +pub struct FileBackend { |
| 23 | + lock_supported: bool, |
| 24 | + file: File, |
| 25 | +} |
| 26 | + |
| 27 | +impl FileBackend { |
| 28 | + /// Creates a new backend which stores data to the given file. |
| 29 | + pub fn new(file: File) -> Result<Self, DatabaseError> { |
| 30 | + match file.try_lock() { |
| 31 | + Ok(locked) => { |
| 32 | + if locked { |
| 33 | + Ok(Self { |
| 34 | + file, |
| 35 | + lock_supported: true, |
| 36 | + }) |
| 37 | + } else { |
| 38 | + Err(DatabaseError::DatabaseAlreadyOpen) |
| 39 | + } |
| 40 | + } |
| 41 | + Err(err) if err.kind() == io::ErrorKind::Unsupported => { |
| 42 | + #[cfg(feature = "logging")] |
| 43 | + warn!( |
| 44 | + "File locks not supported on this platform. You must ensure that only a single process opens the database file, at a time" |
| 45 | + ); |
| 46 | + |
| 47 | + Ok(Self { |
| 48 | + file, |
| 49 | + lock_supported: false, |
| 50 | + }) |
| 51 | + } |
| 52 | + Err(err) => Err(err.into()), |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl StorageBackend for FileBackend { |
| 58 | + fn len(&self) -> Result<u64, io::Error> { |
| 59 | + Ok(self.file.metadata()?.len()) |
| 60 | + } |
| 61 | + |
| 62 | + #[cfg(any(unix, target_os = "wasi"))] |
| 63 | + fn read(&self, offset: u64, len: usize) -> Result<Vec<u8>, io::Error> { |
| 64 | + let mut buffer = vec![0; len]; |
| 65 | + self.file.read_exact_at(&mut buffer, offset)?; |
| 66 | + Ok(buffer) |
| 67 | + } |
| 68 | + |
| 69 | + #[cfg(windows)] |
| 70 | + fn read(&self, mut offset: u64, len: usize) -> std::result::Result<Vec<u8>, io::Error> { |
| 71 | + let mut buffer = vec![0; len]; |
| 72 | + let mut data_offset = 0; |
| 73 | + while data_offset < buffer.len() { |
| 74 | + let read = self.file.seek_read(&mut buffer[data_offset..], offset)?; |
| 75 | + offset += read as u64; |
| 76 | + data_offset += read; |
| 77 | + } |
| 78 | + Ok(buffer) |
| 79 | + } |
| 80 | + |
| 81 | + fn set_len(&self, len: u64) -> Result<(), io::Error> { |
| 82 | + self.file.set_len(len) |
| 83 | + } |
| 84 | + |
| 85 | + #[cfg(not(target_os = "macos"))] |
| 86 | + fn sync_data(&self, _: bool) -> Result<(), io::Error> { |
| 87 | + self.file.sync_data() |
| 88 | + } |
| 89 | + |
| 90 | + #[cfg(target_os = "macos")] |
| 91 | + fn sync_data(&self, eventual: bool) -> Result<(), io::Error> { |
| 92 | + if eventual { |
| 93 | + let code = unsafe { libc::fcntl(self.file.as_raw_fd(), libc::F_BARRIERFSYNC) }; |
| 94 | + if code == -1 { |
| 95 | + Err(io::Error::last_os_error()) |
| 96 | + } else { |
| 97 | + Ok(()) |
| 98 | + } |
| 99 | + } else { |
| 100 | + self.file.sync_data() |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + #[cfg(any(unix, target_os = "wasi"))] |
| 105 | + fn write(&self, offset: u64, data: &[u8]) -> Result<(), io::Error> { |
| 106 | + self.file.write_all_at(data, offset) |
| 107 | + } |
| 108 | + |
| 109 | + #[cfg(windows)] |
| 110 | + fn write(&self, mut offset: u64, data: &[u8]) -> std::result::Result<(), io::Error> { |
| 111 | + let mut data_offset = 0; |
| 112 | + while data_offset < data.len() { |
| 113 | + let written = self.file.seek_write(&data[data_offset..], offset)?; |
| 114 | + offset += written as u64; |
| 115 | + data_offset += written; |
| 116 | + } |
| 117 | + Ok(()) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +impl Drop for FileBackend { |
| 122 | + fn drop(&mut self) { |
| 123 | + if self.lock_supported { |
| 124 | + let _ = self.file.unlock(); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments