2026-11-22 02:49:48 +00:00
|
|
|
// Package debugbundle allocates explicitly requested per-run debug directories.
|
2026-07-18 04:57:50 +00:00
|
|
|
package debugbundle
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Bundle struct {
|
2026-11-22 02:49:48 +00:00
|
|
|
path string
|
|
|
|
|
createdAt time.Time
|
2026-07-18 04:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-22 02:49:48 +00:00
|
|
|
// Allocate starts a capture session. A link failure returns the already-created
|
|
|
|
|
// bundle as well as the error so callers can identify and retain available data.
|
2026-07-18 14:21:26 +00:00
|
|
|
func Allocate(parent, runID string, startedAt time.Time) (*Bundle, error) {
|
2026-07-18 04:57:50 +00:00
|
|
|
if parent == "" {
|
|
|
|
|
return nil, fmt.Errorf("debug parent must not be empty")
|
|
|
|
|
}
|
2026-07-18 14:21:26 +00:00
|
|
|
if err := validateRunID(runID); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-11-22 02:49:48 +00:00
|
|
|
parent, err := filepath.Abs(parent)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("resolve debug parent: %w", err)
|
|
|
|
|
}
|
2026-07-18 04:57:50 +00:00
|
|
|
if err := os.MkdirAll(parent, 0o700); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("create debug parent %q: %w", parent, err)
|
|
|
|
|
}
|
2026-11-22 02:49:48 +00:00
|
|
|
directory := filepath.Join(parent, runID)
|
|
|
|
|
if err := os.Mkdir(directory, 0o700); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("create debug bundle %q: %w", directory, err)
|
|
|
|
|
}
|
|
|
|
|
bundle := &Bundle{path: directory, createdAt: startedAt}
|
|
|
|
|
if err := updateLatest(parent, runID); err != nil {
|
|
|
|
|
return bundle, fmt.Errorf("update debug latest for %q: %w", directory, err)
|
|
|
|
|
}
|
|
|
|
|
return bundle, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func updateLatest(parent, runID string) error {
|
|
|
|
|
temporary, err := os.CreateTemp(parent, ".latest-*")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2026-07-18 14:21:26 +00:00
|
|
|
}
|
2026-11-22 02:49:48 +00:00
|
|
|
name := temporary.Name()
|
|
|
|
|
defer os.Remove(name)
|
|
|
|
|
if err := temporary.Close(); err != nil {
|
|
|
|
|
return err
|
2026-07-18 14:21:26 +00:00
|
|
|
}
|
2026-11-22 02:49:48 +00:00
|
|
|
if err := os.Remove(name); err != nil {
|
|
|
|
|
return err
|
2026-07-18 14:21:26 +00:00
|
|
|
}
|
2026-11-22 02:49:48 +00:00
|
|
|
if err := os.Symlink(runID, name); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return os.Rename(name, filepath.Join(parent, "latest"))
|
2026-07-18 14:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func validateRunID(runID string) error {
|
|
|
|
|
if runID == "" {
|
|
|
|
|
return fmt.Errorf("debug run ID must not be empty")
|
|
|
|
|
}
|
|
|
|
|
if runID != strings.TrimSpace(runID) || strings.ContainsAny(runID, `/\\`) || filepath.IsAbs(runID) || filepath.Clean(runID) != runID || runID == "." || runID == ".." {
|
|
|
|
|
return fmt.Errorf("debug run ID %q must be one safe path component", runID)
|
2026-07-18 04:57:50 +00:00
|
|
|
}
|
2026-07-18 14:21:26 +00:00
|
|
|
return nil
|
2026-07-18 04:57:50 +00:00
|
|
|
}
|
|
|
|
|
func (b *Bundle) Path() string {
|
|
|
|
|
if b == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return b.path
|
|
|
|
|
}
|
|
|
|
|
func (b *Bundle) RunID() string {
|
|
|
|
|
if b == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return filepath.Base(b.path)
|
|
|
|
|
}
|
|
|
|
|
func (b *Bundle) CreatedAt() time.Time {
|
|
|
|
|
if b == nil {
|
|
|
|
|
return time.Time{}
|
|
|
|
|
}
|
|
|
|
|
return b.createdAt
|
|
|
|
|
}
|