diff --git a/internal/cache/cache.go b/internal/cache/cache.go index 462c7b3..5db32a2 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -6,6 +6,7 @@ import ( "net" "net/http" "os" + "path/filepath" "sync" "sync/atomic" "time" @@ -59,6 +60,10 @@ func NewCache(cacheRoot string, mirrorURLs []string, mirroredRepos []string) (*C return nil, err } + if err := checkSymLinks(cr, mirroredRepos); err != nil { + return nil, err + } + return &Cache{ cfg: cfg, cr: cr, @@ -80,3 +85,21 @@ type UpstreamError struct { func (e *UpstreamError) Error() string { return fmt.Sprintf("upstream returned %d", e.StatusCode) } + +func checkSymLinks(cr *os.Root, repos []string) error { + for _, repo := range repos { + dirPath := filepath.Join(repo, "os/x86_64") + if err := cr.MkdirAll(dirPath, 0750); err != nil { + return err + } + lnPath := filepath.Join(dirPath, repo+".db") + srcPath := filepath.Join(dirPath, repo+".db.tar.gz") + if _, err := cr.Lstat(lnPath); err == nil { + continue // link exists + } + if err := cr.Symlink(srcPath, lnPath); err != nil { + return err + } + } + return nil +} diff --git a/internal/cache/cache_test.go b/internal/cache/cache_test.go index 300c45c..3fcb9c4 100644 --- a/internal/cache/cache_test.go +++ b/internal/cache/cache_test.go @@ -7,6 +7,8 @@ import ( "io" "net/http" "net/http/httptest" + "os" + "path/filepath" "testing" "time" ) @@ -206,3 +208,28 @@ func TestFetchRetryNonExist(t *testing.T) { t.Errorf("expected 404 got %d", upstreamErr.StatusCode) } } + +func TestCreateSymlinks(t *testing.T) { + repos := []string{"core", "extra"} + tmp := t.TempDir() + cr, err := os.OpenRoot(tmp) + if err != nil { + t.Fatalf("unable to create tmp dir: %v", err) + } + + if err := checkSymLinks(cr, repos); err != nil { + t.Fatalf("error creating links: %v", err) + } + + for _, repo := range repos { + lnfile := filepath.Join(repo, "os/x86_64", repo+".db") + expected := lnfile + ".tar.gz" + lnval, err := cr.Readlink(lnfile) + if err != nil { + t.Errorf("%s has no link: %v", repo, err) + } + if lnval != expected { + t.Errorf("expected %s got %s", expected, lnval) + } + } +}