crossbuild-core 1.0.0

Core types, models, and traits for the cargo-crossbuild ecosystem
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
//! Cache management for cross-build artifacts, downloads, and sysroots.

use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use anyhow::Result;
use crate::error::CrossBuildError;

/// Cache policy configuration.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CachePolicy {
    pub root: PathBuf,
    pub max_size_bytes: Option<u64>,
    pub max_age: Option<Duration>,
    pub compress: bool,
}

impl Default for CachePolicy {
    fn default() -> Self {
        Self {
            root: PathBuf::from("target").join("crossbuild-cache"),
            max_size_bytes: Some(10 * 1024 * 1024 * 1024), // 10 GB
            max_age: Some(Duration::from_secs(30 * 24 * 60 * 60)), // 30 days
            compress: true,
        }
    }
}

impl CachePolicy {
    pub fn new(root: impl Into<PathBuf>) -> Self {
        Self {
            root: root.into(),
            ..Default::default()
        }
    }

    pub fn with_max_size(mut self, bytes: u64) -> Self {
        self.max_size_bytes = Some(bytes);
        self
    }

    pub fn with_max_age(mut self, age: Duration) -> Self {
        self.max_age = Some(age);
        self
    }

    pub fn with_compression(mut self, compress: bool) -> Self {
        self.compress = compress;
        self
    }

    pub fn absolute_root(&self, workspace_root: &Path) -> PathBuf {
        if self.root.is_absolute() {
            self.root.clone()
        } else {
            workspace_root.join(&self.root)
        }
    }

    pub fn cache_key(&self, workspace_root: &Path, target: &crate::model::TargetTriple) -> String {
        let workspace_label = workspace_root
            .to_string_lossy()
            .replace(['\\', '/', ':'], "_");
        format!("{}::{}", workspace_label, target.triple)
    }

    pub fn download_dir(&self, workspace_root: &Path) -> PathBuf {
        self.absolute_root(workspace_root).join("downloads")
    }

    pub fn sysroot_dir(&self, workspace_root: &Path) -> PathBuf {
        self.absolute_root(workspace_root).join("sysroots")
    }

    pub fn toolchain_dir(&self, workspace_root: &Path) -> PathBuf {
        self.absolute_root(workspace_root).join("toolchains")
    }

    pub fn build_dir(&self, workspace_root: &Path, target: &crate::model::TargetTriple) -> PathBuf {
        self.absolute_root(workspace_root)
            .join("builds")
            .join(&target.triple)
    }

    pub fn metadata_path(&self, workspace_root: &Path) -> PathBuf {
        self.absolute_root(workspace_root).join("metadata.json")
    }
}

/// Cache metadata for tracking entries.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CacheMetadata {
    pub entries: BTreeMap<String, CacheEntry>,
    pub total_size_bytes: u64,
    pub last_cleanup: u64,
}

impl Default for CacheMetadata {
    fn default() -> Self {
        Self {
            entries: BTreeMap::new(),
            total_size_bytes: 0,
            last_cleanup: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
        }
    }
}

/// Individual cache entry.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CacheEntry {
    pub key: String,
    pub path: PathBuf,
    pub size_bytes: u64,
    pub created: u64,
    pub last_accessed: u64,
    pub access_count: u64,
    pub entry_type: CacheEntryType,
    pub metadata: BTreeMap<String, String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
pub enum CacheEntryType {
    Download,
    Sysroot,
    Toolchain,
    BuildArtifact,
    Other,
}

/// Cache manager for handling all cache operations.
pub struct CacheManager {
    policy: CachePolicy,
    workspace_root: PathBuf,
    metadata: CacheMetadata,
}

impl CacheManager {
    /// Returns a reference to the cache policy.
    pub fn policy(&self) -> &CachePolicy {
        &self.policy
    }

    /// Returns current cache statistics.
    pub fn stats(&self) -> CacheStats {
        let mut by_type = std::collections::BTreeMap::new();
        for entry in self.metadata.entries.values() {
            *by_type.entry(entry.entry_type).or_insert(0) += 1;
        }
        CacheStats {
            total_entries: self.metadata.entries.len(),
            total_size_bytes: self.metadata.total_size_bytes,
            by_type,
            root: self.policy.absolute_root(&self.workspace_root),
        }
    }

