Rework generation of links in repo, use Source path.

This commit is contained in:
Andrey Smirnov
2013-12-26 00:37:41 +04:00
parent 14d71a654a
commit 67bcd1dfb0
2 changed files with 33 additions and 18 deletions

29
debian/repository.go vendored
View File

@@ -56,35 +56,40 @@ func (r *Repository) CreateFile(path string) (*os.File, error) {
}
// LinkFromPool links package file from pool to dist's pool location
func (r *Repository) LinkFromPool(prefix string, component string, filename string, hashMD5 string) error {
func (r *Repository) LinkFromPool(prefix string, component string, filename string, hashMD5 string, source string) (string, error) {
sourcePath, err := r.PoolPath(filename, hashMD5)
if err != nil {
return err
return "", err
}
baseName := filepath.Base(filename)
if len(baseName) < 8 {
return fmt.Errorf("package filename %s too short", baseName)
if len(source) < 3 {
return "", fmt.Errorf("package source %s too short", source)
}
var subdir string
if strings.HasPrefix(baseName, "lib") {
subdir = baseName[:4]
if strings.HasPrefix(source, "lib") {
subdir = source[:4]
} else {
subdir = baseName[:1]
subdir = source[:1]
}
poolPath := filepath.Join(r.RootPath, "public", prefix, "pool", component, subdir)
baseName := filepath.Base(filename)
relPath := filepath.Join("pool", component, subdir, source, baseName)
poolPath := filepath.Join(r.RootPath, "public", prefix, "pool", component, subdir, source)
err = os.MkdirAll(poolPath, 0755)
if err != nil {
return err
return "", err
}
_, err = os.Stat(filepath.Join(poolPath, baseName))
if err == nil { // already exists, skip
return relPath, nil
}
err = os.Link(sourcePath, filepath.Join(poolPath, baseName))
return err
return relPath, err
}
// ChecksumsForFile proxies requests to utils.ChecksumsForFile, joining public path

View File

@@ -52,17 +52,26 @@ func (s *RepositorySuite) TestLinkFromPool(c *C) {
tests := []struct {
packageFilename string
MD5 string
source string
expectedFilename string
}{
{
{ // package name regular
packageFilename: "pool/m/mars-invaders_1.03.deb",
MD5: "91b1a1480b90b9e269ca44d897b12575",
expectedFilename: "public/pool/main/m/mars-invaders_1.03.deb",
source: "mars-invaders",
expectedFilename: "pool/main/m/mars-invaders/mars-invaders_1.03.deb",
},
{
{ // lib-like filename
packageFilename: "pool/libm/libmars-invaders_1.03.deb",
MD5: "12c2a1480b90b9e269ca44d897b12575",
expectedFilename: "public/pool/main/libm/libmars-invaders_1.03.deb",
source: "libmars-invaders",
expectedFilename: "pool/main/libm/libmars-invaders/libmars-invaders_1.03.deb",
},
{ // duplicate link, shouldn't panic
packageFilename: "pool/m/mars-invaders_1.03.deb",
MD5: "91b1a1480b90b9e269ca44d897b12575",
source: "mars-invaders",
expectedFilename: "pool/main/m/mars-invaders/mars-invaders_1.03.deb",
},
}
@@ -79,10 +88,11 @@ func (s *RepositorySuite) TestLinkFromPool(c *C) {
file.Write([]byte("Contents"))
file.Close()
err = s.repo.LinkFromPool("", "main", t.packageFilename, t.MD5)
path, err := s.repo.LinkFromPool("", "main", t.packageFilename, t.MD5, t.source)
c.Assert(err, IsNil)
c.Assert(path, Equals, t.expectedFilename)
st, err := os.Stat(filepath.Join(s.repo.RootPath, t.expectedFilename))
st, err := os.Stat(filepath.Join(s.repo.RootPath, "public", t.expectedFilename))
c.Assert(err, IsNil)
info := st.Sys().(*syscall.Stat_t)