cpa-plugin 0.4.1

Part of cliproxy: the native plugin host (CLIProxyAPI C ABI 1, registration schema 6) and plugin store
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//! Store installs (internal/pluginstore/install.go): a release or direct artifact is
//! downloaded, verified, and its library written atomically to
//! `<plugins-dir>/<goos>/<goarch>/<id>-v<version><ext>`.

use cpa_core::gostr::GoStr;
use std::path::{Path, PathBuf};

use super::checksum::{parse_checksums, sha256_hex, verify_checksum};
use super::client::{Client, Release, StoreError, select_release_assets};
use super::manifest::{Manifest, release_version};
use super::registry::{
    Artifact, INSTALL_TYPE_DIRECT, INSTALL_TYPE_GITHUB_RELEASE, InstallPlan, Plugin, normalize_goarch, normalize_goos,
    normalize_install_plan, plugin_install_type, valid_plugin_id, valid_plugin_version, validate_install_plan,
    validate_plugin,
};
use super::version::normalize_version;
use super::zip::{Archive, ReadError};

/// The largest library an archive may expand to. Go inflates up to the size the
/// archive declares, whatever it is.
pub const MAX_LIBRARY_BYTES: u64 = 256 << 20;

/// Go `ErrLoadedPluginLocked`'s text.
pub const LOADED_PLUGIN_LOCKED: &str = "loaded plugin library cannot be overwritten while the server is running";

/// Go `InstallOptions`.
#[derive(Default)]
pub struct InstallOptions {
    pub plugins_dir: String,
    pub goos: String,
    pub goarch: String,
    /// Whether the plugin's library is loaded now; Windows installs that would
    /// overwrite a loaded library are refused.
    pub plugin_loaded: Option<Box<dyn Fn() -> bool + Send + Sync>>,
    /// Runs after verification, before an existing library is replaced.
    pub before_write: Option<Box<dyn Fn() -> Result<(), String> + Send + Sync>>,
}

/// Go `InstallResult`.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct InstallResult {
    pub id: String,
    pub version: String,
    pub release_tag: String,
    pub install_type: String,
    pub path: String,
    pub overwritten: bool,
    pub skipped: bool,
}

/// Go `normalizeInstallOptions` (idempotent): the plugins directory and target
/// platform, defaulted and canonical.
fn normalize_options(options: &mut InstallOptions) {
    options.plugins_dir = match options.plugins_dir.trim() {
        "" => "plugins".into(),
        d => d.into(),
    };
    let goos = match options.goos.trim() {
        "" => crate::platform::goos(),
        g => g,
    };
    let goarch = match options.goarch.trim() {
        "" => crate::platform::goarch(),
        g => g,
    };
    options.goos = normalize_goos(goos);
    options.goarch = normalize_goarch(goarch);
}

fn other(e: impl Into<String>) -> StoreError {
    StoreError::Other(e.into())
}

impl Client {
    /// Go `Install`: a direct install's plan, or the latest GitHub release.
    pub async fn install(&self, mut plugin: Plugin, mut options: InstallOptions) -> Result<InstallResult, StoreError> {
        validate_plugin(&plugin)?;
        normalize_options(&mut options);
        if plugin_install_type(&plugin) == INSTALL_TYPE_DIRECT {
            plugin.version = normalize_version(&plugin.version);
            let plan = plugin.install.clone();
            return self.install_direct(plugin, plan, options).await;
        }
        let release = self.fetch_latest_release(&plugin).await?;
        let latest = release_version(&release)?;
        plugin.version = latest.clone();
        self.install_release(plugin, &release, &latest, options).await
    }

