rsconstruct 0.9.177

Rust based fast build system
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
//! Remote cache support for sharing build artifacts across machines.
//!
//! Supported backends:
//! - `s3://bucket/prefix` - Amazon S3 (requires AWS credentials)
//! - `http://host:port/path` or `https://...` - HTTP server with GET/PUT support
//! - `file:///absolute/path` - Local filesystem (for testing or network mounts)

use anyhow::{Context, Result};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

use crate::errors;
use crate::processor::{check_command_output, run_command_capture};

/// Remote cache backend trait
pub trait RemoteCache: Send + Sync {
    /// Check if an object exists in the remote cache
    fn exists(&self, ctx: &crate::build_context::BuildContext, key: &str) -> Result<bool>;

    /// Upload a local file to remote cache
    fn upload(&self, ctx: &crate::build_context::BuildContext, key: &str, src: &Path)
    -> Result<()>;

    /// Download raw bytes (for index entries)
    fn download_bytes(
        &self,
        ctx: &crate::build_context::BuildContext,
        key: &str,
    ) -> Result<Option<Vec<u8>>>;

    /// Upload raw bytes (for index entries).
    /// Default implementation writes to a temp file and delegates to `upload()`.
    fn upload_bytes(
        &self,
        ctx: &crate::build_context::BuildContext,
        key: &str,
        data: &[u8],
    ) -> Result<()> {
        use std::io::Write;
        let temp_dir = std::env::temp_dir();
        let temp_file = temp_dir.join(format!("rsconstruct-upload-{}", uuid_simple()));
        // create_new refuses to follow a pre-planted symlink or reuse an
        // existing file in the shared temp directory.
        let mut file = fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&temp_file)
            .with_context(|| {
                format!("Failed to create temp upload file: {}", temp_file.display())
            })?;
        file.write_all(data).with_context(|| {
            format!("Failed to write temp upload file: {}", temp_file.display())
        })?;
        drop(file);
        let result = self.upload(ctx, key, &temp_file);
        let _ = fs::remove_file(&temp_file);
        result
    }
}

/// Parse a remote URL and create the appropriate backend
pub fn create_backend(url: &str) -> Result<Box<dyn RemoteCache>> {
    if url.starts_with("s3://") {
        Ok(Box::new(S3Backend::new(url)?))
    } else if url.starts_with("http://") || url.starts_with("https://") {
        Ok(Box::new(HttpBackend::new(url)))
    } else if url.starts_with("file://") {
        Ok(Box::new(FileBackend::new(url)?))
    } else {
        anyhow::bail!(
            "Unsupported remote cache URL: {url}. Supported schemes: s3://, http://, https://, file://"
        )
    }
}

/// S3 backend using AWS CLI
pub struct S3Backend {
    bucket: String,
    prefix: String,
}

impl S3Backend {
    pub fn new(url: &str) -> Result<Self> {
        // Parse s3://bucket/prefix
        let without_scheme = url.strip_prefix("s3://").context("Invalid S3 URL")?;

        let (bucket, prefix) = match without_scheme.find('/') {
            Some(idx) => {
                let (b, p) = without_scheme.split_at(idx);
                (b.to_string(), p[1..].to_string()) // Skip the leading '/'
            }
            None => (without_scheme.to_string(), String::new()),
        };

        anyhow::ensure!(
            !bucket.is_empty(),
            "Invalid S3 URL: missing bucket name in {url}"
        );

        Ok(Self { bucket, prefix })
    }

    fn s3_key(&self, key: &str) -> String {
        if self.prefix.is_empty() {
            key.to_string()
        } else {
            format!("{}/{}", self.prefix.trim_end_matches('/'), key)
        }
    }

    fn s3_uri(&self, key: &str) -> String {
        format!("s3://{}/{}", self.bucket, self.s3_key(key))
    }
}

/// What an `aws` exit says about the object the command addressed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum S3Outcome {
    /// The command succeeded: the object is there.
    Present,
    /// The command failed because the key does not exist: a cache miss.
    Absent,
    /// The command failed for any other reason — expired or missing
    /// credentials, `AccessDenied`, no such bucket, no network. Never a miss:
    /// collapsing these to "absent" silently degrades every build to a full
    /// rebuild with zero diagnostics, so the caller must see them.
    Failed,
}

