Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Delegations prereq 9] Make fileSystemStore.GetMeta read metadata files dynamically #231

Merged
merged 11 commits into from
Mar 9, 2022
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)) {
asraa marked this conversation as resolved.
Show resolved Hide resolved
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