zccache-cli 1.4.3

Command-line interface for the zccache compiler cache
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
use std::fs;
use std::io::{Read, Write};
use std::net::{Shutdown, TcpListener, TcpStream};
use std::path::Path;
use std::process::Command;
use std::sync::{
    atomic::{AtomicBool, Ordering},
    Arc, Mutex, OnceLock,
};
use std::thread::{self, JoinHandle};

use tempfile::TempDir;
use zccache_core::NormalizedPath;

const VERSION: &str = "1.2.3";

fn installer_test_lock() -> &'static Mutex<()> {
    static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
    LOCK.get_or_init(|| Mutex::new(()))
}

fn workspace_root() -> NormalizedPath {
    NormalizedPath::new(
        Path::new(env!("CARGO_MANIFEST_DIR"))
            .parent()
            .expect("crate dir")
            .parent()
            .expect("workspace root"),
    )
}

struct TestServer {
    addr: String,
    stop: Arc<AtomicBool>,
    handle: Option<JoinHandle<()>>,
}

impl TestServer {
    fn start(root: NormalizedPath) -> Self {
        let listener = TcpListener::bind("127.0.0.1:0").expect("bind test server");
        listener
            .set_nonblocking(true)
            .expect("set test server nonblocking");
        let addr = format!("http://{}", listener.local_addr().expect("server addr"));
        let stop = Arc::new(AtomicBool::new(false));
        let stop_flag = Arc::clone(&stop);

        let handle = thread::spawn(move || {
            while !stop_flag.load(Ordering::Relaxed) {
                match listener.accept() {
                    Ok((stream, _)) => serve_connection(stream, &root),
                    Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => {
                        thread::sleep(std::time::Duration::from_millis(20));
                    }
                    Err(_) => break,
                }
            }
        });

        Self {
            addr,
            stop,
            handle: Some(handle),
        }
    }
}

impl Drop for TestServer {
    fn drop(&mut self) {
        self.stop.store(true, Ordering::Relaxed);
        if let Ok(addr) = self
            .addr
            .strip_prefix("http://")
            .unwrap_or(&self.addr)
            .parse::<std::net::SocketAddr>()
        {
            let _ = TcpStream::connect(addr).and_then(|stream| stream.shutdown(Shutdown::Both));
        }
        if let Some(handle) = self.handle.take() {
            let _ = handle.join();
        }
    }
}

fn serve_connection(mut stream: TcpStream, root: &NormalizedPath) {
    let mut buf = [0_u8; 4096];
    let Ok(size) = stream.read(&mut buf) else {
        return;
    };
    if size == 0 {
        return;
    }
    let request = String::from_utf8_lossy(&buf[..size]);
    let Some(line) = request.lines().next() else {
        return;
    };
    let mut parts = line.split_whitespace();
    let method = parts.next().unwrap_or_default();
    let path = parts.next().unwrap_or("/");
    if method != "GET" {
        let _ = write_response(&mut stream, 405, b"method not allowed");
        return;
    }

    let rel = path
        .trim_start_matches('/')
        .replace('/', std::path::MAIN_SEPARATOR_STR);
    let file_path = root.join(rel);
    match fs::read(file_path) {
        Ok(bytes) => {
            let _ = write_response(&mut stream, 200, &bytes);
        }
        Err(_) => {
            let _ = write_response(&mut stream, 404, b"not found");
        }
    }
}

fn write_response(stream: &mut TcpStream, status: u16, body: &[u8]) -> std::io::Result<()> {
    let status_text = match status {
        200 => "OK",
        404 => "Not Found",
        405 => "Method Not Allowed",
        _ => "Error",
    };
    write!(
        stream,
        "HTTP/1.1 {status} {status_text}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
        body.len()
    )?;
    stream.write_all(body)
}

