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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use std::collections::HashSet;
use std::convert::TryFrom;
use std::io::BufRead;

use bitflags::bitflags;
use failure::bail;
use failure::ensure;
use failure::err_msg;
use failure::format_err;
use failure::Error;
use failure::ResultExt;
use hex;
use maplit::hashset;

use crate::java::DataInput;

pub type Checksum = [u8; 20];

#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub struct UniqId {
    pub group: String,
    pub artifact: String,
    pub version: String,
    pub classifier: Option<String>,
    pub extension: Option<String>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FullInfo {
    pub packaging: String,
    pub last_modified: u64,
    pub size: Option<u64>,
    pub source_attached: AttachmentStatus,
    pub javadoc_attached: AttachmentStatus,
    pub signature_attached: AttachmentStatus,
    pub extension: String,
}

#[derive(Debug)]
pub struct Doc {
    pub id: UniqId,
    pub object_info: FullInfo,
    pub modified: u64,
    pub name: Option<String>,
    pub description: Option<String>,
    pub checksum: Option<Checksum>,
}

#[derive(Debug)]
pub enum Event {
    Doc(Doc),
    Delete(UniqId),
    Error {
        error: Error,
        raw: Vec<(String, String)>,
    },
}

pub fn read<R: BufRead, F>(from: R, mut cb: F) -> Result<(), Error>
where
    F: FnMut(Event) -> Result<(), Error>,
{
    let mut from = DataInput::new(from);

    ensure!(1 == from.read_byte()?, "version byte");
    let _timestamp_ms = from.read_long()?;

    loop {
        let fields = read_fields(&mut from).with_context(|_| err_msg("reading fields"))?;

        let fields = match fields {
            Some(fields) => fields,
            None => break,
        };

        let names = fields
            .iter()
            .map(|(key, _value)| key.as_str())
            .collect::<HashSet<_>>();

        if names.contains("del") {
            cb(Event::Delete(read_uniq(
                fields
                    .iter()
                    .find_map(|(key, value)| if "del" == key { Some(value) } else { None })
                    .expect("just checked"),
            )?))?;
            continue;
        }

        if hashset!("DESCRIPTOR", "IDXINFO") == names {
            continue;
        }

        if hashset!("rootGroups", "rootGroupsList") == names {
            continue;
        }

        if hashset!("allGroups", "allGroupsList") == names {
            continue;
        }

        if !(names.contains("u") && names.contains("i") && names.contains("m")) {
            // TODO: move checker fails on 'fields' here
            cb(Event::Error {
                error: err_msg("unrecognised doc type"),
                raw: fields.clone(),
            })?;
            continue;
        }

        cb(match read_doc(&fields) {
            Ok(doc) => Event::Doc(doc),
            Err(error) => Event::Error { error, raw: fields },
        })?;
    }

    Ok(())
}

fn read_doc(fields: &[(String, String)]) -> Result<Doc, Error> {
    let mut you = None;
    let mut eye = None;
    let mut modified = None;
    let mut name = None;
    let mut description = None;
    let mut checksum = None;

    for (field_name, value) in fields {
        match field_name.as_str() {
            "u" => {
                you = Some(
                    read_uniq(&value).with_context(|_| format_err!("reading 'u': {:?}", value))?,
                )
            }
            "i" => {
                eye = Some(
                    read_info(&value).with_context(|_| format_err!("reading 'i': {:?}", value))?,
                )
            }
            "m" => {
                modified = Some(
                    value
                        .parse::<u64>()
                        .with_context(|_| format_err!("reading 'm': {:?}", value))?,
                )
            }
            "n" => name = Some(value.to_string()),
            "d" => description = Some(value.to_string()),
            "1" => checksum = read_checksum(&value).ok(),

            _ => (), // bail!("unrecognised field value: {:?}", field_name),
        }
    }

    Ok(Doc {
        id: you.ok_or_else(|| err_msg("no 'u'"))?,
        object_info: eye.ok_or_else(|| err_msg("no 'i'"))?,
        modified: modified.ok_or_else(|| err_msg("no modified"))?,
        name,
        description,
        checksum,
    })
}

