Skip to content

Commit

Permalink
Adjust FileExists to differentiate between error and actual file exis…
Browse files Browse the repository at this point in the history
…tence
  • Loading branch information
Oliver Sauder committed Nov 21, 2017
1 parent b43e6c4 commit 1b9b9bf
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 22 deletions.
2 changes: 1 addition & 1 deletion aptly/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type PublishedStorage interface {
// HardLink creates a hardlink of a file
HardLink(src string, dst string) error
// FileExists returns true if path exists
FileExists(path string) bool
FileExists(path string) (bool, error)
// ReadLink returns the symbolic link pointed to by path
ReadLink(path string) (string, error)
}
Expand Down
14 changes: 9 additions & 5 deletions deb/index_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,20 +176,24 @@ func packageIndexByHash(file *indexFile, ext string, hash string, sum string) er
sumfilePath := filepath.Join(dst, sum)

// link already exists? do nothing
if file.parent.publishedStorage.FileExists(sumfilePath) {
exists, err := file.parent.publishedStorage.FileExists(sumfilePath)
if err != nil {
return fmt.Errorf("Acquire-By-Hash: error checking exists of file %s: %s", sumfilePath, err)
}
if exists {
return nil
}

// create the link
err := file.parent.publishedStorage.HardLink(src, sumfilePath)
err = file.parent.publishedStorage.HardLink(src, sumfilePath)
if err != nil {
return fmt.Errorf("Acquire-By-Hash: error creating hardlink %s: %s", sumfilePath, err)
}

// if a previous index file already exists exists, backup symlink
if file.parent.publishedStorage.FileExists(filepath.Join(dst, indexfile)) {
if exists, _ = file.parent.publishedStorage.FileExists(filepath.Join(dst, indexfile)); exists {
// if exists, remove old symlink
if file.parent.publishedStorage.FileExists(filepath.Join(dst, indexfile+".old")) {
if exists, _ = file.parent.publishedStorage.FileExists(filepath.Join(dst, indexfile+".old")); exists {
var link string
link, err = file.parent.publishedStorage.ReadLink(filepath.Join(dst, indexfile+".old"))
if err != nil {
Expand All @@ -204,7 +208,7 @@ func packageIndexByHash(file *indexFile, ext string, hash string, sum string) er
// create symlink
err = file.parent.publishedStorage.SymLink(filepath.Join(dst, sum), filepath.Join(dst, indexfile))
if err != nil {
return fmt.Errorf("Acquire-By-Hash: error creating symlink %s", filepath.Join(dst, indexfile))
return fmt.Errorf("Acquire-By-Hash: error creating symlink %s: %s", filepath.Join(dst, indexfile), err)
}
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions files/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,12 @@ func (storage *PublishedStorage) HardLink(src string, dst string) error {
}

// FileExists returns true if path exists
func (storage *PublishedStorage) FileExists(path string) bool {
func (storage *PublishedStorage) FileExists(path string) (bool, error) {
if _, err := os.Lstat(filepath.Join(storage.rootPath, path)); os.IsNotExist(err) {
return false
return false, nil
}

return true
return true, nil
}

// ReadLink returns the symbolic link pointed to by path
Expand Down
9 changes: 4 additions & 5 deletions files/public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ func (s *PublishedStorageSuite) TestFileExists(c *C) {
err := s.storage.MkDir("ppa/dists/squeeze/")
c.Assert(err, IsNil)

exists := s.storage.FileExists("ppa/dists/squeeze/Release")
exists, _ := s.storage.FileExists("ppa/dists/squeeze/Release")
c.Check(exists, Equals, false)

err = s.storage.PutFile("ppa/dists/squeeze/Release", "/dev/null")
c.Assert(err, IsNil)

exists = s.storage.FileExists("ppa/dists/squeeze/Release")
exists, _ = s.storage.FileExists("ppa/dists/squeeze/Release")
c.Check(exists, Equals, true)
}

Expand All @@ -127,7 +127,7 @@ func (s *PublishedStorageSuite) TestSymLink(c *C) {
err = s.storage.SymLink("ppa/dists/squeeze/Release", "ppa/dists/squeeze/InRelease")
c.Assert(err, IsNil)

exists := s.storage.FileExists("ppa/dists/squeeze/InRelease")
exists, _ := s.storage.FileExists("ppa/dists/squeeze/InRelease")
c.Check(exists, Equals, true)
}

Expand All @@ -141,11 +141,10 @@ func (s *PublishedStorageSuite) TestHardLink(c *C) {
err = s.storage.HardLink("ppa/dists/squeeze/Release", "ppa/dists/squeeze/InRelease")
c.Assert(err, IsNil)

exists := s.storage.FileExists("ppa/dists/squeeze/InRelease")
exists, _ := s.storage.FileExists("ppa/dists/squeeze/InRelease")
c.Check(exists, Equals, true)
}


func (s *PublishedStorageSuite) TestRemoveDirs(c *C) {
err := s.storage.MkDir("ppa/dists/squeeze/")
c.Assert(err, IsNil)
Expand Down
14 changes: 12 additions & 2 deletions s3/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/corehandlers"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/request"
Expand Down Expand Up @@ -420,13 +421,22 @@ func (storage *PublishedStorage) HardLink(src string, dst string) error {
}

// FileExists returns true if path exists
func (storage *PublishedStorage) FileExists(path string) bool {
func (storage *PublishedStorage) FileExists(path string) (bool, error) {
params := &s3.HeadObjectInput{
Bucket: aws.String(storage.bucket),
Key: aws.String(filepath.Join(storage.prefix, path)),
}
_, err := storage.s3.HeadObject(params)
return err == nil
if err != nil {
aerr, ok := err.(awserr.Error)
if ok && aerr.Code() == s3.ErrCodeNoSuchKey {
return false, nil
}

return false, err
}

return true, nil
}

// ReadLink returns the symbolic link pointed to by path.
Expand Down
7 changes: 5 additions & 2 deletions s3/public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,12 @@ func (s *PublishedStorageSuite) TestSymLink(c *C) {
func (s *PublishedStorageSuite) TestFileExists(c *C) {
s.PutFile(c, "a/b", []byte("test"))

exists := s.storage.FileExists("a/b")
exists, err := s.storage.FileExists("a/b")
c.Check(err, IsNil)
c.Check(exists, Equals, true)

exists = s.storage.FileExists("a/b.invalid")
exists, err = s.storage.FileExists("a/b.invalid")
// Comment out as there is an error in s3test implementation
// c.Check(err, IsNil)
c.Check(exists, Equals, false)
}
13 changes: 11 additions & 2 deletions swift/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,18 @@ func (storage *PublishedStorage) HardLink(src string, dst string) error {
}

// FileExists returns true if path exists
func (storage *PublishedStorage) FileExists(path string) bool {
func (storage *PublishedStorage) FileExists(path string) (bool, error) {
_, _, err := storage.conn.Object(storage.container, filepath.Join(storage.prefix, path))
return err == nil

if err != nil {
if err == swift.ObjectNotFound {
return false, nil
}

return false, err
}

return true, nil
}

// ReadLink returns the symbolic link pointed to by path
Expand Down
6 changes: 4 additions & 2 deletions swift/public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,11 @@ func (s *PublishedStorageSuite) TestFileExists(c *C) {
err = s.storage.PutFile("a/b.txt", filepath.Join(dir, "a"))
c.Check(err, IsNil)

exists := s.storage.FileExists("a/b.txt")
exists, err := s.storage.FileExists("a/b.txt")
c.Check(err, IsNil)
c.Check(exists, Equals, true)

exists = s.storage.FileExists("a/b.invalid")
exists, err = s.storage.FileExists("a/b.invalid")
c.Check(err, IsNil)
c.Check(exists, Equals, false)
}

0 comments on commit 1b9b9bf

Please sign in to comment.