#[cfg(unix)]
fn make_fake_unix_archive(root: &Path, target: &str) -> NormalizedPath {
    let tag = format!("v{VERSION}");
    let asset_dir = root.join("download").join(&tag);
    let archive_root = asset_dir.join(format!("zccache-{tag}-{target}"));
    fs::create_dir_all(&archive_root).expect("create archive root");
    write_unix_binary(&archive_root.join("zccache"), "zccache");
    write_unix_binary(&archive_root.join("zccache-daemon"), "zccache-daemon");
    write_unix_binary(&archive_root.join("zccache-fp"), "zccache-fp");
    fs::write(archive_root.join("README.md"), "test archive\n").expect("write readme");

    let archive = asset_dir.join(format!("zccache-{tag}-{target}.tar.gz"));
    let status = Command::new("tar")
        .args([
            "-czf",
            archive.to_str().expect("archive path"),
            "-C",
            asset_dir.to_str().expect("asset dir"),
            &format!("zccache-{tag}-{target}"),
        ])
        .status()
        .expect("run tar");
    assert!(status.success(), "tar failed with {status}");
    NormalizedPath::new(archive)
}

#[cfg(unix)]
fn write_unix_binary(path: &Path, name: &str) {
    let script = format!(
        "#!/bin/sh\nif [ \"$1\" = \"--version\" ]; then\n  echo '{name} {VERSION}'\n  exit 0\nfi\necho '{name} invoked'\n"
    );
    fs::write(path, script).expect("write fake unix binary");
    let mut perms = fs::metadata(path).expect("unix metadata").permissions();
    use std::os::unix::fs::PermissionsExt;
    perms.set_mode(0o755);
    fs::set_permissions(path, perms).expect("chmod fake unix binary");
}

#[cfg(windows)]
fn make_fake_windows_archive(root: &Path, target: &str) -> NormalizedPath {
    let tag = format!("v{VERSION}");
    let asset_dir = root.join("download").join(&tag);
    let archive_root = asset_dir.join(format!("zccache-{tag}-{target}"));
    fs::create_dir_all(&archive_root).expect("create archive root");
    write_windows_binary(&archive_root.join("zccache.exe"), "zccache");
    write_windows_binary(&archive_root.join("zccache-daemon.exe"), "zccache-daemon");
    write_windows_binary(&archive_root.join("zccache-fp.exe"), "zccache-fp");
    fs::write(archive_root.join("README.md"), "test archive\r\n").expect("write readme");

    let archive = asset_dir.join(format!("zccache-{tag}-{target}.zip"));
    write_zip_archive(&archive, &asset_dir, &archive_root);
    NormalizedPath::new(archive)
}

#[cfg(windows)]
fn write_windows_binary(path: &Path, name: &str) {
    let source = format!(
        "using System; class Program {{ static int Main(string[] args) {{ if (args.Length > 0 && args[0] == \"--version\") {{ Console.WriteLine(\"{name} {VERSION}\"); return 0; }} Console.WriteLine(\"{name} invoked\"); return 0; }} }}"
    );
    let command = format!(
        "$src = @'\n{source}\n'@; Add-Type -TypeDefinition $src -OutputAssembly '{}' -OutputType ConsoleApplication",
        path.display()
    );
    let status = Command::new("powershell")
        .args(["-NoProfile", "-Command", &command])
        .status()
        .expect("run Add-Type");
    assert!(status.success(), "Add-Type failed with {status}");
}

#[cfg(windows)]
fn write_zip_archive(archive: &Path, base_dir: &Path, root_dir: &Path) {
    use std::fs::File;

    use zip::write::SimpleFileOptions;
    use zip::CompressionMethod;
    use zip::ZipWriter;

    if archive.exists() {
        fs::remove_file(archive).expect("remove existing archive");
    }

    let file = File::create(archive).expect("create zip archive");
    let mut zip = ZipWriter::new(file);
    let options = SimpleFileOptions::default().compression_method(CompressionMethod::Deflated);

    add_directory_to_zip(&mut zip, base_dir, root_dir, options);
    zip.finish().expect("finish zip archive");
}