/// Classify an `aws` exit by its status and stderr. The CLI reports the
/// S3 error code in parentheses — `An error occurred (404) when calling the
/// HeadObject operation: Not Found` from `s3api head-object`, `fatal error:
/// An error occurred (404) when calling the HeadObject operation: Key "x"
/// does not exist` from `s3 cp`, and `(NoSuchKey)` from a `GetObject` —
/// and only those two codes mean the key is missing. Everything else
/// (`(403)`, `(AccessDenied)`, `(ExpiredToken)`, `Unable to locate
/// credentials`, `Could not connect to the endpoint URL`) is a failure.
fn s3_outcome(success: bool, stderr: &str) -> S3Outcome {
    if success {
        S3Outcome::Present
    } else if stderr.contains("(404)") || stderr.contains("(NoSuchKey)") {
        S3Outcome::Absent
    } else {
        S3Outcome::Failed
    }
}

/// The error for an `aws` command that failed for a reason other than a
/// missing key, carrying the CLI's own diagnosis.
fn s3_failure(what: &str, uri: &str, output: &std::process::Output) -> anyhow::Error {
    let stderr = String::from_utf8_lossy(&output.stderr);
    anyhow::anyhow!(
        "{what} failed for {uri} (aws exit {:?}): {}",
        output.status.code(),
        stderr.trim()
    )
}

impl RemoteCache for S3Backend {
    fn exists(&self, ctx: &crate::build_context::BuildContext, key: &str) -> Result<bool> {
        // `s3api head-object`, not `s3 ls`: `ls` matches by prefix, so it
        // reported `objects/ab/cd` as present when only `objects/ab/cdef`
        // existed, and it exits 1 for a missing key the same way it exits
        // for a denied one.
        let mut cmd = Command::new("aws");
        cmd.args([
            "s3api",
            "head-object",
            "--bucket",
            &self.bucket,
            "--key",
            &self.s3_key(key),
        ]);
        let output = run_command_capture(ctx, &cmd)?;
        match s3_outcome(
            output.status.success(),
            &String::from_utf8_lossy(&output.stderr),
        ) {
            S3Outcome::Present => Ok(true),
            S3Outcome::Absent => Ok(false),
            S3Outcome::Failed => Err(s3_failure("S3 head-object", &self.s3_uri(key), &output)),
        }
    }

    fn upload(
        &self,
        ctx: &crate::build_context::BuildContext,
        key: &str,
        src: &Path,
    ) -> Result<()> {
        let mut cmd = Command::new("aws");
        cmd.args([
            "s3",
            "cp",
            &src.display().to_string(),
            &self.s3_uri(key),
            "--only-show-errors",
        ]);
        let output = run_command_capture(ctx, &cmd)?;
        check_command_output(&output, "S3 upload")
    }

    fn download_bytes(
        &self,
        ctx: &crate::build_context::BuildContext,
        key: &str,
    ) -> Result<Option<Vec<u8>>> {
        let mut cmd = Command::new("aws");
        cmd.args(["s3", "cp", &self.s3_uri(key), "-"]);
        let output = run_command_capture(ctx, &cmd)?;
        match s3_outcome(
            output.status.success(),
            &String::from_utf8_lossy(&output.stderr),
        ) {
            S3Outcome::Present => Ok(Some(output.stdout)),
            S3Outcome::Absent => Ok(None),
            S3Outcome::Failed => Err(s3_failure("S3 download", &self.s3_uri(key), &output)),
        }
    }
}

/// HTTP backend using curl
pub struct HttpBackend {
    base_url: String,
}

impl HttpBackend {
    pub fn new(url: &str) -> Self {
        Self {
            base_url: url.trim_end_matches('/').to_string(),
        }
    }

    fn full_url(&self, key: &str) -> String {
        format!("{}/{}", self.base_url, key.trim_start_matches('/'))
    }
}

impl RemoteCache for HttpBackend {
    fn exists(&self, ctx: &crate::build_context::BuildContext, key: &str) -> Result<bool> {
        let mut cmd = Command::new("curl");
        crate::download::apply_retry_args(&mut cmd);
        cmd.args([
            "-s",
            "-o",
            "/dev/null",
            "-w",
            "%{http_code}",
            "--head",
            &self.full_url(key),
        ]);
        let output = run_command_capture(ctx, &cmd)?;
        if !output.status.success() {
            // Transport failure (DNS, connection refused, timeout) must be
            // an error, not "absent" — collapsing them silently degrades
            // every machine to full rebuilds with zero diagnostics.
            anyhow::bail!(
                "HTTP cache unreachable for {} (curl exit {:?})",
                self.full_url(key),
                output.status.code()
            );
        }
        match String::from_utf8_lossy(&output.stdout).trim() {
            "200" => Ok(true),
            "404" | "410" => Ok(false),
            other => anyhow::bail!(
                "HTTP cache returned status {other} for {}",
                self.full_url(key)
            ),
        }
    }

