use std::collections::{BTreeMap, BTreeSet};
use std::ffi::OsString;
use std::fs::{self, File};
use std::io::{self, Read, Seek, Write};
use std::path::{Path, PathBuf};
use nibkeep_base::{EntityId, Hash, SpaceId, StatementId};
use nibkeep_crypto::rand_core::CryptoRng;
use nibkeep_model::{FieldName, Operation, Value, fields};
use nibkeep_std::{
CHECKED_OUT_ON, CHECKOUT, CHECKOUT_OF, CONTENT, FILE, IN_TREE, MEDIA_TYPE, MODIFIED, PATH,
SECRET_FILE, SIZE,
};
use super::entities::NewValue;
use super::export::blake3_of;
use super::files::{HEAD, Sniffing, moment, plain};
use super::{Library, policies};
use crate::NodeError;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CopyState {
Unchanged,
Edited,
Gone,
}
impl CopyState {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Unchanged => "unchanged",
Self::Edited => "edited",
Self::Gone => "gone",
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Checkout {
pub id: EntityId,
pub file: EntityId,
pub path: PathBuf,
pub content: Option<Hash>,
pub copy: CopyState,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CheckedOut {
pub checkout: EntityId,
pub file: EntityId,
pub path: PathBuf,
pub content: Hash,
pub written: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct EditsRecorded {
pub copy: CopyState,
pub version: Option<Hash>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Ended {
pub path: PathBuf,
pub deleted: bool,
pub sealed: bool,
}
#[derive(Clone, Debug)]
struct Record {
id: EntityId,
file: EntityId,
path: PathBuf,
content: Option<Hash>,
size: Option<u64>,
modified: Option<Value>,
}
impl Record {
fn checkout(&self) -> Checkout {
Checkout {
id: self.id,
file: self.file,
path: self.path.clone(),
content: self.content,
copy: self.copy_state(),
}
}
fn copy_state(&self) -> CopyState {
let size = match fs::symlink_metadata(&self.path) {
Ok(metadata) if metadata.is_file() => metadata.len(),
_ => return CopyState::Gone,
};
if self.is_untouched() {
return CopyState::Unchanged;
}
if self.size.is_some_and(|recorded| recorded != size) {
return CopyState::Edited;
}
match (blake3_of(&self.path), self.content) {
(Ok(content), Some(recorded)) if content == recorded => CopyState::Unchanged,
_ => CopyState::Edited,
}
}
fn verified_state(&self) -> CopyState {
match fs::symlink_metadata(&self.path) {
Ok(metadata) if metadata.is_file() => {}
_ => return CopyState::Gone,
}
let unchanged = match self.content {
Some(recorded) => blake3_of(&self.path).is_ok_and(|content| content == recorded),
None => self.is_untouched(),
};
if unchanged {
CopyState::Unchanged
} else {
CopyState::Edited
}
}
fn is_untouched(&self) -> bool {
let Ok(metadata) = fs::symlink_metadata(&self.path) else {
return false;
};
let modified = metadata.modified().ok().and_then(moment).map(Value::Date);
metadata.is_file()
&& self.size == Some(metadata.len())
&& modified.is_some()
&& modified == self.modified
}
}
impl Library {
pub fn checkout(
&mut self,
space: SpaceId,
file: EntityId,
place: &Path,
wall_millis: u64,
rng: &mut impl CryptoRng,
) -> Result<CheckedOut, NodeError> {
self.check_not_revoked(space)?;
let (file, name, content) = self.content_of(space, file)?;
let path = place_for(place, name.as_deref())?;
let mut replaced = None;
if let Some(record) = self.record_at(space, &path)? {
match record.copy_state() {
CopyState::Unchanged if record.file == file && record.content == Some(content) => {
let _ = self.record_opened(space, file, wall_millis);
return Ok(CheckedOut {
checkout: record.id,
file,
path,
content,
written: false,
});
}
CopyState::Gone => replaced = Some(record.id),
_ => return Err(NodeError::PlaceTaken(path)),
}
}
if fs::symlink_metadata(&path).is_ok() {
return Err(NodeError::PlaceTaken(path));
}
let temporary = temporary_for(&path, "checkout");
remove_leftover(&temporary);
let (size, modified) =
match self.write_copy(space, (file, name.as_deref()), &content, &temporary) {
Ok(written) => written,
Err(error) => {
remove_leftover(&temporary);
if matches!(error, NodeError::NoCopyHere(_)) {
self.record_opened(space, file, wall_millis)?;
}
return Err(error);
}
};
let recorded = self.record_checkout(
space,
(file, &path, content),
(size, modified),
replaced,
(wall_millis, rng),
);
let checkout = match recorded {
Ok(checkout) => checkout,
Err(error) => {
remove_leftover(&temporary);
return Err(error);
}
};
if let Err(error) = put_in_place(&temporary, &path) {
remove_leftover(&temporary);
let _ = self.commit(space, vec![deleting(checkout, rng)], wall_millis, rng);
return Err(if error.kind() == io::ErrorKind::AlreadyExists {
NodeError::PlaceTaken(path)
} else {
NodeError::file(&path)(error)
});
}
let _ = self.record_opened(space, file, wall_millis);
Ok(CheckedOut {
checkout,
file,
path,
content,
written: true,
})
}
pub fn view_copy(
&mut self,
space: SpaceId,
file: EntityId,
folder: &Path,
wall_millis: u64,
) -> Result<PathBuf, NodeError> {
let (file, name, content) = self.content_to_view(space, file)?;
let digest = super::hex(&content.digest()[..16]);
let of_content = folder.join(format!("{VIEW_PREFIX}{digest}"));
fs::create_dir_all(&of_content).map_err(NodeError::file(&of_content))?;
let path = place_for(&of_content, Some(&view_name(name.as_deref())))?;
let written = || fs::symlink_metadata(&path).is_ok_and(|found| found.is_file());
if !written() {
let temporary = temporary_for(&path, "view");
remove_read_only(&temporary);
if let Err(error) =
self.write_copy(space, (file, name.as_deref()), &content, &temporary)
{
remove_leftover(&temporary);
let _ = fs::remove_dir(&of_content);
if matches!(error, NodeError::NoCopyHere(_)) {
self.record_opened(space, file, wall_millis)?;
}
return Err(error);
}
match put_in_place(&temporary, &path) {
Ok(()) => {
let read_only = fs::metadata(&path).and_then(|found| {
let mut permissions = found.permissions();
permissions.set_readonly(true);
fs::set_permissions(&path, permissions)
});
read_only.map_err(NodeError::file(&path))?;
}
Err(error) if error.kind() == io::ErrorKind::AlreadyExists && written() => {
remove_read_only(&temporary);
}
Err(error) => {
remove_read_only(&temporary);
return Err(NodeError::file(&path)(error));
}
}
}
let _ = self.record_opened(space, file, wall_millis);
Ok(path)
}
fn content_to_view(
&self,
space: SpaceId,
file: EntityId,
) -> Result<(EntityId, Option<String>, Hash), NodeError> {
let state = self.space(space)?.stored.log().state();
let file = state.canonical(file)?;
let name = state
.current(file, &fields::NAME)?
.iter()
.min_by_key(|statement| statement.id)
.and_then(|statement| statement.value.as_name_text().map(str::to_owned));
let described = || name.clone().unwrap_or_else(|| file.to_string());
if !state.is_deleted(file)? && state.types(file)?.contains(&SECRET_FILE) {
return Err(NodeError::SecretFile(described()));
}
if state.is_deleted(file)? || !state.types(file)?.contains(&FILE) {
return Err(NodeError::NoContent(described()));
}
let content = self
.current_content(space, file)?
.ok_or_else(|| NodeError::NoContent(described()))?;
Ok((file, name, content))
}
pub fn checkouts(&self, space: SpaceId) -> Result<Vec<Checkout>, NodeError> {
Ok(self.records(space)?.iter().map(Record::checkout).collect())
}
pub fn checkout_at(&self, space: SpaceId, path: &Path) -> Result<Option<Checkout>, NodeError> {
let Some(path) = resolved_place(path) else {
return Ok(None);
};
Ok(self.record_at(space, &path)?.as_ref().map(Record::checkout))
}
pub fn checkouts_of(&self, space: SpaceId, file: EntityId) -> Result<Vec<Checkout>, NodeError> {
let file = self.space(space)?.stored.log().state().canonical(file)?;
Ok(self
.records(space)?
.iter()
.filter(|record| record.file == file)
.map(Record::checkout)
.collect())
}
pub fn record_edits(
&mut self,
space: SpaceId,
checkout: EntityId,
force: bool,
wall_millis: u64,
rng: &mut impl CryptoRng,
) -> Result<EditsRecorded, NodeError> {
let record = self.record(space, checkout)?;
let copy = record.verified_state();
if copy != CopyState::Edited {
return Ok(EditsRecorded {
copy,
version: None,
});
}
let current = self.current_content(space, record.file)?;
if current != record.content && !force {
return Err(NodeError::Stale(record.path));
}
let path = &record.path;
let before = fs::metadata(path).map_err(NodeError::file(path))?;
let mut head = Vec::with_capacity(HEAD);
File::open(path)
.and_then(|file| file.take(HEAD as u64).read_to_end(&mut head))
.map_err(NodeError::file(path))?;
let media_type = infer::get(&head).map(|kind| kind.mime_type().to_owned());
let mut reading = Sniffing {
inner: File::open(path).map_err(NodeError::file(path))?,
size: 0,
failed: None,
};
let content = self.import_blob(space, &mut reading, wall_millis, rng)?;
let after = fs::metadata(path).map_err(NodeError::file(path))?;
if after.len() != reading.size
|| before.len() != reading.size
|| after.modified().ok() != before.modified().ok()
{
return Err(NodeError::file(path)(io::Error::other(
"it changed while it was read",
)));
}
let size = Value::Integer(i64::try_from(reading.size).unwrap_or(i64::MAX));
let modified = before.modified().ok().and_then(moment).map(Value::Date);
let mut operations = self.replacing(
space,
record.file,
vec![(CONTENT, Value::Blob(content)), (SIZE, size.clone())],
rng,
)?;
operations.extend(self.replacing_or_clearing(
space,
record.file,
MEDIA_TYPE,
media_type.map(Value::Text),
rng,
)?);
operations.extend(self.replacing(
space,
record.id,
vec![(CONTENT, Value::Blob(content)), (SIZE, size)],
rng,
)?);
operations.extend(self.replacing_or_clearing(space, record.id, MODIFIED, modified, rng)?);
self.commit(space, operations, wall_millis, rng)?;
Ok(EditsRecorded {
copy,
version: Some(content),
})
}
pub fn end_checkout(
&mut self,
space: SpaceId,
checkout: EntityId,
wall_millis: u64,
rng: &mut impl CryptoRng,
) -> Result<Ended, NodeError> {
let record = self.record(space, checkout)?;
let path = record.path.clone();
let mut ended = Ended {
path: path.clone(),
deleted: false,
sealed: false,
};
match record.verified_state() {
CopyState::Edited => return Err(NodeError::Unrecorded(path)),
CopyState::Gone => {}
CopyState::Unchanged => {
if let Some(content) = record.content
&& !self.held_elsewhere(space, &content, &record)?
{
let copy = File::open(&path).map_err(NodeError::file(&path))?;
let sealed = self.import_blob(space, copy, wall_millis, rng)?;
if sealed != content {
return Err(NodeError::Unrecorded(path));
}
ended.sealed = true;
}
fs::remove_file(&path).map_err(NodeError::file(&path))?;
sync_folder_of(&path);
ended.deleted = true;
}
}
remove_leftover(&temporary_for(&path, "checkout"));
self.commit(space, vec![deleting(record.id, rng)], wall_millis, rng)?;
Ok(ended)
}
pub fn settle_checkouts(
&mut self,
known: &BTreeMap<EntityId, Hash>,
wall_millis: u64,
rng: &mut impl CryptoRng,
) -> Result<Vec<PathBuf>, NodeError> {
let spaces: Vec<SpaceId> = self.spaces.keys().copied().collect();
let mut deleted = Vec::new();
for space in spaces {
deleted.extend(self.settle_checkouts_in(space, known, wall_millis, rng)?);
}
Ok(deleted)
}
pub fn checkout_contents(&self) -> Result<BTreeMap<EntityId, Hash>, NodeError> {
let mut contents = BTreeMap::new();
for &space in self.spaces.keys() {
contents.extend(self.checkout_contents_in(space)?);
}
Ok(contents)
}
pub(crate) fn checkout_contents_in(
&self,
space: SpaceId,
) -> Result<BTreeMap<EntityId, Hash>, NodeError> {
Ok(self
.records(space)?
.into_iter()
.filter_map(|record| Some((record.id, record.content?)))
.collect())
}
pub(crate) fn settle_checkouts_in(
&mut self,
space: SpaceId,
known: &BTreeMap<EntityId, Hash>,
wall_millis: u64,
rng: &mut impl CryptoRng,
) -> Result<Vec<PathBuf>, NodeError> {
let mut deleted = Vec::new();
let mut operations = Vec::new();
for record in self.records(space)? {
if record.content.is_some() || !record.path.parent().is_some_and(Path::is_dir) {
continue;
}
let present = match fs::symlink_metadata(&record.path) {
Ok(metadata) => metadata.is_file(),
Err(error) if error.kind() == io::ErrorKind::NotFound => false,
Err(_) => continue,
};
if present {
let unchanged = match known.get(&record.id) {
Some(content) => blake3_of(&record.path).is_ok_and(|found| found == *content),
None => record.is_untouched(),
};
if !unchanged {
operations
.extend(self.replacing_or_clearing(space, record.id, MODIFIED, None, rng)?);
continue;
}
if fs::remove_file(&record.path).is_err() {
continue;
}
sync_folder_of(&record.path);
deleted.push(record.path.clone());
}
remove_leftover(&temporary_for(&record.path, "checkout"));
operations.push(deleting(record.id, rng));
}
if !operations.is_empty() {
let _ = self.commit(space, operations, wall_millis, rng);
}
deleted.extend(self.settle_trees_in(space).unwrap_or_default());
Ok(deleted)
}
pub(crate) fn delete_unedited_checkouts(&self, space: SpaceId) -> Result<(), NodeError> {
for record in self.records(space)? {
if record.verified_state() != CopyState::Unchanged {
continue;
}
if fs::remove_file(&record.path).is_ok() {
sync_folder_of(&record.path);
}
remove_leftover(&temporary_for(&record.path, "checkout"));
}
Ok(())
}
pub(crate) fn checkouts_holding(
&self,
space: SpaceId,
contents: &BTreeSet<Hash>,
) -> Result<Vec<(PathBuf, bool)>, NodeError> {
let mut holding = Vec::new();
for record in self.records(space)? {
if record
.content
.is_some_and(|content| contents.contains(&content))
{
match record.verified_state() {
CopyState::Gone => {}
copy => holding.push((record.path, copy == CopyState::Edited)),
}
}
}
Ok(holding)
}
pub(crate) fn checkout_places_here(
&self,
space: SpaceId,
) -> Result<Vec<super::checking::PlainPlace>, NodeError> {
Ok(self
.records(space)?
.into_iter()
.filter_map(|record| {
let content = record.content?;
let path = record.path.to_string_lossy().into_owned();
let untouched = record.is_untouched();
Some(super::checking::PlainPlace {
file: record.file,
seen: super::checking::seen_at(&path).filter(|_| untouched),
path,
content,
untouched,
size: record.size.unwrap_or(0),
})
})
.collect())
}
pub(crate) fn is_checkout(&self, space: SpaceId, path: &str) -> Result<bool, NodeError> {
let path = Path::new(path);
if self.record_at(space, path)?.is_some() {
return Ok(true);
}
for &other in self.spaces.keys() {
if other != space && self.record_at(other, path)?.is_some() {
return Ok(true);
}
}
Ok(false)
}
fn content_of(
&self,
space: SpaceId,
file: EntityId,
) -> Result<(EntityId, Option<String>, Hash), NodeError> {
let found = self.content_to_view(space, file)?;
let state = self.space(space)?.stored.log().state();
if !state.current(found.0, &IN_TREE)?.is_empty() {
return Err(NodeError::TreeFile(
found.1.unwrap_or_else(|| found.0.to_string()),
));
}
Ok(found)
}
fn current_content(&self, space: SpaceId, file: EntityId) -> Result<Option<Hash>, NodeError> {
let state = self.space(space)?.stored.log().state();
let file = state.canonical(file)?;
Ok(state
.current(file, &CONTENT)?
.iter()
.filter(|statement| matches!(statement.value, Value::Blob(_)))
.min_by_key(|statement| statement.id)
.and_then(|statement| match statement.value {
Value::Blob(content) => Some(content),
_ => None,
}))
}
fn write_copy(
&self,
space: SpaceId,
(file, name): (EntityId, Option<&str>),
content: &Hash,
temporary: &Path,
) -> Result<(u64, Option<Value>), NodeError> {
let open = self.space(space)?;
let mut copy = File::create_new(temporary).map_err(NodeError::file(temporary))?;
let mut written = false;
if open.stored.holds_ordinary_blob(content)
&& let Ok(mut reader) = open.stored.open_blob(content)
{
written = copy_checked(&mut reader, &mut copy, content)
.map_err(NodeError::file(temporary))?;
}
if !written {
for original in self.originals_here(space, content)? {
let Ok(mut reading) = File::open(&original) else {
continue;
};
copy.set_len(0)
.and_then(|()| copy.rewind())
.map_err(NodeError::file(temporary))?;
if copy_checked(&mut reading, &mut copy, content)
.map_err(NodeError::file(temporary))?
{
written = true;
break;
}
}
}
if !written {
let name = name.map_or_else(|| file.to_string(), str::to_owned);
return Err(NodeError::NoCopyHere(name));
}
copy.sync_all().map_err(NodeError::file(temporary))?;
let metadata = copy.metadata().map_err(NodeError::file(temporary))?;
let modified = metadata.modified().ok().and_then(moment).map(Value::Date);
Ok((metadata.len(), modified))
}
fn record_checkout(
&mut self,
space: SpaceId,
(file, path, content): (EntityId, &Path, Hash),
(size, modified): (u64, Option<Value>),
replaced: Option<EntityId>,
(wall_millis, rng): (u64, &mut impl CryptoRng),
) -> Result<EntityId, NodeError> {
self.install_standard(space, &[CHECKOUT], wall_millis, rng)?;
let mut values = vec![
NewValue::new(CHECKOUT_OF, Value::Reference(file)),
NewValue::new(CHECKED_OUT_ON, Value::Reference(self.info.device)),
NewValue::new(PATH, Value::Text(path.to_string_lossy().into_owned())),
NewValue::new(CONTENT, Value::Blob(content)),
NewValue::new(
SIZE,
Value::Integer(i64::try_from(size).unwrap_or(i64::MAX)),
),
];
values.extend(modified.map(|modified| NewValue::new(MODIFIED, modified)));
let (checkout, mut operations) = self.new_entity(space, &[CHECKOUT], values, rng)?;
if let Some(replaced) = replaced {
operations.push(deleting(replaced, rng));
}
self.commit(space, operations, wall_millis, rng)?;
self.record_first_check(
space,
(path.to_string_lossy().into_owned(), content),
wall_millis,
);
Ok(checkout)
}
pub(crate) fn replacing_or_clearing(
&self,
space: SpaceId,
entity: EntityId,
field: FieldName,
value: Option<Value>,
rng: &mut impl CryptoRng,
) -> Result<Vec<Operation>, NodeError> {
if let Some(value) = value {
return self.replacing(space, entity, vec![(field, value)], rng);
}
let state = self.space(space)?.stored.log().state();
let current: Vec<StatementId> = state
.current(entity, &field)?
.iter()
.map(|statement| statement.id)
.collect();
Ok(if current.is_empty() {
Vec::new()
} else {
vec![Operation::retract(entity, current)]
})
}
fn held_elsewhere(
&self,
space: SpaceId,
content: &Hash,
record: &Record,
) -> Result<bool, NodeError> {
if self.space(space)?.stored.holds_ordinary_blob(content) {
return Ok(true);
}
for original in self.originals_here(space, content)? {
if original == record.path {
continue;
}
let same_size = fs::metadata(&original)
.is_ok_and(|metadata| record.size.is_none_or(|size| metadata.len() == size));
if same_size && blake3_of(&original).is_ok_and(|hash| hash == *content) {
return Ok(true);
}
}
Ok(false)
}
pub fn file_content(
&self,
space: SpaceId,
content: &Hash,
limit: u64,
) -> Result<Option<Vec<u8>>, NodeError> {
let open = self.space(space)?;
if open.stored.holds_ordinary_blob(content)
&& let Ok(reader) = open.stored.open_blob(content)
&& let Some(bytes) = read_checked(reader, content, limit)
{
return Ok(Some(bytes));
}
for original in self.originals_here(space, content)? {
if let Ok(file) = File::open(&original)
&& let Some(bytes) = read_checked(file, content, limit)
{
return Ok(Some(bytes));
}
}
Ok(None)
}
pub(crate) fn originals_here(
&self,
space: SpaceId,
content: &Hash,
) -> Result<Vec<PathBuf>, NodeError> {
let open = self.space(space)?;
let state = open.stored.log().state();
let mut places = BTreeSet::new();
for file in open.stored.find(&CONTENT, &Value::Blob(*content))? {
if !state.types(file)?.contains(&FILE) {
continue;
}
for (_, device, place) in policies::origins_of(state, file)? {
if device == self.info.device {
places.insert(PathBuf::from(place));
}
}
}
Ok(places.into_iter().collect())
}
fn record_at(&self, space: SpaceId, path: &Path) -> Result<Option<Record>, NodeError> {
let open = self.space(space)?;
let text = Value::Text(path.to_string_lossy().into_owned());
for checkout in open.stored.find(&PATH, &text)? {
if let Some(record) = self.record_of(space, checkout)?
&& record.path == path
{
return Ok(Some(record));
}
}
Ok(None)
}
fn record(&self, space: SpaceId, checkout: EntityId) -> Result<Record, NodeError> {
self.record_of(space, checkout)?
.ok_or_else(|| NodeError::NotCheckedOut(checkout.to_string()))
}
fn records(&self, space: SpaceId) -> Result<Vec<Record>, NodeError> {
let open = self.space(space)?;
let state = open.stored.log().state();
let mut seen = BTreeSet::new();
let mut records = Vec::new();
for checkout in open
.stored
.find(&CHECKED_OUT_ON, &Value::Reference(self.info.device))?
{
if seen.insert(state.canonical(checkout)?)
&& let Some(record) = self.record_of(space, checkout)?
{
records.push(record);
}
}
records.sort_by(|a, b| a.path.cmp(&b.path).then(a.id.cmp(&b.id)));
Ok(records)
}
fn record_of(&self, space: SpaceId, checkout: EntityId) -> Result<Option<Record>, NodeError> {
let state = self.space(space)?.stored.log().state();
let checkout = state.canonical(checkout)?;
if !super::policies::stands_here(state, checkout)?
|| !state.types(checkout)?.contains(&CHECKOUT)
{
return Ok(None);
}
let first = |field: &FieldName| -> Result<Option<Value>, NodeError> {
Ok(state
.current(checkout, field)?
.iter()
.min_by_key(|statement| statement.id)
.map(|statement| statement.value.clone()))
};
let (Some(Value::Reference(on)), Some(Value::Reference(file)), Some(Value::Text(path))) =
(first(&CHECKED_OUT_ON)?, first(&CHECKOUT_OF)?, first(&PATH)?)
else {
return Ok(None);
};
if state.canonical(on)? != self.info.device {
return Ok(None);
}
Ok(Some(Record {
id: checkout,
file: state.canonical(file)?,
path: PathBuf::from(path),
content: match first(&CONTENT)? {
Some(Value::Blob(content)) => Some(content),
_ => None,
},
size: match first(&SIZE)? {
Some(Value::Integer(size)) => u64::try_from(size).ok(),
_ => None,
},
modified: first(&MODIFIED)?,
}))
}
}
fn deleting(entity: EntityId, rng: &mut impl CryptoRng) -> Operation {
policies::assert(entity, fields::DELETED, Value::Boolean(true), rng)
}
pub(super) fn place_for(place: &Path, name: Option<&str>) -> Result<PathBuf, NodeError> {
let as_folder = place.is_dir()
|| place
.as_os_str()
.to_string_lossy()
.ends_with(['/', std::path::MAIN_SEPARATOR]);
let (folder, file_name) = if as_folder {
let name = name
.filter(|name| is_file_name(name))
.ok_or_else(|| NodeError::NotAFileName(name.unwrap_or_default().to_owned()))?;
(place, OsString::from(name))
} else {
let file_name = place
.file_name()
.ok_or_else(|| NodeError::NotAFileName(place.display().to_string()))?;
let folder = match place.parent() {
Some(parent) if !parent.as_os_str().is_empty() => parent,
_ => Path::new("."),
};
(folder, file_name.to_owned())
};
let folder = fs::canonicalize(folder).map_err(NodeError::file(folder))?;
Ok(plain(folder).join(file_name))
}
fn resolved_place(path: &Path) -> Option<PathBuf> {
let file_name = path.file_name()?;
let mut existing = match path.parent() {
Some(parent) if !parent.as_os_str().is_empty() => parent,
_ => Path::new("."),
};
let mut rest = Vec::new();
loop {
if let Ok(resolved) = fs::canonicalize(existing) {
let mut resolved = plain(resolved);
resolved.extend(rest.iter().rev());
return Some(resolved.join(file_name));
}
rest.push(existing.file_name()?);
existing = existing
.parent()
.filter(|parent| !parent.as_os_str().is_empty())?;
}
}
const VIEW_PREFIX: &str = "view-";
fn view_name(name: Option<&str>) -> String {
let cleaned: String = name
.unwrap_or_default()
.chars()
.map(|char| {
let refused = matches!(char, '<' | '>' | ':' | '"' | '/' | '\\' | '|' | '?' | '*');
if refused || char.is_control() {
'_'
} else {
char
}
})
.collect();
let cleaned = cleaned.trim_end_matches(['.', ' ']).trim_start();
if !is_file_name(cleaned) {
return "file".to_owned();
}
let stem = cleaned.split('.').next().unwrap_or_default().trim_end();
let stem = stem.to_ascii_uppercase();
let numbered = |prefix: &str| {
stem.strip_prefix(prefix).is_some_and(|rest| {
matches!(
rest,
"0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
)
})
};
if matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL") || numbered("COM") || numbered("LPT")
{
format!("_{cleaned}")
} else {
cleaned.to_owned()
}
}
fn remove_read_only(path: &Path) {
if let Ok(found) = fs::symlink_metadata(path) {
let mut permissions = found.permissions();
if permissions.readonly() && !found.file_type().is_symlink() {
#[expect(
clippy::permissions_set_readonly_false,
reason = "the file is deleted next"
)]
permissions.set_readonly(false);
let _ = fs::set_permissions(path, permissions);
}
}
let _ = fs::remove_file(path);
}
#[must_use]
pub fn remove_view_copies(folder: &Path) -> usize {
let mut removed = 0;
let Ok(entries) = fs::read_dir(folder) else {
return 0;
};
for entry in entries.flatten() {
let named = entry
.file_name()
.to_str()
.is_some_and(|name| name.starts_with(VIEW_PREFIX));
if !named || !entry.file_type().is_ok_and(|kind| kind.is_dir()) {
continue;
}
let of_content = entry.path();
for copy in fs::read_dir(&of_content).into_iter().flatten().flatten() {
if copy.file_type().is_ok_and(|kind| !kind.is_dir()) {
let leftover = copy.file_name().to_string_lossy().starts_with(".nib-view-");
remove_read_only(©.path());
if !leftover && fs::symlink_metadata(copy.path()).is_err() {
removed += 1;
}
}
}
let _ = fs::remove_dir(&of_content);
}
removed
}
fn is_file_name(name: &str) -> bool {
!name.is_empty() && name != "." && name != ".." && !name.contains(['/', '\\', '\0'])
}
pub(super) fn temporary_for(path: &Path, what: &str) -> PathBuf {
let name = path.file_name().map(std::ffi::OsStr::as_encoded_bytes);
let digest = blake3::hash(name.unwrap_or_default()).to_hex();
path.with_file_name(format!(".nib-{what}-{}", &digest[..16]))
}
pub(super) fn remove_leftover(temporary: &Path) {
let _ = fs::remove_file(temporary);
}
pub(super) fn put_in_place(temporary: &Path, path: &Path) -> io::Result<()> {
match fs::hard_link(temporary, path) {
Ok(()) => remove_leftover(temporary),
Err(error) if error.kind() == io::ErrorKind::AlreadyExists => return Err(error),
Err(_) => {
if fs::symlink_metadata(path).is_ok() {
return Err(io::ErrorKind::AlreadyExists.into());
}
fs::rename(temporary, path)?;
}
}
sync_folder_of(path);
Ok(())
}
pub(super) fn sync_folder_of(path: &Path) {
#[cfg(unix)]
if let Some(folder) = path.parent()
&& let Ok(opened) = File::open(folder)
{
let _ = opened.sync_all();
}
#[cfg(not(unix))]
let _ = path;
}
fn read_checked(reader: impl Read, content: &Hash, limit: u64) -> Option<Vec<u8>> {
let mut bytes = Vec::new();
reader
.take(limit.saturating_add(1))
.read_to_end(&mut bytes)
.ok()?;
(bytes.len() as u64 <= limit && Hash::of(&bytes) == *content).then_some(bytes)
}
pub(super) fn copy_checked(
from: &mut impl Read,
to: &mut File,
content: &Hash,
) -> io::Result<bool> {
let mut hasher = nibkeep_base::Hasher::new();
let mut buffer = nibkeep_crypto::Zeroizing::new(vec![0; 64 * 1024]);
loop {
let read = match from.read(&mut buffer) {
Ok(0) => break,
Ok(read) => read,
Err(error) if error.kind() == io::ErrorKind::Interrupted => continue,
Err(_) => return Ok(false),
};
to.write_all(&buffer[..read])?;
hasher.write_all(&buffer[..read])?;
}
Ok(hasher.finalize() == *content)
}
#[cfg(test)]
mod tests {
use super::view_name;
#[test]
fn a_copy_to_view_is_named_as_every_file_system_takes() {
assert_eq!(view_name(Some("contract.pdf")), "contract.pdf");
assert_eq!(
view_name(Some("What? 3:2 <final>.txt")),
"What_ 3_2 _final_.txt"
);
assert_eq!(view_name(Some("a/b\\c")), "a_b_c");
assert_eq!(view_name(Some("notes. ")), "notes");
assert_eq!(view_name(Some("tab\there")), "tab_here");
assert_eq!(view_name(Some("con.pdf")), "_con.pdf");
assert_eq!(view_name(Some("LPT1")), "_LPT1");
assert_eq!(view_name(Some("console.txt")), "console.txt");
for unnamed in [None, Some(""), Some(".."), Some(" . ")] {
assert_eq!(view_name(unnamed), "file");
}
}
}