    /// Creates a new cache manager.
    pub fn new(policy: CachePolicy, workspace_root: impl AsRef<Path>) -> Result<Self, CrossBuildError> {
        let workspace_root = workspace_root.as_ref().to_path_buf();
        let root = policy.absolute_root(&workspace_root);
        fs::create_dir_all(&root).map_err(|source| CrossBuildError::Io {
            path: Some(root),
            source,
        })?;

        fs::create_dir_all(policy.download_dir(&workspace_root)).map_err(|source| CrossBuildError::Io {
            path: Some(policy.download_dir(&workspace_root)),
            source,
        })?;
        fs::create_dir_all(policy.sysroot_dir(&workspace_root)).map_err(|source| CrossBuildError::Io {
            path: Some(policy.sysroot_dir(&workspace_root)),
            source,
        })?;
        fs::create_dir_all(policy.toolchain_dir(&workspace_root)).map_err(|source| CrossBuildError::Io {
            path: Some(policy.toolchain_dir(&workspace_root)),
            source,
        })?;

        let metadata_path = policy.metadata_path(&workspace_root);
        let metadata = if metadata_path.exists() {
            let content = fs::read_to_string(&metadata_path).map_err(|source| CrossBuildError::Io {
                path: Some(metadata_path),
                source,
            })?;
            serde_json::from_str(&content).unwrap_or_default()
        } else {
            CacheMetadata::default()
        };

        Ok(Self {
            policy,
            workspace_root,
            metadata,
        })
    }

    /// Gets the path for a cached download.
    pub fn get_download(&self, url: &str, checksum: Option<&str>) -> Option<PathBuf> {
        let key = self.download_key(url, checksum);
        self.metadata.entries.get(&key).and_then(|entry| {
            if entry.path.exists() {
                Some(entry.path.clone())
            } else {
                None
            }
        })
    }

    /// Stores a downloaded file in the cache.
    pub fn store_download(
        &mut self,
        url: &str,
        checksum: Option<&str>,
        source_path: &Path,
    ) -> Result<PathBuf, CrossBuildError> {
        let key = self.download_key(url, checksum);
        let dest = self.policy.download_dir(&self.workspace_root).join(&key);

        fs::copy(source_path, &dest).map_err(|source| CrossBuildError::Io {
            path: Some(dest.clone()),
            source,
        })?;

        let size = fs::metadata(&dest).map(|m| m.len()).unwrap_or(0);
        let now = current_timestamp();

        self.metadata.entries.insert(
            key.clone(),
            CacheEntry {
                key: key.clone(),
                path: dest.clone(),
                size_bytes: size,
                created: now,
                last_accessed: now,
                access_count: 1,
                entry_type: CacheEntryType::Download,
                metadata: {
                    let mut m = BTreeMap::new();
                    m.insert("url".to_string(), url.to_string());
                    if let Some(cs) = checksum {
                        m.insert("checksum".to_string(), cs.to_string());
                    }
                    m
                },
            },
        );
        self.metadata.total_size_bytes += size;
        self.save_metadata()?;

        Ok(dest)
    }

    /// Gets or creates a sysroot cache entry.
    pub fn get_sysroot(&self, target: &crate::model::TargetTriple, provider: &str) -> Option<PathBuf> {
        let key = self.sysroot_key(target, provider);
        self.metadata.entries.get(&key).and_then(|entry| {
            if entry.path.exists() {
                Some(entry.path.clone())
            } else {
                None
            }
        })
    }

    /// Stores a sysroot in the cache.
    pub fn store_sysroot(
        &mut self,
        target: &crate::model::TargetTriple,
        provider: &str,
        source_path: &Path,
    ) -> Result<PathBuf, CrossBuildError> {
        let key = self.sysroot_key(target, provider);
        let dest = self.policy.sysroot_dir(&self.workspace_root).join(&key);

        if source_path.is_dir() {
            copy_dir(source_path, &dest)?;
        } else {
            fs::copy(source_path, &dest).map_err(|source| CrossBuildError::Io {
                path: Some(dest.clone()),
                source,
            })?;
        }

        let size = dir_size(&dest).unwrap_or(0);
        let now = current_timestamp();

        self.metadata.entries.insert(
            key.clone(),
            CacheEntry {
                key: key.clone(),
                path: dest.clone(),
                size_bytes: size,
                created: now,
                last_accessed: now,
                access_count: 1,
                entry_type: CacheEntryType::Sysroot,
                metadata: {
                    let mut m = BTreeMap::new();
                    m.insert("target".to_string(), target.triple.clone());
                    m.insert("provider".to_string(), provider.to_string());
                    m
                },
            },
        );
        self.metadata.total_size_bytes += size;
        self.save_metadata()?;

        Ok(dest)
    }

    /// Gets the build directory for a target.
    pub fn build_dir(&self, target: &crate::model::TargetTriple) -> PathBuf {
        self.policy.build_dir(&self.workspace_root, target)
    }

