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 newest-file selection for offline cache #1425

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion pkg/apk/apk/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,15 @@ func (t *cacheTransport) fetchOffline(cacheFile string) (*http.Response, error)
return nil, err
}

if fi.ModTime().After(newest.ModTime()) {
if (fi.ModTime().After(newest.ModTime()) || newest.IsDir()) && !fi.IsDir() {
newest = fi
}
}

if newest.IsDir() {
return nil, fmt.Errorf("%s is a directory", newest.Name())
}

f, err := os.Open(filepath.Join(cacheDir, newest.Name()))
if err != nil {
return nil, err
Expand Down
41 changes: 41 additions & 0 deletions pkg/apk/apk/implementation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,47 @@ func TestSetRepositories_Empty(t *testing.T) {
require.Error(t, err)
}

func TestInitKeyringCache(t *testing.T) {
cacheDir := t.TempDir()

u, _ := url.Parse("https://alpinelinux.org/keys/alpine-devel%40lists.alpinelinux.org-4a6a0840.rsa.pub")
mainkeyPath, err := cachePathFromURL(cacheDir, *u)
require.NoError(t, err)
u, _ = url.Parse("https://alpinelinux.org/keys/pseudosubkey/alpine-devel%40lists.alpinelinux.org-deadbeef.rsa.pub")
subkeyPath, err := cachePathFromURL(cacheDir, *u)
require.NoError(t, err)

err = os.MkdirAll(filepath.Dir(mainkeyPath), 0o755)
require.NoError(t, err)
err = os.MkdirAll(filepath.Dir(subkeyPath), 0o755)
require.NoError(t, err)

os.WriteFile(subkeyPath, []byte(testDemoKey), 0o644) //nolint:gosec
os.WriteFile(mainkeyPath, []byte(testDemoKey), 0o644) //nolint:gosec

src := apkfs.NewMemFS()
a, err := New(WithFS(src), WithIgnoreMknodErrors(ignoreMknodErrors), WithCache(cacheDir, true, nil))
require.NoError(t, err)

// Add a remote key and a nested remote key
keyfiles := []string{
"https://alpinelinux.org/keys/alpine-devel%40lists.alpinelinux.org-4a6a0840.rsa.pub",
"https://alpinelinux.org/keys/pseudosubkey/alpine-devel%40lists.alpinelinux.org-deadbeef.rsa.pub",
}
// ensure we send things from local
a.SetClient(&http.Client{
Transport: &testLocalTransport{root: testPrimaryPkgDir, basenameOnly: true},
})

require.NoError(t, a.InitKeyring(context.Background(), keyfiles, nil))
// InitKeyring should have copied the local key and remote key to the right place
fi, err := src.ReadDir(DefaultKeyRingPath)
// should be no error reading them
require.NoError(t, err)
// should be 2 keys
require.Len(t, fi, 2)
}

func TestInitKeyring(t *testing.T) {
src := apkfs.NewMemFS()
a, err := New(WithFS(src), WithIgnoreMknodErrors(ignoreMknodErrors))
Expand Down