zccache 1.12.16

Local-first compiler cache for C/C++/Rust/Emscripten
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
#![allow(clippy::missing_errors_doc)]

mod artifact;

use std::path::Path;

use crate::cli_core::core::NormalizedPath;
use crate::cli_core::download::{
    canonical_destination, DownloadAttachResult, DownloadDaemonStatus, DownloadOptions,
    DownloadStatus,
};
use crate::cli_core::download_protocol::{Request, Response};

pub use artifact::{
    ArchiveFormat, DownloadSource, FetchRequest, FetchResult, FetchState, FetchStateKind,
    FetchStatus, WaitMode,
};

#[cfg(unix)]
type ClientConn = crate::cli_core::ipc::IpcConnection;
#[cfg(windows)]
type ClientConn = crate::cli_core::ipc::IpcClientConnection;

pub use crate::cli_core::download_protocol::daemon_mgmt::{
    default_endpoint, lock_file_path, read_lock_file_pid, remove_lock_file, write_lock_file,
};

pub fn check_running_daemon() -> Option<u32> {
    let pid = read_lock_file_pid()?;
    // Verify the PID actually points at the download daemon. A bare
    // is_process_alive check would falsely accept a recycled PID inherited
    // from a restored CI cache (see issue #132).
    if crate::cli_core::ipc::verify_pid_exe_stem(pid, "zccache-download-daemon") {
        Some(pid)
    } else {
        remove_lock_file();
        #[cfg(unix)]
        {
            let _ = std::fs::remove_file(default_endpoint());
        }
        None
    }
}

#[cfg(unix)]
async fn connect_client(endpoint: &str) -> Result<ClientConn, crate::cli_core::ipc::IpcError> {
    crate::cli_core::ipc::connect(endpoint).await
}

#[cfg(windows)]
async fn connect_client(endpoint: &str) -> Result<ClientConn, crate::cli_core::ipc::IpcError> {
    crate::cli_core::ipc::connect(endpoint).await
}

fn resolve_endpoint(explicit: Option<&str>) -> String {
    explicit
        .map(ToOwned::to_owned)
        .or_else(|| std::env::var("ZCCACHE_DOWNLOAD_ENDPOINT").ok())
        .unwrap_or_else(default_endpoint)
}

fn run_async<T>(future: impl std::future::Future<Output = Result<T, String>>) -> Result<T, String> {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .map_err(|e| format!("failed to create tokio runtime: {e}"))?
        .block_on(future)
}

fn find_daemon_binary() -> Option<NormalizedPath> {
    let name = if cfg!(windows) {
        "zccache-download-daemon.exe"
    } else {
        "zccache-download-daemon"
    };

    if let Ok(exe) = std::env::current_exe() {
        if let Some(dir) = exe.parent() {
            let candidate = dir.join(name);
            if candidate.exists() {
                return Some(candidate.into());
            }
        }
    }

    which_on_path(name)
}

fn which_on_path(name: &str) -> Option<NormalizedPath> {
    let path_var = std::env::var_os("PATH")?;
    for dir in std::env::split_paths(&path_var) {
        let candidate = dir.join(name);
        if candidate.is_file() {
            return Some(candidate.into());
        }
        #[cfg(windows)]
        if Path::new(name).extension().is_none() {
            let with_exe = dir.join(format!("{name}.exe"));
            if with_exe.is_file() {
                return Some(with_exe.into());
            }
        }
    }
    None
}

fn spawn_daemon(bin: &Path, endpoint: &str) -> Result<(), String> {
    // Issue #982: backstop for the host no-spawn guard (see
    // `core::config::NO_SPAWN_ENV`) — the download daemon is covered
    // by the same contract as the compile daemon.
    if crate::cli_core::core::config::daemon_spawn_disabled() {
        return Err(crate::cli_core::core::config::no_spawn_error(
            "zccache-download-daemon",
        ));
    }
    let mut cmd = std::process::Command::new(bin);
    cmd.args(["--foreground", "--endpoint", endpoint]);
    cmd.stdin(std::process::Stdio::null());
    cmd.stdout(std::process::Stdio::null());
    cmd.stderr(std::process::Stdio::null());
    #[cfg(windows)]
    {
        use std::os::windows::process::CommandExt;
        const CREATE_NO_WINDOW: u32 = 0x0800_0000;
        cmd.creation_flags(CREATE_NO_WINDOW);
    }
    cmd.spawn()
        .map_err(|e| format!("failed to spawn download daemon: {e}"))?;
    Ok(())
}

