Extend PublishedStorage interface for Acquire-By-Hash

Signed-off-by: André Roth <neolynx@gmail.com>
This commit is contained in:
André Roth
2017-04-17 23:03:43 +02:00
committed by Oliver Sauder
parent bb2db7e500
commit e07912770e
5 changed files with 113 additions and 19 deletions
+8
View File
@@ -73,6 +73,14 @@ type PublishedStorage interface {
Filelist(prefix string) ([]string, error)
// RenameFile renames (moves) file
RenameFile(oldName, newName string) error
// SymLink creates a symbolic link, which can be read with ReadLink
SymLink(src string, dst string) error
// HardLink creates a hardlink of a file
HardLink(src string, dst string) error
// FileExists returns true if path exists
FileExists(path string) bool
// ReadLink returns the symbolic link pointed to by path
ReadLink(path string) (string, error)
}
// FileSystemPublishedStorage is published storage on filesystem
+27 -19
View File
@@ -126,22 +126,20 @@ func (file *indexFile) Finalize(signer pgp.Signer) error {
if file.accessByHash {
sums := file.parent.generatedFiles[file.relativePath+ext]
storage := file.parent.publishedStorage.(aptly.FileSystemPublishedStorage).PublicPath()
src := filepath.Join(storage, file.parent.basePath, file.relativePath)
err = packageIndexByHash(src, file.parent.suffix, ext, storage, filedir, "SHA512", sums.SHA512)
err = packageIndexByHash(file, ext, "SHA512", sums.SHA512)
if err != nil {
fmt.Printf("%s\n", err)
}
err = packageIndexByHash(src, file.parent.suffix, ext, storage, filedir, "SHA256", sums.SHA256)
err = packageIndexByHash(file, ext, "SHA256", sums.SHA256)
if err != nil {
fmt.Printf("%s\n", err)
}
err = packageIndexByHash(src, file.parent.suffix, ext, storage, filedir, "SHA1", sums.SHA1)
err = packageIndexByHash(file, ext, "SHA1", sums.SHA1)
if err != nil {
fmt.Printf("%s\n", err)
}
err = packageIndexByHash(src, file.parent.suffix, ext, storage, filedir, "MD5", sums.MD5)
err = packageIndexByHash(file, ext, "MD5", sums.MD5)
if err != nil {
fmt.Printf("%s\n", err)
}
@@ -182,28 +180,38 @@ func (file *indexFile) Finalize(signer pgp.Signer) error {
return nil
}
func packageIndexByHash(src string, suffix string, ext string, storage string, filedir string, hash string, sum string) error {
func packageIndexByHash(file *indexFile, ext string, hash string, sum string) error {
src := filepath.Join(file.parent.basePath, file.relativePath)
indexfile := path.Base(src + ext)
src = src + suffix + ext
dst := filepath.Join(storage, filedir, "by-hash", hash)
src = src + file.parent.suffix + ext
filedir := filepath.Dir(filepath.Join(file.parent.basePath, file.relativePath))
dst := filepath.Join(filedir, "by-hash", hash)
if _, err := os.Stat(filepath.Join(dst, sum)); err == nil {
// link already exists? do nothing
if file.parent.publishedStorage.FileExists(filepath.Join(dst, sum)) {
return nil
}
err := os.Link(src, filepath.Join(dst, sum))
// create the link
err := file.parent.publishedStorage.HardLink(src, filepath.Join(dst, sum))
if err != nil {
return fmt.Errorf("Access-By-Hash: error creating hardlink %s", filepath.Join(dst, sum))
return fmt.Errorf("Access-By-Hash: error creating hardlink %s: %s", filepath.Join(dst, sum), err)
}
if _, err := os.Stat(filepath.Join(dst, indexfile)); err == nil {
if _, err := os.Stat(filepath.Join(dst, indexfile+".old")); err == nil {
link, _ := os.Readlink(filepath.Join(dst, indexfile+".old"))
os.Remove(filepath.Join(dst, link))
os.Remove(filepath.Join(dst, indexfile+".old"))
// if exists, backup symlink
if file.parent.publishedStorage.FileExists(filepath.Join(dst, indexfile)) {
// if exists, remove old symlink
if file.parent.publishedStorage.FileExists(filepath.Join(dst, indexfile+".old")) {
link, _ := file.parent.publishedStorage.ReadLink(filepath.Join(dst, indexfile+".old"))
file.parent.publishedStorage.Remove(filepath.Join(dst, link))
file.parent.publishedStorage.Remove(filepath.Join(dst, indexfile+".old"))
}
os.Rename(filepath.Join(dst, indexfile), filepath.Join(dst, indexfile+".old"))
file.parent.publishedStorage.RenameFile(filepath.Join(dst, indexfile),
filepath.Join(dst, indexfile+".old"))
}
err = os.Symlink(sum, filepath.Join(dst, indexfile))
// create symlink
err = file.parent.publishedStorage.SymLink(sum, filepath.Join(dst, indexfile))
if err != nil {
return fmt.Errorf("Access-By-Hash: error creating symlink %s", filepath.Join(dst, indexfile))
}
+30
View File
@@ -247,3 +247,33 @@ func (storage *PublishedStorage) Filelist(prefix string) ([]string, error) {
func (storage *PublishedStorage) RenameFile(oldName, newName string) error {
return os.Rename(filepath.Join(storage.rootPath, oldName), filepath.Join(storage.rootPath, newName))
}
// SymLink creates a symbolic link, which can be read with ReadLink
func (storage *PublishedStorage) SymLink(src string, dst string) error {
return os.Symlink(src, filepath.Join(storage.rootPath, dst))
}
// HardLink creates a hardlink of a file
func (storage *PublishedStorage) HardLink(src string, dst string) error {
return os.Link(filepath.Join(storage.rootPath, src), filepath.Join(storage.rootPath, dst))
}
// FileExists returns true if path exists
func (storage *PublishedStorage) FileExists(path string) bool {
list, err := storage.Filelist(filepath.Dir(path))
if err != nil {
return false
}
f := filepath.Base(path)
for _, i := range list {
if i == f {
return true
}
}
return false
}
// ReadLink returns the symbolic link pointed to by path
func (storage *PublishedStorage) ReadLink(path string) (string, error) {
return os.Readlink(path)
}
+24
View File
@@ -384,3 +384,27 @@ func (storage *PublishedStorage) RenameFile(oldName, newName string) error {
return storage.Remove(oldName)
}
// SymLink creates a symbolic link, which can be read with ReadLink
func (storage *PublishedStorage) SymLink(src string, dst string) error {
// TODO: create a file containing dst
return fmt.Errorf("s3: symlinks not implemented")
}
// HardLink creates a hardlink of a file
func (storage *PublishedStorage) HardLink(src string, dst string) error {
// TODO: create a copy of the file
return fmt.Errorf("s3: hardlinks not implemented")
}
// FileExists returns true if path exists
func (storage *PublishedStorage) FileExists(path string) bool {
// TODO: implement
return false
}
// ReadLink returns the symbolic link pointed to by path
func (storage *PublishedStorage) ReadLink(path string) (string, error) {
// TODO: read the path and return the content of the file
return "", fmt.Errorf("s3: ReadLink not implemented")
}
+24
View File
@@ -264,3 +264,27 @@ func (storage *PublishedStorage) RenameFile(oldName, newName string) error {
return nil
}
// SymLink creates a symbolic link, which can be read with ReadLink
func (storage *PublishedStorage) SymLink(src string, dst string) error {
// TODO: create a file containing dst
return fmt.Errorf("SWIFT: symlinks not implemented")
}
// HardLink creates a hardlink of a file
func (storage *PublishedStorage) HardLink(src string, dst string) error {
// TODO: create a copy of the file
return fmt.Errorf("SWIFT: hardlinks not implemented")
}
// FileExists returns true if path exists
func (storage *PublishedStorage) FileExists(path string) bool {
// TODO: implement
return false
}
// ReadLink returns the symbolic link pointed to by path
func (storage *PublishedStorage) ReadLink(path string) (string, error) {
// TODO: read the path and return the content of the file
return "", fmt.Errorf("SWIFT: ReadLink not implemented")
}