New methods for public root: Filelist and Remove. #8

This commit is contained in:
Andrey Smirnov
2014-04-22 17:05:32 +04:00
parent 1bac201687
commit d69eaeff4e
3 changed files with 62 additions and 1 deletions

View File

@@ -34,8 +34,12 @@ type PublishedStorage interface {
CreateFile(path string) (*os.File, error)
// RemoveDirs removes directory structure under public path
RemoveDirs(path string) error
// Remove removes single file under public path
Remove(path string) error
// LinkFromPool links package file from pool to dist's pool location
LinkFromPool(prefix string, component string, poolDirectory string, sourcePool PackagePool, sourcePath string) (string, error)
LinkFromPool(publishedDirectory string, sourcePool PackagePool, sourcePath string) error
// Filelist returns list of files under prefix
Filelist(prefix string) ([]string, error)
// ChecksumsForFile proxies requests to utils.ChecksumsForFile, joining public path
ChecksumsForFile(path string) (utils.ChecksumInfo, error)
// RenameFile renames (moves) file

View File

@@ -38,6 +38,13 @@ func (storage *PublishedStorage) CreateFile(path string) (*os.File, error) {
return os.Create(filepath.Join(storage.rootPath, path))
}
// Remove removes single file under public path
func (storage *PublishedStorage) Remove(path string) error {
filepath := filepath.Join(storage.rootPath, path)
fmt.Printf("Removing %s...\n", filepath)
return os.Remove(filepath)
}
// RemoveDirs removes directory structure under public path
func (storage *PublishedStorage) RemoveDirs(path string) error {
filepath := filepath.Join(storage.rootPath, path)
@@ -72,6 +79,24 @@ func (storage *PublishedStorage) LinkFromPool(publishedDirectory string, sourceP
return os.Link(sourcePath, filepath.Join(poolPath, baseName))
}
// Filelist returns list of files under prefix
func (storage *PublishedStorage) Filelist(prefix string) ([]string, error) {
root := filepath.Join(storage.rootPath, prefix)
result := []string{}
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
result = append(result, path[len(root)+1:])
}
return nil
})
return result, err
}
// ChecksumsForFile proxies requests to utils.ChecksumsForFile, joining public path
func (storage *PublishedStorage) ChecksumsForFile(path string) (utils.ChecksumInfo, error) {
return utils.ChecksumsForFile(filepath.Join(storage.rootPath, path))

View File

@@ -44,6 +44,23 @@ func (s *PublishedStorageSuite) TestCreateFile(c *C) {
c.Assert(err, IsNil)
}
func (s *PublishedStorageSuite) TestFilelist(c *C) {
err := s.storage.MkDir("ppa/pool/main/a/ab/")
c.Assert(err, IsNil)
file, err := s.storage.CreateFile("ppa/pool/main/a/ab/a.deb")
c.Assert(err, IsNil)
defer file.Close()
file2, err := s.storage.CreateFile("ppa/pool/main/a/ab/b.deb")
c.Assert(err, IsNil)
defer file2.Close()
list, err := s.storage.Filelist("ppa/pool/main/")
c.Check(err, IsNil)
c.Check(list, DeepEquals, []string{"a/ab/a.deb", "a/ab/b.deb"})
}
func (s *PublishedStorageSuite) TestRenameFile(c *C) {
err := s.storage.MkDir("ppa/dists/squeeze/")
c.Assert(err, IsNil)
@@ -74,6 +91,21 @@ func (s *PublishedStorageSuite) TestRemoveDirs(c *C) {
c.Assert(os.IsNotExist(err), Equals, true)
}
func (s *PublishedStorageSuite) TestRemove(c *C) {
err := s.storage.MkDir("ppa/dists/squeeze/")
c.Assert(err, IsNil)
file, err := s.storage.CreateFile("ppa/dists/squeeze/Release")
c.Assert(err, IsNil)
defer file.Close()
err = s.storage.Remove("ppa/dists/squeeze/Release")
_, err = os.Stat(filepath.Join(s.storage.rootPath, "ppa/dists/squeeze/Release"))
c.Assert(err, NotNil)
c.Assert(os.IsNotExist(err), Equals, true)
}
func (s *PublishedStorageSuite) TestLinkFromPool(c *C) {
tests := []struct {
prefix string