ouisync/directory/
content.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//! Directory content

use super::entry_data::EntryData;
use crate::{
    blob::BlobId,
    error::{Error, Result},
    protocol::Bump,
    version_vector::VersionVector,
};
use serde::Deserialize;
use std::{
    cmp::Ordering,
    collections::{
        btree_map::{self, Entry},
        BTreeMap,
    },
};

/// Version of the Directory serialization format.
pub const VERSION: u64 = 2;

#[derive(Clone, Debug)]
pub(super) struct Content {
    entries: v2::Entries,
}

impl Content {
    pub fn empty() -> Self {
        Self {
            entries: BTreeMap::new(),
        }
    }

    pub fn deserialize(mut input: &[u8]) -> Result<Self> {
        let version = vint64::decode(&mut input).map_err(|_| Error::MalformedDirectory)?;
        let entries = match version {
            VERSION => deserialize_entries(input),
            1 => Ok(v2::from_v1(deserialize_entries(input)?)),
            0 => Ok(v2::from_v1(v1::from_v0(deserialize_entries(input)?))),
            _ => Err(Error::StorageVersionMismatch),
        };

        Ok(Self { entries: entries? })
    }

    pub fn serialize(&self) -> Vec<u8> {
        let mut output = Vec::new();
        output.extend_from_slice(vint64::encode(VERSION).as_ref());
        bincode::serialize_into(&mut output, &self.entries)
            .expect("failed to serialize directory content");
        output
    }

    pub fn iter(&self) -> btree_map::Iter<String, EntryData> {
        self.entries.iter()
    }

    pub fn get_key_value(&self, name: &str) -> Option<(&String, &EntryData)> {
        self.entries.get_key_value(name)
    }

    pub fn get_mut(&mut self, name: &str) -> Option<&mut EntryData> {
        self.entries.get_mut(name)
    }

    /// Inserts an entry into this directory. Returns the difference between the new and the old
    /// version vectors.
    pub fn insert(
        &mut self,
        name: String,
        new_data: EntryData,
    ) -> Result<VersionVector, EntryExists> {
        match self.entries.entry(name) {
            Entry::Vacant(entry) => {
                let diff = new_data.version_vector().clone();
                entry.insert(new_data);
                Ok(diff)
            }
            Entry::Occupied(mut entry) => {
                check_replace(entry.get(), &new_data)?;
                let diff = new_data
                    .version_vector()
                    .saturating_sub(entry.get().version_vector());
                entry.insert(new_data);
                Ok(diff)
            }
        }
    }

    /// Checks whether an entry can be inserted into this directory without actually inserting it.
    /// If so, returns the blob_id of the existing entry (if any).
    pub fn check_insert(
        &self,
        name: &str,
        new_data: &EntryData,
    ) -> Result<Option<BlobId>, EntryExists> {
        if let Some(old_data) = self.entries.get(name) {
            check_replace(old_data, new_data)
        } else {
            Ok(None)
        }
    }

    /// Updates the version vector of entry at `name`. Returns the difference between the old and
    /// the new version vectors.
    pub fn bump(&mut self, name: &str, bump: Bump) -> Result<VersionVector> {
        Ok(bump.apply(
            self.entries
                .get_mut(name)
                .ok_or(Error::EntryNotFound)?
                .version_vector_mut(),
        ))
    }

    /// Initial version vector for a new entry to be inserted.
    pub fn initial_version_vector(&self, name: &str) -> VersionVector {
        if let Some(EntryData::Tombstone(entry)) = self.entries.get(name) {
            entry.version_vector.clone()
        } else {
            VersionVector::new()
        }
    }
}

#[derive(Debug)]
pub(crate) enum EntryExists {
    /// The existing entry is more up-to-date and points to the same blob than the one being
    /// inserted
    Same,
    /// The existing entry either points to a different blob or is concurrent
    Different,
}

impl From<EntryExists> for Error {
    fn from(_: EntryExists) -> Self {
        Self::EntryExists
    }
}

