Skip to content

Commit c089c3a

Browse files
committed
fix(search): Add LastWriteTime fingerprint to cache staleness check
In-place manifest edits (version bumps, new binaries) that keep the same file count previously evaded staleness detection until the 24h timer expired. Now stores the maximum LastWriteTimeUtc across all manifests during cache build and validates it on load alongside the existing file-count check. Computed during the same Get-ChildItem enumeration that already counts files — zero additional I/O.
1 parent 1cbb7b3 commit c089c3a

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

libexec/scoop-search.ps1

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@ function init_search_cache {
2525
$cache = Get-Content $searchCachePath -Raw | ConvertFrom-Json -ErrorAction Stop
2626
$cacheAge = (Get-Date) - [datetime]::Parse($cache.timestamp)
2727
if ($cacheAge.TotalHours -ge 24) { return $false }
28-
# Cross-check file count for staleness
29-
$currentCount = 0
28+
# Cross-check file count and last-write fingerprint for staleness
29+
$currentCount = 0; $currentMaxWrite = [datetime]::MinValue
3030
Get-LocalBucket | ForEach-Object {
3131
$dir = Find-BucketDirectory $_
32-
$currentCount += (Get-ChildItem $dir -Filter '*.json' -Recurse -ErrorAction SilentlyContinue).Count
32+
$items = Get-ChildItem $dir -Filter '*.json' -Recurse -ErrorAction SilentlyContinue
33+
$currentCount += $items.Count
34+
foreach ($item in $items) {
35+
if ($item.LastWriteTimeUtc -gt $currentMaxWrite) { $currentMaxWrite = $item.LastWriteTimeUtc }
36+
}
3337
}
3438
if ($cache.fileCount -ne $currentCount) { return $false }
39+
if ($cache.maxWriteUtc -ne $currentMaxWrite.ToString('o')) { return $false }
3540
$script:searchIndexApps = @{}
3641
foreach ($prop in $cache.apps.PSObject.Properties) {
3742
$script:searchIndexApps[$prop.Name] = @{ path = $prop.Value.path; bucket = $prop.Value.bucket }
@@ -46,9 +51,14 @@ function init_search_cache {
4651

4752
function build_search_cache {
4853
$allPathsByBucket = @{}
54+
$maxWriteUtc = [datetime]::MinValue
4955
Get-LocalBucket | ForEach-Object {
5056
$dir = Find-BucketDirectory $_
51-
$paths = @(Get-ChildItem $dir -Filter '*.json' -Recurse -ErrorAction SilentlyContinue | ForEach-Object { $_.FullName })
57+
$items = Get-ChildItem $dir -Filter '*.json' -Recurse -ErrorAction SilentlyContinue
58+
$paths = @($items | ForEach-Object { $_.FullName })
59+
foreach ($item in $items) {
60+
if ($item.LastWriteTimeUtc -gt $maxWriteUtc) { $maxWriteUtc = $item.LastWriteTimeUtc }
61+
}
5262
if ($paths.Count -gt 0) { $allPathsByBucket[$_] = $paths }
5363
}
5464
$totalCount = ($allPathsByBucket.Values | ForEach-Object { $_.Count } | Measure-Object -Sum).Sum
@@ -84,7 +94,7 @@ function build_search_cache {
8494
} catch { }
8595
}
8696
}
87-
$cacheData = [PSCustomObject]@{ timestamp = (Get-Date).ToString('o'); fileCount = $totalCount; apps = [PSCustomObject]$newApps; bins = [PSCustomObject]$newBins }
97+
$cacheData = [PSCustomObject]@{ timestamp = (Get-Date).ToString('o'); fileCount = $totalCount; maxWriteUtc = $maxWriteUtc.ToString('o'); apps = [PSCustomObject]$newApps; bins = [PSCustomObject]$newBins }
8898
$cacheData | ConvertTo-Json -Compress -Depth 4 | Set-Content $searchCachePath -Encoding UTF8
8999
$script:searchIndexApps = $newApps
90100
$script:searchIndexBins = $newBins

0 commit comments

Comments
 (0)