async fn ensure_daemon(endpoint: &str) -> Result<(), String> {
    if connect_client(endpoint).await.is_ok() {
        return Ok(());
    }
    if let Some(pid) = check_running_daemon() {
        for _ in 0..20 {
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
            if connect_client(endpoint).await.is_ok() {
                return Ok(());
            }
        }
        return Err(format!(
            "download daemon process {pid} exists but is not accepting connections"
        ));
    }
    // Issue #982: refuse before binary resolution so the guard's message
    // wins over "cannot find zccache-download-daemon binary".
    if crate::cli_core::core::config::daemon_spawn_disabled() {
        return Err(crate::cli_core::core::config::no_spawn_error(
            "zccache-download-daemon",
        ));
    }
    let bin = find_daemon_binary().ok_or("cannot find zccache-download-daemon binary")?;
    spawn_daemon(&bin, endpoint)?;
    for _ in 0..100 {
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        if connect_client(endpoint).await.is_ok() {
            return Ok(());
        }
    }
    Err("download daemon started but did not accept connections after 10s".to_string())
}

#[derive(Clone)]
pub struct DownloadClient {
    endpoint: Option<String>,
}

impl DownloadClient {
    #[must_use]
    pub fn new(endpoint: Option<String>) -> Self {
        Self { endpoint }
    }

    #[must_use]
    pub fn resolved_endpoint(&self) -> String {
        resolve_endpoint(self.endpoint.as_deref())
    }

    pub fn start_daemon(&self) -> Result<(), String> {
        let client = self.clone();
        run_async(async move { client.start_daemon_async().await })
    }

    pub fn stop_daemon(&self) -> Result<bool, String> {
        let client = self.clone();
        run_async(async move { client.stop_daemon_async().await })
    }

    pub fn daemon_status(&self) -> Result<DownloadDaemonStatus, String> {
        let client = self.clone();
        run_async(async move { client.daemon_status_async().await })
    }

    pub async fn start_daemon_async(&self) -> Result<(), String> {
        let endpoint = self.resolved_endpoint();
        ensure_daemon(&endpoint).await
    }

    pub async fn stop_daemon_async(&self) -> Result<bool, String> {
        let endpoint = self.resolved_endpoint();
        let mut conn = match connect_client(&endpoint).await {
            Ok(conn) => conn,
            Err(_) => return Ok(false),
        };
        conn.send(&Request::Shutdown)
            .await
            .map_err(|e| format!("failed to send shutdown to download daemon: {e}"))?;
        match conn.recv::<Response>().await {
            Ok(Some(Response::ShuttingDown)) => Ok(true),
            Ok(Some(Response::Error { message })) => Err(message),
            Ok(Some(other)) => Err(format!("unexpected response: {other:?}")),
            Ok(None) => Err("download daemon closed connection unexpectedly".to_string()),
            Err(e) => Err(format!("broken connection to download daemon: {e}")),
        }
    }

    pub async fn daemon_status_async(&self) -> Result<DownloadDaemonStatus, String> {
        let endpoint = self.resolved_endpoint();
        let mut conn = connect_client(&endpoint)
            .await
            .map_err(|e| format!("download daemon not running at {endpoint}: {e}"))?;
        conn.send(&Request::Status)
            .await
            .map_err(|e| format!("failed to query download daemon: {e}"))?;
        match conn.recv::<Response>().await {
            Ok(Some(Response::Status(status))) => Ok(status),
            Ok(Some(Response::Error { message })) => Err(message),
            Ok(Some(other)) => Err(format!("unexpected response: {other:?}")),
            Ok(None) => Err("download daemon closed connection unexpectedly".to_string()),
            Err(e) => Err(format!("broken connection to download daemon: {e}")),
        }
    }

    pub fn download(
        &self,
        url: &str,
        destination: &Path,
        options: DownloadOptions,
    ) -> Result<DownloadHandle, String> {
        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .map_err(|e| format!("failed to create runtime: {e}"))?;
        let async_handle = runtime.block_on(self.download_async(url, destination, options))?;
        Ok(DownloadHandle {
            runtime,
            conn: async_handle.conn,
            initiator: async_handle.initiator,
            download_id: async_handle.download_id,
        })
    }

