Skip to content

# milestone patch v3.0.1 - a1 #8

Description

@aj1126

milestone patch v3.0.1 - a1

1. Hash Cache Deserialization & Date Precision

File: src/MyBookTools.psm1
Function: Update-MyBookHashCache

Issue: The hash cache invalidates on every run, failing to bypass unchanged files. Furthermore, a MethodInvocationException crashes the script during cache lookups.
Root Cause: 1. ConvertFrom-Json in PowerShell 5.1 outputs a PSCustomObject, not a standard hashtable. Calling .ContainsKey() on it triggers a fatal error.
2. [datetime] objects lose tick precision during JSON serialization. Direct comparison of a file's $meta.LastWriteTime to the deserialized date fails.

Resolution:
Convert the PSCustomObject to a hashtable during ingestion and enforce ISO 8601 string formatting ('o') for reliable date comparisons.

    $cache = @{}
    if (Test-Path $CachePath) {
        $json = Get-Content -Path $CachePath -Raw | ConvertFrom-Json
        if ($json) {
            foreach ($prop in $json.psobject.properties) {
                $cache[$prop.Name] = $prop.Value
            }
        }
    }

    $result = @{}

    Get-ChildItem -Path $RootPath -Recurse -File -ErrorAction SilentlyContinue |
        ForEach-Object {
            $key = $_.FullName
            $meta = @{
                Length        = $_.Length
                LastWriteTime = $_.LastWriteTime.ToString('o') # Format securely for string comparison
            }

            if ($null -ne $cache[$key] -and
                $cache[$key].Length -eq $meta.Length -and
                $cache[$key].LastWriteTime -eq $meta.LastWriteTime) {

                $hash = $cache[$key].Hash
            } else {
                try { $hash = (Get-FileHash -LiteralPath $_.FullName).Hash } catch { $hash = $null }
            }

            $result[$key] = @{
                Length        = $meta.Length
                LastWriteTime = $meta.LastWriteTime
                Hash          = $hash
            }
        }

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions