mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-01-12 03:21:33 +00:00
Package repository, first version.
This commit is contained in:
37
debian/repository.go
vendored
Normal file
37
debian/repository.go
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
package debian
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Repository abstract file system with package pool and published package repos
|
||||
type Repository struct {
|
||||
RootPath string
|
||||
}
|
||||
|
||||
// NewRepository creates new instance of repository which specified root
|
||||
func NewRepository(root string) *Repository {
|
||||
return &Repository{RootPath: root}
|
||||
}
|
||||
|
||||
// PoolPath returns full path to package file in pool
|
||||
//
|
||||
// PoolPath checks that final path doesn't go out of repository root path
|
||||
func (r *Repository) PoolPath(filename string) (string, error) {
|
||||
filename = filepath.Clean(filename)
|
||||
if strings.HasPrefix(filename, ".") {
|
||||
return "", fmt.Errorf("filename %s starts with dot", filename)
|
||||
}
|
||||
|
||||
if filepath.IsAbs(filename) {
|
||||
return "", fmt.Errorf("absolute filename %s not supported", filename)
|
||||
}
|
||||
|
||||
if strings.HasPrefix(filename, "pool/") {
|
||||
filename = filename[5:]
|
||||
}
|
||||
|
||||
return filepath.Join(r.RootPath, "pool", filename), nil
|
||||
}
|
||||
39
debian/repository_test.go
vendored
Normal file
39
debian/repository_test.go
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
package debian
|
||||
|
||||
import (
|
||||
. "launchpad.net/gocheck"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type RepositorySuite struct {
|
||||
repo *Repository
|
||||
}
|
||||
|
||||
var _ = Suite(&RepositorySuite{})
|
||||
|
||||
func (s *RepositorySuite) SetUpTest(c *C) {
|
||||
s.repo = NewRepository(c.MkDir())
|
||||
}
|
||||
|
||||
func (s *RepositorySuite) TestPoolPath(c *C) {
|
||||
path, err := s.repo.PoolPath("a/b/package.deb")
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(path, Equals, filepath.Join(s.repo.RootPath, "pool", "a/b/package.deb"))
|
||||
|
||||
path, err = s.repo.PoolPath("pool/a/b/package.deb")
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(path, Equals, filepath.Join(s.repo.RootPath, "pool", "a/b/package.deb"))
|
||||
|
||||
_, err = s.repo.PoolPath("/dev/stdin")
|
||||
c.Assert(err, ErrorMatches, "absolute filename.*")
|
||||
|
||||
_, err = s.repo.PoolPath("../../../etc/passwd")
|
||||
c.Assert(err, ErrorMatches, ".*starts with dot")
|
||||
|
||||
_, err = s.repo.PoolPath("pool/a/../../../etc/passwd")
|
||||
c.Assert(err, ErrorMatches, ".*starts with dot")
|
||||
|
||||
path, err = s.repo.PoolPath("./etc/passwd")
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(path, Equals, filepath.Join(s.repo.RootPath, "pool", "etc/passwd"))
|
||||
}
|
||||
Reference in New Issue
Block a user