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

fix: dir package optimize #104

Merged
merged 1 commit into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
JeyJeyGao marked this conversation as resolved.
Show resolved Hide resolved
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.")
rgnote marked this conversation as resolved.
Show resolved Hide resolved
}
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