1package workbench23import (4 "fmt"5 "os"6 "path/filepath"7 "strings"89 "github.com/FelineStateMachine/atlas/format/vnext"10)1112// completedArtifact keeps the exact regular file that passed the native13// integrity gate open. Inspector and download consumers therefore read the14// validated inode rather than reopening a replaceable pathname.15type completedArtifact struct {16 path string17 name string18 source *os.File19 reader *vnext.Reader20 descriptor vnext.Descriptor21}2223func (artifact *completedArtifact) Close() error { return artifact.source.Close() }2425func (w *Workbench) openCompletedArtifact() (*completedArtifact, error) {26 run := w.supervisor.Snapshot()27 if run.Name != "build" || run.Running || run.Failed || run.FinishedAt.IsZero() || run.Artifact == "" {28 return nil, fmt.Errorf("no successful completed Atlas build is available")29 }30 absLibrary, err := filepath.Abs(w.targets.Registry)31 if err != nil {32 return nil, err33 }34 absArtifact, err := filepath.Abs(run.Artifact)35 if err != nil {36 return nil, err37 }38 relative, err := filepath.Rel(absLibrary, absArtifact)39 if err != nil || relative == "." || relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) || filepath.Ext(relative) != ".atlas" {40 return nil, fmt.Errorf("build artifact is outside the Atlas library")41 }42 root, err := os.OpenRoot(absLibrary)43 if err != nil {44 return nil, fmt.Errorf("open Atlas library: %w", err)45 }46 defer root.Close()47 linkInfo, err := root.Lstat(relative)48 if err != nil {49 return nil, fmt.Errorf("stat completed Atlas artifact: %w", err)50 }51 if linkInfo.Mode()&os.ModeSymlink != 0 {52 return nil, fmt.Errorf("completed Atlas artifact is a symbolic link")53 }54 source, err := root.Open(relative)55 if err != nil {56 return nil, fmt.Errorf("open completed Atlas artifact: %w", err)57 }58 info, err := source.Stat()59 if err != nil {60 source.Close()61 return nil, fmt.Errorf("stat opened Atlas artifact: %w", err)62 }63 if !info.Mode().IsRegular() {64 source.Close()65 return nil, fmt.Errorf("completed Atlas artifact is not a regular file")66 }67 reader, err := vnext.Open(source, info.Size(), vnext.StandardSchema())68 if err != nil {69 source.Close()70 return nil, fmt.Errorf("open completed Atlas artifact: %w", err)71 }72 if err := reader.Validate(); err != nil {73 source.Close()74 return nil, fmt.Errorf("validate completed Atlas artifact: %w", err)75 }76 return &completedArtifact{77 path: absArtifact, name: filepath.Base(absArtifact), source: source, reader: reader,78 descriptor: vnext.DescriptorOf(absArtifact, reader.VolumeID, reader.Release, info.Size()),79 }, nil80}