use super::Error;
use crate::pristine::*;
use chrono::{DateTime, Utc};
use std::collections::{BTreeSet, HashSet};
#[cfg(feature = "zstd")]
pub mod v3;
#[cfg(feature = "zstd")]
use std::io::Write;
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub enum Atom<Change> {
NewVertex(NewVertex<Change>),
EdgeMap(EdgeMap<Change>),
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct NewVertex<Change> {
pub up_context: Vec<Position<Change>>,
pub down_context: Vec<Position<Change>>,
pub flag: EdgeFlags,
pub start: ChangePosition,
pub end: ChangePosition,
pub inode: Position<Change>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct EdgeMap<Change> {
pub edges: Vec<NewEdge<Change>>,
pub inode: Position<Change>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct NewEdge<Change> {
pub previous: EdgeFlags,
pub flag: EdgeFlags,
pub from: Position<Change>,
pub to: Vertex<Change>,
pub introduced_by: Change,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct ChangeHeader {
pub message: String,
pub description: Option<String>,
pub timestamp: DateTime<Utc>,
pub authors: Vec<Author>,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct Author {
pub name: String,
#[serde(default = "default_none")]
pub full_name: Option<String>,
#[serde(default = "default_none")]
pub email: Option<String>,
}
fn default_none() -> Option<String> {
None
}
impl std::fmt::Display for Author {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
if self.full_name.is_none() && self.email.is_none() {
write!(fmt, "{:?}", self.name)
} else {
write!(fmt, "{{ name = {:?}", self.name)?;
if let Some(ref f) = self.full_name {
write!(fmt, ", full_name = {:?}", f)?;
}
if let Some(ref f) = self.email {
write!(fmt, ", email = {:?}", f)?;
}
write!(fmt, " }}")
}
}
}
impl Default for ChangeHeader {
fn default() -> Self {
ChangeHeader {
message: String::new(),
description: None,
timestamp: Utc::now(),
authors: Vec::new(),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct LocalChange<Local> {
pub offsets: Offsets,
pub hashed: Hashed<Local>,
pub unhashed: Option<toml::Value>,
pub contents: Vec<u8>,
}
impl std::ops::Deref for LocalChange<Local> {
type Target = Hashed<Local>;
fn deref(&self) -> &Self::Target {
&self.hashed
}
}
impl std::ops::DerefMut for LocalChange<Local> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.hashed
}
}
pub const VERSION: u64 = 4;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Hashed<Local> {
pub version: u64,
pub header: ChangeHeader,
pub dependencies: Vec<Hash>,
pub extra_known: Vec<Hash>,
pub metadata: Vec<u8>,
pub changes: Vec<Record<Option<Hash>, Local>>,
pub contents_hash: Hash,
}
pub type Change = LocalChange<Local>;
pub fn dependencies<'a, Local: 'a, I: Iterator<Item = &'a Record<Option<Hash>, Local>>, T: TxnT>(
txn: &T,
channel: &ChannelRef<T>,
changes: I,
) -> (Vec<Hash>, Vec<Hash>) {
let mut deps = BTreeSet::new();
let mut zombie_deps = BTreeSet::new();
let channel = channel.r.borrow();
for ch in changes.flat_map(|r| r.iter()) {
match *ch {
Atom::NewVertex(NewVertex {
ref up_context,
ref down_context,
..
}) => {
for up in up_context.iter().chain(down_context.iter()) {
match up.change {
None | Some(Hash::None) => {}
Some(ref dep) => {
deps.insert(dep.clone());
}
}
}
}
Atom::EdgeMap(EdgeMap { ref edges, .. }) => {
for e in edges {
assert!(!e.flag.contains(EdgeFlags::PARENT));
assert!(e.introduced_by != Some(Hash::None));
if let Some(p) = e.from.change {
deps.insert(p);
}
if let Some(p) = e.introduced_by {
deps.insert(p);
}
if let Some(p) = e.to.change {
deps.insert(p);
}
add_zombie_deps_from(txn, &channel, &mut zombie_deps, e.from);
add_zombie_deps_to(txn, &channel, &mut zombie_deps, e.to)
}
}
}
}
let deps = minimize_deps(txn, &channel, &deps);
for d in deps.iter() {
zombie_deps.remove(d);
}
let mut deps: Vec<Hash> = deps.into_iter().collect();
deps.sort_by(|a, b| {
let a = txn.get_internal(*a).unwrap();
let b = txn.get_internal(*b).unwrap();
txn.get_changeset(&channel.changes, a, None)
.cmp(&txn.get_changeset(&channel.changes, b, None))
});
let mut zombie_deps: Vec<Hash> = zombie_deps.into_iter().collect();
zombie_deps.sort_by(|a, b| {
let a = txn.get_internal(*a).unwrap();
let b = txn.get_internal(*b).unwrap();
txn.get_changeset(&channel.changes, a, None)
.cmp(&txn.get_changeset(&channel.changes, b, None))
});
(deps, zombie_deps)
}
pub fn full_dependencies<T: TxnT>(txn: &T, channel: &ChannelRef<T>) -> (Vec<Hash>, Vec<Hash>) {
let mut deps = BTreeSet::new();
let channel = channel.borrow();
for (_, (ch, _)) in changeid_log(txn, &channel, 0) {
let h = txn.get_external(ch).unwrap();
deps.insert(h);
}
let deps = minimize_deps(txn, &channel, &deps);
(deps, Vec::new())
}
fn add_zombie_deps_from<T: TxnT>(
txn: &T,
channel: &Channel<T>,
zombie_deps: &mut BTreeSet<Hash>,
e_from: Position<Option<Hash>>,
) {
let e_from = if let Some(p) = e_from.change {
Position {
change: txn.get_internal(p).unwrap(),
pos: e_from.pos,
}
} else {
return;
};
let from = find_block_end(txn, channel, e_from).unwrap();
for edge in iter_adjacent(txn, channel, from, EdgeFlags::empty(), EdgeFlags::all()) {
if let Some(ext) = txn.get_external(edge.introduced_by) {
if let Hash::None = ext {
} else {
zombie_deps.insert(ext.to_owned());
}
}
if let Some(ext) = txn.get_external(edge.dest.change) {
if let Hash::None = ext {
} else {
zombie_deps.insert(ext.to_owned());
}
}
}
}
fn add_zombie_deps_to<T: TxnT>(
txn: &T,
channel: &Channel<T>,
zombie_deps: &mut BTreeSet<Hash>,
e_to: Vertex<Option<Hash>>,
) {
let to_pos = if let Some(p) = e_to.change {
Position {
change: txn.get_internal(p).unwrap(),
pos: e_to.start,
}
} else {
return;
};
let mut to = find_block(txn, channel, to_pos).unwrap();
loop {
for edge in iter_adjacent(txn, channel, to, EdgeFlags::empty(), EdgeFlags::all()) {
if let Some(ext) = txn.get_external(edge.introduced_by) {
if let Hash::None = ext {
} else {
zombie_deps.insert(ext.to_owned());
}
}
if let Some(ext) = txn.get_external(edge.dest.change) {
if let Hash::None = ext {
} else {
zombie_deps.insert(ext.to_owned());
}
}
}
if to.end >= e_to.end {
break;
}
to = find_block(txn, channel, to.end_pos()).unwrap();
}
}
fn minimize_deps<T: TxnT>(txn: &T, channel: &Channel<T>, deps: &BTreeSet<Hash>) -> Vec<Hash> {
let mut min_time = std::u64::MAX;
let mut internal_deps = Vec::new();
let mut internal_deps_ = HashSet::new();
for h in deps.iter() {
if let Hash::None = h {
continue;
}
debug!("h = {:?}", h);
let id = txn.get_internal(*h).unwrap();
debug!("id = {:?}", id);
let time = txn.get_changeset(&channel.changes, id, None).unwrap();
debug!("time = {:?}", time);
min_time = min_time.min(time);
internal_deps.push((id, true));
internal_deps_.insert(id);
}
internal_deps.sort_by(|a, b| a.1.cmp(&b.1));
let mut visited = HashSet::new();
while let Some((id, is_root)) = internal_deps.pop() {
if is_root {
if !internal_deps_.contains(&id) {
continue;
}
} else {
if internal_deps_.remove(&id) {
debug!("removing dep {:?}", id);
}
}
if !visited.insert(id) {
continue;
}
let mut cursor = txn.iter_dep(id);
while let Some((id0, dep)) = txn.cursor_dep_next(&mut cursor.cursor) {
trace!("minimize loop = {:?} {:?}", id0, dep);
if id0 < id {
continue;
} else if id0 > id {
break;
}
let time = if let Some(time) = txn.get_changeset(&channel.changes, dep, None) {
time
} else {
panic!(
"not found in channel {:?}: id = {:?} depends on {:?}",
channel.name(),
id,
dep
);
};
trace!("time = {:?}", time);
if time >= min_time {
internal_deps.push((dep, false))
}
}
}
internal_deps_
.into_iter()
.map(|id| txn.get_external(id).unwrap())
.collect()
}
impl Change {
pub fn knows(&self, hash: &Hash) -> bool {
self.extra_known.contains(hash) || self.dependencies.contains(&hash)
}
pub fn has_edge(
&self,
hash: Hash,
from: Position<Option<Hash>>,
to: Position<Option<Hash>>,
flags: crate::pristine::EdgeFlags,
) -> bool {
debug!("has_edge: {:?} {:?} {:?} {:?}", hash, from, to, flags);
for change_ in self.changes.iter() {
for change_ in change_.iter() {
match change_ {
Atom::NewVertex(n) => {
debug!("has_edge: {:?}", n);
if from.change == Some(hash) && from.pos >= n.start && from.pos <= n.end {
if to.change == Some(hash) {
return flags | EdgeFlags::FOLDER
== EdgeFlags::BLOCK | EdgeFlags::FOLDER;
} else {
if n.down_context.iter().any(|d| *d == to) {
return flags.is_empty();
} else {
return false;
}
}
} else if to.change == Some(hash) && to.pos >= n.start && to.pos <= n.end {
if n.up_context.iter().any(|d| *d == from) {
return flags | EdgeFlags::FOLDER
== EdgeFlags::BLOCK | EdgeFlags::FOLDER;
} else {
return false;
}
}
}
Atom::EdgeMap(e) => {
debug!("has_edge: {:?}", e);
if e.edges
.iter()
.any(|e| e.from == from && e.to.start_pos() == to && e.flag == flags)
{
return true;
}
}
}
}
}
debug!("not found");
false
}
}
impl Atom<Option<Hash>> {
pub fn inode(&self) -> Position<Option<Hash>> {
match self {
Atom::NewVertex(ref n) => n.inode,
Atom::EdgeMap(ref n) => n.inode,
}
}
pub fn inverse(&self, hash: &Hash) -> Self {
match *self {
Atom::NewVertex(NewVertex {
ref up_context,
flag,
start,
end,
ref inode,
..
}) => {
let mut edges = Vec::new();
for up in up_context {
let previous = flag | EdgeFlags::BLOCK;
edges.push(NewEdge {
previous,
flag: previous | EdgeFlags::DELETED,
from: Position {
change: Some(if let Some(ref h) = up.change {
*h
} else {
*hash
}),
pos: up.pos,
},
to: Vertex {
change: Some(*hash),
start,
end,
},
introduced_by: Some(*hash),
})
}
Atom::EdgeMap(EdgeMap {
edges,
inode: Position {
change: Some(if let Some(p) = inode.change { p } else { *hash }),
pos: inode.pos,
},
})
}
Atom::EdgeMap(EdgeMap {
ref edges,
ref inode,
}) => Atom::EdgeMap(EdgeMap {
inode: Position {
change: Some(if let Some(p) = inode.change { p } else { *hash }),
pos: inode.pos,
},
edges: edges
.iter()
.map(|e| {
let mut e = e.clone();
e.introduced_by = Some(*hash);
std::mem::swap(&mut e.flag, &mut e.previous);
e
})
.collect(),
}),
}
}
}
impl EdgeMap<Option<Hash>> {
fn concat(mut self, e: EdgeMap<Option<Hash>>) -> Self {
assert_eq!(self.inode, e.inode);
self.edges.extend(e.edges.into_iter());
EdgeMap {
inode: self.inode,
edges: self.edges,
}
}
}
impl<L: Clone> Record<Option<Hash>, L> {
pub fn inverse(&self, hash: &Hash) -> Self {
match self {
Record::FileMove { del, add, path } => Record::FileMove {
del: add.inverse(hash),
add: del.inverse(hash),
path: path.clone(),
},
Record::FileDel {
del,
contents,
path,
} => Record::FileUndel {
undel: del.inverse(hash),
contents: contents.as_ref().map(|c| c.inverse(hash)),
path: path.clone(),
},
Record::FileUndel {
undel,
contents,
path,
} => Record::FileDel {
del: undel.inverse(hash),
contents: contents.as_ref().map(|c| c.inverse(hash)),
path: path.clone(),
},
Record::FileAdd {
add_name,
add_inode,
contents,
path,
} => {
let del = match (add_name.inverse(hash), add_inode.inverse(hash)) {
(Atom::EdgeMap(e0), Atom::EdgeMap(e1)) => Atom::EdgeMap(e0.concat(e1)),
_ => unreachable!(),
};
Record::FileDel {
del,
contents: contents.as_ref().map(|c| c.inverse(hash)),
path: path.clone(),
}
}
Record::SolveNameConflict { name, path } => Record::UnsolveNameConflict {
name: name.inverse(hash),
path: path.clone(),
},
Record::UnsolveNameConflict { name, path } => Record::SolveNameConflict {
name: name.inverse(hash),
path: path.clone(),
},
Record::Edit { change, local } => Record::Edit {
change: change.inverse(hash),
local: local.clone(),
},
Record::Replacement {
change,
replacement,
local,
} => Record::Replacement {
change: replacement.inverse(hash),
replacement: change.inverse(hash),
local: local.clone(),
},
Record::SolveOrderConflict { change, local } => Record::UnsolveOrderConflict {
change: change.inverse(hash),
local: local.clone(),
},
Record::UnsolveOrderConflict { change, local } => Record::SolveOrderConflict {
change: change.inverse(hash),
local: local.clone(),
},
Record::ResurrectZombies { change, local } => Record::Edit {
change: change.inverse(hash),
local: local.clone(),
},
}
}
}
impl Change {
pub fn inverse(&self, hash: &Hash, header: ChangeHeader, metadata: Vec<u8>) -> Self {
let dependencies = vec![*hash];
let contents_hash = Hasher::default().finish();
Change {
offsets: Offsets::default(),
hashed: Hashed {
version: VERSION,
header,
dependencies,
extra_known: self.extra_known.clone(),
metadata,
changes: self.changes.iter().map(|r| r.inverse(hash)).collect(),
contents_hash,
},
contents: Vec::new(),
unhashed: None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Local {
pub path: String,
pub line: usize,
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub enum Record<Hash, Local> {
FileMove {
del: Atom<Hash>,
add: Atom<Hash>,
path: String,
},
FileDel {
del: Atom<Hash>,
contents: Option<Atom<Hash>>,
path: String,
},
FileUndel {
undel: Atom<Hash>,
contents: Option<Atom<Hash>>,
path: String,
},
FileAdd {
add_name: Atom<Hash>,
add_inode: Atom<Hash>,
contents: Option<Atom<Hash>>,
path: String,
},
SolveNameConflict {
name: Atom<Hash>,
path: String,
},
UnsolveNameConflict {
name: Atom<Hash>,
path: String,
},
Edit {
change: Atom<Hash>,
local: Local,
},
Replacement {
change: Atom<Hash>,
replacement: Atom<Hash>,
local: Local,
},
SolveOrderConflict {
change: Atom<Hash>,
local: Local,
},
UnsolveOrderConflict {
change: Atom<Hash>,
local: Local,
},
ResurrectZombies {
change: Atom<Hash>,
local: Local,
},
}
#[doc(hidden)]
pub struct RecordIter<R, C> {
rec: Option<R>,
extra: Option<C>,
extra2: Option<C>,
}
impl<Context, Local> IntoIterator for Record<Context, Local> {
type IntoIter = RecordIter<Record<Context, Local>, Atom<Context>>;
type Item = Atom<Context>;
fn into_iter(self) -> Self::IntoIter {
RecordIter {
rec: Some(self),
extra: None,
extra2: None,
}
}
}
impl<Context, Local> Record<Context, Local> {
pub fn iter(&self) -> RecordIter<&Record<Context, Local>, &Atom<Context>> {
RecordIter {
rec: Some(self),
extra: None,
extra2: None,
}
}
pub fn rev_iter(&self) -> RevRecordIter<&Record<Context, Local>, &Atom<Context>> {
RevRecordIter {
rec: Some(self),
extra: None,
extra2: None,
}
}
}
impl<Context, Local> Iterator for RecordIter<Record<Context, Local>, Atom<Context>> {
type Item = Atom<Context>;
fn next(&mut self) -> Option<Self::Item> {
if let Some(extra) = self.extra.take() {
Some(extra)
} else if let Some(extra) = self.extra2.take() {
Some(extra)
} else if let Some(rec) = self.rec.take() {
match rec {
Record::FileMove { del, add, .. } => {
self.extra = Some(add);
Some(del)
}
Record::FileDel { del, contents, .. } => {
self.extra = contents;
Some(del)
}
Record::FileUndel {
undel, contents, ..
} => {
self.extra = contents;
Some(undel)
}
Record::FileAdd {
add_name,
add_inode,
contents,
..
} => {
self.extra = Some(add_inode);
self.extra2 = contents;
Some(add_name)
}
Record::SolveNameConflict { name, .. } => Some(name),
Record::UnsolveNameConflict { name, .. } => Some(name),
Record::Edit { change, .. } => Some(change),
Record::Replacement {
change,
replacement,
..
} => {
self.extra = Some(replacement);
Some(change)
}
Record::SolveOrderConflict { change, .. } => Some(change),
Record::UnsolveOrderConflict { change, .. } => Some(change),
Record::ResurrectZombies { change, .. } => Some(change),
}
} else {
None
}
}
}
impl<'a, Context, Local> Iterator for RecordIter<&'a Record<Context, Local>, &'a Atom<Context>> {
type Item = &'a Atom<Context>;
fn next(&mut self) -> Option<Self::Item> {
if let Some(extra) = self.extra.take() {
Some(extra)
} else if let Some(extra) = self.extra2.take() {
Some(extra)
} else if let Some(rec) = self.rec.take() {
match *rec {
Record::FileMove {
ref del, ref add, ..
} => {
self.extra = Some(add);
Some(del)
}
Record::FileDel {
ref del,
ref contents,
..
} => {
self.extra = contents.as_ref();
Some(del)
}
Record::FileUndel {
ref undel,
ref contents,
..
} => {
self.extra = contents.as_ref();
Some(undel)
}
Record::FileAdd {
ref add_name,
ref add_inode,
ref contents,
..
} => {
self.extra = Some(add_inode);
self.extra2 = contents.as_ref();
Some(&add_name)
}
Record::SolveNameConflict { ref name, .. } => Some(&name),
Record::UnsolveNameConflict { ref name, .. } => Some(&name),
Record::Edit { change: ref c, .. } => Some(c),
Record::Replacement {
replacement: ref r,
change: ref c,
..
} => {
self.extra = Some(r);
Some(c)
}
Record::SolveOrderConflict { ref change, .. } => Some(change),
Record::UnsolveOrderConflict { ref change, .. } => Some(change),
Record::ResurrectZombies { ref change, .. } => Some(change),
}
} else {
None
}
}
}
pub struct RevRecordIter<R, C> {
rec: Option<R>,
extra: Option<C>,
extra2: Option<C>,
}
impl<'a, Context, Local> Iterator for RevRecordIter<&'a Record<Context, Local>, &'a Atom<Context>> {
type Item = &'a Atom<Context>;
fn next(&mut self) -> Option<Self::Item> {
if let Some(extra) = self.extra.take() {
Some(extra)
} else if let Some(extra) = self.extra2.take() {
Some(extra)
} else if let Some(rec) = self.rec.take() {
match *rec {
Record::FileMove {
ref del, ref add, ..
} => {
self.extra = Some(del);
Some(add)
}
Record::FileDel {
ref del,
ref contents,
..
} => {
if let Some(ref c) = contents {
self.extra = Some(del);
Some(c)
} else {
Some(del)
}
}
Record::FileUndel {
ref undel,
ref contents,
..
} => {
if let Some(ref c) = contents {
self.extra = Some(undel);
Some(c)
} else {
Some(undel)
}
}
Record::FileAdd {
ref add_name,
ref add_inode,
ref contents,
..
} => {
if let Some(ref c) = contents {
self.extra = Some(add_inode);
self.extra2 = Some(add_name);
Some(c)
} else {
self.extra = Some(add_name);
Some(add_inode)
}
}
Record::SolveNameConflict { ref name, .. } => Some(&name),
Record::UnsolveNameConflict { ref name, .. } => Some(&name),
Record::Edit { change: ref c, .. } => Some(c),
Record::Replacement {
replacement: ref r,
change: ref c,
..
} => {
self.extra = Some(c);
Some(r)
}
Record::SolveOrderConflict { ref change, .. } => Some(change),
Record::UnsolveOrderConflict { ref change, .. } => Some(change),
Record::ResurrectZombies { ref change, .. } => Some(change),
}
} else {
None
}
}
}
impl Atom<Option<ChangeId>> {
fn globalize<T: TxnT>(&self, txn: &T) -> Atom<Option<Hash>> {
match self {
Atom::NewVertex(NewVertex {
up_context,
down_context,
start,
end,
flag,
inode,
}) => Atom::NewVertex(NewVertex {
up_context: up_context
.iter()
.map(|&up| Position {
change: up.change.and_then(|a| txn.get_external(a)),
pos: up.pos,
})
.collect(),
down_context: down_context
.iter()
.map(|&down| Position {
change: down.change.and_then(|a| txn.get_external(a)),
pos: down.pos,
})
.collect(),
start: *start,
end: *end,
flag: *flag,
inode: Position {
change: inode.change.and_then(|a| txn.get_external(a)),
pos: inode.pos,
},
}),
Atom::EdgeMap(EdgeMap { edges, inode }) => Atom::EdgeMap(EdgeMap {
edges: edges
.iter()
.map(|edge| NewEdge {
previous: edge.previous,
flag: edge.flag,
from: Position {
change: edge.from.change.and_then(|a| txn.get_external(a)),
pos: edge.from.pos,
},
to: Vertex {
change: edge.to.change.and_then(|a| txn.get_external(a)),
start: edge.to.start,
end: edge.to.end,
},
introduced_by: edge.introduced_by.map(|a| {
if let Some(a) = txn.get_external(a) {
a
} else {
panic!("introduced by {:?}", a);
}
}),
})
.collect(),
inode: Position {
change: inode.change.and_then(|a| txn.get_external(a)),
pos: inode.pos,
},
}),
}
}
}
impl<H> Record<H, Local> {
pub fn path(&self) -> &str {
match self {
Record::FileMove { ref path, .. }
| Record::FileDel { ref path, .. }
| Record::FileUndel { ref path, .. }
| Record::SolveNameConflict { ref path, .. }
| Record::UnsolveNameConflict { ref path, .. }
| Record::FileAdd { ref path, .. } => path,
Record::Edit { ref local, .. }
| Record::Replacement { ref local, .. }
| Record::SolveOrderConflict { ref local, .. }
| Record::UnsolveOrderConflict { ref local, .. }
| Record::ResurrectZombies { ref local, .. } => &local.path,
}
}
pub fn line(&self) -> Option<usize> {
match self {
Record::FileMove { .. }
| Record::FileDel { .. }
| Record::FileUndel { .. }
| Record::SolveNameConflict { .. }
| Record::UnsolveNameConflict { .. }
| Record::FileAdd { .. } => None,
Record::Edit { ref local, .. }
| Record::Replacement { ref local, .. }
| Record::SolveOrderConflict { ref local, .. }
| Record::UnsolveOrderConflict { ref local, .. }
| Record::ResurrectZombies { ref local, .. } => Some(local.line),
}
}
}
impl<Local> Record<Option<ChangeId>, Local> {
pub fn globalize<T: TxnT>(self, txn: &T) -> Record<Option<Hash>, Local> {
match self {
Record::FileMove { del, add, path } => Record::FileMove {
del: del.globalize(txn),
add: add.globalize(txn),
path,
},
Record::FileDel {
del,
contents,
path,
} => Record::FileDel {
del: del.globalize(txn),
contents: contents.as_ref().map(|del| del.globalize(txn)),
path,
},
Record::FileUndel {
undel,
contents,
path,
} => Record::FileUndel {
undel: undel.globalize(txn),
contents: contents.as_ref().map(|del| del.globalize(txn)),
path,
},
Record::SolveNameConflict { name, path } => Record::SolveNameConflict {
name: name.globalize(txn),
path,
},
Record::UnsolveNameConflict { name, path } => Record::UnsolveNameConflict {
name: name.globalize(txn),
path,
},
Record::FileAdd {
add_inode,
add_name,
contents,
path,
} => Record::FileAdd {
add_name: add_name.globalize(txn),
add_inode: add_inode.globalize(txn),
contents: contents.as_ref().map(|add| add.globalize(txn)),
path,
},
Record::Edit { change, local } => Record::Edit {
change: change.globalize(txn),
local,
},
Record::Replacement {
change,
replacement,
local,
} => Record::Replacement {
change: change.globalize(txn),
replacement: replacement.globalize(txn),
local,
},
Record::SolveOrderConflict { change, local } => Record::SolveOrderConflict {
change: change.globalize(txn),
local,
},
Record::UnsolveOrderConflict { change, local } => Record::UnsolveOrderConflict {
change: change.globalize(txn),
local,
},
Record::ResurrectZombies { change, local } => Record::ResurrectZombies {
change: change.globalize(txn),
local,
},
}
}
}
#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq, Eq)]
pub struct Offsets {
version: u64,
hashed_len: u64,
unhashed_off: u64,
unhashed_len: u64,
contents_off: u64,
contents_len: u64,
total: u64,
}
impl<L> LocalChange<L> {
#[cfg(feature = "zstd")]
const OFFSETS_SIZE: u64 = 56;
pub fn make_change<T: TxnT>(
txn: &T,
channel: &ChannelRef<T>,
changes: Vec<Record<Option<Hash>, L>>,
contents: Vec<u8>,
header: ChangeHeader,
metadata: Vec<u8>,
) -> Self {
let (dependencies, extra_known) = dependencies(txn, channel, changes.iter());
trace!("make_change, contents = {:?}", contents);
let contents_hash = {
let mut hasher = Hasher::default();
hasher.update(&contents);
hasher.finish()
};
debug!("make_change, contents_hash = {:?}", contents_hash);
LocalChange {
offsets: Offsets::default(),
hashed: Hashed {
version: VERSION,
header,
changes,
contents_hash,
metadata,
dependencies,
extra_known,
},
contents,
unhashed: None,
}
}
pub fn new() -> Self {
LocalChange {
offsets: Offsets::default(),
hashed: Hashed {
version: VERSION,
header: ChangeHeader::default(),
changes: Vec::new(),
contents_hash: Hasher::default().finish(),
metadata: Vec::new(),
dependencies: Vec::new(),
extra_known: Vec::new(),
},
unhashed: None,
contents: Vec::new(),
}
}
}
#[cfg(feature = "zstd")]
const LEVEL: usize = 1;
#[cfg(feature = "zstd")]
const FRAME_SIZE: usize = 256;
#[cfg(feature = "zstd")]
fn compress<W: Write>(input: &[u8], mut w: W) -> Result<(), anyhow::Error> {
let mut cstream = zstd_seekable::SeekableCStream::new(LEVEL, FRAME_SIZE).unwrap();
let mut output = [0; 4096];
let mut input_pos = 0;
while input_pos < input.len() {
let (out_pos, inp_pos) = cstream.compress(&mut output, &input[input_pos..])?;
w.write_all(&output[..out_pos])?;
input_pos += inp_pos;
}
while let Ok(n) = cstream.end_stream(&mut output) {
if n == 0 {
break;
}
w.write_all(&output[..n])?;
}
Ok(())
}
impl Change {
pub fn size_no_contents<R: std::io::Read + std::io::Seek>(
r: &mut R,
) -> Result<u64, anyhow::Error> {
let pos = r.seek(std::io::SeekFrom::Current(0))?;
let mut off = [0u8; Self::OFFSETS_SIZE as usize];
r.read_exact(&mut off)?;
let off: Offsets = bincode::deserialize(&off)?;
if off.version != VERSION {
return Err(Error::VersionMismatch.into());
}
r.seek(std::io::SeekFrom::Start(pos))?;
Ok(off.contents_off)
}
#[cfg(feature = "zstd")]
pub fn serialize<W: Write>(&self, mut w: W) -> Result<Hash, anyhow::Error> {
let mut offsets = Offsets::default();
offsets.version = VERSION;
let mut hashed = Vec::new();
bincode::serialize_into(&mut hashed, &self.hashed)?;
offsets.hashed_len = hashed.len() as u64;
trace!("hashed = {:?}", hashed);
let mut hasher = Hasher::default();
hasher.update(&hashed);
let hash = hasher.finish();
debug!("{:?}", hash);
let unhashed = if let Some(ref un) = self.unhashed {
let s = toml::ser::to_string(un).unwrap();
s.into()
} else {
Vec::new()
};
offsets.unhashed_len = unhashed.len() as u64;
let mut hashed_comp = Vec::new();
compress(&hashed, &mut hashed_comp)?;
offsets.unhashed_off = Self::OFFSETS_SIZE + hashed_comp.len() as u64;
let mut unhashed_comp = Vec::new();
compress(&unhashed, &mut unhashed_comp)?;
offsets.contents_off = offsets.unhashed_off + unhashed_comp.len() as u64;
let mut contents_comp = Vec::new();
compress(&self.contents, &mut contents_comp)?;
offsets.contents_len = self.contents.len() as u64;
offsets.total = offsets.contents_off + contents_comp.len() as u64;
bincode::serialize_into(&mut w, &offsets)?;
w.write_all(&hashed_comp)?;
w.write_all(&unhashed_comp)?;
w.write_all(&contents_comp)?;
Ok(hash)
}
#[cfg(feature = "zstd")]
pub fn check_from_buffer(buf: &[u8], hash: &Hash) -> Result<(), anyhow::Error> {
let offsets: Offsets = bincode::deserialize_from(&buf[..Self::OFFSETS_SIZE as usize])?;
if offsets.version != VERSION {
return Err(Error::VersionMismatch.into());
}
debug!("check_from_buffer, offsets = {:?}", offsets);
let mut s = zstd_seekable::Seekable::init_buf(
&buf[Self::OFFSETS_SIZE as usize..offsets.unhashed_off as usize],
)?;
let mut buf_ = Vec::new();
buf_.resize(offsets.hashed_len as usize, 0);
s.decompress(&mut buf_[..], 0)?;
debug!("check_from_buffer, buf_ = {:?}", buf_);
let mut hasher = Hasher::default();
hasher.update(&buf_);
let computed_hash = hasher.finish();
debug!("{:?} {:?}", computed_hash, hash);
if &computed_hash != hash {
return Err((Error::ChangeHashMismatch {
claimed: *hash,
computed: computed_hash,
})
.into());
}
let hashed: Hashed<Local> = bincode::deserialize(&buf_)?;
buf_.clear();
buf_.resize(offsets.contents_len as usize, 0);
let mut s = zstd_seekable::Seekable::init_buf(&buf[offsets.contents_off as usize..])?;
buf_.resize(offsets.contents_len as usize, 0);
s.decompress(&mut buf_[..], 0)?;
let mut hasher = Hasher::default();
debug!("contents = {:?}", buf_);
hasher.update(&buf_);
let computed_hash = hasher.finish();
debug!(
"contents hash: {:?}, computed: {:?}",
hashed.contents_hash, computed_hash
);
if computed_hash != hashed.contents_hash {
return Err((Error::ContentsHashMismatch {
claimed: hashed.contents_hash,
computed: computed_hash,
})
.into());
}
Ok(())
}
#[cfg(feature = "zstd")]
pub fn deserialize(file: &str, hash: Option<&Hash>) -> Result<Self, anyhow::Error> {
use std::io::Read;
let mut r = std::fs::File::open(file)?;
let mut buf = vec![0u8; Self::OFFSETS_SIZE as usize];
r.read_exact(&mut buf)?;
let offsets: Offsets = bincode::deserialize(&buf)?;
if offsets.version != VERSION {
return Err(Error::VersionMismatch.into());
}
debug!("offsets = {:?}", offsets);
buf.clear();
buf.resize((offsets.unhashed_off - Self::OFFSETS_SIZE) as usize, 0);
r.read_exact(&mut buf)?;
let hashed: Hashed<Local> = {
let mut s = zstd_seekable::Seekable::init_buf(&buf[..])?;
let mut out = vec![0u8; offsets.hashed_len as usize];
s.decompress(&mut out[..], 0)?;
let mut hasher = Hasher::default();
hasher.update(&out);
let computed_hash = hasher.finish();
if let Some(hash) = hash {
if &computed_hash != hash {
return Err((Error::ChangeHashMismatch {
claimed: *hash,
computed: computed_hash,
})
.into());
}
}
bincode::deserialize_from(&out[..])?
};
buf.clear();
buf.resize((offsets.contents_off - offsets.unhashed_off) as usize, 0);
let unhashed = if buf.is_empty() {
None
} else {
r.read_exact(&mut buf)?;
let mut s = zstd_seekable::Seekable::init_buf(&buf[..])?;
let mut out = vec![0u8; offsets.unhashed_len as usize];
s.decompress(&mut out[..], 0)?;
Some(toml::de::from_slice(&out)?)
};
debug!("unhashed = {:?}", unhashed);
buf.clear();
buf.resize((offsets.total - offsets.contents_off) as usize, 0);
let contents = if r.read_exact(&mut buf).is_ok() {
let mut s = zstd_seekable::Seekable::init_buf(&buf[..])?;
let mut contents = vec![0u8; offsets.contents_len as usize];
s.decompress(&mut contents[..], 0)?;
contents
} else {
Vec::new()
};
debug!("contents = {:?}", contents);
Ok(LocalChange {
offsets,
hashed,
unhashed,
contents,
})
}
pub fn hash(&self) -> Result<Hash, anyhow::Error> {
let input = bincode::serialize(&self.hashed)?;
let mut hasher = Hasher::default();
hasher.update(&input);
Ok(hasher.finish())
}
}
use crate::changestore::*;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::io::BufRead;
#[cfg(feature = "text-changes")]
impl LocalChange<Local> {
const DEPS_LINE: &'static str = "# Dependencies\n";
const CHANGES_LINE: &'static str = "# Changes\n";
pub fn write_all_deps<F: FnMut(Hash) -> Result<(), anyhow::Error>>(
&self,
mut f: F,
) -> Result<(), anyhow::Error> {
for c in self.changes.iter() {
for c in c.iter() {
match *c {
Atom::NewVertex(ref n) => {
for change in n
.up_context
.iter()
.chain(n.down_context.iter())
.map(|c| c.change)
.chain(std::iter::once(n.inode.change))
{
if let Some(change) = change {
if let Hash::None = change {
continue;
}
f(change)?
}
}
}
Atom::EdgeMap(ref e) => {
for edge in e.edges.iter() {
for change in &[
edge.from.change,
edge.to.change,
edge.introduced_by,
e.inode.change,
] {
if let Some(change) = *change {
if let Hash::None = change {
continue;
}
f(change)?
}
}
}
}
}
}
}
Ok(())
}
pub fn write<W: Write, C: ChangeStore, F: FnMut(&Local, Position<Option<Hash>>) -> String>(
&self,
changes: &C,
hash: Option<Hash>,
mut file_name: F,
write_header: bool,
mut w: W,
) -> Result<(), anyhow::Error> {
if let Some(h) = hash {
let mut hasher = Hasher::default();
hasher.update(&self.contents);
let hash = hasher.finish();
if hash != self.contents_hash {
return Err((Error::MissingContents {
hash: h.to_base32(),
})
.into());
}
}
if write_header {
w.write_all(toml::ser::to_string_pretty(&self.header)?.as_bytes())?;
w.write_all(b"\n")?;
}
let mut hashes = HashMap::new();
let mut i = 2;
let mut needs_newline = false;
if !self.dependencies.is_empty() {
w.write_all(Self::DEPS_LINE.as_bytes())?;
needs_newline = true;
for dep in self.dependencies.iter() {
hashes.insert(*dep, i);
writeln!(w, "[{}] {}", i, dep.to_base32())?;
i += 1;
}
}
self.write_all_deps(|change| {
if let Entry::Vacant(e) = hashes.entry(change) {
e.insert(i);
if !needs_newline {
w.write_all(Self::DEPS_LINE.as_bytes())?;
needs_newline = true;
}
writeln!(w, "[{}]+{}", i, change.to_base32())?;
i += 1;
}
Ok(())
})?;
if !self.extra_known.is_empty() {
needs_newline = true;
for dep in self.extra_known.iter() {
writeln!(w, "[*] {}", dep.to_base32())?;
i += 1;
}
}
if !self.changes.is_empty() {
if needs_newline {
w.write_all(b"\n")?
}
w.write_all(Self::CHANGES_LINE.as_bytes())?;
for (n, rec) in self.changes.iter().enumerate() {
write!(w, "\n{}. ", n + 1)?;
rec.write(changes, &mut file_name, &hashes, &self.contents, &mut w)?
}
}
Ok(())
}
}
impl Change {
pub fn read_and_deps<R: BufRead, T: TxnT>(
r: R,
updatables: &mut HashMap<usize, crate::InodeUpdate>,
txn: &T,
channel: &ChannelRef<T>,
) -> Result<Self, anyhow::Error> {
let (mut change, extra_dependencies) = Self::read_(r, updatables)?;
let (mut deps, extra) = dependencies(txn, channel, change.hashed.changes.iter());
deps.extend(extra_dependencies.into_iter());
change.hashed.dependencies = deps;
change.hashed.extra_known = extra;
Ok(change)
}
pub fn read<R: BufRead>(
r: R,
updatables: &mut HashMap<usize, crate::InodeUpdate>,
) -> Result<Self, anyhow::Error> {
Ok(Self::read_(r, updatables)?.0)
}
fn read_<R: BufRead>(
mut r: R,
updatables: &mut HashMap<usize, crate::InodeUpdate>,
) -> Result<(Self, HashSet<Hash>), anyhow::Error> {
use self::text_changes::*;
let mut section = Section::Header(String::new());
let mut change = Change {
offsets: Offsets::default(),
hashed: Hashed {
version: VERSION,
header: ChangeHeader {
authors: Vec::new(),
message: String::new(),
description: None,
timestamp: chrono::Utc::now(),
},
dependencies: Vec::new(),
extra_known: Vec::new(),
metadata: Vec::new(),
changes: Vec::new(),
contents_hash: Hasher::default().finish(),
},
unhashed: None,
contents: Vec::new(),
};
let conclude_section = |change: &mut Change,
section: Section,
contents: &mut Vec<u8>|
-> Result<(), anyhow::Error> {
match section {
Section::Header(ref s) => {
debug!("header = {:?}", s);
change.header = toml::de::from_str(&s)?;
Ok(())
}
Section::Deps => Ok(()),
Section::Changes {
mut changes,
current,
..
} => {
if has_newvertices(¤t) {
contents.push(0)
}
if let Some(c) = current {
debug!("next action = {:?}", c);
changes.push(c)
}
change.changes = changes;
Ok(())
}
}
};
let mut h = String::new();
let mut contents = Vec::new();
let mut deps = HashMap::new();
let mut extra_dependencies = HashSet::new();
while r.read_line(&mut h)? > 0 {
debug!("h = {:?}", h);
if h == Self::DEPS_LINE {
let section = std::mem::replace(&mut section, Section::Deps);
conclude_section(&mut change, section, &mut contents)?;
} else if h == Self::CHANGES_LINE {
let section = std::mem::replace(
&mut section,
Section::Changes {
changes: Vec::new(),
current: None,
offsets: HashMap::new(),
},
);
conclude_section(&mut change, section, &mut contents)?;
} else {
use regex::Regex;
lazy_static! {
static ref DEPS: Regex = Regex::new(r#"\[(\d*|\*)\](\+| ) *(\S*)"#).unwrap();
static ref KNOWN: Regex = Regex::new(r#"(\S*)"#).unwrap();
}
match section {
Section::Header(ref mut s) => s.push_str(&h),
Section::Deps => {
if let Some(d) = DEPS.captures(&h) {
let hash = Hash::from_base32(d[3].as_bytes()).unwrap();
if let Ok(n) = d[1].parse() {
if &d[2] == " " {
change.hashed.dependencies.push(hash);
}
deps.insert(n, hash);
} else if &d[1] == "*" {
change.hashed.extra_known.push(hash);
} else {
extra_dependencies.insert(hash);
}
}
}
Section::Changes {
ref mut current,
ref mut changes,
ref mut offsets,
} => {
if let Some(next) =
Record::read(updatables, current, &mut contents, &deps, offsets, &h)?
{
debug!("next action = {:?}", next);
changes.push(next)
}
}
}
}
h.clear();
}
conclude_section(&mut change, section, &mut contents)?;
change.contents = contents;
change.contents_hash = {
let mut hasher = Hasher::default();
hasher.update(&change.contents);
hasher.finish()
};
Ok((change, extra_dependencies))
}
}
#[cfg(feature = "text-changes")]
impl Record<Option<Hash>, Local> {
fn write<
W: std::io::Write,
C: ChangeStore,
F: FnMut(&Local, Position<Option<Hash>>) -> String,
>(
&self,
changes: &C,
mut file_name: F,
hashes: &HashMap<Hash, usize>,
change_contents: &[u8],
mut w: W,
) -> Result<(), anyhow::Error> {
use self::text_changes::*;
match self {
Record::FileMove { del, add, path } => match add {
Atom::NewVertex(ref add) => {
let name = std::str::from_utf8(
&change_contents[add.start.0 as usize + 2..add.end.0 as usize],
)
.unwrap();
let perms = crate::pristine::InodeMetadata::from_basename(
&change_contents[add.start.0 as usize..add.start.0 as usize + 2],
);
write!(w, "Moved: {:?} {:?} {:o} ", path, name, perms.0)?;
write_pos(&mut w, hashes, del.inode())?;
write!(w, "\n")?;
write_atom(&mut w, hashes, &del)?;
write!(w, "up")?;
for c in add.up_context.iter() {
write!(w, " ")?;
write_pos(&mut w, hashes, *c)?
}
write!(w, ", down")?;
for c in add.down_context.iter() {
write!(w, " ")?;
write_pos(&mut w, hashes, *c)?
}
w.write_all(b"\n")?;
}
Atom::EdgeMap(_) => {
write!(w, "Moved: {:?} ", path)?;
write_pos(&mut w, hashes, del.inode())?;
write!(w, "\n")?;
write_atom(&mut w, hashes, &add)?;
write_atom(&mut w, hashes, &del)?;
}
},
Record::FileDel {
del,
contents,
path,
} => {
write!(w, "File deletion: {:?} ", path)?;
write_pos(&mut w, hashes, del.inode())?;
write!(w, "\n")?;
write_atom(&mut w, hashes, &del)?;
if let Some(ref contents) = contents {
write_atom(&mut w, hashes, &contents)?;
write!(w, "\n")?;
print_change_contents(&mut w, changes, contents, change_contents)?;
} else {
write!(w, "\n")?;
}
}
Record::FileUndel {
undel,
contents,
path,
} => {
write!(w, "File un-deletion: {:?} ", path)?;
write_pos(&mut w, hashes, undel.inode())?;
write!(w, "\n")?;
write_atom(&mut w, hashes, &undel)?;
if let Some(ref contents) = contents {
write_atom(&mut w, hashes, &contents)?;
print_change_contents(&mut w, changes, contents, change_contents)?;
} else {
write!(w, "\n")?;
}
}
Record::FileAdd {
add_name,
contents,
path,
..
} => {
if let Atom::NewVertex(ref n) = add_name {
let name = std::str::from_utf8(
&change_contents[n.start.0 as usize + 2..n.end.0 as usize],
)
.unwrap();
let perms = crate::pristine::InodeMetadata::from_basename(
&change_contents[n.start.0 as usize..n.start.0 as usize + 2],
);
let parent = if let Some(p) = crate::path::parent(&path) {
if p.is_empty() {
"/"
} else {
p
}
} else {
"/"
};
write!(
w,
"File addition: {:?} in {:?} {:o}\n up",
name, parent, perms.0
)?;
assert!(n.down_context.is_empty());
for c in n.up_context.iter() {
write!(w, " ")?;
write_pos(&mut w, hashes, *c)?
}
writeln!(w, ", new {}:{}", n.start.0, n.end.0)?;
}
if let Some(Atom::NewVertex(ref n)) = contents {
let c = &change_contents[n.start.0 as usize..n.end.0 as usize];
print_contents(&mut w, "+", c)?;
if !c.ends_with(b"\n") {
writeln!(w, "\\")?
}
}
}
Record::Edit { change, local } => {
write!(w, "Edit in {} ", file_name(&local, change.inode()))?;
write_pos(&mut w, hashes, change.inode())?;
writeln!(w, "")?;
write_atom(&mut w, hashes, &change)?;
print_change_contents(&mut w, changes, change, change_contents)?;
}
Record::Replacement {
change,
replacement,
local,
} => {
write!(w, "Replacement in {} ", file_name(&local, change.inode()))?;
write_pos(&mut w, hashes, change.inode())?;
writeln!(w, "")?;
write_atom(&mut w, hashes, &change)?;
write_atom(&mut w, hashes, &replacement)?;
print_change_contents(&mut w, changes, change, change_contents)?;
print_change_contents(&mut w, changes, replacement, change_contents)?;
}
Record::SolveNameConflict { name, path } => {
write!(w, "Solving a name conflict in {:?} ", path)?;
write_pos(&mut w, hashes, name.inode())?;
write!(w, ": ")?;
write_deleted_names(&mut w, changes, name)?;
write!(w, "\n")?;
write_atom(&mut w, hashes, &name)?;
}
Record::UnsolveNameConflict { name, path } => {
write!(w, "Un-solving a name conflict in {:?} ", path)?;
write_pos(&mut w, hashes, name.inode())?;
write!(w, ": ")?;
write_deleted_names(&mut w, changes, name)?;
write!(w, "\n")?;
write_atom(&mut w, hashes, &name)?;
}
Record::SolveOrderConflict { change, local } => {
write!(
w,
"Solving an order conflict in {} ",
file_name(&local, change.inode())
)?;
write_pos(&mut w, hashes, change.inode())?;
writeln!(w, "")?;
write_atom(&mut w, hashes, &change)?;
print_change_contents(&mut w, changes, change, change_contents)?;
}
Record::UnsolveOrderConflict { change, local } => {
write!(
w,
"Un-solving an order conflict in {} ",
file_name(&local, change.inode())
)?;
write_pos(&mut w, hashes, change.inode())?;
writeln!(w, "")?;
write_atom(&mut w, hashes, &change)?;
print_change_contents(&mut w, changes, change, change_contents)?;
}
Record::ResurrectZombies { change, local } => {
write!(
w,
"Resurrecting zombie lines in {:?}:{} ",
local.path, local.line
)?;
write_pos(&mut w, hashes, change.inode())?;
writeln!(w, "")?;
write_atom(&mut w, hashes, &change)?;
print_change_contents(&mut w, changes, change, change_contents)?;
}
}
Ok(())
}
}
#[cfg(feature = "text-changes")]
impl Record<Option<Hash>, Local> {
fn read(
updatables: &mut HashMap<usize, crate::InodeUpdate>,
current: &mut Option<Self>,
mut contents_: &mut Vec<u8>,
changes: &HashMap<usize, Hash>,
offsets: &mut HashMap<u64, ChangePosition>,
h: &str,
) -> Result<Option<Self>, Error> {
use self::text_changes::*;
use regex::Regex;
lazy_static! {
static ref FILE_ADDITION: Regex =
Regex::new(r#"(?P<n>\d+)\. File addition: "(?P<name>[^"]*)" in "(?P<parent>[^"]*)" (?P<perm>\d+)"#).unwrap();
static ref EDIT: Regex =
Regex::new(r#"(\d+)\. Edit in ([^:]+):(\d+) (\d+\.\d+)"#).unwrap();
static ref REPLACEMENT: Regex =
Regex::new(r#"(\d+)\. Replacement in ([^:]+):(\d+) (\d+\.\d+)"#).unwrap();
static ref FILE_DELETION: Regex =
Regex::new(r#"(\d+)\. File deletion: "([^"]*)" (\d+\.\d+)"#).unwrap();
static ref FILE_UNDELETION: Regex =
Regex::new(r#"(\d+)\. File un-deletion: "([^"]*)" (\d+\.\d+)"#).unwrap();
static ref MOVE: Regex =
Regex::new(r#"(\d+)\. Moved: "(?P<former>[^"]*)" "(?P<new>[^"]*)" (?P<perm>\d+) (?P<inode>.*)"#).unwrap();
static ref MOVE_: Regex = Regex::new(r#"(\d+)\. Moved: "([^"]*)" (.*)"#).unwrap();
static ref NAME_CONFLICT: Regex = Regex::new(
r#"(\d+)\. ((Solving)|(Un-solving)) a name conflict in "([^"]*)" (.*): .*"#
)
.unwrap();
static ref ORDER_CONFLICT: Regex = Regex::new(
r#"(\d+)\. ((Solving)|(Un-solving)) an order conflict in (.*):(\d+) (\d+\.\d+)"#
)
.unwrap();
static ref ZOMBIE: Regex =
Regex::new(r#"(\d+)\. Resurrecting zombie lines in (?P<path>"[^"]+"):(?P<line>\d+) (?P<inode>\d+\.\d+)"#)
.unwrap();
static ref CONTEXT: Regex = Regex::new(
r#"up ((\d+\.\d+ )*\d+\.\d+)(, new (\d+):(\d+))?(, down ((\d+\.\d+ )*\d+\.\d+))?"#
)
.unwrap();
}
if let Some(cap) = FILE_ADDITION.captures(h) {
if has_newvertices(current) {
contents_.push(0)
}
let mut add_name = default_newvertex();
add_name.start = ChangePosition(contents_.len() as u64);
add_name.flag = EdgeFlags::FOLDER | EdgeFlags::BLOCK;
let name = &cap.name("name").unwrap().as_str();
let path = {
let parent = cap.name("parent").unwrap().as_str();
(if parent == "/" {
String::new()
} else {
parent.to_string()
}) + name
};
let meta = cap
.name("perm")
.unwrap()
.as_str()
.chars()
.fold(0, |x, c| x * 8 + (c as u16 - b'0' as u16));
let meta = InodeMetadata(meta);
meta.write(&mut contents_).unwrap();
contents_.extend(name.as_bytes());
add_name.end = ChangePosition(contents_.len() as u64);
let mut add_inode = default_newvertex();
add_inode.flag = EdgeFlags::FOLDER | EdgeFlags::BLOCK;
add_inode.up_context.push(Position {
change: None,
pos: ChangePosition(contents_.len() as u64),
});
contents_.push(0);
add_inode.start = ChangePosition(contents_.len() as u64);
add_inode.end = ChangePosition(contents_.len() as u64);
contents_.push(0);
let n = cap.name("n").unwrap().as_str().parse().unwrap();
if let Entry::Occupied(mut e) = updatables.entry(n) {
if let crate::InodeUpdate::Add { ref mut pos, .. } = e.get_mut() {
*pos = add_inode.start
}
}
Ok(std::mem::replace(
current,
Some(Record::FileAdd {
add_name: Atom::NewVertex(add_name),
add_inode: Atom::NewVertex(add_inode),
contents: None,
path,
}),
))
} else if let Some(cap) = EDIT.captures(h) {
if has_newvertices(current) {
contents_.push(0)
}
let mut v = default_newvertex();
v.inode = parse_pos(changes, &cap[4]);
Ok(std::mem::replace(
current,
Some(Record::Edit {
change: Atom::NewVertex(v),
local: Local {
path: cap[2].to_string(),
line: cap[3].parse().unwrap(),
},
}),
))
} else if let Some(cap) = REPLACEMENT.captures(h) {
if has_newvertices(current) {
contents_.push(0)
}
let mut v = default_newvertex();
v.inode = parse_pos(changes, &cap[4]);
Ok(std::mem::replace(
current,
Some(Record::Replacement {
change: Atom::NewVertex(v.clone()),
replacement: Atom::NewVertex(v),
local: Local {
path: cap[2].to_string(),
line: cap[3].parse().unwrap(),
},
}),
))
} else if let Some(cap) = FILE_DELETION.captures(h) {
if has_newvertices(current) {
contents_.push(0)
}
let mut del = default_edgemap();
del.inode = parse_pos(changes, &cap[3]);
Ok(std::mem::replace(
current,
Some(Record::FileDel {
del: Atom::EdgeMap(del),
contents: None,
path: cap[2].to_string(),
}),
))
} else if let Some(cap) = FILE_UNDELETION.captures(h) {
if has_newvertices(current) {
contents_.push(0)
}
let mut undel = default_edgemap();
undel.inode = parse_pos(changes, &cap[3]);
Ok(std::mem::replace(
current,
Some(Record::FileUndel {
undel: Atom::EdgeMap(undel),
contents: None,
path: cap[2].to_string(),
}),
))
} else if let Some(cap) = NAME_CONFLICT.captures(h) {
if has_newvertices(current) {
contents_.push(0)
}
let mut name = default_edgemap();
debug!("cap = {:?}", cap);
name.inode = parse_pos(changes, &cap[6]);
Ok(std::mem::replace(
current,
if &cap[2] == "Solving" {
Some(Record::SolveNameConflict {
name: Atom::EdgeMap(name),
path: cap[5].to_string(),
})
} else {
Some(Record::UnsolveNameConflict {
name: Atom::EdgeMap(name),
path: cap[5].to_string(),
})
},
))
} else if let Some(cap) = MOVE.captures(h) {
if has_newvertices(current) {
contents_.push(0)
}
let mut add = default_newvertex();
add.start = ChangePosition(contents_.len() as u64);
add.flag = EdgeFlags::FOLDER | EdgeFlags::BLOCK;
let name = cap.name("new").unwrap().as_str();
let meta = cap
.name("perm")
.unwrap()
.as_str()
.chars()
.fold(0, |x, c| x * 8 + (c as u16 - b'0' as u16));
let meta = InodeMetadata(meta);
meta.write(&mut contents_).unwrap();
contents_.extend(name.as_bytes());
add.end = ChangePosition(contents_.len() as u64);
let mut del = default_edgemap();
del.inode = parse_pos(changes, cap.name("inode").unwrap().as_str());
Ok(std::mem::replace(
current,
Some(Record::FileMove {
del: Atom::EdgeMap(del),
add: Atom::NewVertex(add),
path: cap[2].to_string(),
}),
))
} else if let Some(cap) = MOVE_.captures(h) {
if has_newvertices(current) {
contents_.push(0)
}
let mut add = default_edgemap();
let mut del = default_edgemap();
add.inode = parse_pos(changes, &cap[3]);
del.inode = add.inode;
Ok(std::mem::replace(
current,
Some(Record::FileMove {
del: Atom::EdgeMap(del),
add: Atom::EdgeMap(add),
path: cap[2].to_string(),
}),
))
} else if let Some(cap) = ORDER_CONFLICT.captures(h) {
if has_newvertices(current) {
contents_.push(0)
}
Ok(std::mem::replace(
current,
Some(if &cap[2] == "Solving" {
let mut v = default_newvertex();
v.inode = parse_pos(changes, &cap[7]);
Record::SolveOrderConflict {
change: Atom::NewVertex(v),
local: Local {
path: cap[5].to_string(),
line: cap[6].parse().unwrap(),
},
}
} else {
let mut v = default_edgemap();
v.inode = parse_pos(changes, &cap[7]);
Record::UnsolveOrderConflict {
change: Atom::EdgeMap(v),
local: Local {
path: cap[5].to_string(),
line: cap[6].parse().unwrap(),
},
}
}),
))
} else if let Some(cap) = ZOMBIE.captures(h) {
if has_newvertices(current) {
contents_.push(0)
}
let mut v = default_edgemap();
v.inode = parse_pos(changes, &cap.name("inode").unwrap().as_str());
Ok(std::mem::replace(
current,
Some(Record::ResurrectZombies {
change: Atom::EdgeMap(v),
local: Local {
path: cap.name("path").unwrap().as_str().parse().unwrap(),
line: cap.name("line").unwrap().as_str().parse().unwrap(),
},
}),
))
} else {
match current {
Some(Record::FileAdd {
ref mut contents,
ref mut add_name,
..
}) => {
if h.starts_with("+") {
if contents.is_none() {
let mut v = default_newvertex();
let inode = Position {
change: None,
pos: ChangePosition(contents_.len() as u64 - 1),
};
v.up_context.push(inode);
v.inode = inode;
v.start = ChangePosition(contents_.len() as u64);
*contents = Some(Atom::NewVertex(v));
}
if let Some(Atom::NewVertex(ref mut contents)) = contents {
if h.starts_with("+") {
text_changes::parse_line_add(h, contents, contents_)
}
}
} else if h.starts_with("\\") {
if let Some(Atom::NewVertex(ref mut contents)) = contents {
if contents_[contents.end.0 as usize - 1] == b'\n' {
assert_eq!(contents.end.0 as usize, contents_.len());
contents_.pop();
contents.end.0 -= 1;
}
}
} else if let Some(cap) = CONTEXT.captures(h) {
if let Atom::NewVertex(ref mut name) = add_name {
name.up_context = parse_pos_vec(changes, offsets, &cap[1])?;
if let (Some(new_start), Some(new_end)) = (cap.get(4), cap.get(5)) {
offsets.insert(new_start.as_str().parse().unwrap(), name.start);
offsets.insert(new_end.as_str().parse().unwrap(), name.end);
offsets.insert(
new_end.as_str().parse::<u64>().unwrap() + 1,
name.end + 1,
);
}
}
}
Ok(None)
}
Some(Record::FileDel {
ref mut del,
ref mut contents,
..
}) => {
if let Some(edges) = parse_edges(changes, h) {
if let Atom::EdgeMap(ref mut e) = del {
if edges[0].flag.contains(EdgeFlags::FOLDER) {
*e = EdgeMap {
inode: e.inode,
edges,
}
} else {
*contents = Some(Atom::EdgeMap(EdgeMap {
inode: e.inode,
edges,
}))
}
}
}
Ok(None)
}
Some(Record::FileUndel {
ref mut undel,
ref mut contents,
..
}) => {
if let Some(edges) = parse_edges(changes, h) {
if let Atom::EdgeMap(ref mut e) = undel {
if edges[0].flag.contains(EdgeFlags::FOLDER) {
*e = EdgeMap {
inode: e.inode,
edges,
}
} else {
*contents = Some(Atom::EdgeMap(EdgeMap {
inode: e.inode,
edges,
}))
}
}
}
Ok(None)
}
Some(Record::FileMove {
ref mut del,
ref mut add,
..
}) => {
if let Some(edges) = parse_edges(changes, h) {
if edges[0].flag.contains(EdgeFlags::DELETED) {
*del = Atom::EdgeMap(EdgeMap {
inode: del.inode(),
edges,
});
return Ok(None);
} else if let Atom::EdgeMap(ref mut add) = add {
if add.edges.is_empty() {
*add = EdgeMap {
inode: add.inode,
edges,
};
return Ok(None);
}
}
} else if let Some(cap) = CONTEXT.captures(h) {
if let Atom::NewVertex(ref mut c) = add {
debug!("cap = {:?}", cap);
c.up_context = parse_pos_vec(changes, offsets, &cap[1])?;
if let Some(cap) = cap.get(7) {
c.down_context = parse_pos_vec(changes, offsets, cap.as_str())?;
}
}
}
Ok(None)
}
Some(Record::Edit { ref mut change, .. }) => {
debug!("edit {:?}", h);
if h.starts_with("+ ") {
if let Atom::NewVertex(ref mut change) = change {
if change.start == change.end {
change.start = ChangePosition(contents_.len() as u64);
}
text_changes::parse_line_add(h, change, contents_)
}
} else if h.starts_with("\\") {
if let Atom::NewVertex(ref mut change) = change {
if contents_[change.end.0 as usize - 1] == b'\n' {
assert_eq!(change.end.0 as usize, contents_.len());
contents_.pop();
change.end.0 -= 1;
}
}
} else if let Some(cap) = CONTEXT.captures(h) {
if let Atom::NewVertex(ref mut c) = change {
debug!("cap = {:?}", cap);
c.up_context = parse_pos_vec(changes, offsets, &cap[1])?;
if let Some(cap) = cap.get(7) {
c.down_context = parse_pos_vec(changes, offsets, cap.as_str())?;
}
}
} else if let Some(edges) = parse_edges(changes, h) {
*change = Atom::EdgeMap(EdgeMap {
inode: change.inode(),
edges,
});
}
Ok(None)
}
Some(Record::Replacement {
ref mut change,
ref mut replacement,
..
}) => {
if h.starts_with("+ ") {
if let Atom::NewVertex(ref mut repl) = replacement {
if repl.start == repl.end {
repl.start = ChangePosition(contents_.len() as u64);
}
text_changes::parse_line_add(h, repl, contents_)
}
} else if h.starts_with("\\") {
if let Atom::NewVertex(ref mut repl) = replacement {
if contents_[repl.end.0 as usize - 1] == b'\n' {
assert_eq!(repl.end.0 as usize, contents_.len());
contents_.pop();
repl.end.0 -= 1;
}
}
} else if let Some(cap) = CONTEXT.captures(h) {
debug!("cap = {:?}", cap);
if let Atom::NewVertex(ref mut repl) = replacement {
repl.up_context = parse_pos_vec(changes, offsets, &cap[1])?;
if let Some(cap) = cap.get(7) {
repl.down_context = parse_pos_vec(changes, offsets, cap.as_str())?;
}
}
} else if let Some(edges) = parse_edges(changes, h) {
*change = Atom::EdgeMap(EdgeMap {
inode: change.inode(),
edges,
});
}
Ok(None)
}
Some(Record::SolveNameConflict { ref mut name, .. })
| Some(Record::UnsolveNameConflict { ref mut name, .. }) => {
if let Some(edges) = parse_edges(changes, h) {
*name = Atom::EdgeMap(EdgeMap {
edges,
inode: name.inode(),
})
}
Ok(None)
}
Some(Record::SolveOrderConflict { ref mut change, .. }) => {
if h.starts_with("+ ") {
if let Atom::NewVertex(ref mut change) = change {
if change.start == change.end {
change.start = ChangePosition(contents_.len() as u64);
}
text_changes::parse_line_add(h, change, contents_)
}
} else if let Some(cap) = CONTEXT.captures(h) {
debug!("cap = {:?}", cap);
if let Atom::NewVertex(ref mut change) = change {
change.up_context = parse_pos_vec(changes, offsets, &cap[1])?;
if let Some(cap) = cap.get(7) {
change.down_context =
parse_pos_vec(changes, offsets, cap.as_str())?;
}
if let (Some(new_start), Some(new_end)) = (cap.get(4), cap.get(5)) {
let new_start = new_start.as_str().parse::<u64>().unwrap();
let new_end = new_end.as_str().parse::<u64>().unwrap();
change.start = ChangePosition(contents_.len() as u64);
change.end =
ChangePosition(contents_.len() as u64 + new_end - new_start);
offsets.insert(new_end, change.end);
}
}
}
Ok(None)
}
Some(Record::UnsolveOrderConflict { ref mut change, .. }) => {
if let Some(edges) = parse_edges(changes, h) {
if let Atom::EdgeMap(ref mut change) = change {
change.edges = edges
}
}
Ok(None)
}
Some(Record::ResurrectZombies { ref mut change, .. }) => {
if let Some(edges) = parse_edges(changes, h) {
if let Atom::EdgeMap(ref mut change) = change {
change.edges = edges
}
}
Ok(None)
}
None => {
debug!("current = {:#?}", current);
debug!("h = {:?}", h);
Ok(None)
}
}
}
}
}
#[cfg(feature = "text-changes")]
mod text_changes {
use super::*;
lazy_static! {
static ref POS: regex::Regex = regex::Regex::new(r#"(\d+)\.(\d+)"#).unwrap();
static ref EDGE: regex::Regex =
regex::Regex::new(r#"\s*(?P<prev>[BFD]*):(?P<flag>[BFD]*)\s+(?P<up_c>\d+)\.(?P<up_l>\d+)\s*->\s*(?P<c>\d+)\.(?P<l0>\d+):(?P<l1>\d+)/(?P<intro>\d+)\s*"#).unwrap();
}
pub fn default_newvertex() -> NewVertex<Option<Hash>> {
NewVertex {
start: ChangePosition(0),
end: ChangePosition(0),
flag: EdgeFlags::empty(),
up_context: Vec::new(),
down_context: Vec::new(),
inode: Position {
change: Some(Hash::None),
pos: ChangePosition(0),
},
}
}
pub fn default_edgemap() -> EdgeMap<Option<Hash>> {
EdgeMap {
edges: Vec::new(),
inode: Position {
change: Some(Hash::None),
pos: ChangePosition(0),
},
}
}
pub fn has_newvertices<L>(current: &Option<Record<Option<Hash>, L>>) -> bool {
match current {
Some(Record::FileAdd { contents: None, .. }) | None => false,
Some(rec) => rec.iter().any(|e| {
if let Atom::NewVertex(_) = e {
true
} else {
false
}
}),
}
}
pub fn parse_pos_vec(
changes: &HashMap<usize, Hash>,
offsets: &HashMap<u64, ChangePosition>,
s: &str,
) -> Result<Vec<Position<Option<Hash>>>, Error> {
let mut v = Vec::new();
for pos in POS.captures_iter(s) {
let change: usize = (&pos[1]).parse().unwrap();
let pos: u64 = (&pos[2]).parse().unwrap();
let pos = if change == 0 {
if let Some(&pos) = offsets.get(&pos) {
pos
} else {
debug!("inconsistent change: {:?} {:?}", s, offsets);
return Err(Error::InconsistentChange);
}
} else {
ChangePosition(pos)
};
v.push(Position {
change: change_ref(changes, change),
pos,
})
}
Ok(v)
}
fn change_ref(changes: &HashMap<usize, Hash>, change: usize) -> Option<Hash> {
debug!("change_ref {:?} {:?}", changes, change);
if change == 0 {
None
} else if change == 1 {
Some(Hash::None)
} else {
Some(*changes.get(&change).unwrap())
}
}
pub fn parse_pos(changes: &HashMap<usize, Hash>, s: &str) -> Position<Option<Hash>> {
let pos = POS.captures(s).unwrap();
let change: usize = (&pos[1]).parse().unwrap();
let pos: u64 = (&pos[2]).parse().unwrap();
Position {
change: change_ref(changes, change),
pos: ChangePosition(pos),
}
}
pub fn parse_edges(
changes: &HashMap<usize, Hash>,
s: &str,
) -> Option<Vec<NewEdge<Option<Hash>>>> {
debug!("parse_edges {:?}", s);
let mut result = Vec::new();
for edge in s.split(",") {
debug!("parse edge {:?}", edge);
if let Some(cap) = EDGE.captures(edge) {
let previous = read_flag(cap.name("prev").unwrap().as_str());
let flag = read_flag(cap.name("flag").unwrap().as_str());
let change0: usize = cap.name("up_c").unwrap().as_str().parse().unwrap();
let pos0: u64 = cap.name("up_l").unwrap().as_str().parse().unwrap();
let change1: usize = cap.name("c").unwrap().as_str().parse().unwrap();
let start1: u64 = cap.name("l0").unwrap().as_str().parse().unwrap();
let end1: u64 = cap.name("l1").unwrap().as_str().parse().unwrap();
let introduced_by: usize = cap.name("intro").unwrap().as_str().parse().unwrap();
result.push(NewEdge {
previous,
flag,
from: Position {
change: change_ref(changes, change0),
pos: ChangePosition(pos0),
},
to: Vertex {
change: change_ref(changes, change1),
start: ChangePosition(start1),
end: ChangePosition(end1),
},
introduced_by: change_ref(changes, introduced_by),
})
} else {
debug!("not parsed");
return None;
}
}
Some(result)
}
pub fn parse_line_add(h: &str, change: &mut NewVertex<Option<Hash>>, contents_: &mut Vec<u8>) {
let h = h.as_bytes();
debug!("parse_line_add {:?} {:?}", change.end, change.start);
debug!("parse_line_add {:?}", h);
if h.len() > 2 {
let h = &h[2..h.len()];
contents_.extend(h);
} else if h.len() > 1 {
contents_.push(b'\n');
}
debug!("contents_.len() = {:?}", contents_.len());
trace!("contents_ = {:?}", contents_);
change.end = ChangePosition(contents_.len() as u64);
}
pub fn print_contents<W: std::io::Write>(
w: &mut W,
pref: &str,
contents: &[u8],
) -> Result<(), anyhow::Error> {
if let Ok(contents) = std::str::from_utf8(&contents) {
for l in contents.lines() {
writeln!(w, "{} {}", pref, l)?;
}
} else {
writeln!(w, "{}b{}", pref, data_encoding::BASE64.encode(contents))?
}
Ok(())
}
pub fn print_change_contents<W: std::io::Write, C: ChangeStore>(
w: &mut W,
changes: &C,
change: &Atom<Option<Hash>>,
change_contents: &[u8],
) -> Result<(), anyhow::Error> {
match change {
Atom::NewVertex(ref n) => {
let c = &change_contents[n.start.0 as usize..n.end.0 as usize];
print_contents(w, "+", c)?;
if !c.ends_with(b"\n") {
writeln!(w, "\\")?
}
Ok(())
}
Atom::EdgeMap(ref n) if n.edges[0].flag.contains(EdgeFlags::DELETED) => {
let mut buf = Vec::new();
let mut current = None;
for e in n.edges.iter() {
if Some(e.to) == current {
continue;
}
buf.clear();
changes.get_contents_ext(e.to, &mut buf)?;
print_contents(w, "-", &buf[..])?;
current = Some(e.to)
}
Ok(())
}
_ => Ok(()),
}
}
pub fn write_deleted_names<W: std::io::Write, C: ChangeStore>(
w: &mut W,
changes: &C,
del: &Atom<Option<Hash>>,
) -> Result<(), anyhow::Error> {
if let Atom::EdgeMap(ref e) = del {
let mut buf = Vec::new();
let mut is_first = true;
for d in e.edges.iter() {
buf.clear();
changes.get_contents_ext(d.to, &mut buf)?;
if !buf.is_empty() {
let name = std::str::from_utf8(buf.split_at(2).1).unwrap();
write!(w, "{}{:?}", if is_first { "" } else { ", " }, name)?;
is_first = false;
}
}
}
Ok(())
}
pub fn write_flag<W: std::io::Write>(mut w: W, flag: EdgeFlags) -> Result<(), anyhow::Error> {
if flag.contains(EdgeFlags::BLOCK) {
w.write_all(b"B")?;
}
if flag.contains(EdgeFlags::FOLDER) {
w.write_all(b"F")?;
}
if flag.contains(EdgeFlags::DELETED) {
w.write_all(b"D")?;
}
assert!(!flag.contains(EdgeFlags::PARENT));
assert!(!flag.contains(EdgeFlags::PSEUDO));
Ok(())
}
pub fn read_flag(s: &str) -> EdgeFlags {
let mut f = EdgeFlags::empty();
for i in s.chars() {
match i {
'B' => f |= EdgeFlags::BLOCK,
'F' => f |= EdgeFlags::FOLDER,
'D' => f |= EdgeFlags::DELETED,
c => panic!("read_flag: {:?}", c),
}
}
f
}
pub fn write_pos<W: std::io::Write>(
mut w: W,
hashes: &HashMap<Hash, usize>,
pos: Position<Option<Hash>>,
) -> Result<(), anyhow::Error> {
let change = if let Some(Hash::None) = pos.change {
1
} else if let Some(ref c) = pos.change {
*hashes.get(c).unwrap()
} else {
0
};
write!(w, "{}.{}", change, pos.pos.0)?;
Ok(())
}
pub fn write_atom<W: std::io::Write>(
w: &mut W,
hashes: &HashMap<Hash, usize>,
atom: &Atom<Option<Hash>>,
) -> Result<(), anyhow::Error> {
match atom {
Atom::NewVertex(ref n) => write_newvertex(w, hashes, n),
Atom::EdgeMap(ref n) => write_edgemap(w, hashes, n),
}
}
pub fn write_newvertex<W: std::io::Write>(
mut w: W,
hashes: &HashMap<Hash, usize>,
n: &NewVertex<Option<Hash>>,
) -> Result<(), anyhow::Error> {
write!(w, " up")?;
for c in n.up_context.iter() {
write!(w, " ")?;
write_pos(&mut w, hashes, *c)?
}
write!(w, ", new {}:{}", n.start.0, n.end.0)?;
if !n.down_context.is_empty() {
write!(w, ", down")?;
for c in n.down_context.iter() {
write!(w, " ")?;
write_pos(&mut w, hashes, *c)?
}
}
w.write_all(b"\n")?;
Ok(())
}
pub fn write_edgemap<W: std::io::Write>(
mut w: W,
hashes: &HashMap<Hash, usize>,
n: &EdgeMap<Option<Hash>>,
) -> Result<(), anyhow::Error> {
let mut is_first = true;
for c in n.edges.iter() {
if !is_first {
write!(w, ", ")?;
}
is_first = false;
write_flag(&mut w, c.previous)?;
write!(w, ":")?;
write_flag(&mut w, c.flag)?;
write!(w, " ")?;
write_pos(&mut w, hashes, c.from)?;
write!(w, " -> ")?;
write_pos(&mut w, hashes, c.to.start_pos())?;
let h = if let Some(h) = hashes.get(c.introduced_by.as_ref().unwrap()) {
h
} else {
panic!("introduced_by = {:?}, not found", c.introduced_by);
};
write!(w, ":{}/{}", c.to.end.0, h)?;
}
write!(w, "\n")?;
Ok(())
}
#[cfg(feature = "text-changes")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Section {
Header(String),
Deps,
Changes {
changes: Vec<Record<Option<Hash>, Local>>,
current: Option<Record<Option<Hash>, Local>>,
offsets: HashMap<u64, ChangePosition>,
},
}
}
#[cfg(feature = "zstd")]
pub struct ChangeFile<'a> {
s: Option<zstd_seekable::Seekable<'a, OffFile>>,
hashed: Hashed<Local>,
hash: Hash,
unhashed: Option<toml::Value>,
}
struct OffFile {
f: std::fs::File,
start: u64,
}
unsafe impl Send for OffFile {}
impl std::io::Read for OffFile {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
self.f.read(buf)
}
}
impl std::io::Seek for OffFile {
fn seek(&mut self, from: std::io::SeekFrom) -> Result<u64, std::io::Error> {
use std::io::SeekFrom;
let from = match from {
SeekFrom::Start(s) => SeekFrom::Start(s + self.start),
c => c,
};
self.f.seek(from)
}
}
#[cfg(feature = "zstd")]
impl<'a> ChangeFile<'a> {
pub fn open(hash: Hash, path: &str) -> Result<Self, anyhow::Error> {
use std::io::Read;
let mut r = std::fs::File::open(path)?;
let mut buf = Vec::new();
buf.resize(Change::OFFSETS_SIZE as usize, 0);
r.read_exact(&mut buf)?;
let offsets: Offsets = bincode::deserialize(&buf)?;
if offsets.version != VERSION {
return Err(Error::VersionMismatch.into());
}
buf.clear();
buf.resize((offsets.unhashed_off - Change::OFFSETS_SIZE) as usize, 0);
r.read_exact(&mut buf)?;
let mut buf2 = vec![0u8; offsets.hashed_len as usize];
let hashed: Hashed<Local> = {
let mut s = zstd_seekable::Seekable::init_buf(&buf)?;
s.decompress(&mut buf2, 0)?;
bincode::deserialize(&buf2)?
};
buf.resize((offsets.contents_off - offsets.unhashed_off) as usize, 0);
let unhashed = if buf.is_empty() {
None
} else {
r.read_exact(&mut buf)?;
let mut s = zstd_seekable::Seekable::init_buf(&buf)?;
buf2.resize(offsets.unhashed_len as usize, 0);
s.decompress(&mut buf2, 0)?;
Some(toml::de::from_slice(&buf2)?)
};
let m = r.metadata()?;
let s = if offsets.contents_off >= m.len() {
None
} else {
Some(zstd_seekable::Seekable::init(Box::new(OffFile {
f: r,
start: offsets.contents_off,
}))?)
};
Ok(ChangeFile {
s,
hashed,
hash,
unhashed,
})
}
pub fn has_contents(&self) -> bool {
self.s.is_some()
}
pub fn read_contents(&mut self, offset: u64, buf: &mut [u8]) -> Result<usize, anyhow::Error> {
debug!("read_contents {:?} {:?}", offset, buf.len());
if let Some(ref mut s) = self.s {
Ok(s.decompress(buf, offset)?)
} else {
Err((Error::MissingContents {
hash: self.hash.to_base32(),
})
.into())
}
}
pub fn hashed(&self) -> &Hashed<Local> {
&self.hashed
}
pub fn unhashed(&self) -> &Option<toml::Value> {
&self.unhashed
}
}