    /// Cleans up old or excess cache entries.
    pub fn cleanup(&mut self) -> Result<CleanupReport, CrossBuildError> {
        let mut report = CleanupReport::default();
        let now = current_timestamp();

        // Remove expired entries
        if let Some(max_age) = self.policy.max_age {
            let cutoff = now - max_age.as_secs();
            let expired: Vec<_> = self.metadata.entries
                .iter()
                .filter(|(_, entry)| entry.last_accessed < cutoff)
                .map(|(k, _)| k.clone())
                .collect();

            for key in expired {
                if let Some(entry) = self.metadata.entries.remove(&key) {
                    if entry.path.exists() {
                        remove_entry(&entry.path)?;
                    }
                    report.removed_entries += 1;
                    report.freed_bytes += entry.size_bytes;
                    self.metadata.total_size_bytes = self.metadata.total_size_bytes.saturating_sub(entry.size_bytes);
                }
            }
        }

        // Enforce size limit
        if let Some(max_size) = self.policy.max_size_bytes {
            if self.metadata.total_size_bytes > max_size {
                // Sort by last accessed (LRU)
                let mut entries: Vec<_> = self.metadata.entries
                    .iter()
                    .map(|(k, v)| (k.clone(), v.last_accessed, v.size_bytes, v.path.clone()))
                    .collect();
                entries.sort_by_key(|(_, last_accessed, _, _)| *last_accessed);

                for (key, _, _size, path) in entries {
                    if self.metadata.total_size_bytes <= max_size {
                        break;
                    }
                    if let Some(entry) = self.metadata.entries.remove(&key) {
                        if path.exists() {
                            remove_entry(&path)?;
                        }
                        report.removed_entries += 1;
                        report.freed_bytes += entry.size_bytes;
                        self.metadata.total_size_bytes = self.metadata.total_size_bytes.saturating_sub(entry.size_bytes);
                    }
                }
            }
        }

        self.metadata.last_cleanup = now;
        self.save_metadata()?;

        Ok(report)
    }

    fn download_key(&self, url: &str, checksum: Option<&str>) -> String {
        use sha2::{Digest, Sha256};
        let mut hasher = Sha256::new();
        hasher.update(url.as_bytes());
        if let Some(cs) = checksum {
            hasher.update(cs.as_bytes());
        }
        hex::encode(hasher.finalize())[..16].to_string()
    }

    fn sysroot_key(&self, target: &crate::model::TargetTriple, provider: &str) -> String {
        use sha2::{Digest, Sha256};
        let mut hasher = Sha256::new();
        hasher.update(target.triple.as_bytes());
        hasher.update(provider.as_bytes());
        format!("sysroot-{}", &hex::encode(hasher.finalize())[..16])
    }

    fn save_metadata(&self) -> Result<(), CrossBuildError> {
        let path = self.policy.metadata_path(&self.workspace_root);
        let content = serde_json::to_string_pretty(&self.metadata)
            .map_err(|e| CrossBuildError::configuration(e.to_string()))?;
        fs::write(&path, content).map_err(|source| CrossBuildError::Io {
            path: Some(path),
            source,
        })
    }
}

/// Cache statistics.
#[derive(Debug, Clone)]
pub struct CacheStats {
    pub total_entries: usize,
    pub total_size_bytes: u64,
    pub by_type: BTreeMap<CacheEntryType, usize>,
    pub root: PathBuf,
}

/// Cleanup report.
#[derive(Debug, Default, Clone)]
pub struct CleanupReport {
    pub removed_entries: usize,
    pub freed_bytes: u64,
}

fn current_timestamp() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}

fn copy_dir(src: &Path, dest: &Path) -> Result<(), CrossBuildError> {
    fs::create_dir_all(dest).map_err(|source| CrossBuildError::Io {
        path: Some(dest.to_path_buf()),
        source,
    })?;

    for entry in fs::read_dir(src).map_err(|source| CrossBuildError::Io {
        path: Some(src.to_path_buf()),
        source,
    })? {
        let entry = entry.map_err(|source| CrossBuildError::Io {
            path: Some(src.to_path_buf()),
            source,
        })?;
        let src_path = entry.path();
        let dest_path = dest.join(entry.file_name());

        if src_path.is_dir() {
            copy_dir(&src_path, &dest_path)?;
        } else {
            fs::copy(&src_path, &dest_path).map_err(|source| CrossBuildError::Io {
                path: Some(dest_path),
                source,
            })?;
        }
    }
    Ok(())
}

fn dir_size(path: &Path) -> Result<u64, CrossBuildError> {
    let mut size = 0;
    for entry in fs::read_dir(path).map_err(|source| CrossBuildError::Io {
        path: Some(path.to_path_buf()),
        source,
    })? {
        let entry = entry.map_err(|source| CrossBuildError::Io {
            path: Some(path.to_path_buf()),
            source,
        })?;
        let path = entry.path();
        if path.is_dir() {
            size += dir_size(&path)?;
        } else {
            size += fs::metadata(&path).map(|m| m.len()).unwrap_or(0);
        }
    }
    Ok(size)
}

