Skip to content

Commit 885c290

Browse files
[Delegations prereq 9] Make fileSystemStore.GetMeta read metadata files dynamically (#231)
* [Delegations prereq] Use a verify.DB for delegation in client Splitting up #175 * stash * Add tests to make sure the top level targets 'delegation' edge has associated keys. Make NewDelegationsIterator return an error if the passed DB is missing the top level targets role * [Delegations prereq] Make signers addressible by key ID in LocalStore Splitting up #175 * Clarify naming * Add local_store_test.go * Another test case * [Delegations prereq] Use a verify.DB for delegation in client Splitting up #175 * stash * Add tests to make sure the top level targets 'delegation' edge has associated keys. Make NewDelegationsIterator return an error if the passed DB is missing the top level targets role * [Delegations prereq 9] Make fileSystemStore.GetMeta read metadata files dynamically
1 parent d85e0a2 commit 885c290

File tree

1 file changed

+58
-14
lines changed

1 file changed

+58
-14
lines changed

local_store.go

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ package tuf
33
import (
44
"bytes"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"io"
9+
"io/fs"
810
"io/ioutil"
911
"os"
1012
"path/filepath"
1113
"strings"
1214

1315
"github.com/theupdateframework/go-tuf/data"
1416
"github.com/theupdateframework/go-tuf/encrypted"
17+
"github.com/theupdateframework/go-tuf/internal/roles"
1518
"github.com/theupdateframework/go-tuf/internal/sets"
1619
"github.com/theupdateframework/go-tuf/pkg/keys"
1720
"github.com/theupdateframework/go-tuf/util"
@@ -218,25 +221,66 @@ func (f *fileSystemStore) stagedDir() string {
218221
return filepath.Join(f.dir, "staged")
219222
}
220223

224+
func isMetaFile(e os.DirEntry) (bool, error) {
225+
name := e.Name()
226+
if e.IsDir() || !(filepath.Ext(name) == ".json" && roles.IsTopLevelManifest(name)) {
227+
return false, nil
228+
}
229+
230+
info, err := e.Info()
231+
if err != nil {
232+
return false, err
233+
}
234+
235+
return info.Mode().IsRegular(), nil
236+
}
237+
221238
func (f *fileSystemStore) GetMeta() (map[string]json.RawMessage, error) {
222-
meta := make(map[string]json.RawMessage)
223-
var err error
224-
notExists := func(path string) bool {
225-
_, err := os.Stat(path)
226-
return os.IsNotExist(err)
227-
}
228-
for _, name := range topLevelMetadata {
229-
path := filepath.Join(f.stagedDir(), name)
230-
if notExists(path) {
231-
path = filepath.Join(f.repoDir(), name)
232-
if notExists(path) {
233-
continue
234-
}
239+
// Build a map of metadata names (e.g. root.json) to their full paths
240+
// (whether in the committed repo dir, or in the staged repo dir).
241+
metaPaths := map[string]string{}
242+
243+
rd := f.repoDir()
244+
committed, err := os.ReadDir(f.repoDir())
245+
if err != nil && !errors.Is(err, fs.ErrNotExist) {
246+
return nil, fmt.Errorf("could not list repo dir: %w", err)
247+
}
248+
249+
for _, e := range committed {
250+
imf, err := isMetaFile(e)
251+
if err != nil {
252+
return nil, err
253+
}
254+
if imf {
255+
name := e.Name()
256+
metaPaths[name] = filepath.Join(rd, name)
235257
}
236-
meta[name], err = ioutil.ReadFile(path)
258+
}
259+
260+
sd := f.stagedDir()
261+
staged, err := os.ReadDir(sd)
262+
if err != nil && !errors.Is(err, fs.ErrNotExist) {
263+
return nil, fmt.Errorf("could not list staged dir: %w", err)
264+
}
265+
266+
for _, e := range staged {
267+
imf, err := isMetaFile(e)
268+
if err != nil {
269+
return nil, err
270+
}
271+
if imf {
272+
name := e.Name()
273+
metaPaths[name] = filepath.Join(sd, name)
274+
}
275+
}
276+
277+
meta := make(map[string]json.RawMessage)
278+
for name, path := range metaPaths {
279+
f, err := ioutil.ReadFile(path)
237280
if err != nil {
238281
return nil, err
239282
}
283+
meta[name] = f
240284
}
241285
return meta, nil
242286
}

0 commit comments

Comments
 (0)