#[cfg(windows)]
fn add_directory_to_zip(
    zip: &mut zip::ZipWriter<std::fs::File>,
    base_dir: &Path,
    dir: &Path,
    options: zip::write::SimpleFileOptions,
) {
    let dir_name = dir
        .strip_prefix(base_dir)
        .expect("directory inside base")
        .to_string_lossy()
        .replace('\\', "/");
    zip.add_directory(format!("{dir_name}/"), options)
        .expect("add zip directory");

    let mut entries = fs::read_dir(dir)
        .expect("read zip source dir")
        .collect::<Result<Vec<_>, _>>()
        .expect("collect zip source dir");
    entries.sort_by_key(|entry| entry.file_name());

    for entry in entries {
        let path = entry.path();
        if path.is_dir() {
            add_directory_to_zip(zip, base_dir, &path, options);
            continue;
        }

        let name = path
            .strip_prefix(base_dir)
            .expect("file inside base")
            .to_string_lossy()
            .replace('\\', "/");
        zip.start_file(name, options).expect("start zip file");

        let bytes = fs::read(&path).expect("read zip file bytes");
        zip.write_all(&bytes).expect("write zip file bytes");
    }
}

#[cfg(unix)]
#[test]
fn install_sh_installs_release_archive() {
    let _guard = installer_test_lock().lock().expect("installer test lock");
    let temp = TempDir::new().expect("tempdir");
    let release_root = temp.path().join("release");
    fs::create_dir_all(&release_root).expect("create release root");
    let target = match (std::env::consts::OS, std::env::consts::ARCH) {
        ("linux", "x86_64") => "x86_64-unknown-linux-musl",
        ("linux", "aarch64") => "aarch64-unknown-linux-musl",
        ("macos", "x86_64") => "x86_64-apple-darwin",
        ("macos", "aarch64") => "aarch64-apple-darwin",
        other => panic!("unsupported test target: {other:?}"),
    };
    let _archive = make_fake_unix_archive(&release_root, target);
    let server = TestServer::start(release_root.into());

    let home = temp.path().join("home");
    let install_dir = temp.path().join("bin");
    fs::create_dir_all(&home).expect("create home");

    let status = Command::new("sh")
        .arg(workspace_root().join("install.sh"))
        .args(["--bin-dir", install_dir.to_str().expect("install dir")])
        .env("HOME", &home)
        .env("SHELL", "/bin/bash")
        .env(
            "ZCCACHE_INSTALL_BASE_URL",
            format!("{}/download-placeholder", server.addr).replace("/download-placeholder", ""),
        )
        .env("ZCCACHE_INSTALL_VERSION", VERSION)
        .status()
        .expect("run install.sh");
    assert!(status.success(), "install.sh failed with {status}");

    let version = Command::new(install_dir.join("zccache"))
        .arg("--version")
        .output()
        .expect("run installed zccache");
    assert!(version.status.success(), "installed zccache failed");
    assert_eq!(
        String::from_utf8_lossy(&version.stdout).trim(),
        format!("zccache {VERSION}")
    );

    let profile = fs::read_to_string(home.join(".profile")).expect("read profile");
    assert!(
        profile.contains(install_dir.to_str().expect("install dir")),
        "profile missing install path"
    );
    assert!(
        install_dir.join("zccache-daemon").exists(),
        "daemon not installed"
    );
    assert!(
        install_dir.join("zccache-fp").exists(),
        "fingerprint tool not installed"
    );
}

#[cfg(unix)]
#[test]
fn install_sh_supports_global_mode() {
    let _guard = installer_test_lock().lock().expect("installer test lock");
    let temp = TempDir::new().expect("tempdir");
    let release_root = temp.path().join("release");
    fs::create_dir_all(&release_root).expect("create release root");
    let target = match (std::env::consts::OS, std::env::consts::ARCH) {
        ("linux", "x86_64") => "x86_64-unknown-linux-musl",
        ("linux", "aarch64") => "aarch64-unknown-linux-musl",
        ("macos", "x86_64") => "x86_64-apple-darwin",
        ("macos", "aarch64") => "aarch64-apple-darwin",
        other => panic!("unsupported test target: {other:?}"),
    };
    let _archive = make_fake_unix_archive(&release_root, target);
    let server = TestServer::start(release_root.into());

    let home = temp.path().join("home");
    let install_dir = temp.path().join("global-bin");
    fs::create_dir_all(&home).expect("create home");

    let status = Command::new("sh")
        .arg(workspace_root().join("install.sh"))
        .args([
            "--global",
            "--bin-dir",
            install_dir.to_str().expect("install dir"),
        ])
        .env("HOME", &home)
        .env("SHELL", "/bin/bash")
        .env("ZCCACHE_INSTALL_BASE_URL", &server.addr)
        .env("ZCCACHE_INSTALL_VERSION", VERSION)
        .status()
        .expect("run install.sh");
    assert!(status.success(), "install.sh failed with {status}");
    assert!(
        install_dir.join("zccache").exists(),
        "zccache not installed"
    );
    assert!(
        !home.join(".profile").exists(),
        "global install should not edit user profile"
    );
}

