create db links if not exist for all configured repos
This commit is contained in:
Vendored
+23
@@ -6,6 +6,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
@@ -59,6 +60,10 @@ func NewCache(cacheRoot string, mirrorURLs []string, mirroredRepos []string) (*C
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := checkSymLinks(cr, mirroredRepos); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return &Cache{
|
return &Cache{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
cr: cr,
|
cr: cr,
|
||||||
@@ -80,3 +85,21 @@ type UpstreamError struct {
|
|||||||
func (e *UpstreamError) Error() string {
|
func (e *UpstreamError) Error() string {
|
||||||
return fmt.Sprintf("upstream returned %d", e.StatusCode)
|
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
|
||||||
|
}
|
||||||
|
|||||||
Vendored
+27
@@ -7,6 +7,8 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -206,3 +208,28 @@ func TestFetchRetryNonExist(t *testing.T) {
|
|||||||
t.Errorf("expected 404 got %d", upstreamErr.StatusCode)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user