    fn upload(
        &self,
        ctx: &crate::build_context::BuildContext,
        key: &str,
        src: &Path,
    ) -> Result<()> {
        let mut cmd = Command::new("curl");
        crate::download::apply_retry_args(&mut cmd);
        cmd.args([
            "-s",
            "-f",
            "-X",
            "PUT",
            "--data-binary",
            &format!("@{}", src.display()),
            &self.full_url(key),
        ]);
        let output = run_command_capture(ctx, &cmd)?;
        check_command_output(&output, "HTTP upload")
    }

    fn download_bytes(
        &self,
        ctx: &crate::build_context::BuildContext,
        key: &str,
    ) -> Result<Option<Vec<u8>>> {
        // Body goes to a temp file so stdout carries only the status code —
        // that is what lets a 404 (a normal cache miss) be distinguished
        // from a transport failure (an error the caller must see).
        let url = self.full_url(key);
        let tmp = std::env::temp_dir().join(format!("rsconstruct-download-{}", uuid_simple()));
        let mut cmd = Command::new("curl");
        crate::download::apply_retry_args(&mut cmd);
        cmd.args(["-s", "-o"])
            .arg(&tmp)
            .args(["-w", "%{http_code}", &url]);
        let output = run_command_capture(ctx, &cmd);
        let result = (|| {
            let output = output?;
            if !output.status.success() {
                anyhow::bail!(
                    "HTTP cache unreachable for {url} (curl exit {:?})",
                    output.status.code()
                );
            }
            match String::from_utf8_lossy(&output.stdout).trim() {
                "200" => {
                    let data = fs::read(&tmp).with_context(|| {
                        format!("Failed to read downloaded cache entry: {}", tmp.display())
                    })?;
                    Ok(Some(data))
                }
                "404" | "410" => Ok(None),
                other => anyhow::bail!("HTTP cache returned status {other} for {url}"),
            }
        })();
        let _ = fs::remove_file(&tmp);
        result
    }
}

/// File backend for local/network filesystem
pub struct FileBackend {
    base_path: PathBuf,
}

impl FileBackend {
    pub fn new(url: &str) -> Result<Self> {
        // Parse file:///path
        let path = url.strip_prefix("file://").context("Invalid file:// URL")?;

        let base_path = PathBuf::from(path);

        // Create base directory if it doesn't exist
        fs::create_dir_all(&base_path).with_context(|| {
            format!(
                "Failed to create remote cache directory: {}",
                base_path.display()
            )
        })?;

        Ok(Self { base_path })
    }

    fn full_path(&self, key: &str) -> PathBuf {
        self.base_path.join(key)
    }
}

impl RemoteCache for FileBackend {
    fn exists(&self, _ctx: &crate::build_context::BuildContext, key: &str) -> Result<bool> {
        Ok(self.full_path(key).exists())
    }

    fn upload(
        &self,
        _ctx: &crate::build_context::BuildContext,
        key: &str,
        src: &Path,
    ) -> Result<()> {
        let dest = self.full_path(key);

        let parent = dest.parent().with_context(|| {
            format!(
                "Remote cache key has no parent directory: {}",
                dest.display()
            )
        })?;
        fs::create_dir_all(parent).with_context(|| {
            format!(
                "Failed to create directory for local cache upload: {}",
                parent.display()
            )
        })?;

        // Copy to a unique temp name and rename into place: the shared mount
        // is read by other machines, which must never observe a half-written
        // object; racing uploaders of the same key harmlessly overwrite.
        let tmp = parent.join(format!(".tmp-upload-{}", uuid_simple()));
        fs::copy(src, &tmp)
            .with_context(|| format!("Failed to copy to remote cache: {}", tmp.display()))?;

        // Make read-only to prevent corruption, consistent with local cache objects
        crate::platform::set_permissions_mode(&tmp, 0o444).with_context(|| {
            format!(
                "Failed to set remote cache object read-only: {}",
                tmp.display()
            )
        })?;

        if let Err(e) = fs::rename(&tmp, &dest) {
            let _ = fs::remove_file(&tmp);
            if !dest.exists() {
                return Err(e).with_context(|| {
                    format!(
                        "Failed to move remote cache object into place: {}",
                        dest.display()
                    )
                });
            }
        }

        Ok(())
    }

