ovfetch 0.2.2

Resolve, download, and hash-verify the OpenVINO-enabled ONNX Runtime an Intel NPU needs
Documentation
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! Downloading, re-verifying, assembling, and installing a resolved plan.

use crate::consensus::Trust;
use crate::net;
use crate::resolve::Plan;
use crate::version::Ver;
use anyhow::{Context, Result, bail};
use std::fs;
use std::os::unix::fs::symlink;
use std::path::{Path, PathBuf};

const SUMS: &str = "SHA256SUMS";
const LOCK: &str = "ovfetch.lock.json";

struct TempDir(PathBuf);

impl TempDir {
    fn new() -> Result<Self> {
        let p = std::env::temp_dir().join(format!("ovfetch-{}", std::process::id()));
        fs::create_dir_all(&p)?;
        Ok(Self(p))
    }
}

impl Drop for TempDir {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.0);
    }
}

/// Downloads from PyPI, falling back to a mirror only when PyPI is
/// unreachable. Mirrors otherwise just vote on the hash. A digest mismatch
/// aborts rather than falling through: it means the bytes served differ from
/// what every source agreed on.
fn fetch(urls: &[String], sha256: &str, dest: &Path) -> Result<()> {
    let mut last = None;
    for url in urls {
        match net::download(url, dest) {
            Ok(got) if got == sha256 => return Ok(()),
            Ok(got) => bail!(
                "{url} served bytes with sha256 {got}, but every source agreed on {sha256}. Aborting."
            ),
            Err(e) => {
                eprintln!("note: {e:#}; trying the next mirror");
                last = Some(e);
            }
        }
    }
    Err(last.unwrap_or_else(|| anyhow::anyhow!("no source to download from")))
}

fn unzip(archive: &Path, dest: &Path, keep: impl Fn(&str) -> bool, flatten: bool) -> Result<()> {
    let mut zip = zip::ZipArchive::new(fs::File::open(archive)?)?;
    for i in 0..zip.len() {
        let mut entry = zip.by_index(i)?;
        let Some(rel) = entry.enclosed_name() else {
            continue;
        };
        let name = rel.to_string_lossy().into_owned();
        if entry.is_dir() || !keep(&name) {
            continue;
        }
        let out = if flatten {
            dest.join(rel.file_name().context("entry without a file name")?)
        } else {
            dest.join(&rel)
        };
        fs::create_dir_all(out.parent().context("entry without a parent")?)?;
        std::io::copy(&mut entry, &mut fs::File::create(&out)?)?;
    }
    Ok(())
}

fn is_runtime_lib(name: &str) -> bool {
    let file = name.rsplit('/').next().unwrap_or(name);
    file.starts_with("lib") && file.contains(".so")
}

/// Wheels store soname aliases as full copies. Collapse byte-identical files
/// into symlinks to the longest name, and add the `libonnxruntime.so{,.1}`
/// names ort-sys links against, which the wheel omits.
fn normalise_links(lib: &Path) -> Result<()> {
    let mut files: Vec<PathBuf> = fs::read_dir(lib)?
        .flatten()
        .map(|e| e.path())
        .filter(|p| p.is_file() && !p.is_symlink())
        .collect();
    files.sort_by_key(|p| std::cmp::Reverse(p.file_name().map(|n| n.len()).unwrap_or(0)));
    let stem = |p: &Path| {
        p.file_name()
            .and_then(|n| n.to_str())
            .and_then(|n| n.split(".so").next())
            .map(str::to_owned)
    };
    let mut kept: Vec<(PathBuf, Vec<u8>)> = Vec::new();
    for f in files {
        let bytes = fs::read(&f)?;
        if let Some((target, _)) = kept
            .iter()
            .find(|(k, b)| stem(k) == stem(&f) && *b == bytes)
        {
            fs::remove_file(&f)?;
            symlink(target.file_name().context("no file name")?, &f)?;
        } else {
            kept.push((f, bytes));
        }
    }
    let full = fs::read_dir(lib)?
        .flatten()
        .map(|e| e.file_name().to_string_lossy().into_owned())
        .find(|n| n.starts_with("libonnxruntime.so.") && n.matches('.').count() == 4)
        .context("no versioned libonnxruntime.so in the build")?;
    for (link, target) in [
        ("libonnxruntime.so.1", full.as_str()),
        ("libonnxruntime.so", "libonnxruntime.so.1"),
    ] {
        let p = lib.join(link);
        if !p.exists() {
            symlink(target, p)?;
        }
    }
    for entry in fs::read_dir(lib)?.flatten() {
        let path = entry.path();
        if !path.is_file() || path.is_symlink() {
            continue;
        }
        if let Some(soname) = soname(&fs::read(&path)?) {
            let link = lib.join(&soname);
            if soname != entry.file_name().to_string_lossy() && link.symlink_metadata().is_err() {
                symlink(entry.file_name(), link)?;
            }
        }
    }
    Ok(())
}

