# HaoRui - C-drive write checker # Usage: .\scripts\check-c-drive.ps1 [-Baseline] # Note: written in UTF-8 with BOM so PowerShell 5.1 parses Chinese correctly. param([switch]$Baseline) $ErrorActionPreference = 'Stop' $root = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path) $snapPath = Join-Path $root '.tmp\c-drive-snapshot.txt' # Hotspot dirs under C that our build might touch $hotspots = @( "$env:USERPROFILE\.gradle", "$env:USERPROFILE\.android", "$env:USERPROFILE\.m2", "$env:USERPROFILE\.npm", "$env:LOCALAPPDATA\Android", "$env:LOCALAPPDATA\Temp", "$env:TEMP" ) function Get-Snapshot { $rows = @() foreach ($h in $hotspots) { if (-not (Test-Path $h)) { $rows += "$h|0|0|-"; continue } $files = Get-ChildItem -Path $h -Recurse -File -ErrorAction SilentlyContinue $count = ($files | Measure-Object).Count $sum = ($files | Measure-Object -Property Length -Sum).Sum if ($null -eq $sum) { $sum = 0 } $newest = ($files | Sort-Object LastWriteTime -Descending | Select-Object -First 1) $ts = if ($newest) { $newest.LastWriteTime.ToString('yyyy-MM-dd HH:mm:ss') } else { '-' } $rows += "$h|$count|$sum|$ts" } return $rows } $snap = Get-Snapshot if ($Baseline) { $snap | Set-Content -Path $snapPath -Encoding utf8 Write-Host "[BASELINE SAVED] $snapPath" -ForegroundColor Green foreach ($r in $snap) { Write-Host " $r" } exit 0 } if (-not (Test-Path $snapPath)) { Write-Host "[MISSING BASELINE] run: .\scripts\check-c-drive.ps1 -Baseline" -ForegroundColor Yellow Write-Host "Current hotspots:" -ForegroundColor Cyan foreach ($r in $snap) { Write-Host " $r" } exit 1 } $base = Get-Content $snapPath -Encoding utf8 Write-Host "---- C-drive write check ----" -ForegroundColor Cyan Write-Host ("{0,-34} {1,10} {2,14} {3,20} {4}" -f 'dir', 'files', 'bytes', 'newest', 'status') Write-Host ("-" * 92) $violations = 0 for ($i = 0; $i -lt $snap.Count; $i++) { $b = $base[$i].Split('|') $n = $snap[$i].Split('|') $changed = ($b[1] -ne $n[1]) -or ($b[2] -ne $n[2]) if ($changed) { $violations++ } $mark = if ($changed) { '[CHANGED]' } else { '[OK]' } $leaf = Split-Path $n[0] -Leaf Write-Host ("{0,-34} {1,10} {2,14} {3,20} {4}" -f $leaf, $n[1], $n[2], $n[3], $mark) } Write-Host "" if ($violations -eq 0) { Write-Host "[PASS] C-drive zero write" -ForegroundColor Green exit 0 } else { Write-Host "[WARN] $violations hotspot dir(s) changed" -ForegroundColor Yellow Write-Host " If caused by build, verify env.ps1 was loaded first." -ForegroundColor Yellow exit 1 }