Files
EvoBGP/internal/bundle/extract.go
T

47 lines
1.2 KiB
Go

package bundle
import (
"fmt"
"os"
pathpkg "path"
"path/filepath"
"strings"
)
// WriteExtractedFiles writes payload files from a verified bundle under root (directories created as needed).
func WriteExtractedFiles(root string, v *VerifiedContents) error {
root = filepath.Clean(root)
for _, fe := range v.Manifest.Files {
data, ok := v.Files[fe.Path]
if !ok {
return fmt.Errorf("bundle: missing file %q", fe.Path)
}
rel := pathpkg.Clean(strings.TrimPrefix(fe.Path, "/"))
if rel == "." || strings.HasPrefix(rel, "..") {
return fmt.Errorf("bundle: unsafe path %q", fe.Path)
}
dest := filepath.Join(root, filepath.FromSlash(rel))
relToRoot, err := filepath.Rel(root, dest)
if err != nil || strings.HasPrefix(relToRoot, "..") {
return fmt.Errorf("bundle: path escapes root: %q", fe.Path)
}
if err := os.MkdirAll(filepath.Dir(dest), 0o755); err != nil {
return err
}
if err := os.WriteFile(dest, data, 0o644); err != nil {
return err
}
}
return nil
}
// FindMainBirdConf returns the first path ending with bird.conf from extracted manifest order.
func (v *VerifiedContents) FindMainBirdConf() string {
for _, fe := range v.Manifest.Files {
if strings.HasSuffix(fe.Path, "bird.conf") {
return fe.Path
}
}
return ""
}