#[cfg(windows)]
#[test]
fn install_ps1_installs_release_archive() {
    let _guard = installer_test_lock().lock().expect("installer test lock");
    let temp = TempDir::new().expect("tempdir");
    let release_root = temp.path().join("release");
    fs::create_dir_all(&release_root).expect("create release root");
    let target = match std::env::consts::ARCH {
        "x86_64" => "x86_64-pc-windows-msvc",
        "aarch64" => "aarch64-pc-windows-msvc",
        other => panic!("unsupported test arch: {other}"),
    };
    let _archive = make_fake_windows_archive(&release_root, target);
    let server = TestServer::start(release_root.into());

    let home = temp.path().join("home");
    let install_dir = temp.path().join("bin");
    fs::create_dir_all(&home).expect("create home");

    let status = Command::new("powershell")
        .args([
            "-NoProfile",
            "-ExecutionPolicy",
            "Bypass",
            "-File",
            workspace_root()
                .join("install.ps1")
                .to_str()
                .expect("install.ps1 path"),
            "-BinDir",
            install_dir.to_str().expect("install dir"),
            "-Version",
            VERSION,
        ])
        .env("HOME", &home)
        .env("USERPROFILE", &home)
        .env(
            "ZCCACHE_INSTALL_BASE_URL",
            format!("{}/download-placeholder", server.addr).replace("/download-placeholder", ""),
        )
        .env("ZCCACHE_NO_MODIFY_PATH", "1")
        .status()
        .expect("run install.ps1");
    assert!(status.success(), "install.ps1 failed with {status}");

    let version = Command::new(install_dir.join("zccache.exe"))
        .arg("--version")
        .output()
        .expect("run installed zccache.exe");
    assert!(version.status.success(), "installed zccache.exe failed");
    assert_eq!(
        String::from_utf8_lossy(&version.stdout).trim(),
        format!("zccache {VERSION}")
    );
    assert!(
        install_dir.join("zccache-daemon.exe").exists(),
        "daemon not installed"
    );
    assert!(
        install_dir.join("zccache-fp.exe").exists(),
        "fingerprint tool not installed"
    );
}

#[cfg(windows)]
#[test]
fn install_ps1_supports_global_mode() {
    let _guard = installer_test_lock().lock().expect("installer test lock");
    let temp = TempDir::new().expect("tempdir");
    let release_root = temp.path().join("release");
    fs::create_dir_all(&release_root).expect("create release root");
    let target = match std::env::consts::ARCH {
        "x86_64" => "x86_64-pc-windows-msvc",
        "aarch64" => "aarch64-pc-windows-msvc",
        other => panic!("unsupported test arch: {other}"),
    };
    let _archive = make_fake_windows_archive(&release_root, target);
    let server = TestServer::start(release_root.into());

    let install_dir = temp.path().join("global-bin");

    let status = Command::new("powershell")
        .args([
            "-NoProfile",
            "-ExecutionPolicy",
            "Bypass",
            "-File",
            workspace_root()
                .join("install.ps1")
                .to_str()
                .expect("install.ps1 path"),
            "-Global",
            "-BinDir",
            install_dir.to_str().expect("install dir"),
            "-Version",
            VERSION,
        ])
        .env("ZCCACHE_INSTALL_BASE_URL", &server.addr)
        .env("ZCCACHE_NO_MODIFY_PATH", "1")
        .status()
        .expect("run install.ps1");
    assert!(status.success(), "install.ps1 failed with {status}");
    assert!(
        install_dir.join("zccache.exe").exists(),
        "zccache.exe not installed"
    );
}