/// DT_SONAME of a little-endian ELF64 shared object, if it has one.
fn soname(elf: &[u8]) -> Option<String> {
    fn uint(b: &[u8], at: usize, len: usize) -> Option<usize> {
        let raw = b.get(at..at.checked_add(len)?)?;
        Some(raw.iter().rev().fold(0usize, |a, &x| (a << 8) | x as usize))
    }
    if elf.get(..4)? != b"\x7fELF" || *elf.get(4)? != 2 || *elf.get(5)? != 1 {
        return None;
    }
    let shoff = uint(elf, 0x28, 8)?;
    let shentsize = uint(elf, 0x3a, 2)?;
    let shnum = uint(elf, 0x3c, 2)?;
    let section = |i: usize| shoff.checked_add(i.checked_mul(shentsize)?);
    for i in 0..shnum {
        let sh = section(i)?;
        if uint(elf, sh + 4, 4)? != 6 {
            continue; // not SHT_DYNAMIC
        }
        let (off, size) = (uint(elf, sh + 0x18, 8)?, uint(elf, sh + 0x20, 8)?);
        let strtab = section(uint(elf, sh + 0x28, 4)?)?;
        let str_off = uint(elf, strtab + 0x18, 8)?;
        for d in (off..off.checked_add(size)?).step_by(16) {
            if uint(elf, d, 8)? == 14 {
                let start = str_off.checked_add(uint(elf, d + 8, 8)?)?;
                let name = elf.get(start..)?;
                let end = name.iter().position(|&c| c == 0)?;
                return String::from_utf8(name[..end].to_vec()).ok();
            }
        }
    }
    None
}

fn write_sums(dir: &Path) -> Result<()> {
    let mut names: Vec<String> = fs::read_dir(dir)?
        .flatten()
        .filter(|e| e.path().is_file() && !e.path().is_symlink())
        .map(|e| e.file_name().to_string_lossy().into_owned())
        .filter(|n| n != SUMS && n != LOCK)
        .collect();
    names.sort();
    let mut out = String::new();
    for n in names {
        out.push_str(&format!(
            "{}  {n}\n",
            net::sha256_hex(&fs::read(dir.join(&n))?)
        ));
    }
    fs::write(dir.join(SUMS), out)?;
    Ok(())
}

pub fn verify(prefix: &Path) -> Result<()> {
    let sums = fs::read_to_string(prefix.join(SUMS)).with_context(|| {
        format!(
            "{} has no {SUMS}; was it installed by ovfetch?",
            prefix.display()
        )
    })?;
    let mut bad = Vec::new();
    for line in sums.lines() {
        let Some((digest, name)) = line.split_once("  ") else {
            continue;
        };
        match fs::read(prefix.join(name)) {
            Ok(bytes) if net::sha256_hex(&bytes) == digest => {}
            Ok(_) => bad.push(format!("{name}: modified")),
            Err(e) => bad.push(format!("{name}: {e}")),
        }
    }
    for name in untracked(prefix, &sums)? {
        bad.push(format!("{name}: not installed by ovfetch"));
    }
    if !bad.is_empty() {
        bail!(
            "{} does not match its {SUMS}:\n  {}",
            prefix.display(),
            bad.join("\n  ")
        );
    }
    Ok(())
}

/// Entries in `prefix` that `sums` does not cover: anything other than a
/// listed file, a symlink to one, or the sums and lock files themselves.
fn untracked(prefix: &Path, sums: &str) -> Result<Vec<String>> {
    let listed: Vec<&str> = sums
        .lines()
        .filter_map(|l| l.split_once("  "))
        .map(|(_, n)| n)
        .collect();
    let mut out = Vec::new();
    for e in fs::read_dir(prefix)?.flatten() {
        let n = e.file_name().to_string_lossy().into_owned();
        let owned = n == SUMS
            || n == LOCK
            || listed.contains(&n.as_str())
            || (e.path().is_symlink()
                && fs::canonicalize(e.path()).is_ok_and(|t| {
                    t.parent() == fs::canonicalize(prefix).ok().as_deref()
                        && t.file_name()
                            .is_some_and(|f| listed.contains(&&*f.to_string_lossy()))
                }));
        if !owned {
            out.push(n);
        }
    }
    out.sort();
    Ok(out)
}

/// What install clears from a prefix before copying the new build in.
fn is_replaced(name: &str) -> bool {
    name.starts_with("libonnxruntime")
        || name.starts_with("libopenvino")
        || name.starts_with("libtbb")
        || name == SUMS
        || name == LOCK
}

pub struct Options {
    pub allow_unverified: bool,
    pub allow_downgrade: bool,
    pub force: bool,
}

fn installed_sha256(prefix: &Path) -> Option<String> {
    let lock: serde_json::Value =
        serde_json::from_str(&fs::read_to_string(prefix.join(LOCK)).ok()?).ok()?;
    Some(lock["artifact"]["sha256"].as_str()?.to_owned())
}

