Linking package files from pool.

This commit is contained in:
Andrey Smirnov
2013-12-24 13:56:46 +04:00
parent 94628fc035
commit 8d9062cf0f
2 changed files with 76 additions and 0 deletions

33
debian/repository.go vendored
View File

@@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
)
// Repository directory structure:
@@ -52,3 +53,35 @@ func (r *Repository) MkDir(path string) error {
func (r *Repository) CreateFile(path string) (*os.File, error) {
return os.Create(filepath.Join(r.RootPath, "public", path))
}
// LinkFromPool links package file from pool to dist's pool location
func (r *Repository) LinkFromPool(prefix string, component string, filename string, hashMD5 string) error {
sourcePath, err := r.PoolPath(filename, hashMD5)
if err != nil {
return err
}
baseName := filepath.Base(filename)
if len(baseName) < 8 {
return fmt.Errorf("package filename %s too short", baseName)
}
var subdir string
if strings.HasPrefix(baseName, "lib") {
subdir = baseName[:4]
} else {
subdir = baseName[:1]
}
poolPath := filepath.Join(r.RootPath, "public", prefix, "pool", component, subdir)
err = os.MkdirAll(poolPath, 0755)
if err != nil {
return err
}
err = os.Link(sourcePath, filepath.Join(poolPath, baseName))
return err
}

View File

@@ -4,6 +4,7 @@ import (
. "launchpad.net/gocheck"
"os"
"path/filepath"
"syscall"
)
type RepositorySuite struct {
@@ -46,3 +47,45 @@ func (s *RepositorySuite) TestCreateFile(c *C) {
_, err = os.Stat(filepath.Join(s.repo.RootPath, "public/ppa/dists/squeeze/Release"))
c.Assert(err, IsNil)
}
func (s *RepositorySuite) TestLinkFromPool(c *C) {
tests := []struct {
packageFilename string
MD5 string
expectedFilename string
}{
{
packageFilename: "pool/m/mars-invaders_1.03.deb",
MD5: "91b1a1480b90b9e269ca44d897b12575",
expectedFilename: "public/pool/main/m/mars-invaders_1.03.deb",
},
{
packageFilename: "pool/libm/libmars-invaders_1.03.deb",
MD5: "12c2a1480b90b9e269ca44d897b12575",
expectedFilename: "public/pool/main/libm/libmars-invaders_1.03.deb",
},
}
for _, t := range tests {
poolPath, err := s.repo.PoolPath(t.packageFilename, t.MD5)
c.Assert(err, IsNil)
err = os.MkdirAll(filepath.Dir(poolPath), 0755)
c.Assert(err, IsNil)
file, err := os.Create(poolPath)
c.Assert(err, IsNil)
file.Write([]byte("Contents"))
file.Close()
err = s.repo.LinkFromPool("", "main", t.packageFilename, t.MD5)
c.Assert(err, IsNil)
st, err := os.Stat(filepath.Join(s.repo.RootPath, t.expectedFilename))
c.Assert(err, IsNil)
info := st.Sys().(*syscall.Stat_t)
c.Check(info.Nlink, Equals, uint16(2))
}
}