fn remove_entry(path: &Path) -> Result<(), CrossBuildError> {
    if path.is_dir() {
        fs::remove_dir_all(path).map_err(|source| CrossBuildError::Io {
            path: Some(path.to_path_buf()),
            source,
        })
    } else {
        fs::remove_file(path).map_err(|source| CrossBuildError::Io {
            path: Some(path.to_path_buf()),
            source,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::TargetTriple;
    use tempfile::tempdir;

    #[test]
    fn cache_policy_default() {
        let policy = CachePolicy::default();
        assert_eq!(policy.root, PathBuf::from("target").join("crossbuild-cache"));
        assert_eq!(policy.max_size_bytes, Some(10 * 1024 * 1024 * 1024));
    }

    #[test]
    fn cache_key_generation() {
        let policy = CachePolicy::default();
        let workspace = PathBuf::from("/home/user/project");
        let target = TargetTriple::parse("x86_64-unknown-linux-gnu").unwrap();

        let key = policy.cache_key(&workspace, &target);
        assert!(key.contains("home_user_project"));
        assert!(key.contains("x86_64-unknown-linux-gnu"));
    }

    #[test]
    fn cache_manager_creation() {
        let dir = tempdir().unwrap();
        let policy = CachePolicy::new(dir.path().join("cache"));
        let manager = CacheManager::new(policy, dir.path()).unwrap();

        let stats = manager.stats();
        assert_eq!(stats.total_entries, 0);
        assert_eq!(stats.total_size_bytes, 0);
    }

    #[test]
    fn download_caching() {
        let dir = tempdir().unwrap();
        let policy = CachePolicy::new(dir.path().join("cache"));
        let mut manager = CacheManager::new(policy, dir.path()).unwrap();

        // Create a test file
        let source = dir.path().join("test-download");
        fs::write(&source, b"test content").unwrap();

        // Store in cache
        let cached = manager.store_download(
            "https://example.com/file",
            Some("sha256:abc123"),
            &source,
        ).unwrap();

        assert!(cached.exists());

        // Retrieve from cache
        let retrieved = manager.get_download("https://example.com/file", Some("sha256:abc123"));
        assert_eq!(retrieved, Some(cached));

        // Different checksum should not match
        let retrieved2 = manager.get_download("https://example.com/file", Some("sha256:different"));
        assert_eq!(retrieved2, None);
    }

    #[test]
    fn sysroot_caching() {
        let dir = tempdir().unwrap();
        let policy = CachePolicy::new(dir.path().join("cache"));
        let mut manager = CacheManager::new(policy, dir.path()).unwrap();

        let target = TargetTriple::parse("x86_64-unknown-linux-gnu").unwrap();

        // Create a test sysroot
        let sysroot = dir.path().join("sysroot");
        fs::create_dir_all(sysroot.join("lib")).unwrap();
        fs::write(sysroot.join("lib").join("libc.so"), b"fake").unwrap();

        // Store in cache
        let cached = manager.store_sysroot(&target, "rustup", &sysroot).unwrap();

        assert!(cached.exists());
        assert!(cached.join("lib").join("libc.so").exists());

        // Retrieve from cache
        let retrieved = manager.get_sysroot(&target, "rustup");
        assert_eq!(retrieved, Some(cached));
    }

    #[test]
    fn cleanup_removes_old_entries() {
        let dir = tempdir().unwrap();
        let policy = CachePolicy::new(dir.path().join("cache"))
            .with_max_age(Duration::from_secs(60)); // 1 minute
        let mut manager = CacheManager::new(policy, dir.path()).unwrap();

        let target = TargetTriple::parse("x86_64-unknown-linux-gnu").unwrap();

        // Create entries with old timestamps
        let sysroot = dir.path().join("sysroot");
        fs::create_dir_all(sysroot.join("lib")).unwrap();
        fs::write(sysroot.join("lib").join("libc.so"), b"fake").unwrap();

        let cached = manager.store_sysroot(&target, "rustup", &sysroot).unwrap();

        // Manually set old timestamp
        if let Some(entry) = manager.metadata.entries.get_mut(&manager.sysroot_key(&target, "rustup")) {
            entry.last_accessed = current_timestamp() - 120; // 2 minutes ago
        }
        manager.save_metadata().unwrap();

        // Run cleanup
        let report = manager.cleanup().unwrap();
        assert_eq!(report.removed_entries, 1);
        assert!(!cached.exists());
    }
}