|
| 1 | +use anyhow::{bail, Context, Result}; |
| 2 | +use std::env; |
| 3 | +use std::fs; |
| 4 | +use std::path::{Path, PathBuf}; |
| 5 | +use walkdir::WalkDir; |
| 6 | + |
| 7 | +fn main() -> Result<()> { |
| 8 | + let repo_root = find_repo_root().context("Could not locate repo root (looked for `.git/` or `licenses/`)")?; |
| 9 | + |
| 10 | + check_license_symlinks(&repo_root)?; |
| 11 | + log::info!("All LICENSE files are valid symlinks into `licenses/`."); |
| 12 | + Ok(()) |
| 13 | +} |
| 14 | + |
| 15 | +fn find_repo_root() -> Option<PathBuf> { |
| 16 | + let mut dir = env::current_dir().ok()?.to_path_buf(); |
| 17 | + loop { |
| 18 | + if dir.join(".git").is_dir() || dir.join("licenses").is_dir() { |
| 19 | + return Some(dir); |
| 20 | + } |
| 21 | + if !dir.pop() { |
| 22 | + return None; |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +fn relative_to(path: &Path, root: &Path) -> String { |
| 28 | + match path.strip_prefix(root) { |
| 29 | + Ok(rel) => rel.display().to_string(), |
| 30 | + Err(_) => path.display().to_string(), // fallback if not under repo_root |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +fn check_license_symlinks(repo_root: &Path) -> Result<()> { |
| 35 | + let licenses_dir = repo_root.join("licenses"); |
| 36 | + if !licenses_dir.is_dir() { |
| 37 | + bail!( |
| 38 | + "Required directory 'licenses/' not found at the repo root: {}", |
| 39 | + licenses_dir.display() |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + let licenses_dir_canon = fs::canonicalize(&licenses_dir) |
| 44 | + .with_context(|| format!("Could not canonicalize licenses dir: {}", licenses_dir.display()))?; |
| 45 | + |
| 46 | + let ignore_list = ["LICENSE.txt", "crates/sqltest/standards/LICENSE"]; |
| 47 | + let mut errors: Vec<String> = Vec::new(); |
| 48 | + |
| 49 | + for entry in WalkDir::new(repo_root).into_iter().filter_map(Result::ok) { |
| 50 | + let name = entry.file_name().to_string_lossy(); |
| 51 | + if name != "LICENSE" && name != "LICENSE.txt" { |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + let path = entry.into_path(); |
| 56 | + let rel_str = relative_to(&path, repo_root); |
| 57 | + if ignore_list.contains(&rel_str.as_str()) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + if let Err(e) = validate_one_license(path, repo_root, &licenses_dir_canon) { |
| 62 | + // include the relative path to make the report easy to scan |
| 63 | + errors.push(e.to_string()); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if !errors.is_empty() { |
| 68 | + bail!("Found invalid LICENSE symlinks:\n{}", errors.join("\n")); |
| 69 | + } |
| 70 | + |
| 71 | + Ok(()) |
| 72 | +} |
| 73 | + |
| 74 | +fn validate_one_license(path: PathBuf, repo_root: &Path, licenses_dir_canon: &Path) -> Result<()> { |
| 75 | + let meta = fs::symlink_metadata(&path) |
| 76 | + .with_context(|| format!("Could not stat file {}", relative_to(&path, repo_root)))?; |
| 77 | + |
| 78 | + if !meta.file_type().is_symlink() { |
| 79 | + bail!( |
| 80 | + "{}: Must be a symlink pointing into 'licenses/'.", |
| 81 | + relative_to(&path, repo_root) |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + let raw_target = fs::read_link(&path) |
| 86 | + .with_context(|| format!("Could not read symlink target {}", relative_to(&path, repo_root)))?; |
| 87 | + |
| 88 | + let resolved = |
| 89 | + fs::canonicalize(resolve_relative_target(&raw_target, path.parent().unwrap())).with_context(|| { |
| 90 | + format!( |
| 91 | + "{}: Broken symlink (target {}).", |
| 92 | + relative_to(&path, repo_root), |
| 93 | + raw_target.display() |
| 94 | + ) |
| 95 | + })?; |
| 96 | + |
| 97 | + if !is_within(&resolved, licenses_dir_canon) { |
| 98 | + bail!( |
| 99 | + "{}: Symlink target must be inside 'licenses/' (got {}).", |
| 100 | + relative_to(&path, repo_root), |
| 101 | + relative_to(&resolved, repo_root) |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + Ok(()) |
| 106 | +} |
| 107 | + |
| 108 | +/// Is `child` inside `base`? |
| 109 | +fn is_within(child: &Path, base: &Path) -> bool { |
| 110 | + child.ancestors().any(|a| a == base) |
| 111 | +} |
| 112 | + |
| 113 | +/// Resolve a relative symlink target against its parent directory. |
| 114 | +fn resolve_relative_target(target: &Path, link_parent: &Path) -> PathBuf { |
| 115 | + if target.is_absolute() { |
| 116 | + target.to_path_buf() |
| 117 | + } else { |
| 118 | + link_parent.join(target) |
| 119 | + } |
| 120 | +} |
0 commit comments