    /// Go `InstallManifest`.
    pub async fn install_manifest(
        &self,
        manifest: &Manifest,
        mut options: InstallOptions,
    ) -> Result<InstallResult, StoreError> {
        manifest.validate()?;
        normalize_options(&mut options);
        match manifest.install_type().as_str() {
            INSTALL_TYPE_DIRECT => {
                let plugin = self.direct_plugin_from_manifest(manifest).await?;
                let plan = plugin.install.clone();
                self.install_direct(plugin, plan, options).await
            }
            INSTALL_TYPE_GITHUB_RELEASE => {
                self.install_version(manifest.plugin(), &manifest.release_tag, &manifest.version, options)
                    .await
            }
            _ => Err(other(format!(
                "unsupported install type {}",
                cpa_common::gostr::quote(&manifest.install.install_type)
            ))),
        }
    }

    /// Go `InstallVersion`: a fixed release tag and version.
    pub async fn install_version(
        &self,
        mut plugin: Plugin,
        release_tag: &str,
        version: &str,
        mut options: InstallOptions,
    ) -> Result<InstallResult, StoreError> {
        validate_plugin(&plugin)?;
        normalize_options(&mut options);
        let version = normalize_version(version);
        if !valid_plugin_version(&version) {
            return Err(other(format!(
                "invalid plugin version {}",
                cpa_common::gostr::quote(&version)
            )));
        }
        let release_tag = match release_tag.trim() {
            "" => version.clone(),
            t => t.to_owned(),
        };
        let release = self.fetch_release_by_tag(&plugin, &release_tag).await?;
        let resolved = release_version(&release)?;
        if resolved != version {
            return Err(other(format!(
                "release tag {} resolved version {}, want {}",
                cpa_common::gostr::quote(&release_tag),
                cpa_common::gostr::quote(&resolved),
                cpa_common::gostr::quote(&version)
            )));
        }
        plugin.version = version.clone();
        self.install_release(plugin, &release, &version, options).await
    }

    async fn install_release(
        &self,
        mut plugin: Plugin,
        release: &Release,
        version: &str,
        options: InstallOptions,
    ) -> Result<InstallResult, StoreError> {
        let (archive, checksums) =
            select_release_assets(release, &plugin.id, &plugin.version, &options.goos, &options.goarch)?;
        let archive_data = self
            .download_asset(&archive)
            .await
            .map_err(|e| e.context(&format!("download {}", archive.name)))?;
        let checksum_data = self
            .download_asset(&checksums)
            .await
            .map_err(|e| e.context("download checksums.txt"))?;
        let sums = parse_checksums(&checksum_data)?;
        verify_checksum(&archive.name, &archive_data, &sums)?;
        plugin.version = version.to_owned();
        let mut result = install_archive(&archive_data, &plugin, options)?;
        result.install_type = INSTALL_TYPE_GITHUB_RELEASE.into();
        result.release_tag = release.tag_name.trim().to_owned();
        Ok(result)
    }

    /// Go `InstallDirect`.
    pub async fn install_direct(
        &self,
        mut plugin: Plugin,
        plan: InstallPlan,
        mut options: InstallOptions,
    ) -> Result<InstallResult, StoreError> {
        plugin.id = plugin.id.trim().to_owned();
        plugin.version = normalize_version(&plugin.version);
        if !valid_plugin_id(&plugin.id) {
            return Err(other(format!(
                "invalid plugin id {}",
                cpa_common::gostr::quote(&plugin.id)
            )));
        }
        if !valid_plugin_version(&plugin.version) {
            return Err(other(format!(
                "invalid plugin version {}",
                cpa_common::gostr::quote(&plugin.version)
            )));
        }
        let mut plan = normalize_install_plan(&plan);
        plan.install_type = INSTALL_TYPE_DIRECT.into();
        validate_install_plan(&plan)?;
        normalize_options(&mut options);
        let artifact = select_artifact(&plan, &options.goos, &options.goarch)?;
        let data = self
            .download_artifact(&artifact)
            .await
            .map_err(|e| e.context("download artifact"))?;
        verify_artifact_checksum(&artifact, &data)?;
        let mut result = install_archive(&data, &plugin, options)?;
        result.install_type = INSTALL_TYPE_DIRECT.into();
        Ok(result)
    }

