use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::io;
use std::path::Path;
use std::time::SystemTime;
use nibkeep_base::{EntityId, SpaceId, StatementId};
use nibkeep_log::ChangeId;
use nibkeep_model::storage::Statements as _;
use nibkeep_model::{Operation, Value};
use nibkeep_std::{CONTENT, LINK_TARGET, SECRET_CONTENT};
use super::compare::{same_content, still_competes};
use super::history::READ_LIMIT;
use super::index::{Conflict, Entry, Index, Version};
use super::locate::{Located, located};
use super::secrecy::{NIBSECRET, lines};
use super::walk::{Found, FoundKind, look, nanos, open_found};
use super::write::{Content, Place, copy_version, same_version, undo_moves_at};
use super::{CutShort, TreeState, settle_tree, version_of};
use crate::NodeError;
use crate::library::Library;
use crate::library::checkouts::sync_folder_of;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Updated {
pub written: Vec<String>,
pub moved: Vec<(String, String)>,
pub deleted: Vec<String>,
pub merged: Vec<String>,
pub conflicts: Vec<String>,
pub kept: Vec<(String, String)>,
pub contested: Vec<String>,
pub skipped: Vec<(String, String)>,
pub waiting: Vec<CutShort>,
pub held: Vec<String>,
pub sealed: Vec<String>,
}
#[derive(Clone)]
struct Arriving {
path: String,
entity: EntityId,
base: Option<Version>,
version: Version,
competing: Vec<Version>,
written: BTreeSet<String>,
deletions: Vec<String>,
secret: bool,
}
impl Arriving {
fn nothing_new(&self) -> bool {
let versions = if self.competing.is_empty() {
self.base.as_ref() == Some(&self.version)
} else {
self.competing
.iter()
.all(|version| self.written.contains(&version.key()))
};
versions && self.deletions.iter().all(|key| self.written.contains(key))
}
fn competes(&self) -> bool {
!self.competing.is_empty() || !self.deletions.is_empty()
}
fn wrote_out(&self, updated: &mut Updated, changes: &mut Changes) {
if !self.competes() {
return;
}
let mut keys: BTreeSet<String> = self.competing.iter().map(Version::key).collect();
keys.insert(self.version.key());
keys.extend(self.deletions.iter().cloned());
changes.written_out.push((self.entity, keys));
if !self.deletions.is_empty() {
updated.contested.push(self.path.clone());
}
}
}
struct Here<'a> {
space: SpaceId,
tree: EntityId,
place: Place<'a>,
index: &'a Index,
whole: bool,
conflicts: BTreeMap<String, Conflict>,
read_at: i64,
}
#[derive(Default)]
struct Changes {
set: Vec<(String, Entry)>,
removed: Vec<String>,
written_out: Vec<(EntityId, BTreeSet<String>)>,
unknown: bool,
}
impl Library {
pub fn update_tree(
&self,
space: SpaceId,
tree: EntityId,
partial: bool,
) -> Result<Updated, NodeError> {
self.update_tree_with(space, tree, (partial, false), None)
}
pub fn update_tree_agreeing(
&self,
space: SpaceId,
tree: EntityId,
partial: bool,
) -> Result<Updated, NodeError> {
self.update_tree_with(space, tree, (partial, true), None)
}
pub fn update_tree_cut_after(
&self,
space: SpaceId,
tree: EntityId,
written: usize,
) -> Result<Updated, NodeError> {
self.update_tree_with(space, tree, (false, true), Some(written))
}
fn update_tree_with(
&self,
space: SpaceId,
tree: EntityId,
(partial, agreed): (bool, bool),
cut: Option<usize>,
) -> Result<Updated, NodeError> {
let _locked = self.lock_trees()?;
let root = self.root_here(space, tree)?;
let mut index = self.index()?;
let purged = self.learn_purges(space, &index)?;
let apart = self.apart();
let place = Place {
root: &root,
apart: &apart,
};
let now = self.tree_state(space, tree)?;
settle_tree((tree, &root, &apart), &mut index, (&purged, &now))?;
self.follow_cut_short(space, (tree, &root, &apart), &mut index, &now)?;
let entries = index.entries(tree)?;
if !entries.is_empty()
&& entries
.keys()
.all(|path| located(&root, path, &apart) == Located::Absent)
{
return Err(NodeError::TreeEmptied(root));
}
let mut updated = Updated::default();
let in_full: BTreeSet<EntityId> = now.by_entity.keys().copied().collect();
let sealed: Vec<String> = now.sealed.keys().cloned().collect();
let (state, taken) =
self.state_to_update(space, tree, (now, partial), (&index, &mut updated))?;
let here = Here {
space,
tree,
place,
index: &index,
whole: index.knows(tree)?,
conflicts: index.conflicts(tree)?,
read_at: nanos(SystemTime::now()).unwrap_or(0),
};
let by_entity: BTreeMap<EntityId, (String, Entry)> = entries
.iter()
.map(|(path, entry)| (entry.entity, (path.clone(), entry.clone())))
.collect();
let mut changes = Changes::default();
let written = index.written_out(tree)?;
let (mut deleted, mut moves, mut arriving) =
plan_update((&state, &in_full), &by_entity, &written);
updated.sealed = sealed;
if !agreed {
self.hold_back(
space,
(&state, &entries),
(&mut deleted, &mut moves, &mut arriving),
&mut updated,
)?;
}
tell_moved_apart(&state, &by_entity, &mut updated);
let noted = note_writing(&index, tree, &arriving)?;
for (path, entry) in &deleted {
delete_copy(place, (path, entry), &mut updated, &mut changes.removed);
}
let failed = carry_out_moves(place, (&entries, &moves), (&mut updated, &mut changes));
let held = held_paths(&entries, &changes);
self.arrive_all(
&here,
(&arriving, &failed, &held),
cut,
(&mut updated, &mut changes),
);
if cut.is_some() {
return Ok(updated);
}
let whole = here.whole;
index.change(tree, &changes.removed, &changes.set)?;
self.note_secrets(space, (tree, &index), &state, &changes.set)?;
for (entity, keys) in &changes.written_out {
index.write_out(tree, *entity, keys)?;
}
clear_obsolete((tree, place, &index), &state, &mut updated)?;
self.settle_repositories(space, (tree, &index), &mut updated)?;
if partial {
index.take_parts(tree, &taken)?;
}
if whole || (!changes.unknown && updated.skipped.is_empty()) {
index.know(tree)?;
}
if noted {
index.forget_writing(tree)?;
}
updated.written.sort();
updated.merged.sort();
updated.contested.sort();
updated.conflicts.sort();
updated.conflicts.dedup();
Ok(updated)
}
fn arrive_all(
&self,
here: &Here<'_>,
(arriving, failed, held): (
&[Arriving],
&BTreeSet<EntityId>,
&BTreeMap<String, EntityId>,
),
cut: Option<usize>,
(updated, changes): (&mut Updated, &mut Changes),
) {
for arrival in arriving {
if cut.is_some_and(|cut| updated.written.len() >= cut) {
return;
}
if failed.contains(&arrival.entity) {
let why = "it could not be moved where the tree has it".to_owned();
updated.skipped.push((arrival.path.clone(), why));
continue;
}
if goes_beside(held, here, arrival) {
self.write_beside(here, arrival, updated);
continue;
}
if let Err(error) = self.arrive(here, arrival, (updated, changes)) {
updated
.skipped
.push((arrival.path.clone(), error.to_string()));
}
}
}
fn state_to_update(
&self,
space: SpaceId,
tree: EntityId,
(now, partial): (TreeState, bool),
(index, updated): (&Index, &mut Updated),
) -> Result<(TreeState, Vec<EntityId>), NodeError> {
let cut = self.cut_short(space, tree)?;
let revisions = cut.iter().map(|cut| cut.revision).collect();
if partial {
return Ok((now, revisions));
}
let this_replica = self.replica.signing.verifying_key();
let taken = index.taken_parts(tree)?;
let mut without = BTreeSet::new();
for cut in cut {
if taken.contains(&cut.revision) {
continue;
}
let changes = self.revision_changes(space, tree, cut.revision)?;
let log = self.space(space)?.stored.log();
let own = match changes.first() {
Some(first) => log
.change(*first)?
.is_some_and(|change| *change.author() == this_replica),
None => true,
};
if !own {
without.extend(changes);
updated.waiting.push(cut);
}
}
if without.is_empty() {
return Ok((now, revisions));
}
Ok((self.tree_state_without(space, tree, &without)?, revisions))
}
fn arrive(
&self,
here: &Here<'_>,
arrival: &Arriving,
(updated, changes): (&mut Updated, &mut Changes),
) -> Result<(), NodeError> {
let path = &arrival.path;
let nothing_new = arrival.nothing_new();
let entry = |found: &Found, version: &Version, known: bool| {
entry_for(arrival.entity, (found, version, known), here.read_at)
};
let on_disk = match located(here.place.root, path, here.place.apart) {
Located::Here(on_disk) => on_disk,
Located::Absent
if !arrival.deletions.is_empty()
&& arrival.competing.is_empty()
&& arrival.base.as_ref() == Some(&arrival.version) =>
{
arrival.wrote_out(updated, changes);
return Ok(());
}
Located::Absent if arrival.base.is_some() => {
if !nothing_new {
let why = "deleted or moved here, and changed elsewhere: restore it, or \
commit with force to delete it"
.to_owned();
updated.kept.push((path.clone(), why));
}
return Ok(());
}
Located::Absent => {
let content = Content::of(&arrival.version, arrival.secret);
match self.write_new(here.space, here.place, path, &content) {
Ok(found) if arrival.competing.is_empty() => {
updated.written.push(path.clone());
let written = entry(&found, &arrival.version, true);
changes.set.push((path.clone(), written));
arrival.wrote_out(updated, changes);
}
Ok(found) => {
updated.written.push(path.clone());
let written = Arriving {
base: Some(arrival.version.clone()),
..arrival.clone()
};
return self.arrive_at_copy(
here,
&written,
&found.on_disk,
(updated, changes),
);
}
Err(why) => updated.skipped.push((path.clone(), why)),
}
return Ok(());
}
Located::Unknown(why) => {
updated.skipped.push((path.clone(), why));
return Ok(());
}
};
if nothing_new {
return Ok(());
}
self.arrive_at_copy(here, arrival, &on_disk, (updated, changes))
}
fn arrive_at_copy(
&self,
here: &Here<'_>,
arrival: &Arriving,
on_disk: &Path,
(updated, changes): (&mut Updated, &mut Changes),
) -> Result<(), NodeError> {
let path = &arrival.path;
let entry = |found: &Found, version: &Version, known: bool| {
entry_for(arrival.entity, (found, version, known), here.read_at)
};
let found = look(on_disk).ok();
let Some((found, holds)) =
found.and_then(|found| copy_version(&found).map(|holds| (found, holds)))
else {
updated
.skipped
.push((path.clone(), "it cannot be read".to_owned()));
return Ok(());
};
if arrival.base.is_none() {
if same_version(&found, &holds, &arrival.version) {
changes
.set
.push((path.clone(), entry(&found, &arrival.version, true)));
} else {
changes.unknown = true;
let why = "which version it holds is not known, as the index of copies was \
lost: commit it, or restore it"
.to_owned();
updated.kept.push((path.clone(), why));
}
return Ok(());
}
let candidates: Vec<&Version> = if arrival.competing.is_empty() {
vec![&arrival.version]
} else {
arrival
.competing
.iter()
.filter(|version| !arrival.written.contains(&version.key()))
.collect()
};
let others: Vec<&Version> = candidates
.into_iter()
.filter(|other| !same_version(&found, &holds, other))
.filter(|other| arrival.base.as_ref() != Some(*other))
.collect();
let unedited = arrival
.base
.as_ref()
.is_some_and(|base| same_version(&found, &holds, base));
if others.is_empty() {
let known = arrival.competing.is_empty() && unedited;
changes
.set
.push((path.clone(), entry(&found, &arrival.version, known)));
arrival.wrote_out(updated, changes);
return Ok(());
}
if unedited && arrival.competing.is_empty() {
let content = Content::of(&arrival.version, arrival.secret);
match self.write_over(here.space, &found, &content) {
Ok(()) => {
updated.written.push(path.clone());
if let Ok(now) = look(on_disk) {
let written = entry(&now, &arrival.version, true);
changes.set.push((path.clone(), written));
arrival.wrote_out(updated, changes);
}
}
Err(why) => updated.skipped.push((path.clone(), why)),
}
return Ok(());
}
let Some(clashed) = self.merge_arrival(here, (arrival, &found), others, updated)? else {
return Ok(());
};
if clashed {
updated.conflicts.push(path.clone());
} else {
updated.merged.push(path.clone());
}
if let Ok(now) = look(on_disk) {
changes
.set
.push((path.clone(), entry(&now, &arrival.version, false)));
arrival.wrote_out(updated, changes);
}
Ok(())
}
fn merge_arrival(
&self,
here: &Here<'_>,
(arrival, found): (&Arriving, &Found),
others: Vec<&Version>,
updated: &mut Updated,
) -> Result<Option<bool>, NodeError> {
let path = &arrival.path;
let space = here.space;
let base = if arrival.competing.is_empty() {
arrival.base.clone()
} else {
let written = matches!(
arrival.base,
Some(Version::File {
executable: true,
..
})
);
self.common_base(space, arrival.entity)?
.map(|base| match base {
Version::File { content, .. } => Version::File {
content,
executable: written,
},
link @ Version::Link { .. } => link,
})
};
let mut ours = match &found.kind {
FoundKind::File => {
let mut bytes = Vec::new();
let read = open_found(found)
.and_then(|file| {
io::Read::read_to_end(&mut io::Read::take(file, READ_LIMIT + 1), &mut bytes)
})
.map_err(NodeError::file(&found.on_disk))?;
(read as u64 <= READ_LIMIT).then_some(bytes)
}
FoundKind::Link(_) => None,
};
let secret = arrival.secret;
let base_bytes = match &base {
Some(Version::File { content, .. }) => self.version_bytes(space, content, secret)?,
Some(Version::Link { .. }) => None,
None => Some(Vec::new()),
};
let mut clashed = false;
let mut merged_any = false;
for other in others {
let other_bytes = match other {
Version::File { content, .. } => self.version_bytes(space, content, secret)?,
Version::Link { .. } => None,
};
let text = match (&ours, &base_bytes, &other_bytes) {
(Some(ours), Some(base), Some(other)) => [ours, base, other]
.iter()
.all(|bytes| super::diff::is_text(bytes))
.then(|| (ours.clone(), base.clone(), other.clone())),
_ => None,
};
if let Some((mine, from, theirs)) = text {
match diffy::merge_bytes(&from, &mine, &theirs) {
Ok(merged) => ours = Some(merged),
Err(marked) => {
ours = Some(marked);
clashed = true;
}
}
merged_any = true;
continue;
}
let device = self.device_of(space, arrival.entity, other);
let Some((beside, there)) = place_beside(here, (path, &device), other) else {
let why = format!("{path}.conflict-{device} and the names after it are taken");
updated.skipped.push((path.clone(), why));
return Ok(None);
};
clashed = true;
if there {
continue;
}
here.index.mark(
here.tree,
&beside,
"NibKeep wrote it, the other version of a file, from another device",
)?;
if let Err(why) =
self.write_new(space, here.place, &beside, &Content::of(other, secret))
{
here.index
.unmark(here.tree, std::slice::from_ref(&beside))?;
updated.skipped.push((path.clone(), why));
return Ok(None);
}
}
if merged_any && let Some(bytes) = &ours {
let executable = merged_executable(found, base.as_ref(), &arrival.version);
let merged = Content::bytes(bytes, executable, secret);
if let Err(why) = self.write_over(space, found, &merged) {
updated.skipped.push((path.clone(), why));
return Ok(None);
}
}
Ok(Some(clashed))
}
fn write_beside(&self, here: &Here<'_>, arrival: &Arriving, updated: &mut Updated) {
let device = self.device_of(here.space, arrival.entity, &arrival.version);
let named = (1..=99)
.map(|number| match number {
1 => format!("{}.conflict-{device}", arrival.path),
_ => format!("{}.conflict-{device}-{number}", arrival.path),
})
.find(|beside| match here.conflicts.get(beside) {
Some(conflict) => conflict.entity == arrival.entity,
None => located(here.place.root, beside, here.place.apart) == Located::Absent,
});
let Some(beside) = named else {
let why = format!(
"{}.conflict-{device} and the names after it are taken",
arrival.path
);
updated.skipped.push((arrival.path.clone(), why));
return;
};
let content = Content::of(&arrival.version, arrival.secret);
let written = match located(here.place.root, &beside, here.place.apart) {
Located::Absent
if here.conflicts.get(&beside).is_some_and(|conflict| {
conflict.entity == arrival.entity
&& conflict
.written
.as_ref()
.is_some_and(|written| same_content(written, &arrival.version))
}) =>
{
let why = "the other file at this path, settled here: commit to record it";
updated.kept.push((beside, why.to_owned()));
return;
}
Located::Absent => {
let why = "NibKeep wrote it, the file of another device at the same path";
match here.index.mark(here.tree, &beside, why) {
Ok(()) => match self.write_new(here.space, here.place, &beside, &content) {
Ok(_) => Ok(()),
Err(why) => {
let _ = here.index.unmark(here.tree, std::slice::from_ref(&beside));
Err(why)
}
},
Err(error) => Err(error.to_string()),
}
}
Located::Here(on_disk) => match look(&on_disk) {
Ok(found) => {
let holds = copy_version(&found);
let before = here
.conflicts
.get(&beside)
.filter(|conflict| conflict.entity == arrival.entity)
.and_then(|conflict| conflict.written.clone());
let holding = |version: &Version| {
holds
.as_ref()
.is_some_and(|holds| same_content(holds, version))
};
if holding(&arrival.version) {
Ok(())
} else if before.as_ref().is_some_and(holding) {
self.write_over(here.space, &found, &content)
} else {
Err(format!("{beside} is in the way"))
}
}
Err(why) => Err(why),
},
Located::Unknown(why) => Err(why),
};
let marked = written.and_then(|()| {
here.index
.mark_conflict(
here.tree,
(&beside, &arrival.path),
(arrival.entity, &arrival.version),
)
.map_err(|error| error.to_string())
});
match marked {
Ok(()) => updated.conflicts.push(arrival.path.clone()),
Err(why) => updated.skipped.push((arrival.path.clone(), why)),
}
}
fn settle_repositories(
&self,
space: SpaceId,
(tree, index): (EntityId, &Index),
updated: &mut Updated,
) -> Result<(), NodeError> {
let recorded: BTreeMap<EntityId, String> = self
.recorded_repositories(space, tree)?
.into_iter()
.map(|recorded| (recorded.entity, recorded.path))
.collect();
let mut gone = Vec::new();
for (path, (entity, _)) in index.repositories(tree)? {
match recorded.get(&entity) {
Some(now) if *now == path => {}
Some(now) => {
let why =
format!("a Git repository moved elsewhere to {now}: move its folder there");
updated.kept.push((path, why));
}
None => {
let why = "a Git repository deleted from the tree elsewhere, whose folder stays \
with its work until you delete it";
index.mark(tree, &path, why)?;
updated.kept.push((path.clone(), why.to_owned()));
gone.push(path);
}
}
}
index.forget_repositories(tree, &gone)
}
fn device_of(&self, space: SpaceId, entity: EntityId, version: &Version) -> String {
let (fields, value) = match version {
Version::File { content, .. } => {
(&[CONTENT, SECRET_CONTENT][..], Value::Blob(*content))
}
Version::Link { target } => (&[LINK_TARGET][..], Value::Text(target.clone())),
};
let name = {
let open = self.space(space).ok();
let statement = open.and_then(|open| {
let state = open.stored.log().state();
fields.iter().find_map(|field| {
state
.current(entity, field)
.ok()?
.into_iter()
.find(|statement| statement.value == value)
.map(|statement| statement.id)
})
});
statement
.and_then(|statement| self.device_name_of(space, statement))
.unwrap_or_else(|| "elsewhere".to_owned())
};
let kept: String = name
.chars()
.take(40)
.map(|character| {
if character.is_alphanumeric() || matches!(character, '-' | '_') {
character
} else {
'-'
}
})
.collect();
if kept.trim_matches('-').is_empty() {
"elsewhere".to_owned()
} else {
kept
}
}
fn common_base(&self, space: SpaceId, entity: EntityId) -> Result<Option<Version>, NodeError> {
let open = self.space(space)?;
let state = open.stored.log().state();
let storage = state.storage();
let mut competing = Vec::new();
for field in [CONTENT, SECRET_CONTENT, LINK_TARGET] {
for id in storage.of_field(entity, &field)? {
if state.is_current(id)? {
competing.push(id);
}
}
}
let mut common: Option<BTreeSet<StatementId>> = None;
for id in competing {
let before = self.superseded_through(space, id)?;
common = Some(match common {
Some(common) => common.intersection(&before).copied().collect(),
None => before,
});
}
let mut last: Option<(nibkeep_time::Timestamp, Version)> = None;
for id in common.unwrap_or_default() {
let Some(recorded) = storage.get(id)? else {
continue;
};
let version = match recorded.statement.value.clone() {
Value::Blob(content) => Version::File {
content,
executable: false,
},
Value::Text(target) => Version::Link { target },
_ => continue,
};
if last.as_ref().is_none_or(|(at, _)| recorded.at > *at) {
last = Some((recorded.at, version));
}
}
Ok(last.map(|(_, version)| version))
}
fn superseded_through(
&self,
space: SpaceId,
statement: StatementId,
) -> Result<BTreeSet<StatementId>, NodeError> {
let open = self.space(space)?;
let log = open.stored.log();
let mut seen = BTreeSet::new();
let mut waiting = vec![statement];
while let Some(id) = waiting.pop() {
let change = log
.state()
.storage()
.get(id)?
.and_then(|recorded| recorded.change);
let Some(change) = change else {
continue;
};
let Some(payload) = log.payload(ChangeId::from_hash(change), &open.keys)? else {
continue;
};
for operation in &payload.operations {
if let Operation::Assert {
statement,
supersedes,
..
} = operation
&& statement.id == id
{
for before in supersedes {
if seen.insert(*before) {
waiting.push(*before);
}
}
}
}
}
Ok(seen)
}
#[allow(
clippy::type_complexity,
reason = "the three lists of an update, changed in place"
)]
fn hold_back(
&self,
space: SpaceId,
(state, entries): (&TreeState, &BTreeMap<String, Entry>),
(deleted, moves, arriving): (
&mut Vec<(String, Entry)>,
&mut Vec<(String, String)>,
&mut Vec<Arriving>,
),
updated: &mut Updated,
) -> Result<(), NodeError> {
let is_nibsecret =
|path: &str| path == NIBSECRET || path.ends_with(&format!("/{NIBSECRET}"));
let mut held: BTreeSet<EntityId> = BTreeSet::new();
moves.retain(|(from, to)| {
let Some(entry) = entries.get(from) else {
return true;
};
if state.is_secret(entry.entity) || is_nibsecret(from) {
updated.held.push(format!("{from} -> {to}"));
held.insert(entry.entity);
return false;
}
true
});
deleted.retain(|(path, entry)| {
if is_nibsecret(path) {
updated.held.push(path.clone());
held.insert(entry.entity);
return false;
}
true
});
for arrival in arriving.iter() {
if held.contains(&arrival.entity) || !is_nibsecret(&arrival.path) {
continue;
}
let (
Some(Version::File { content: was, .. }),
Version::File {
content: becomes, ..
},
) = (&arrival.base, &arrival.version)
else {
continue;
};
if was == becomes {
continue;
}
let was = self.content_bytes(space, was)?.unwrap_or_default();
let becomes = self.content_bytes(space, becomes)?.unwrap_or_default();
let was = String::from_utf8_lossy(&was);
let becomes = String::from_utf8_lossy(&becomes);
if !lines(&was).is_subset(&lines(&becomes)) {
updated.held.push(arrival.path.clone());
held.insert(arrival.entity);
}
}
arriving.retain(|arrival| !held.contains(&arrival.entity));
updated.held.sort();
Ok(())
}
}
fn held_paths(entries: &BTreeMap<String, Entry>, changes: &Changes) -> BTreeMap<String, EntityId> {
let mut held: BTreeMap<String, EntityId> = entries
.iter()
.filter(|(path, _)| !changes.removed.contains(path))
.map(|(path, entry)| (path.clone(), entry.entity))
.collect();
held.extend(
changes
.set
.iter()
.map(|(path, entry)| (path.clone(), entry.entity)),
);
held
}
fn tell_moved_apart(
state: &TreeState,
by_entity: &BTreeMap<EntityId, (String, Entry)>,
updated: &mut Updated,
) {
for (entity, paths) in &state.paths {
if let Some((here_at, _)) = by_entity.get(entity)
&& paths.contains(here_at)
{
let elsewhere: Vec<&str> = paths
.iter()
.filter(|path| *path != here_at)
.map(String::as_str)
.collect();
let why = format!(
"moved apart, also to {}: commit to keep it here",
elsewhere.join(", ")
);
updated.kept.push((here_at.clone(), why));
}
}
}
fn place_beside(
here: &Here<'_>,
(path, device): (&str, &str),
other: &Version,
) -> Option<(String, bool)> {
for number in 1..=99 {
let beside = if number == 1 {
format!("{path}.conflict-{device}")
} else {
format!("{path}.conflict-{device}-{number}")
};
match located(here.place.root, &beside, here.place.apart) {
Located::Absent => return Some((beside, false)),
Located::Here(on_disk) => {
let holds = look(&on_disk).ok().and_then(|found| copy_version(&found));
if holds.is_some_and(|holds| same_content(&holds, other)) {
return Some((beside, true));
}
}
Located::Unknown(_) => {}
}
}
None
}
fn entry_for(
entity: EntityId,
(found, version, known): (&Found, &Version, bool),
read_at: i64,
) -> Entry {
Entry {
entity,
version: version.clone(),
size: found.size,
modified: if known { found.modified } else { None },
read_at,
}
}
fn merged_executable(found: &Found, base: Option<&Version>, arrived: &Version) -> bool {
let is = |version: Option<&Version>| {
matches!(
version,
Some(Version::File {
executable: true,
..
})
)
};
match found.executable {
Some(here) if here != is(base) => here,
_ => is(Some(arrived)),
}
}
fn delete_copy(
place: Place<'_>,
(path, entry): (&String, &Entry),
updated: &mut Updated,
removed: &mut Vec<String>,
) {
let on_disk = match located(place.root, path, place.apart) {
Located::Here(on_disk) => on_disk,
Located::Absent => {
removed.push(path.clone());
return;
}
Located::Unknown(why) => {
updated.skipped.push((path.clone(), why));
return;
}
};
let Ok(found) = look(&on_disk) else {
updated
.skipped
.push((path.clone(), "it cannot be read".to_owned()));
return;
};
let unedited =
copy_version(&found).is_some_and(|holds| same_version(&found, &holds, &entry.version));
if !unedited {
let why = "deleted elsewhere, and edited here".to_owned();
updated.kept.push((path.clone(), why));
return;
}
if !super::write::still_the_copy(&found) {
updated
.kept
.push((path.clone(), "it changed meanwhile".to_owned()));
return;
}
match fs::remove_file(&on_disk) {
Ok(()) => {
sync_folder_of(&on_disk);
updated.deleted.push(path.clone());
removed.push(path.clone());
}
Err(error) => updated.skipped.push((path.clone(), error.to_string())),
}
}
fn carry_out_moves(
place: Place<'_>,
(entries, moves): (&BTreeMap<String, Entry>, &[(String, String)]),
(updated, changes): (&mut Updated, &mut Changes),
) -> BTreeSet<EntityId> {
let at = undo_moves_at(place, moves, &mut updated.skipped);
let mut failed = BTreeSet::new();
for (from, to) in moves {
let Some(entry) = entries.get(from) else {
continue;
};
if at.get(from) == Some(to) {
updated.moved.push((from.clone(), to.clone()));
changes.removed.push(from.clone());
changes.set.push((to.clone(), entry.clone()));
} else {
failed.insert(entry.entity);
}
}
failed
}
fn note_writing(index: &Index, tree: EntityId, arriving: &[Arriving]) -> Result<bool, NodeError> {
let secrets: Vec<(String, EntityId, Version)> = arriving
.iter()
.filter(|arrival| arrival.secret && !arrival.nothing_new())
.map(|arrival| {
(
arrival.path.clone(),
arrival.entity,
arrival.version.clone(),
)
})
.collect();
if secrets.is_empty() {
return Ok(false);
}
index.about_to_write(tree, &secrets)?;
Ok(true)
}
#[allow(clippy::type_complexity, reason = "three lists, made once")]
fn plan_update(
(state, in_full): (&TreeState, &BTreeSet<EntityId>),
by_entity: &BTreeMap<EntityId, (String, Entry)>,
written: &BTreeMap<EntityId, BTreeSet<String>>,
) -> (Vec<(String, Entry)>, Vec<(String, String)>, Vec<Arriving>) {
let mut deleted = Vec::new();
let mut moves = Vec::new();
let mut arriving = Vec::new();
let entities: BTreeSet<EntityId> = state
.by_entity
.keys()
.chain(by_entity.keys())
.copied()
.collect();
for entity in entities {
match (by_entity.get(&entity), version_of(state, entity)) {
(Some(_), None) if in_full.contains(&entity) => {}
(Some((path, entry)), None) => deleted.push((path.clone(), entry.clone())),
(ours, Some((path, version))) => {
let Some(version) = version else {
continue;
};
let path = match ours {
Some((from, _)) if state.at(entity, from) => from.clone(),
_ => path,
};
if let Some((from, _)) = ours
&& *from != path
{
moves.push((from.clone(), path.clone()));
}
arriving.push(Arriving {
path,
entity,
base: ours.map(|(_, entry)| entry.version.clone()),
version,
competing: state.versions.get(&entity).cloned().unwrap_or_default(),
written: written.get(&entity).cloned().unwrap_or_default(),
deletions: state.contested.get(&entity).cloned().unwrap_or_default(),
secret: state.is_secret(entity),
});
}
(None, None) => {}
}
}
(deleted, moves, arriving)
}
fn goes_beside(held: &BTreeMap<String, EntityId>, here: &Here<'_>, arrival: &Arriving) -> bool {
match held.get(&arrival.path) {
Some(entity) => *entity != arrival.entity,
None if arrival.base.is_some() || !here.whole => false,
None => match located(here.place.root, &arrival.path, here.place.apart) {
Located::Here(on_disk) => look(&on_disk)
.ok()
.and_then(|found| copy_version(&found).map(|holds| (found, holds)))
.is_none_or(|(found, holds)| !same_version(&found, &holds, &arrival.version)),
Located::Absent | Located::Unknown(_) => false,
},
}
}
fn clear_obsolete(
(tree, place, index): (EntityId, Place<'_>, &Index),
state: &TreeState,
updated: &mut Updated,
) -> Result<(), NodeError> {
let placed: BTreeSet<EntityId> = index
.entries(tree)?
.values()
.map(|entry| entry.entity)
.collect();
for (beside, conflict) in index.conflicts(tree)? {
if still_competes(state, &placed, &conflict) {
continue;
}
let on_disk = match located(place.root, &beside, place.apart) {
Located::Here(on_disk) => on_disk,
Located::Absent => {
index.unmark(tree, std::slice::from_ref(&beside))?;
continue;
}
Located::Unknown(_) => continue,
};
let found = look(&on_disk).ok();
let as_written = found.as_ref().is_some_and(|found| {
copy_version(found)
.zip(conflict.written.as_ref())
.is_some_and(|(holds, written)| same_content(&holds, written))
});
let still = found.is_some_and(|found| {
look(&on_disk).is_ok_and(|now| {
now.identity == found.identity
&& now.size == found.size
&& now.modified == found.modified
})
});
if as_written && still && fs::remove_file(&on_disk).is_ok() {
sync_folder_of(&on_disk);
index.unmark(tree, std::slice::from_ref(&beside))?;
updated.deleted.push(beside);
} else {
index.forget_conflict(tree, &beside)?;
}
}
Ok(())
}