Files
magpie/internal/sessions/digest.go
T
yetone c14fa0ce93 Bring back 919780414 and 88d2962e0, which 698ed4adb undid
698ed4adb (a busy port names its holder) was committed from a tree made
before 919780414 and 88d2962e0 landed (09:38, eight minutes before it),
and took both out with no word in its body: quotatable.js, the Usage
Cards / Table switch and its test, and sessions' 16-byte head and
prefix hashes (digest.go) were gone from main from v0.1.3823. This puts
both back as they were, on top of 698ed4adb's own change, which is kept.

Run at this tree, on mote (cherry-pick of both onto origin/main
db0c1b039, no conflicts):
- gofmt -l internal/sessions: clean.
- go vet ./internal/sessions ./internal/gui; GOOS=windows and
  GOOS=linux -tags nogui go vet ./internal/sessions: ok.
- go test -count=1 ./internal/sessions: ok 83.6s.
- go test -run 'Budget|Lazy|I18n' ./internal/gui: ok (the core script
  budget holds with both changes).
- GUI, chromium and webkit: usage-quota-table, gateway-port-holder,
  i18n-keys, i18n-en-no-cjk, old-webkit: 44 of 46. The two reds are
  old-webkit's "compat.js is the first" (headerpresets.js, #6030) and
  "the styles hold up in Safari 15.0" (#5919), red the same on an
  origin/main db0c1b039 copy.
Not run: the full Go and GUI suites.
2026-11-12 09:57:52 +08:00

60 lines
1.7 KiB
Go

package sessions
import (
"crypto/sha256"
"encoding/hex"
"strings"
)
// digest is a file's head or sampled prefix as kept to know it again
// (headDigest, prefixHash): the first 16 bytes of its SHA-256, in the
// state itself. As hex strings, 64 and 74 bytes of their own, they were
// 144 bytes a file beside its state; 18k files' states held 2.6 MB of
// them (#2278). The zero digest is none: the file couldn't be read.
type digest [16]byte
// MarshalText is d in hex, "" for none.
func (d digest) MarshalText() ([]byte, error) {
if d == (digest{}) {
return nil, nil
}
return []byte(hex.EncodeToString(d[:])), nil
}
// UnmarshalText reads a digest as written, or as a magpie before it wrote
// the whole hash: the 64 hex of hashHead's, or prefixHash's "sample-v1:"
// before them, whose first 32 are this digest. A cache from before reads
// as the same files, so nothing is read again for it. Anything else is
// none, which matches no file.
func (d *digest) UnmarshalText(b []byte) error {
*d = parseDigest(string(b))
return nil
}
// parseDigest is the digest s spells (UnmarshalText), none when it spells
// none.
func parseDigest(s string) digest {
var d digest
s = strings.TrimPrefix(s, "sample-v1:")
if (len(s) != 32 && len(s) != 64) || s == "" {
return d
}
if _, err := hex.Decode(d[:], []byte(s[:32])); err != nil {
return digest{}
}
return d
}
// String is d as MarshalText writes it, for the shards' gob, whose
// field stays a string so a shard written before reads the same.
func (d digest) String() string {
b, _ := d.MarshalText()
return string(b)
}
// headDigest is the digest of a file's first bytes, head.
func headDigest(head string) digest {
sum := sha256.Sum256([]byte(head))
return digest(sum[:16])
}