    /// Go `directPluginFromManifest`: pinned artifacts, or the plugin re-read from
    /// its source registry.
    async fn direct_plugin_from_manifest(&self, manifest: &Manifest) -> Result<Plugin, StoreError> {
        let mut plugin = manifest.plugin();
        plugin.version = normalize_version(&manifest.version);
        plugin.install = normalize_install_plan(&plugin.install);
        plugin.install.install_type = INSTALL_TYPE_DIRECT.into();
        if !plugin.install.artifacts.is_empty() {
            return Ok(plugin);
        }
        let source_url = match manifest.source_url.trim() {
            "" => self.registry_url.trim().to_owned(),
            u => u.to_owned(),
        };
        if source_url.is_empty() {
            return Err(other("direct install manifest missing source-url"));
        }
        let mut source_client = self.clone();
        source_client.registry_url = source_url;
        let registry = source_client
            .fetch_registry()
            .await
            .map_err(|e| e.context("fetch direct install source"))?;
        let id = manifest.id.trim();
        let resolved = registry.plugin_by_id(id).ok_or_else(|| {
            other(format!(
                "direct install plugin {} not found in source",
                cpa_common::gostr::quote(id)
            ))
        })?;
        let install_type = plugin_install_type(resolved);
        if install_type != INSTALL_TYPE_DIRECT {
            return Err(other(format!(
                "direct install plugin {} resolved as {}",
                cpa_common::gostr::quote(id),
                cpa_common::gostr::quote(&install_type)
            )));
        }
        Ok(direct_plugin_version(resolved.clone(), id, &manifest.version)?)
    }
}

/// Go `directPluginVersion`: the plugin's own version or one of its versions.
fn direct_plugin_version(mut plugin: Plugin, id: &str, version: &str) -> Result<Plugin, String> {
    let version = normalize_version(version);
    let q = cpa_common::gostr::quote;
    if normalize_version(&plugin.version) == version {
        plugin.version = version.clone();
        plugin.install = normalize_install_plan(&plugin.install);
        plugin.install.install_type = INSTALL_TYPE_DIRECT.into();
        validate_install_plan(&plugin.install)
            .map_err(|e| format!("direct install plugin {} version {}: {e}", q(id), q(&version)))?;
        return Ok(plugin);
    }
    let candidate = plugin
        .versions
        .iter()
        .find(|c| normalize_version(&c.version) == version)
        .cloned()
        .ok_or_else(|| {
            format!(
                "direct install plugin {} version {} not found in source",
                q(id),
                q(&version)
            )
        })?;
    plugin.version = version.clone();
    plugin.install = normalize_install_plan(&candidate.install);
    if plugin.install.install_type.is_empty() {
        plugin.install.install_type = INSTALL_TYPE_DIRECT.into();
    }
    if plugin.install.install_type != INSTALL_TYPE_DIRECT {
        return Err(format!(
            "direct install plugin {} version {} resolved as {}",
            q(id),
            q(&version),
            q(&plugin.install.install_type)
        ));
    }
    validate_install_plan(&plugin.install)
        .map_err(|e| format!("direct install plugin {} version {}: {e}", q(id), q(&version)))?;
    Ok(plugin)
}

/// Go `SelectArtifact`.
pub fn select_artifact(plan: &InstallPlan, goos: &str, goarch: &str) -> Result<Artifact, String> {
    let plan = normalize_install_plan(plan);
    let (goos, goarch) = (normalize_goos(goos), normalize_goarch(goarch));
    if plan.install_type != INSTALL_TYPE_DIRECT {
        return Err(format!(
            "install type {} is not direct",
            cpa_common::gostr::quote(&plan.install_type)
        ));
    }
    plan.artifacts
        .into_iter()
        .find(|a| a.goos == goos && a.goarch == goarch)
        .ok_or_else(|| format!("artifact not found for {goos}/{goarch}"))
}

/// Go `VerifyArtifactChecksum`.
pub fn verify_artifact_checksum(artifact: &Artifact, data: &[u8]) -> Result<(), String> {
    let expected = artifact.sha256.trim().go_lower();
    if expected.is_empty() {
        return Err("artifact checksum missing".into());
    }
    if sha256_hex(data) != expected {
        return Err("artifact checksum mismatch".into());
    }
    Ok(())
}

