A mirror with peers records the checksums of files that had none, so they are copied too

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-10-29 18:36:59 +01:00
1 parent 0033a530c7
commit 785e06ecbf
3 files changed
+66

No files matched your search

+38
View File
@@ -3,6 +3,7 @@ package mirror
import (
"context"
"log/slog"
"path/filepath"
"strings"
"time"
)
@@ -24,6 +25,13 @@ type Peers struct {
// Run reads the peers until ctx ends.
func (p *Peers) Run(ctx context.Context) {
// Files fetched before checksums were kept have none, and a peer copies
// only what it can check: they are given theirs, beside the sync.
go func() {
if n := p.M.FillChecksums(ctx); n > 0 {
p.Log.Info("recorded the checksums of files that had none, so peer mirrors can copy them", "files", n)
}
}()
t := time.NewTicker(PeerInterval)
defer t.Stop()
for {
@@ -122,3 +130,33 @@ func (p *Peers) SyncOnce(ctx context.Context, c *Client) {
}
}
}
// FillChecksums records the sha256 of every file the mirror holds without
// one, and says how many it recorded.
func (m *Manager) FillChecksums(ctx context.Context) int {
entries, err := List(m.root)
if err != nil {
return 0
}
n := 0
for _, e := range entries {
if ctx.Err() != nil {
break
}
path := filepath.Join(m.root, e.Model, e.Version, e.File)
if readChecksum(path) != "" || m.isRunning(e.Model, e.Version, e.File) {
continue
}
sum, err := sha256File(path)
if err != nil {
m.log.Warn("could not read a file's checksum", "file", path, "err", err)
continue
}
if err := writeChecksum(path, sum); err != nil {
m.log.Warn("could not record a file's checksum", "file", path, "err", err)
continue
}
n++
}
return n
}