use std::ffi::OsString;
use std::fs::{self, File, OpenOptions};
use std::io;
use std::path::{Path, PathBuf};
use super::{InitHooks, InitStage, layout};
pub(crate) enum RootKind {
Missing,
Empty,
Existing,
}
pub(crate) struct CreatedPaths {
root: PathBuf,
root_created: bool,
paths: Vec<PathBuf>,
}
impl CreatedPaths {
pub(crate) fn new(root: &Path, root_created: bool) -> Self {
Self {
root: root.to_path_buf(),
root_created,
paths: Vec::new(),
}
}
pub(crate) fn record(&mut self, path: PathBuf) {
self.paths.push(path);
}
pub(crate) fn cleanup(mut self) {
while let Some(path) = self.paths.pop() {
if path.is_dir() {
let _ = fs::remove_dir(&path);
} else {
let _ = fs::remove_file(&path);
}
}
if self.root_created {
let _ = fs::remove_dir(&self.root);
}
}
}
pub(crate) fn classify_root(root: &Path) -> io::Result<RootKind> {
if !root.exists() {
return Ok(RootKind::Missing);
}
if !root.is_dir() {
return Ok(RootKind::Existing);
}
if fs::read_dir(root)?.next().is_none() {
Ok(RootKind::Empty)
} else {
Ok(RootKind::Existing)
}
}
pub(crate) fn create_root(root: &Path) -> io::Result<()> {
fs::create_dir(root)
}
pub(crate) fn validate_root_layout(root: &Path) -> io::Result<bool> {
let mut data_found = false;
let mut log_found = false;
for entry in fs::read_dir(root)? {
let entry = entry?;
let file_type = entry.file_type()?;
match entry.file_name().to_str() {
Some("data") if file_type.is_dir() && !data_found => data_found = true,
Some("log") if file_type.is_dir() && !log_found => log_found = true,
_ => return Ok(false),
}
}
Ok(data_found && log_found)
}
pub(crate) fn create_initial_log_file<H: InitHooks>(
root: &Path,
created: &mut CreatedPaths,
hooks: &H,
) -> io::Result<()> {
let path = layout::initial_log_file(root);
let file = OpenOptions::new()
.write(true)
.create_new(true)
.open(&path)?;
created.record(path);
hooks.sync_file(&file, InitStage::LogFile)?;
hooks.sync_directory(&layout::log_dir(root), InitStage::LogDirectory)
}
pub(crate) fn create_initial_files<H: InitHooks>(
root: &Path,
created: &mut CreatedPaths,
hooks: &H,
) -> io::Result<()> {
let data = layout::data_dir(root);
fs::create_dir(&data)?;
created.record(data.clone());
let log = layout::log_dir(root);
fs::create_dir(&log)?;
created.record(log);
for level in 0_u8..=14 {
let path = layout::active_file(root, level);
let file = OpenOptions::new()
.write(true)
.create_new(true)
.open(&path)?;
created.record(path);
hooks.sync_file(&file, InitStage::ActiveFile(level))?;
}
hooks.sync_directory(&data, InitStage::DataDirectory)?;
hooks.sync_directory(root, InitStage::RootDirectory)?;
Ok(())
}
#[cfg(unix)]
fn open_data_directory(path: &Path) -> io::Result<File> {
use std::os::unix::fs::OpenOptionsExt;
let file = OpenOptions::new()
.read(true)
.custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK | libc::O_DIRECTORY)
.open(path)
.map_err(|error| {
if error.raw_os_error() == Some(libc::ELOOP) {
invalid_data("data directory is a symbolic link")
} else {
error
}
})?;
if !file.metadata()?.is_dir() {
return Err(invalid_data("data path is not a directory"));
}
Ok(file)
}
#[cfg(windows)]
fn open_data_directory(path: &Path) -> io::Result<File> {
use std::os::windows::fs::{MetadataExt, OpenOptionsExt};
const FILE_ATTRIBUTE_REPARSE_POINT: u32 = 0x0000_0400;
const FILE_FLAG_BACKUP_SEMANTICS: u32 = 0x0200_0000;
const FILE_FLAG_OPEN_REPARSE_POINT: u32 = 0x0020_0000;
let file = OpenOptions::new()
.read(true)
.custom_flags(FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT)
.open(path)?;
let metadata = file.metadata()?;
if metadata.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT != 0 {
return Err(invalid_data("data directory is a reparse point"));
}
if !metadata.is_dir() {
return Err(invalid_data("data path is not a directory"));
}
Ok(file)
}
#[cfg(not(any(unix, windows)))]
fn open_data_directory(path: &Path) -> io::Result<File> {
let metadata = fs::symlink_metadata(path)?;
if metadata.file_type().is_symlink() {
return Err(invalid_data("data directory is a symbolic link"));
}
if !metadata.is_dir() {
return Err(invalid_data("data path is not a directory"));
}
File::open(path)
}
#[cfg(unix)]
fn sync_open_directory(directory: &File) -> io::Result<()> {
directory.sync_all()
}
#[cfg(windows)]
fn sync_open_directory(directory: &File) -> io::Result<()> {
match directory.sync_all() {
Ok(()) => Ok(()),
Err(error)
if matches!(
error.kind(),
io::ErrorKind::PermissionDenied | io::ErrorKind::InvalidInput
) =>
{
Ok(())
}
Err(error) => Err(error),
}
}
#[cfg(not(any(unix, windows)))]
fn sync_open_directory(directory: &File) -> io::Result<()> {
directory.sync_all()
}
#[cfg(unix)]
fn open_data_file(path: &Path, write: bool) -> io::Result<File> {
use std::os::unix::fs::OpenOptionsExt;
let mut options = OpenOptions::new();
options
.read(true)
.write(write)
.custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK);
match options.open(path) {
Ok(file) => Ok(file),
Err(error) if error.raw_os_error() == Some(libc::ELOOP) => {
Err(invalid_data("data path is a symbolic link"))
}
Err(error) => Err(error),
}
}
#[cfg(windows)]
fn open_data_file(path: &Path, write: bool) -> io::Result<File> {
use std::os::windows::fs::{MetadataExt, OpenOptionsExt};
const FILE_ATTRIBUTE_REPARSE_POINT: u32 = 0x0000_0400;
const FILE_FLAG_OPEN_REPARSE_POINT: u32 = 0x0020_0000;
let mut options = OpenOptions::new();
options
.read(true)
.write(write)
.custom_flags(FILE_FLAG_OPEN_REPARSE_POINT);
let file = options.open(path)?;
if file.metadata()?.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT != 0 {
return Err(invalid_data("data path is a reparse point"));
}
Ok(file)
}
#[cfg(not(any(unix, windows)))]
fn open_data_file(path: &Path, write: bool) -> io::Result<File> {
let metadata = fs::symlink_metadata(path)?;
if metadata.file_type().is_symlink() {
return Err(invalid_data("data path is a symbolic link"));
}
OpenOptions::new().read(true).write(write).open(path)
}
fn precheck_regular_path(path: &Path, message: &str) -> io::Result<()> {
let metadata = fs::symlink_metadata(path)?;
let file_type = metadata.file_type();
if file_type.is_symlink() || !file_type.is_file() {
return Err(invalid_data(message));
}
Ok(())
}
fn validate_regular_file(file: &File, message: &str) -> io::Result<fs::Metadata> {
let metadata = file.metadata()?;
if !metadata.is_file() {
return Err(invalid_data(message));
}
Ok(metadata)
}
pub(crate) fn validate_segment_files(root: &Path, state: &super::state::State) -> io::Result<()> {
let data_dir = layout::data_dir(root);
let _data_directory = open_data_directory(&data_dir)?;
for record in state.segments.values() {
let path = data_dir.join(format!("{:x}.{:07x}", record.level, record.version));
precheck_regular_path(&path, "segment path is not a regular file")?;
let write = record.state == super::state::SegmentState::Active;
let file = open_data_file(&path, write)?;
let metadata = validate_regular_file(&file, "segment path is not a regular file")?;
let slot_size = super::SUPPORTED_BLOCK_SIZES[usize::from(record.level)];
let expected = record
.frontier
.checked_mul(slot_size)
.ok_or_else(|| invalid_data("segment length overflow"))?;
let actual = metadata.len();
match record.state {
super::state::SegmentState::Sealed if actual != expected => {
return Err(invalid_data("sealed segment length mismatch"));
}
super::state::SegmentState::Active if actual < expected || actual % slot_size != 0 => {
return Err(invalid_data("invalid active segment length"));
}
super::state::SegmentState::Active if actual > expected => {
file.set_len(expected)?;
file.sync_all()?;
}
_ => {}
}
}
for (&id, &logical_len) in &state.large_blocks {
let path = data_dir.join(format!("l{id:015x}"));
precheck_regular_path(&path, "large block path is not a regular file")?;
let file = open_data_file(&path, false)?;
let metadata = validate_regular_file(&file, "large block path is not a regular file")?;
if metadata.len() != logical_len {
return Err(invalid_data("large block file length mismatch"));
}
}
Ok(())
}
pub(crate) fn remove_orphan_data_files(root: &Path, state: &super::state::State) -> io::Result<()> {
let data_dir = layout::data_dir(root);
let data_directory = open_data_directory(&data_dir)?;
let mut referenced = std::collections::BTreeSet::<OsString>::new();
for record in state.segments.values() {
referenced.insert(OsString::from(format!(
"{:x}.{:07x}",
record.level, record.version
)));
}
for id in state.large_blocks.keys() {
referenced.insert(OsString::from(format!("l{id:015x}")));
}
for entry in fs::read_dir(&data_dir)? {
let entry = entry?;
let name = entry.file_name();
if referenced.contains(&name) {
continue;
}
let file_type = entry.file_type()?;
if file_type.is_symlink() || !file_type.is_file() {
return Err(invalid_data("data directory contains a non-regular entry"));
}
let file = open_data_file(&entry.path(), false)?;
validate_regular_file(&file, "data directory contains a non-regular entry")?;
fs::remove_file(entry.path())?;
}
sync_open_directory(&data_directory)
}
fn invalid_data(message: &str) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, message)
}
#[cfg(unix)]
pub(crate) fn sync_directory(path: &Path) -> io::Result<()> {
File::open(path)?.sync_all()
}
#[cfg(windows)]
pub(crate) fn sync_directory(path: &Path) -> io::Result<()> {
match File::open(path).and_then(|file| file.sync_all()) {
Ok(()) => Ok(()),
Err(error)
if matches!(
error.kind(),
io::ErrorKind::PermissionDenied | io::ErrorKind::InvalidInput
) =>
{
Ok(())
}
Err(error) => Err(error),
}
}