cmd/pkgserver: expose size and store pre-encoded ident

This change also handles SIGSEGV correctly in newStatusHandler, and makes serving status fully zero copy.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2026-03-11 03:32:09 +09:00
parent fb36c54025
commit 5e114f3932
2 changed files with 24 additions and 16 deletions

View File

@@ -1,9 +1,7 @@
package main
import (
"bytes"
"encoding/json"
"io"
"log"
"net/http"
"path"
@@ -11,7 +9,6 @@ import (
"sync"
"hakurei.app/internal/info"
"hakurei.app/internal/pkg"
"hakurei.app/internal/rosa"
)
@@ -50,19 +47,27 @@ func (index *packageIndex) newStatusHandler(disposition bool) http.HandlerFunc {
if disposition {
contentType = "application/octet-stream"
// quoting like this is unsound, but okay, because metadata is hardcoded
contentDisposition := `attachment; filename="`
contentDisposition += m.Name + "-"
if m.Version != "" {
contentDisposition += m.Version + "-"
}
contentDisposition += pkg.Encode(m.ident.Value()) + `.log"`
contentDisposition += m.ids + `.log"`
w.Header().Set("Content-Disposition", contentDisposition)
}
w.Header().Set("Content-Type", contentType)
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
_, err := io.Copy(w, bytes.NewReader(m.status))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
if err := func() (err error) {
defer index.handleAccess(&err)()
_, err = w.Write(m.status)
return
}(); err != nil {
log.Println(err)
http.Error(
w, "cannot deliver status, contact maintainers",
http.StatusInternalServerError,
)
}
}
}

View File

@@ -4,7 +4,6 @@ import (
"errors"
"slices"
"strings"
"unique"
"hakurei.app/internal/pkg"
"hakurei.app/internal/rosa"
@@ -23,6 +22,9 @@ const (
type packageIndex struct {
sorts [sortOrderEnd + 1][rosa.PresetUnexportedStart]*metadata
names map[string]*metadata
// Taken from [rosa.Report] if available.
handleAccess func(*error) func()
}
// metadata holds [rosa.Metadata] extended with additional information.
@@ -33,11 +35,13 @@ type metadata struct {
// Populated via [rosa.Toolchain.Version], [rosa.Unversioned] is equivalent
// to the zero value. Otherwise, the zero value is invalid.
Version string `json:"version,omitempty"`
// Output data size, available if present in report.
Size int64 `json:"size,omitempty"`
// Whether the underlying [pkg.Artifact] is present in the report.
HasReport bool `json:"report"`
// Ident resolved from underlying [pkg.Artifact].
ident unique.Handle[pkg.ID]
// Ident string encoded ahead of time.
ids string
// Backed by [rosa.Report], access must be prepared by HandleAccess.
status []byte
}
@@ -46,6 +50,7 @@ type metadata struct {
func (index *packageIndex) populate(cache *pkg.Cache, report *rosa.Report) (err error) {
if report != nil {
defer report.HandleAccess(&err)()
index.handleAccess = report.HandleAccess
}
var work [rosa.PresetUnexportedStart]*metadata
@@ -65,12 +70,10 @@ func (index *packageIndex) populate(cache *pkg.Cache, report *rosa.Report) (err
}
if cache != nil && report != nil {
m.ident = cache.Ident(rosa.Std.Load(p))
status, n := report.ArtifactOf(m.ident)
if n >= 0 {
m.HasReport = true
m.status = status
}
id := cache.Ident(rosa.Std.Load(p))
m.ids = pkg.Encode(id.Value())
m.status, m.Size = report.ArtifactOf(id)
m.HasReport = m.Size >= 0
}
work[p] = &m