/// OpenVINO version of whatever ovfetch installed in `prefix` before.
pub fn installed_openvino(prefix: &Path) -> Option<Ver> {
    let lock: serde_json::Value =
        serde_json::from_str(&fs::read_to_string(prefix.join(LOCK)).ok()?).ok()?;
    Ver::parse(lock["openvino"].as_str()?)
}

pub fn install(
    plan: &Plan,
    prefix: &Path,
    opts: &Options,
    mirror_urls: impl Fn(&str) -> Vec<String>,
) -> Result<()> {
    if let (Some(have), Some(want)) = (installed_openvino(prefix), Ver::parse(&plan.openvino))
        && want < have
        && !opts.allow_downgrade
    {
        bail!(
            "{} already has OpenVINO {have}, newer than the {want} this plan resolves to. \
                 Keeping it; pass --allow-downgrade to replace it anyway.",
            prefix.display()
        );
    }
    if plan.artifact.trust == Trust::Unverified && !opts.allow_unverified {
        bail!(
            "{} is not in the reviewed ledger (see `ovfetch resolve`).\n\
             Sources agreed on their hashes, but nothing pins them yet. Pass --allow-unverified to accept that.",
            plan.artifact.id
        );
    }
    if !opts.force
        && installed_sha256(prefix).as_deref() == Some(plan.artifact.sha256.as_str())
        && verify(prefix).is_ok()
    {
        eprintln!(
            "already installed: {} matches {}; pass --force to reinstall",
            prefix.display(),
            plan.artifact.id
        );
        return Ok(());
    }
    if prefix.exists() {
        let sums = fs::read_to_string(prefix.join(SUMS)).unwrap_or_default();
        let foreign: Vec<String> = untracked(prefix, &sums)?
            .into_iter()
            .filter(|n| !is_replaced(n))
            .collect();
        if !foreign.is_empty() {
            bail!(
                "{} holds files ovfetch did not install and will not remove:\n  {}\n\
                 Delete them, or pick an empty --prefix.",
                prefix.display(),
                foreign.join("\n  ")
            );
        }
    }
    let tmp = TempDir::new()?;
    let lib = tmp.0.join("lib");
    fs::create_dir_all(&lib)?;

    let whl = tmp.0.join("ort.whl");
    let mut urls = mirror_urls(&plan.artifact.id);
    if urls.is_empty() {
        urls.push(plan.artifact.url.clone());
    }
    fetch(&urls, &plan.artifact.sha256, &whl)?;
    unzip(
        &whl,
        &lib,
        |n| n.starts_with("onnxruntime/capi/") && is_runtime_lib(n),
        true,
    )?;

    normalise_links(&lib)?;
    write_sums(&lib)?;
    fs::write(lib.join(LOCK), serde_json::to_string_pretty(plan)?)?;
    verify(&lib)?;

    fs::create_dir_all(prefix).with_context(|| format!("creating {}", prefix.display()))?;
    for e in fs::read_dir(prefix)?.flatten() {
        if is_replaced(&e.file_name().to_string_lossy()) {
            fs::remove_file(e.path())?;
        }
    }
    for e in fs::read_dir(&lib)?.flatten() {
        let dest = prefix.join(e.file_name());
        if e.path().is_symlink() {
            symlink(fs::read_link(e.path())?, dest)?;
        } else {
            fs::copy(e.path(), dest)?;
        }
    }
    verify(prefix)?;
    eprintln!(
        "installed ONNX Runtime {} + OpenVINO {} into {}",
        plan.artifact.onnxruntime,
        plan.openvino,
        prefix.display()
    );
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn reads_soname_from_a_shared_object() {
        let Some(libc) = ["/lib/x86_64-linux-gnu/libc.so.6", "/usr/lib64/libc.so.6"]
            .iter()
            .find_map(|p| fs::read(p).ok())
        else {
            return;
        };
        assert_eq!(soname(&libc).as_deref(), Some("libc.so.6"));
        assert_eq!(soname(b"not elf"), None);
    }

    #[test]
    fn verify_flags_files_it_did_not_write() {
        let dir = TempDir::new().unwrap();
        let p = &dir.0;
        fs::write(p.join("libonnxruntime.so.1.23.0"), b"ort").unwrap();
        symlink("libonnxruntime.so.1.23.0", p.join("libonnxruntime.so.1")).unwrap();
        write_sums(p).unwrap();
        fs::write(p.join(LOCK), "{}").unwrap();
        verify(p).unwrap();

        fs::write(
            p.join("onnxruntime_pybind11_state.cpython-313-x86_64-linux-gnu.so"),
            b"x",
        )
        .unwrap();
        symlink("/etc/hostname", p.join("libstray.so")).unwrap();
        let err = format!("{:#}", verify(p).unwrap_err());
        assert!(
            err.contains(
                "onnxruntime_pybind11_state.cpython-313-x86_64-linux-gnu.so: not installed"
            )
        );
        assert!(err.contains("libstray.so: not installed"));
        assert!(!err.contains("libonnxruntime"));
    }
}