Skip to content

Commit

Permalink
[Delegations prereq 9] Make fileSystemStore.GetMeta read metadata fil…
Browse files Browse the repository at this point in the history
…es dynamically
  • Loading branch information
ethan-lowman-dd committed Mar 8, 2022
1 parent ac27c73 commit e43aa52
Showing 1 changed file with 58 additions and 14 deletions.
72 changes: 58 additions & 14 deletions local_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package tuf
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/theupdateframework/go-tuf/data"
"github.com/theupdateframework/go-tuf/encrypted"
"github.com/theupdateframework/go-tuf/internal/roles"
"github.com/theupdateframework/go-tuf/internal/sets"
"github.com/theupdateframework/go-tuf/pkg/keys"
"github.com/theupdateframework/go-tuf/util"
Expand Down Expand Up @@ -218,25 +221,66 @@ func (f *fileSystemStore) stagedDir() string {
return filepath.Join(f.dir, "staged")
}

func isMetaFile(e os.DirEntry) (bool, error) {
name := e.Name()
if e.IsDir() || !(filepath.Ext(name) == ".json" && roles.IsTopLevelManifest(name)) {
return false, nil
}

info, err := e.Info()
if err != nil {
return false, err
}

return info.Mode().IsRegular(), nil
}

func (f *fileSystemStore) GetMeta() (map[string]json.RawMessage, error) {
meta := make(map[string]json.RawMessage)
var err error
notExists := func(path string) bool {
_, err := os.Stat(path)
return os.IsNotExist(err)
}
for _, name := range topLevelMetadata {
path := filepath.Join(f.stagedDir(), name)
if notExists(path) {
path = filepath.Join(f.repoDir(), name)
if notExists(path) {
continue
}
// Build a map of metadata names (e.g. root.json) to their full paths
// (whether in the committed repo dir, or in the staged repo dir).
metaPaths := map[string]string{}

rd := f.repoDir()
committed, err := os.ReadDir(f.repoDir())
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return nil, fmt.Errorf("could not list repo dir: %w", err)
}

for _, e := range committed {
imf, err := isMetaFile(e)
if err != nil {
return nil, err
}
if imf {
name := e.Name()
metaPaths[name] = filepath.Join(rd, name)
}
meta[name], err = ioutil.ReadFile(path)
}

sd := f.stagedDir()
staged, err := os.ReadDir(sd)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return nil, fmt.Errorf("could not list staged dir: %w", err)
}

for _, e := range staged {
imf, err := isMetaFile(e)
if err != nil {
return nil, err
}
if imf {
name := e.Name()
metaPaths[name] = filepath.Join(sd, name)
}
}

meta := make(map[string]json.RawMessage)
for name, path := range metaPaths {
f, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
meta[name] = f
}
return meta, nil
}
Expand Down

0 comments on commit e43aa52

Please sign in to comment.