/// Go `InstallArchive`: the target library from the archive root, written unless
/// the installed file is identical.
pub fn install_archive(data: &[u8], plugin: &Plugin, options: InstallOptions) -> Result<InstallResult, StoreError> {
    let mut options = options;
    normalize_options(&mut options);
    let options = &options;
    let q = cpa_common::gostr::quote;
    let id = plugin.id.trim();
    if !valid_plugin_id(id) {
        return Err(format!("invalid plugin id {}", q(&plugin.id)).into());
    }
    let version = normalize_version(&plugin.version);
    if !valid_plugin_version(&version) {
        return Err(format!("invalid plugin version {}", q(&plugin.version)).into());
    }
    let archive = Archive::new(data).map_err(|e| format!("open zip: {e}"))?;
    let (library, mode) = read_target_library(&archive, id, &version, &options.goos)?;
    let target = install_target_path(options, id, &version)?;
    let overwritten = match std::fs::metadata(&target) {
        Ok(_) => true,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => false,
        Err(e) => return Err(format!("stat target plugin: {}", go_path_error("stat", &target, &e)).into()),
    };
    let result = |overwritten: bool, skipped: bool| InstallResult {
        id: id.to_owned(),
        version: version.clone(),
        path: target.to_string_lossy().into_owned(),
        overwritten,
        skipped,
        ..Default::default()
    };
    if overwritten {
        let existing = std::fs::read(&target)
            .map_err(|e| format!("read target plugin: {}", go_path_error("open", &target, &e)))?;
        if existing == library {
            return Ok(result(true, true));
        }
        if let Some(before_write) = &options.before_write {
            before_write().map_err(|e| format!("prepare plugin write: {e}"))?;
        }
        // Go `loadedPluginInstallBlocked`: the platform first, then the callback.
        if options.goos.eq_ignore_ascii_case("windows") && options.plugin_loaded.as_ref().is_some_and(|f| f()) {
            return Err(StoreError::LoadedPluginLocked);
        }
    }
    write_file_atomic(&target, &library, mode)?;
    Ok(result(overwritten, false))
}

fn install_target_path(options: &InstallOptions, id: &str, version: &str) -> Result<PathBuf, String> {
    let version = normalize_version(version);
    if !valid_plugin_version(&version) {
        return Err(format!("invalid plugin version {}", cpa_common::gostr::quote(&version)));
    }
    Ok(Path::new(&options.plugins_dir)
        .join(&options.goos)
        .join(&options.goarch)
        .join(versioned_file_name(id, &version, &options.goos)))
}

/// Go `readTargetLibrary`: exactly one library at the archive root, named for the
/// plugin (optionally versioned); its permission bits, 0755 when none.
fn read_target_library(archive: &Archive<'_>, id: &str, version: &str, goos: &str) -> Result<(Vec<u8>, u32), String> {
    let target_name = format!("{}{}", id.trim(), extension(goos));
    let versioned = versioned_file_name(id, version, goos);
    let mut target = None;
    for entry in &archive.entries {
        let cleaned = clean_zip_name(&entry.name)?;
        if entry.is_dir() {
            continue;
        }
        if !entry.is_regular() {
            return Err(format!("zip entry {} is not a regular file", entry.name));
        }
        let lower = cleaned.go_lower();
        if !(lower.ends_with(".dylib") || lower.ends_with(".so") || lower.ends_with(".dll")) {
            continue;
        }
        if cleaned != target_name && cleaned != versioned {
            let base = cleaned.rsplit('/').next().unwrap_or_default();
            if base == target_name || base == versioned {
                return Err("target dynamic library must be at zip root".into());
            }
            return Err(format!("dynamic library filename must be {target_name} or {versioned}"));
        }
        if target.is_some() {
            return Err("zip contains multiple target dynamic libraries".into());
        }
        target = Some(entry);
    }
    let entry = target.ok_or_else(|| format!("zip does not contain {target_name}"))?;
    // Reading stops one byte past the declared size, so bounding it bounds inflation.
    if entry.size() > MAX_LIBRARY_BYTES {
        return Err(format!(
            "{target_name} exceeds maximum allowed size of {MAX_LIBRARY_BYTES} bytes"
        ));
    }
    let data = archive.read(entry).map_err(|e| match e {
        ReadError::Open(e) => format!("open {target_name}: {e}"),
        ReadError::Read(e) => format!("read {target_name}: {e}"),
    })?;
    let mode = match entry.perm() {
        0 => 0o755,
        m => m,
    };
    Ok((data, mode))
}