    pub async fn download_async(
        &self,
        url: &str,
        destination: &Path,
        options: DownloadOptions,
    ) -> Result<AsyncDownloadHandle, String> {
        let endpoint = self.resolved_endpoint();
        let url = url.to_string();
        let destination = canonical_destination(destination).map_err(|e| e.to_string())?;
        ensure_daemon(&endpoint).await?;
        let mut conn = connect_client(&endpoint)
            .await
            .map_err(|e| format!("cannot connect to download daemon at {endpoint}: {e}"))?;
        conn.send(&Request::DownloadAttach {
            url: url.clone(),
            destination,
            options,
        })
        .await
        .map_err(|e| format!("failed to send attach request: {e}"))?;
        match conn.recv::<Response>().await {
            Ok(Some(Response::DownloadAttached {
                download_id,
                initiator,
                status: _,
            })) => Ok(AsyncDownloadHandle {
                conn,
                initiator,
                download_id,
            }),
            Ok(Some(Response::Error { message })) => Err(message),
            Ok(Some(other)) => Err(format!("unexpected response: {other:?}")),
            Ok(None) => Err("download daemon closed connection unexpectedly".to_string()),
            Err(e) => Err(format!("broken connection to download daemon: {e}")),
        }
    }
}

pub struct AsyncDownloadHandle {
    conn: ClientConn,
    initiator: bool,
    download_id: String,
}

impl AsyncDownloadHandle {
    #[must_use]
    pub fn initiator(&self) -> bool {
        self.initiator
    }

    #[must_use]
    pub fn download_id(&self) -> &str {
        &self.download_id
    }

    pub async fn status(&mut self) -> Result<DownloadStatus, String> {
        send_download_command(&mut self.conn, Request::DownloadStatus).await
    }

    pub async fn wait(&mut self, timeout_ms: Option<u64>) -> Result<DownloadStatus, String> {
        send_download_command(&mut self.conn, Request::DownloadWait { timeout_ms }).await
    }

    pub async fn cancel(&mut self) -> Result<DownloadStatus, String> {
        send_download_command(&mut self.conn, Request::DownloadCancel).await
    }

    pub fn close(self) -> Result<(), String> {
        Ok(())
    }
}

pub struct DownloadHandle {
    runtime: tokio::runtime::Runtime,
    conn: ClientConn,
    initiator: bool,
    download_id: String,
}

impl DownloadHandle {
    #[must_use]
    pub fn initiator(&self) -> bool {
        self.initiator
    }

    #[must_use]
    pub fn download_id(&self) -> &str {
        &self.download_id
    }

    pub fn status(&mut self) -> Result<DownloadStatus, String> {
        self.runtime.block_on(send_download_command(
            &mut self.conn,
            Request::DownloadStatus,
        ))
    }

    pub fn wait(&mut self, timeout_ms: Option<u64>) -> Result<DownloadStatus, String> {
        self.runtime.block_on(send_download_command(
            &mut self.conn,
            Request::DownloadWait { timeout_ms },
        ))
    }

    pub fn cancel(&mut self) -> Result<DownloadStatus, String> {
        self.runtime.block_on(send_download_command(
            &mut self.conn,
            Request::DownloadCancel,
        ))
    }

    pub fn close(self) -> Result<(), String> {
        Ok(())
    }
}

async fn send_download_command(
    conn: &mut ClientConn,
    request: Request,
) -> Result<DownloadStatus, String> {
    let action = match &request {
        Request::DownloadStatus => "status",
        Request::DownloadWait { .. } => "wait",
        Request::DownloadCancel => "cancel",
        _ => "download",
    };
    conn.send(&request)
        .await
        .map_err(|e| format!("failed to send {action} request: {e}"))?;
    match conn.recv::<Response>().await {
        Ok(Some(Response::DownloadStatusResult { status })) => Ok(status),
        Ok(Some(Response::DownloadFinished { status })) => Ok(status),
        Ok(Some(Response::DownloadCancelled { status })) => Ok(status),
        Ok(Some(Response::Error { message })) => Err(message),
        Ok(Some(other)) => Err(format!("unexpected response: {other:?}")),
        Ok(None) => Err("download daemon closed connection unexpectedly".to_string()),
        Err(e) => Err(format!("broken connection to download daemon: {e}")),
    }
}

pub fn is_terminal(status: &DownloadStatus) -> bool {
    matches!(
        status.phase,
        crate::cli_core::download::DownloadPhase::Completed
            | crate::cli_core::download::DownloadPhase::Cancelled
            | crate::cli_core::download::DownloadPhase::Failed
    )
}

pub fn coerce_attach_result(
    download_id: String,
    initiator: bool,
    status: DownloadStatus,
) -> DownloadAttachResult {
    DownloadAttachResult {
        download_id,
        initiator,
        status,
    }
}