Skip to content

Commit

Permalink
fix: dir package optimize
Browse files Browse the repository at this point in the history
1. GetPath logic: if the given path doesn't exist, return the first
   possible in the union directories, so when create a new file the
   UnionDirFS will select user directory which was provided as the first
   directory when create the UnionDirFS
2. change the constant name

Signed-off-by: Junjie Gao <junjiegao@microsoft.com>
  • Loading branch information
JeyJeyGao committed Aug 12, 2022
1 parent f3bae83 commit 51a69ee
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 37 deletions.
21 changes: 15 additions & 6 deletions dir/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ func (u unionDirFS) Open(name string) (fs.File, error) {
// GetPath returns the path of the named file or directory under the FS
//
// if path exists, it returns the first existing path in union directories (dirs)
// if path doesn't exist, it returns fs.ErrNotExist error
//
// if path doesn't exist, it returns the first possible path in the union directories
// for creating new file and a fs.ErrNotExist error.
func (u unionDirFS) GetPath(elem ...string) (string, error) {
var targetPath string
pathSuffix := path.Join(elem...)
for _, dir := range u.Dirs {
targetPath = filepath.Join(dir.Root(), pathSuffix)
_, err := fs.Stat(dir, pathSuffix)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
Expand All @@ -92,10 +92,19 @@ func (u unionDirFS) GetPath(elem ...string) (string, error) {
}
// got the first existing file path and break
// return the path with current OS separator
return targetPath, nil
return filepath.Join(dir.Root(), pathSuffix), nil
}
// if the given path does not exist, return the first possible path in the
// union directories for creating new file.
var fallbackPath string
if len(u.Dirs) == 0 {
return "", &fs.PathError{
Op: "getpath",
Err: errors.New("the union directory is empty"),
}
}
// return the last possible path for creating new file
return targetPath, &fs.PathError{
fallbackPath = filepath.Join(u.Dirs[0].Root(), pathSuffix)
return fallbackPath, &fs.PathError{
Op: "getpath",
Err: fs.ErrNotExist,
Path: pathSuffix,
Expand Down
2 changes: 1 addition & 1 deletion dir/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func TestPath(t *testing.T) {
path: []string{"plugin/a/a.exe"},
},
{name: "Path does not exist",
want: "system/plugin/c/c.exe",
want: "user/plugin/c/c.exe",
usrFS: fstest.MapFS{"plugin/a/a.exe": {Data: []byte("user a")}},
sysFS: fstest.MapFS{"plugin/a/b.exe": {Data: []byte("system b")}},
path: []string{"plugin/c/c.exe"},
Expand Down
26 changes: 11 additions & 15 deletions dir/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ package dir

import (
"errors"
"fmt"
"io/fs"

"github.com/opencontainers/go-digest"
)

const (
// CertificateExtension defines the extension of the certificate files
CertificateExtension = ".crt"

// ConfigFile is the name of config file
ConfigFile = "config.json"

// KeyExtension defines the extension of the key files
KeyExtension = ".key"
// LocalCertificateExtension defines the extension of the certificate files
LocalCertificateExtension = ".crt"

// LocalKeyExtension defines the extension of the key files
LocalKeyExtension = ".key"

// LocalKeysDir is the directory name for local key store
LocalKeysDir = "localkeys"
Expand Down Expand Up @@ -60,17 +59,14 @@ func (p *PathManager) Config() string {
return path
}

// LocalKey returns path of the local private keys or certificate
// LocalKey returns path of the local private key and it's certificate
// in the localkeys directory
//
// extension: support .crt|.key
func (p *PathManager) Localkey(name string, extension string) string {
if extension != KeyExtension && extension != CertificateExtension {
panic(fmt.Sprintf("doesn't support the extension `%s`", extension))
}
path, err := p.UserConfigFS.GetPath(LocalKeysDir, name+extension)
func (p *PathManager) Localkey(name string) (keyPath, certPath string) {
keyPath, err := p.UserConfigFS.GetPath(LocalKeysDir, name+LocalKeyExtension)
checkError(err)
return path
certPath, err = p.UserConfigFS.GetPath(LocalKeysDir, name+LocalCertificateExtension)
checkError(err)
return keyPath, certPath
}

// SigningKeyConfig return the path of signingkeys.json files
Expand Down
19 changes: 4 additions & 15 deletions dir/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,24 +137,13 @@ func TestPathManager_LocalKey(t *testing.T) {
NewRootedFS("/home/exampleuser/.config/notation/", nil),
),
}
localkeyPath := path.Localkey("key1", KeyExtension)
if localkeyPath != "/home/exampleuser/.config/notation/localkeys/key1"+KeyExtension {
keyPath, certPath := path.Localkey("key1")
if keyPath != "/home/exampleuser/.config/notation/localkeys/key1"+LocalKeyExtension {
t.Fatal("get Localkey() failed.")
}
}

func TestPathManager_LocalKeyFailed(t *testing.T) {
path := &PathManager{
UserConfigFS: NewUnionDirFS(
NewRootedFS("/home/exampleuser/.config/notation/", nil),
),
if certPath != "/home/exampleuser/.config/notation/localkeys/key1"+LocalCertificateExtension {
t.Fatal("get Localkey() failed.")
}
defer func() {
if d := recover(); d == nil {
t.Fatal("get Localkey() extension check failed.")
}
}()
path.Localkey("key1", ".acr")
}

func TestPathManager_SigningKeyConfig(t *testing.T) {
Expand Down

0 comments on commit 51a69ee

Please sign in to comment.