fn versioned_file_name(id: &str, version: &str, goos: &str) -> String {
    format!("{}-v{}{}", id.trim(), normalize_version(version), extension(goos))
}

/// Go `pluginExtension`.
fn extension(goos: &str) -> &'static str {
    match goos.trim().go_lower().as_str() {
        "darwin" | "mac" | "macos" | "osx" => ".dylib",
        "windows" => ".dll",
        _ => ".so",
    }
}

/// Go `cleanZipName`: relative, slash-separated, inside the archive root.
fn clean_zip_name(name: &str) -> Result<String, String> {
    if name.trim().is_empty() {
        return Err("zip entry has empty name".into());
    }
    if name.contains('\\') {
        return Err(format!("zip entry {name} uses backslash path separators"));
    }
    if name.starts_with('/') {
        return Err(format!("zip entry {name} is absolute"));
    }
    let cleaned = clean_path(name);
    if cleaned == "." || cleaned == ".." || cleaned.starts_with("../") {
        return Err(format!("zip entry {name} escapes archive root"));
    }
    Ok(cleaned)
}

/// Go `path.Clean` for a relative path.
fn clean_path(name: &str) -> String {
    let mut out: Vec<&str> = Vec::new();
    for segment in name.split('/') {
        match segment {
            "" | "." => {}
            ".." => {
                if out.last().is_some_and(|s| *s != "..") {
                    out.pop();
                } else {
                    out.push("..");
                }
            }
            s => out.push(s),
        }
    }
    if out.is_empty() { ".".into() } else { out.join("/") }
}

/// Go `writeFileAtomic`: a temp file beside the target, synced, then renamed.
fn write_file_atomic(target: &Path, data: &[u8], mode: u32) -> Result<(), String> {
    use std::io::Write as _;
    let dir = target.parent().unwrap_or(Path::new("."));
    // Go `os.MkdirAll(dir, 0o755)`.
    let mut builder = std::fs::DirBuilder::new();
    builder.recursive(true);
    #[cfg(unix)]
    std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o755);
    builder
        .create(dir)
        .map_err(|e| format!("create plugin directory: {}", go_path_error("mkdir", dir, &e)))?;
    let base = target
        .file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_default();
    let (mut temp, temp_path) = create_temp(dir, &base)?;
    let cleanup = |e: String| {
        let _ = std::fs::remove_file(&temp_path);
        e
    };
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt as _;
        temp.set_permissions(std::fs::Permissions::from_mode(mode))
            .map_err(|e| {
                cleanup(format!(
                    "chmod temp plugin file: {}",
                    go_path_error("chmod", &temp_path, &e)
                ))
            })?;
    }
    #[cfg(not(unix))]
    let _ = mode;
    temp.write_all(data).map_err(|e| {
        cleanup(format!(
            "write temp plugin file: {}",
            go_path_error("write", &temp_path, &e)
        ))
    })?;
    temp.sync_all().map_err(|e| {
        cleanup(format!(
            "sync temp plugin file: {}",
            go_path_error("sync", &temp_path, &e)
        ))
    })?;
    drop(temp);
    if let Err(e) = std::fs::rename(&temp_path, target) {
        if cfg!(windows) {
            if let Err(e) = std::fs::remove_file(target)
                && e.kind() != std::io::ErrorKind::NotFound
            {
                return Err(cleanup(format!(
                    "remove old plugin file: {}",
                    go_path_error("remove", target, &e)
                )));
            }
            return std::fs::rename(&temp_path, target).map_err(|e| {
                cleanup(format!(
                    "install plugin file: {}",
                    go_link_error(&temp_path, target, &e)
                ))
            });
        }
        return Err(cleanup(format!(
            "install plugin file: {}",
            go_link_error(&temp_path, target, &e)
        )));
    }
    Ok(())
}

