Fix issue with ETag/MD5 comparison, add extra info in error messages. #15

This commit is contained in:
Andrey Smirnov
2014-07-21 17:43:42 +04:00
parent de0954732a
commit 237d25fe5b
+29 -5
View File
@@ -8,6 +8,7 @@ import (
"github.com/smira/aptly/files" "github.com/smira/aptly/files"
"os" "os"
"path/filepath" "path/filepath"
"strings"
) )
// PublishedStorage abstract file system with published files (actually hosted on S3) // PublishedStorage abstract file system with published files (actually hosted on S3)
@@ -51,6 +52,11 @@ func NewPublishedStorage(accessKey, secretKey, region, bucket, defaultACL, prefi
return NewPublishedStorageRaw(auth, awsRegion, bucket, defaultACL, prefix) return NewPublishedStorageRaw(auth, awsRegion, bucket, defaultACL, prefix)
} }
// String
func (storage *PublishedStorage) String() string {
return fmt.Sprintf("S3: %s:%s/%s", storage.s3.Region.Name, storage.bucket.Name, storage.prefix)
}
// MkDir creates directory recursively under public path // MkDir creates directory recursively under public path
func (storage *PublishedStorage) MkDir(path string) error { func (storage *PublishedStorage) MkDir(path string) error {
// no op for S3 // no op for S3
@@ -75,12 +81,20 @@ func (storage *PublishedStorage) PutFile(path string, sourceFilename string) err
return err return err
} }
return storage.bucket.PutReader(filepath.Join(storage.prefix, path), source, fi.Size(), "binary/octet-stream", storage.acl) err = storage.bucket.PutReader(filepath.Join(storage.prefix, path), source, fi.Size(), "binary/octet-stream", storage.acl)
if err != nil {
return fmt.Errorf("error uploading %s to %s: %s", sourceFilename, storage, err)
}
return nil
} }
// Remove removes single file under public path // Remove removes single file under public path
func (storage *PublishedStorage) Remove(path string) error { func (storage *PublishedStorage) Remove(path string) error {
return storage.bucket.Del(filepath.Join(storage.prefix, path)) err := storage.bucket.Del(filepath.Join(storage.prefix, path))
if err != nil {
return fmt.Errorf("error deleting %s from %s: %s", path, storage, err)
}
return nil
} }
// RemoveDirs removes directory structure under public path // RemoveDirs removes directory structure under public path
@@ -88,6 +102,10 @@ func (storage *PublishedStorage) RemoveDirs(path string, progress aptly.Progress
const page = 1000 const page = 1000
filelist, err := storage.Filelist(path) filelist, err := storage.Filelist(path)
if err != nil {
return err
}
numParts := (len(filelist) + page - 1) / page numParts := (len(filelist) + page - 1) / page
for i := 0; i < numParts; i++ { for i := 0; i < numParts; i++ {
@@ -104,6 +122,9 @@ func (storage *PublishedStorage) RemoveDirs(path string, progress aptly.Progress
} }
err = storage.bucket.MultiDel(paths) err = storage.bucket.MultiDel(paths)
if err != nil {
return fmt.Errorf("error deleting multiple paths from %s: %s", storage, err)
}
if err != nil { if err != nil {
return err return err
} }
@@ -135,14 +156,17 @@ func (storage *PublishedStorage) LinkFromPool(publishedDirectory string, sourceP
dstKey, err = storage.bucket.GetKey(poolPath) dstKey, err = storage.bucket.GetKey(poolPath)
if err != nil { if err != nil {
if s3err, ok := err.(*s3.Error); !ok || s3err.StatusCode != 404 { if s3err, ok := err.(*s3.Error); !ok || s3err.StatusCode != 404 {
return err return fmt.Errorf("error getting information about %s from %s: %s", poolPath, storage, err)
} }
} else { } else {
if dstKey.ETag == sourceMD5 { fmt.Printf("dstKey.Etag = %#v, sourceMD5 = %#v\n", dstKey.ETag, sourceMD5)
if strings.Replace(dstKey.ETag, "\"", "", -1) == sourceMD5 {
fmt.Printf("skipping upload\n")
return nil return nil
} }
} }
fmt.Printf("uploading\n")
return storage.PutFile(relPath, sourcePath) return storage.PutFile(relPath, sourcePath)
} }
@@ -157,7 +181,7 @@ func (storage *PublishedStorage) Filelist(prefix string) ([]string, error) {
for { for {
contents, err := storage.bucket.List(prefix, "", marker, 1000) contents, err := storage.bucket.List(prefix, "", marker, 1000)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("error listing under prefix %s in %s: %s", prefix, storage, err)
} }
lastKey := "" lastKey := ""
for _, key := range contents.Contents { for _, key := range contents.Contents {