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
use crate::changestore::{ChangeStore, FileMetadata};
use crate::path;
use crate::pristine::*;
use crate::HashMap;

mod output;
pub use output::*;
mod archive;
pub use archive::*;

#[derive(Debug, Error)]
pub enum OutputError<
    ChangestoreError: std::error::Error + 'static,
    Txn: std::error::Error + 'static,
    W: std::error::Error + Send + 'static,
> {
    #[error("Working copy error: {0}")]
    WorkingCopy(W),
    #[error(transparent)]
    Pristine(#[from] PristineOutputError<ChangestoreError, Txn>),
}

#[derive(Debug, Error)]
pub enum PristineOutputError<ChangestoreError: std::error::Error, Txn: std::error::Error + 'static>
{
    #[error(transparent)]
    Txn(Txn),
    #[error("Changestore error: {0}")]
    Changestore(ChangestoreError),
    #[error(transparent)]
    Io(#[from] std::io::Error),
    #[error(transparent)]
    Fs(#[from] crate::fs::FsError<Txn>),
}

impl<C: std::error::Error, T: std::error::Error + 'static> From<TxnErr<T>>
    for PristineOutputError<C, T>
{
    fn from(e: TxnErr<T>) -> Self {
        PristineOutputError::Txn(e.0)
    }
}

impl<C: std::error::Error, T: std::error::Error + 'static, W: std::error::Error + Send>
    From<TxnErr<T>> for OutputError<C, T, W>
{
    fn from(e: TxnErr<T>) -> Self {
        OutputError::Pristine(e.into())
    }
}

#[derive(Debug, Error)]
pub enum FileError<ChangestoreError: std::error::Error + 'static, T: std::error::Error + 'static> {
    #[error(transparent)]
    Changestore(ChangestoreError),
    #[error(transparent)]
    Txn(T),
    #[error(transparent)]
    Io(#[from] std::io::Error),
}

impl<C: std::error::Error, T: std::error::Error + 'static> From<FileError<C, T>>
    for PristineOutputError<C, T>
{
    fn from(e: FileError<C, T>) -> Self {
        match e {
            FileError::Changestore(e) => PristineOutputError::Changestore(e),
            FileError::Io(e) => PristineOutputError::Io(e),
            FileError::Txn(t) => PristineOutputError::Txn(t),
        }
    }
}

impl<C: std::error::Error, T: std::error::Error + 'static> From<TxnErr<T>> for FileError<C, T> {
    fn from(e: TxnErr<T>) -> Self {
        FileError::Txn(e.0)
    }
}

#[derive(Debug, Clone)]
struct OutputItem {
    parent: Inode,
    path: String,
    tmp: Option<String>,
    meta: InodeMetadata,
    pos: Position<ChangeId>,
    is_zombie: bool,
}

fn collect_children<T: GraphTxnT, P: ChangeStore>(
    txn: &T,
    changes: &P,
    channel: &T::Graph,
    inode_pos: Position<ChangeId>,
    inode: Inode,
    path: &str,
    tmp: Option<&str>,
    prefix_basename: Option<&str>,
    files: &mut HashMap<String, Vec<(Vertex<ChangeId>, OutputItem)>>,
) -> Result<(), PristineOutputError<P::Error, T::GraphError>> {
    debug!("path = {:?}", path);
    for e in iter_adjacent(
        txn,
        channel,
        inode_pos.inode_vertex(),
        EdgeFlags::FOLDER,
        EdgeFlags::FOLDER | EdgeFlags::PSEUDO | EdgeFlags::BLOCK,
    )? {
        let e = e?;
        let name_vertex = txn.find_block(channel, e.dest()).unwrap();
        let mut name_buf = Vec::new();
        let FileMetadata {
            basename,
            metadata: perms,
            ..
        } = changes
            .get_file_meta(
                |h| txn.get_external(&h).unwrap().map(|x| x.into()),
                *name_vertex,
                &mut name_buf,
            )
            .map_err(PristineOutputError::Changestore)?;
        debug!("filename: {:?} {:?}", perms, basename);
        let mut name = path.to_string();
        if let Some(next) = prefix_basename {
            if next != basename {
                continue;
            }
        }
        path::push(&mut name, basename);
        debug!("name_vertex: {:?} {:?}", e, name_vertex);
        let child = if let Some(child) = iter_adjacent(
            txn,
            channel,
            *name_vertex,
            EdgeFlags::FOLDER,
            EdgeFlags::FOLDER | EdgeFlags::BLOCK | EdgeFlags::PSEUDO,
        )?
        .next()
        {
            child?
        } else {
            let mut edge = None;
            for e in iter_adjacent(
                txn,
                channel,
                *name_vertex,
                EdgeFlags::FOLDER,
                EdgeFlags::all(),
            )? {
                let e = e?;
                if !e.flag().contains(EdgeFlags::PARENT) {
                    edge = Some(e);
                    break;
                }
            }
            let e = edge.unwrap();
            let mut f = std::fs::File::create("debug_output").unwrap();
            debug_root(txn, channel, e.dest().inode_vertex(), &mut f, false).unwrap();
            panic!("no child");
        };

        debug!("child: {:?}", child);
        let v = files.entry(name).or_insert_with(Vec::new);
        v.push((
            *name_vertex,
            OutputItem {
                parent: inode,
                path: path.to_string(),
                tmp: tmp.map(String::from),
                meta: perms,
                pos: child.dest(),
                is_zombie: is_zombie(txn, channel, child.dest())?,
            },
        ));
    }
    Ok(())
}

fn is_zombie<T: GraphTxnT>(
    txn: &T,
    channel: &T::Graph,
    pos: Position<ChangeId>,
) -> Result<bool, TxnErr<T::GraphError>> {
    let f = EdgeFlags::FOLDER | EdgeFlags::PARENT | EdgeFlags::DELETED;
    if let Some(n) =
        iter_adjacent(txn, channel, pos.inode_vertex(), f, f | EdgeFlags::BLOCK)?.next()
    {
        n?;
        Ok(true)
    } else {
        Ok(false)
    }
}

pub fn output_file<
    T: TreeTxnT + ChannelTxnT,
    C: crate::changestore::ChangeStore,
    V: crate::vertex_buffer::VertexBuffer,
>(
    changes: &C,
    txn: &T,
    channel: &T::Channel,
    v0: Position<ChangeId>,
    out: &mut V,
) -> Result<(), FileError<C::Error, T::GraphError>> {
    let mut forward = Vec::new();
    let mut graph = crate::alive::retrieve(&*txn, txn.graph(&*channel), v0)?;
    crate::alive::output_graph(changes, &*txn, &*channel, out, &mut graph, &mut forward)?;
    Ok(())
}