fn read_fields<R: BufRead>(f: &mut DataInput<R>) -> Result<Option<Vec<(String, String)>>, Error> {
    if f.check_eof()? {
        return Ok(None);
    }

    let field_count = f
        .read_int()
        .with_context(|_| err_msg("reading field count (first field)"))?;

    let field_count = usize::try_from(field_count)?;
    let mut ret = Vec::with_capacity(field_count);

    for field in 0..field_count {
        ret.push(read_field(f).with_context(|_| format_err!("reading field {}", field))?);
    }

    Ok(Some(ret))
}

fn read_field<R: BufRead>(f: &mut DataInput<R>) -> Result<(String, String), Error> {
    let flags = u8::try_from(f.read_byte()?)?;
    let _flags = FieldFlag::from_bits(flags).ok_or_else(|| err_msg("decoding field flags"))?;

    let name_len = f.read_unsigned_short()?;
    let name = f.read_utf8(usize::try_from(name_len).unwrap())?;

    // yup, they went out of their way to use signed data here
    let value_len = usize::try_from(f.read_int()?)?;
    let value = f.read_utf8(value_len)?;

    Ok((name, value))
}

#[inline]
fn read_checksum(value: &str) -> Result<[u8; 20], Error> {
    let decoded = hex::decode(value).with_context(|_| err_msg("decoding checksum"))?;
    ensure!(20 == decoded.len(), "checksum was wrong length");
    let mut arr = [0u8; 20];
    arr.copy_from_slice(&decoded);
    Ok(arr)
}

fn read_uniq(value: &str) -> Result<UniqId, Error> {
    let mut parts = value.split('|');

    Ok(UniqId {
        group: parts
            .next()
            .ok_or_else(|| err_msg("short uniq: group"))?
            .to_string(),
        artifact: parts
            .next()
            .ok_or_else(|| err_msg("short uniq: artifact"))?
            .to_string(),
        version: parts
            .next()
            .ok_or_else(|| err_msg("short uniq: version"))?
            .to_string(),
        classifier: not_na(
            parts
                .next()
                .ok_or_else(|| err_msg("short uniq: classifier"))?,
        )
        .map(|v| v.to_string()),
        extension: parts.next().map(|v| v.to_string()),
    })
}

fn read_info(value: &str) -> Result<FullInfo, Error> {
    let mut parts = value.split('|');
    Ok(FullInfo {
        packaging: parts
            .next()
            .ok_or_else(|| err_msg("short info: packaging"))?
            .to_string(),
        last_modified: parts
            .next()
            .ok_or_else(|| err_msg("short info: time"))?
            .parse::<u64>()
            .with_context(|_| err_msg("reading time"))?,
        size: read_size(parts.next().ok_or_else(|| err_msg("short i: size"))?)?,
        source_attached: AttachmentStatus::read(
            parts
                .next()
                .ok_or_else(|| err_msg("short info: sources flag"))?,
        )?,
        javadoc_attached: AttachmentStatus::read(
            parts.next().ok_or_else(|| err_msg("short info: flag 2"))?,
        )?,
        signature_attached: AttachmentStatus::read(
            parts.next().ok_or_else(|| err_msg("short info: flag 3"))?,
        )?,
        extension: parts
            .next()
            .ok_or_else(|| err_msg("short info: extension"))?
            .to_string(),
    })
}

fn read_size(value: &str) -> Result<Option<u64>, Error> {
    if "-1" == value {
        return Ok(None);
    }

    Ok(Some(
        value
            .parse::<u64>()
            .with_context(|_| err_msg("reading size"))?,
    ))
}

fn not_na(value: &str) -> Option<&str> {
    if "NA" == value {
        None
    } else {
        Some(value)
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum AttachmentStatus {
    Absent,
    Present,
    Unavailable,
}

impl AttachmentStatus {
    fn read(value: &str) -> Result<AttachmentStatus, Error> {
        Ok(match value.parse::<u8>() {
            Ok(0) => AttachmentStatus::Absent,
            Ok(1) => AttachmentStatus::Present,
            Ok(2) => AttachmentStatus::Unavailable,
            other => bail!("invalid attachment value: {:?}: {:?}", value, other),
        })
    }
}

bitflags! {
    struct FieldFlag: u8 {
        const INDEXED    = 0b0000_0001;
        const TOKENIZED  = 0b0000_0010;
        const STORED     = 0b0000_0100;
        const COMPRESSED = 0b0000_1000;
    }
}