/// `os.CreateTemp(dir, "."+base+".tmp-*")`.
pub(crate) fn create_temp(dir: &Path, base: &str) -> Result<(std::fs::File, PathBuf), String> {
    use std::time::{SystemTime, UNIX_EPOCH};
    let seed = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_nanos())
        .unwrap_or_default();
    for attempt in 0..10_000u128 {
        let suffix = (seed.wrapping_add(attempt * 7919) % 4_294_967_296) as u32;
        let path = dir.join(format!(".{base}.tmp-{suffix}"));
        let mut options = std::fs::OpenOptions::new();
        options.write(true).create_new(true);
        #[cfg(unix)]
        std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600);
        match options.open(&path) {
            Ok(file) => return Ok((file, path)),
            Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue,
            Err(e) => {
                return Err(format!(
                    "create temp plugin file: {}",
                    go_path_error("open", &dir.join(format!(".{base}.tmp-*")), &e)
                ));
            }
        }
    }
    Err("create temp plugin file: too many attempts".into())
}

/// Go's `*PathError` text.
fn go_path_error(op: &str, path: &Path, e: &std::io::Error) -> String {
    format!("{op} {}: {}", path.display(), errno_text(e))
}

/// Go's `*LinkError` text for a rename.
fn go_link_error(from: &Path, to: &Path, e: &std::io::Error) -> String {
    format!("rename {} {}: {}", from.display(), to.display(), errno_text(e))
}

fn errno_text(e: &std::io::Error) -> String {
    let text = e.to_string();
    let text = text.split(" (os error").next().unwrap_or_default();
    let mut chars = text.chars();
    chars
        .next()
        .map_or_else(String::new, |c| c.to_lowercase().chain(chars).collect())
}

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

    #[test]
    fn zip_names_follow_go() {
        assert_eq!(clean_zip_name("a/./b.so"), Ok("a/b.so".into()));
        assert_eq!(clean_zip_name("a/../b.so"), Ok("b.so".into()));
        assert!(clean_zip_name("../b.so").is_err());
        assert!(clean_zip_name("a/../../b.so").is_err());
        assert!(clean_zip_name("/b.so").is_err());
        assert!(clean_zip_name("a\\b.so").is_err());
        assert_eq!(versioned_file_name("p", "v1.2", "darwin"), "p-v1.2.dylib");
    }

    /// A library declaring more than the ceiling is refused before it is inflated; at
    /// the ceiling's side of the check it is read as usual.
    #[test]
    fn library_size_is_bounded() {
        let mut data = crate::testing::stored_zip(&[("p.so", b"lib", 0o755)]);
        assert_eq!(
            read_target_library(&Archive::new(&data).unwrap(), "p", "1.0.0", "linux")
                .unwrap()
                .0,
            b"lib"
        );
        // The central directory's uncompressed size: EOCD offset field, then +24.
        let eocd = data.len() - 22;
        let central = u32::from_le_bytes(data[eocd + 16..eocd + 20].try_into().unwrap()) as usize;
        let declared = (MAX_LIBRARY_BYTES + 1) as u32;
        data[central + 24..central + 28].copy_from_slice(&declared.to_le_bytes());
        let err = read_target_library(&Archive::new(&data).unwrap(), "p", "1.0.0", "linux").unwrap_err();
        assert_eq!(
            err,
            format!("p.so exceeds maximum allowed size of {MAX_LIBRARY_BYTES} bytes")
        );
    }
}