* usage: same-tick rewrites refresh cached totals and request pages Trust change stamps only when they were settled at the snapshot read's start. Fingerprint recent snapshots and append prefixes, and discard fingerprint trust when the content changes during a read. Fixes https://github.com/yetone/magpie/issues/1305 Verified: deterministic old-predicate FAIL/new PASS; usage tests with race; nogui vet; Linux nogui and Windows builds; usage test cross-compilation for Windows, Darwin and the unsupported-stamp fallback on FreeBSD. Full nogui suite exposed the independent sessions TestList empty-session limit failure. Native Linux GUI vet/build lack pkg-config and GTK/WebKit; native Windows/NTFS execution was not run. * usage: keep FILETIME stamp ages valid beyond UnixNano range Convert whole seconds and the 100ns fraction separately. Checked the epoch, current dates with fractional ticks and year 2500; Windows build and usage test cross-compilation pass. Native Windows execution was not run. * usage: keep history cached after an append settles An unchanged log now validates a recent snapshot's nonempty fingerprint before publishing a settled clone. Preserve its version and sealed blocks so the summary and page caches stay warm without mutating readers' snapshots. Follow-up to the review on https://github.com/yetone/magpie/pull/1328, for https://github.com/yetone/magpie/issues/1305. On e28e686, the real-clock append/read/settle/read tests fail with a version bump and 32,428 additional allocations over 4,000 versus 10 rows. With this change the targeted race run passes, with 165 extra allocations. The existing performance test now measures this path on the real clock. Verification uses Go 1.26.8, a temporary HOME and pinned Go caches. Native Linux GUI checks require missing pkg-config/GTK/WebKit libraries; native Windows/NTFS execution is not available. No GUI assets change. Implementation and verification used AI assistance. Final checks: usage race PASS (421 test results); nogui suite PASS (7,273 test results, 44 packages; 21 skipped tests, 3 packages without tests). Nogui vet, Linux nogui build, Windows build, and usage test cross-compilation for Windows amd64, Darwin arm64 and FreeBSD amd64 PASS. Native Linux GUI vet/build were attempted and failed at the missing pkg-config executable.
107 lines
3.0 KiB
Go
107 lines
3.0 KiB
Go
//go:build windows
|
|
|
|
package usage
|
|
|
|
import (
|
|
"os"
|
|
"time"
|
|
"unsafe"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
// fileBasicInfo mirrors FILE_BASIC_INFO as returned by
|
|
// GetFileInformationByHandleEx(FileBasicInfo). All four times are FILETIME
|
|
// values (int64, 100ns since 1601) and FileAttributes is followed by explicit
|
|
// padding so the structure stays 40 bytes on every architecture.
|
|
type fileBasicInfo struct {
|
|
CreationTime int64
|
|
LastAccessTime int64
|
|
LastWriteTime int64
|
|
ChangeTime int64
|
|
FileAttributes uint32
|
|
_ uint32
|
|
}
|
|
|
|
// statLogFile opens the log for attribute access only, allowing concurrent
|
|
// writers and rotation to rename or delete it while the handle is open. The
|
|
// returned FileInfo preserves the underlying os.FileInfo (inode/size/mtime
|
|
// semantics) and carries the change-time stamp.
|
|
func statLogFile(path string) (os.FileInfo, error) {
|
|
p, err := windows.UTF16PtrFromString(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
h, err := windows.CreateFile(
|
|
p,
|
|
windows.FILE_READ_ATTRIBUTES,
|
|
windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE,
|
|
nil,
|
|
windows.OPEN_EXISTING,
|
|
windows.FILE_ATTRIBUTE_NORMAL,
|
|
0,
|
|
)
|
|
if err != nil {
|
|
// Go's opener handles long paths on Windows versions where the raw
|
|
// API requires an extended path prefix. Keep the same-handle stamp.
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
return statLogHandle(f)
|
|
}
|
|
f := os.NewFile(uintptr(h), path)
|
|
if f == nil {
|
|
windows.CloseHandle(h)
|
|
return nil, os.ErrInvalid
|
|
}
|
|
info, err := statLogHandle(f)
|
|
// The caller owns the snapshot FileInfo, not the handle. Close our own.
|
|
f.Close()
|
|
return info, err
|
|
}
|
|
|
|
// statLogHandle stamps the FileInfo reported by the caller's open handle. The
|
|
// same handle is used for both the portable stat and the change-time query so
|
|
// the two can never observe different files. A handle that does not support
|
|
// the attribute query yields the plain FileInfo with an empty stamp rather
|
|
// than a stat error, keeping the fingerprint fallback intact.
|
|
func statLogHandle(f *os.File) (os.FileInfo, error) {
|
|
info, err := f.Stat()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return stampLogHandle(f, info), nil
|
|
}
|
|
|
|
func stampLogHandle(f *os.File, info os.FileInfo) os.FileInfo {
|
|
if f == nil || info == nil {
|
|
return info
|
|
}
|
|
var raw fileBasicInfo
|
|
if err := windows.GetFileInformationByHandleEx(
|
|
windows.Handle(f.Fd()),
|
|
windows.FileBasicInfo,
|
|
(*byte)(unsafe.Pointer(&raw)),
|
|
uint32(unsafe.Sizeof(raw)),
|
|
); err != nil {
|
|
return info
|
|
}
|
|
if raw.ChangeTime == 0 {
|
|
return info
|
|
}
|
|
changed := time.Unix(raw.ChangeTime/10_000_000-11_644_473_600, raw.ChangeTime%10_000_000*100)
|
|
return logFileInfo{FileInfo: info, changeTime: changed}
|
|
}
|
|
|
|
// nativeLogChangeTime extracts the time captured at snapshot time. It never
|
|
// re-opens or re-stats the path: an old snapshot must keep its old stamp even
|
|
// after the log has been rewritten or appended to.
|
|
func nativeLogChangeTime(info os.FileInfo) time.Time {
|
|
if stamped, ok := info.(logFileInfo); ok {
|
|
return stamped.changeTime
|
|
}
|
|
return time.Time{}
|
|
}
|