    fn download_bytes(
        &self,
        _ctx: &crate::build_context::BuildContext,
        key: &str,
    ) -> Result<Option<Vec<u8>>> {
        let path = self.full_path(key);
        if !path.exists() {
            return Ok(None);
        }

        let data = fs::read(&path)
            .with_context(|| format!("Failed to read from remote cache: {}", path.display()))?;

        Ok(Some(data))
    }

    // upload_bytes deliberately NOT overridden: the trait default writes a
    // temp file and delegates to `upload()`, whose copy → chmod → rename
    // discipline is what keeps other machines reading the shared mount from
    // ever observing a half-written entry. A previous override here did a
    // bare `fs::write` at the final key — torn reads for consumers, and a
    // hard EACCES failure on every re-push over the 0o444 file.
}

/// Generate a simple unique identifier (timestamp + pid + counter)
fn uuid_simple() -> String {
    use std::sync::atomic::{AtomicU64, Ordering};
    use std::time::{SystemTime, UNIX_EPOCH};

    static COUNTER: AtomicU64 = AtomicU64::new(0);

    let timestamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect(errors::SYSTEM_CLOCK)
        .as_nanos();
    let pid = std::process::id();
    // Relaxed is fine: this counter only needs to be unique within a process,
    // not synchronized with other memory operations.
    let seq = COUNTER.fetch_add(1, Ordering::Relaxed);

    format!("{timestamp:x}-{pid:x}-{seq:x}")
}

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

    #[test]
    fn test_file_backend() {
        let temp_dir = TempDir::new().expect("failed to create temp dir");
        let url = format!("file://{}", temp_dir.path().display());
        let backend = FileBackend::new(&url).expect("failed to create file backend");
        let ctx = crate::build_context::BuildContext::new();

        // Test upload and download bytes
        let key = "test/data.txt";
        let data = b"hello world";

        backend
            .upload_bytes(&ctx, key, data)
            .expect("upload failed");
        assert!(backend.exists(&ctx, key).expect("exists check failed"));

        let downloaded = backend.download_bytes(&ctx, key).expect("download failed");
        assert_eq!(downloaded, Some(data.to_vec()));
    }

    #[test]
    fn test_s3_url_parsing() {
        let backend =
            S3Backend::new("s3://my-bucket/cache/prefix").expect("failed to parse S3 URL");
        assert_eq!(backend.bucket, "my-bucket");
        assert_eq!(backend.prefix, "cache/prefix");
        assert_eq!(
            backend.s3_key("objects/ab/cdef"),
            "cache/prefix/objects/ab/cdef"
        );

        let backend2 = S3Backend::new("s3://bucket").expect("failed to parse S3 URL");
        assert_eq!(backend2.bucket, "bucket");
        assert_eq!(backend2.prefix, "");
        assert_eq!(backend2.s3_key("objects/ab/cdef"), "objects/ab/cdef");
    }

    /// Only a missing key is a cache miss. Every other `aws` failure —
    /// expired credentials, `AccessDenied`, a missing bucket, no network —
    /// used to come back as "absent" too, which degraded every build to a
    /// full rebuild without a word; those must be errors.
    #[test]
    fn s3_failures_are_classified_by_stderr() {
        assert_eq!(s3_outcome(true, ""), S3Outcome::Present);
        assert_eq!(
            s3_outcome(
                false,
                "An error occurred (404) when calling the HeadObject operation: Not Found\n"
            ),
            S3Outcome::Absent
        );
        assert_eq!(
            s3_outcome(
                false,
                "fatal error: An error occurred (404) when calling the HeadObject operation: \
                 Key \"objects/ab/cd\" does not exist"
            ),
            S3Outcome::Absent
        );
        assert_eq!(
            s3_outcome(
                false,
                "An error occurred (NoSuchKey) when calling the GetObject operation: \
                 The specified key does not exist."
            ),
            S3Outcome::Absent
        );
        for stderr in [
            "An error occurred (403) when calling the HeadObject operation: Forbidden",
            "An error occurred (AccessDenied) when calling the GetObject operation: Access Denied",
            "An error occurred (ExpiredToken) when calling the HeadObject operation: \
             The provided token has expired.",
            "An error occurred (NoSuchBucket) when calling the GetObject operation: \
             The specified bucket does not exist",
            "Unable to locate credentials. You can configure credentials by running \"aws configure\".",
            "Could not connect to the endpoint URL: \"https://s3.example.amazonaws.com/\"",
            "",
        ] {
            assert_eq!(s3_outcome(false, stderr), S3Outcome::Failed, "{stderr:?}");
        }
    }
}