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
use crate::changestore::ChangeStore;
use crate::path;
use crate::pristine::*;
use std::collections::HashMap;

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

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

fn collect_children<T: TxnT, P: ChangeStore>(
    txn: &T,
    changes: &P,
    channel: &Channel<T>,
    inode_pos: Position<ChangeId>,
    inode: Inode,
    path: &str,
    prefix_basename: Option<&str>,
    files: &mut HashMap<String, Vec<(Vertex<ChangeId>, OutputItem)>>,
) -> Result<(), anyhow::Error> {
    debug!("path = {:?}", path);
    for e in iter_adjacent(
        txn,
        &channel,
        inode_pos.inode_vertex(),
        EdgeFlags::empty(),
        EdgeFlags::all(),
    ) {
        debug!("inode_pos, e = {:?}", e);
    }
    for e in iter_adjacent(
        txn,
        &channel,
        inode_pos.inode_vertex(),
        EdgeFlags::FOLDER,
        EdgeFlags::FOLDER | EdgeFlags::PSEUDO | EdgeFlags::BLOCK,
    ) {
        // org id Gb4FgVTFFkCPgh6xtUjeL/ntA6wAsIKhRiiX/8b4Jy8=
        // This unwrap is ok since e.dest is in the channel.
        let name_vertex = find_block(txn, &channel, e.dest).unwrap();
        let mut name_buf = Vec::new();
        changes.get_contents(|h| txn.get_external(h), name_vertex, &mut name_buf)?;
        let (perms, basename) = name_buf.as_slice().split_at(2);
        let (perms, basename) = (
            InodeMetadata::from_basename(perms),
            std::str::from_utf8(basename).unwrap(),
        );
        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);
        // org id RFCdVpFfGZuILPerwkNtNS2jmRrxrMOGl0kh7aVMVmM=
        debug!("name_vertex: {:?} {:?}", e, name_vertex);
        for e in iter_adjacent(
            txn,
            &channel,
            name_vertex,
            EdgeFlags::empty(),
            EdgeFlags::all(),
        ) {
            debug!("e = {:?}", e);
        }
        let child = if let Some(child) = iter_adjacent(
            txn,
            &channel,
            name_vertex,
            EdgeFlags::FOLDER,
            EdgeFlags::FOLDER | EdgeFlags::BLOCK | EdgeFlags::PSEUDO,
        )
        .next()
        {
            child
        } else {
            let child = iter_adjacent(
                txn,
                &channel,
                name_vertex,
                EdgeFlags::FOLDER,
                EdgeFlags::all(),
            )
            .filter(|e| !e.flag.contains(EdgeFlags::PARENT))
            .next()
            .unwrap();

            let mut f = std::fs::File::create("debug_output").unwrap();
            debug_root(txn, channel, child.dest.inode_vertex(), &mut f).unwrap();
            panic!("no child");
        };

        debug!("child: {:?}", child);
        let child_inode = txn
            .get_revinodes(child.dest, None)
            .map(|inode| (inode, txn.get_inodes(inode, None).unwrap()));
        debug!("child_inode: {:?}", child_inode);
        let v = files.entry(name).or_insert(Vec::new());
        v.push((
            name_vertex,
            OutputItem {
                parent: inode,
                path: path.to_string(),
                meta: perms,
                pos: child.dest,
                inode: child_inode,
                is_zombie: is_zombie(txn, channel, child.dest),
            },
        ));
    }
    Ok(())
}

fn is_zombie<T: TxnT>(txn: &T, channel: &Channel<T>, pos: Position<ChangeId>) -> bool {
    let f = EdgeFlags::FOLDER | EdgeFlags::PARENT | EdgeFlags::DELETED;
    iter_adjacent(txn, &channel, pos.inode_vertex(), f, f | EdgeFlags::BLOCK)
        .next()
        .is_some()
}