238 lines
7.5 KiB
Go
238 lines
7.5 KiB
Go
package artifacts
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/pathsafe"
|
|
)
|
|
|
|
// AttemptViews records previous managed targets so a stage that self-skips can
|
|
// restore its navigation views without touching unrelated workspace content.
|
|
type AttemptViews struct {
|
|
root, runID, name string
|
|
previous map[string]*string
|
|
}
|
|
|
|
// BeginAttemptViews creates actual diagnostics directories and atomically
|
|
// advances each managed relative link. It accepts no arbitrary link targets.
|
|
func BeginAttemptViews(sessionRoot, runID, name string) (*AttemptViews, error) {
|
|
if err := pathsafe.ValidateOpaqueSegment(runID); err != nil {
|
|
return nil, fmt.Errorf("run identity: %w", err)
|
|
}
|
|
if runID == ".preflight" || runID == ".validation" {
|
|
return nil, fmt.Errorf("run identity %q is reserved for probes", runID)
|
|
}
|
|
if err := pathsafe.ValidateOpaqueSegment(name); err != nil {
|
|
return nil, fmt.Errorf("diagnostic name: %w", err)
|
|
}
|
|
root, err := os.OpenRoot(sessionRoot)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer root.Close()
|
|
paths := buildSessionPathsFromRoot("", "", "", sessionRoot)
|
|
for _, dir := range []string{SessionRunLogsDir(paths, runID, name), SessionRunDebugDir(paths, runID, name), paths.LogsDir, paths.DebugDir} {
|
|
relative, err := filepath.Rel(sessionRoot, dir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := fileops.EnsureWorkspaceDirectoryUnderRoot(sessionRoot, relative); err != nil {
|
|
return nil, fmt.Errorf("prepare attempt directory %q: %w", relative, err)
|
|
}
|
|
}
|
|
views := &AttemptViews{root: sessionRoot, runID: runID, name: name, previous: map[string]*string{}}
|
|
for _, category := range []string{"logs", "debug"} {
|
|
parent, err := root.OpenRoot(category)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
previous, err := existingManagedView(parent, sessionRoot, name, category)
|
|
_ = parent.Close()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
views.previous[category] = previous
|
|
}
|
|
var updated []string
|
|
for _, category := range []string{"logs", "debug"} {
|
|
parent, err := root.OpenRoot(category)
|
|
if err != nil {
|
|
return nil, errors.Join(err, views.revertCategories(updated))
|
|
}
|
|
target := attemptTarget(sessionRoot, runID, name, category)
|
|
err = replaceManagedView(parent, sessionRoot, name, category, target)
|
|
_ = parent.Close()
|
|
if err != nil {
|
|
return nil, errors.Join(err, views.revertCategories(updated))
|
|
}
|
|
updated = append(updated, category)
|
|
}
|
|
return views, nil
|
|
}
|
|
|
|
func attemptTarget(sessionRoot, runID, name, category string) string {
|
|
paths := buildSessionPathsFromRoot("", "", "", sessionRoot)
|
|
view, destination := SessionLatestLogsPath(paths, name), SessionRunLogsDir(paths, runID, name)
|
|
if category == "debug" {
|
|
view, destination = SessionLatestDebugPath(paths, name), SessionRunDebugDir(paths, runID, name)
|
|
}
|
|
relative, _ := filepath.Rel(filepath.Dir(view), destination)
|
|
return filepath.ToSlash(relative)
|
|
}
|
|
|
|
func existingManagedView(parent *os.Root, sessionRoot, name, category string) (*string, error) {
|
|
info, err := parent.Lstat(name)
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if info.Mode()&os.ModeSymlink == 0 {
|
|
return nil, fmt.Errorf("managed %s view %q conflicts with existing content", category, name)
|
|
}
|
|
target, err := parent.Readlink(name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
paths := buildSessionPathsFromRoot("", "", "", sessionRoot)
|
|
view := SessionLatestLogsPath(paths, name)
|
|
if category == "debug" {
|
|
view = SessionLatestDebugPath(paths, name)
|
|
}
|
|
resolved := filepath.Clean(filepath.Join(filepath.Dir(view), filepath.FromSlash(target)))
|
|
relative, err := filepath.Rel(paths.RunsDir, resolved)
|
|
parts := strings.Split(filepath.ToSlash(relative), "/")
|
|
if err != nil || len(parts) != 3 || parts[1] != name || parts[2] != category || parts[0] == ".preflight" || parts[0] == ".validation" || pathsafe.ValidateOpaqueSegment(parts[0]) != nil || target != attemptTarget(sessionRoot, parts[0], name, category) {
|
|
return nil, fmt.Errorf("managed %s view %q has an unexpected target %q", category, name, target)
|
|
}
|
|
return &target, nil
|
|
}
|
|
|
|
// InspectManagedAttemptViews reports whether any recognized managed view exists.
|
|
// Dangling links count as present; their targets are never followed.
|
|
func InspectManagedAttemptViews(sessionRoot string, names []string) (bool, error) {
|
|
return visitManagedAttemptViews(sessionRoot, names, false)
|
|
}
|
|
|
|
// RemoveManagedAttemptViews removes recognized managed links without following them.
|
|
func RemoveManagedAttemptViews(sessionRoot string, names []string) error {
|
|
_, err := visitManagedAttemptViews(sessionRoot, names, true)
|
|
return err
|
|
}
|
|
|
|
func visitManagedAttemptViews(sessionRoot string, names []string, remove bool) (bool, error) {
|
|
for _, name := range names {
|
|
if err := pathsafe.ValidateOpaqueSegment(name); err != nil {
|
|
return false, fmt.Errorf("invalid managed view %q: %w", name, err)
|
|
}
|
|
}
|
|
root, err := os.OpenRoot(sessionRoot)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer root.Close()
|
|
found := false
|
|
for _, category := range []string{"logs", "debug"} {
|
|
info, err := root.Lstat(category)
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
continue
|
|
}
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
|
|
return false, fmt.Errorf("managed %s directory is unsafe", category)
|
|
}
|
|
parent, err := root.OpenRoot(category)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
for _, name := range names {
|
|
target, err := existingManagedView(parent, sessionRoot, name, category)
|
|
if err == nil && target != nil {
|
|
found = true
|
|
if remove {
|
|
err = parent.Remove(name)
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
err = nil
|
|
}
|
|
}
|
|
}
|
|
if err != nil {
|
|
_ = parent.Close()
|
|
return false, err
|
|
}
|
|
}
|
|
_ = parent.Close()
|
|
}
|
|
return found, nil
|
|
}
|
|
|
|
func replaceManagedView(parent *os.Root, sessionRoot, name, category, target string) error {
|
|
if _, err := existingManagedView(parent, sessionRoot, name, category); err != nil {
|
|
return err
|
|
}
|
|
var random [16]byte
|
|
if _, err := rand.Read(random[:]); err != nil {
|
|
return err
|
|
}
|
|
temporary := "." + name + "." + hex.EncodeToString(random[:]) + ".link"
|
|
if err := parent.Symlink(target, temporary); err != nil {
|
|
return fmt.Errorf("create managed %s view: %w", category, err)
|
|
}
|
|
defer parent.Remove(temporary)
|
|
if _, err := existingManagedView(parent, sessionRoot, name, category); err != nil {
|
|
return err
|
|
}
|
|
if err := parent.Rename(temporary, name); err != nil {
|
|
return fmt.Errorf("install managed %s view: %w", category, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Revert restores prior links only if they still point to this attempt.
|
|
func (v *AttemptViews) Revert() error {
|
|
if v == nil {
|
|
return nil
|
|
}
|
|
return v.revertCategories([]string{"logs", "debug"})
|
|
}
|
|
|
|
func (v *AttemptViews) revertCategories(categories []string) error {
|
|
root, err := os.OpenRoot(v.root)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer root.Close()
|
|
for _, category := range categories {
|
|
parent, err := root.OpenRoot(category)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
current, err := existingManagedView(parent, v.root, v.name, category)
|
|
if err == nil && (current == nil || *current != attemptTarget(v.root, v.runID, v.name, category)) {
|
|
err = fmt.Errorf("managed %s view changed during attempt", category)
|
|
}
|
|
if err == nil {
|
|
if previous := v.previous[category]; previous != nil {
|
|
err = replaceManagedView(parent, v.root, v.name, category, *previous)
|
|
} else {
|
|
err = parent.Remove(v.name)
|
|
}
|
|
}
|
|
_ = parent.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|