impl<'a> IntoIterator for &'a Content {
    type Item = <Self::IntoIter as Iterator>::Item;
    type IntoIter = btree_map::Iter<'a, String, EntryData>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

fn deserialize_entries<'a, T: Deserialize<'a>>(input: &'a [u8]) -> Result<T, Error> {
    bincode::deserialize(input).map_err(|_| Error::MalformedDirectory)
}

fn check_replace(old: &EntryData, new: &EntryData) -> Result<Option<BlobId>, EntryExists> {
    // Replace entries only if the new version is more up to date than the old version.

    match (
        new.version_vector().partial_cmp(old.version_vector()),
        new,
        old,
    ) {
        (Some(Ordering::Greater), _, _) => Ok(old.blob_id().copied()),
        (Some(Ordering::Equal | Ordering::Less), EntryData::File(new), EntryData::File(old))
            if new.blob_id == old.blob_id =>
        {
            Err(EntryExists::Same)
        }
        (
            Some(Ordering::Equal | Ordering::Less),
            EntryData::Directory(new),
            EntryData::Directory(old),
        ) if new.blob_id == old.blob_id => Err(EntryExists::Same),
        (
            Some(Ordering::Equal | Ordering::Less),
            EntryData::Tombstone(new),
            EntryData::Tombstone(old),
        ) if new.cause == old.cause => Err(EntryExists::Same),
        (
            Some(Ordering::Equal | Ordering::Less),
            EntryData::File(_) | EntryData::Directory(_) | EntryData::Tombstone(_),
            EntryData::File(_) | EntryData::Directory(_) | EntryData::Tombstone(_),
        ) => Err(EntryExists::Different),
        (None, _, _) => Err(EntryExists::Different),
    }
}

mod v2 {
    use super::{
        super::entry_data::{EntryData, EntryTombstoneData, TombstoneCause},
        v1,
    };
    use std::collections::BTreeMap;

    pub(super) type Entries = BTreeMap<String, EntryData>;

    pub(super) fn from_v1(v1: v1::Entries) -> Entries {
        v1.into_iter()
            .map(|(name, data)| {
                let data = match data {
                    v1::EntryData::File(data) => EntryData::File(data),
                    v1::EntryData::Directory(data) => EntryData::Directory(data),
                    v1::EntryData::Tombstone(v1::EntryTombstoneData { version_vector }) => {
                        EntryData::Tombstone(EntryTombstoneData {
                            cause: TombstoneCause::Removed,
                            version_vector,
                        })
                    }
                };

                (name, data)
            })
            .collect()
    }
}

mod v1 {
    use super::v0;
    use std::collections::BTreeMap;
    pub(super) use v0::{EntryData, EntryTombstoneData};

    pub(super) type Entries = BTreeMap<String, v0::EntryData>;

    pub(super) fn from_v0(v0: v0::Entries) -> Entries {
        use crate::conflict;

        let mut v1 = BTreeMap::new();

        for (name, versions) in v0 {
            if versions.len() <= 1 {
                // If there is only one version, insert it directly
                if let Some(data) = versions.into_values().next() {
                    v1.insert(name, data);
                }
            } else {
                // If there is more than one version, create unique name for each of them and insert
                // them as separate entries
                for (author_id, data) in versions {
                    v1.insert(conflict::create_unique_name(&name, &author_id), data);
                }
            }
        }

        v1
    }
}

mod v0 {
    use super::super::entry_data::{EntryDirectoryData, EntryFileData};
    use crate::{crypto::sign::PublicKey, version_vector::VersionVector};
    use serde::Deserialize;
    use std::collections::BTreeMap;

    pub(super) type Entries = BTreeMap<String, BTreeMap<PublicKey, EntryData>>;

    #[derive(Deserialize)]
    pub(super) enum EntryData {
        File(EntryFileData),
        Directory(EntryDirectoryData),
        Tombstone(EntryTombstoneData),
    }

    #[derive(Deserialize)]
    pub(super) struct EntryTombstoneData {
        pub version_vector: VersionVector,
    }
}