Reduce required usage of LocalPackagePool

Several sections of the code *required* a LocalPackagePool, but they
could still perform their operations with a standard PackagePool.

Signed-off-by: Ryan Gonzalez <ryan.gonzalez@collabora.com>
This commit is contained in:
Ryan Gonzalez
2022-05-16 14:14:45 -05:00
committed by André Roth
parent 1ebd37f9ad
commit 19255debb9
3 changed files with 51 additions and 11 deletions
+13 -1
View File
@@ -2,7 +2,10 @@ package api
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"sort"
"strings"
"sync"
@@ -482,7 +485,16 @@ func apiMirrorsUpdate(c *gin.Context) {
var e error
// provision download location
task.TempDownPath, e = context.PackagePool().(aptly.LocalPackagePool).GenerateTempPath(task.File.Filename)
if pp, ok := context.PackagePool().(aptly.LocalPackagePool); ok {
task.TempDownPath, e = pp.GenerateTempPath(task.File.Filename)
} else {
var file *os.File
file, e = ioutil.TempFile("", task.File.Filename)
if e == nil {
task.TempDownPath = file.Name()
file.Close()
}
}
if e != nil {
pushError(e)
continue
+12 -1
View File
@@ -2,6 +2,8 @@ package cmd
import (
"fmt"
"io/ioutil"
"os"
"strings"
"sync"
@@ -161,7 +163,16 @@ func aptlyMirrorUpdate(cmd *commander.Command, args []string) error {
var e error
// provision download location
task.TempDownPath, e = context.PackagePool().(aptly.LocalPackagePool).GenerateTempPath(task.File.Filename)
if pp, ok := context.PackagePool().(aptly.LocalPackagePool); ok {
task.TempDownPath, e = pp.GenerateTempPath(task.File.Filename)
} else {
var file *os.File
file, e = ioutil.TempFile("", task.File.Filename)
if e == nil {
task.TempDownPath = file.Name()
file.Close()
}
}
if e != nil {
pushError(e)
continue
+26 -9
View File
@@ -133,26 +133,37 @@ func (storage *PublishedStorage) LinkFromPool(publishedPrefix, publishedRelPath,
baseName := filepath.Base(fileName)
poolPath := filepath.Join(storage.rootPath, publishedPrefix, publishedRelPath, filepath.Dir(fileName))
var localSourcePool aptly.LocalPackagePool
if storage.linkMethod != LinkMethodCopy {
pp, ok := sourcePool.(aptly.LocalPackagePool)
if !ok {
return fmt.Errorf("cannot link %s from non-local pool %s", baseName, sourcePool)
}
localSourcePool = pp
}
err := os.MkdirAll(poolPath, 0777)
if err != nil {
return err
}
var dstStat, srcStat os.FileInfo
var dstStat os.FileInfo
dstStat, err = os.Stat(filepath.Join(poolPath, baseName))
if err == nil {
// already exists, check source file
srcStat, err = sourcePool.Stat(sourcePath)
if err != nil {
// source file doesn't exist? problem!
return err
}
if storage.linkMethod == LinkMethodCopy {
srcSize, err := sourcePool.Size(sourcePath)
if err != nil {
// source file doesn't exist? problem!
return err
}
if storage.verifyMethod == VerificationMethodFileSize {
// if source and destination have the same size, no need to copy
if srcStat.Size() == dstStat.Size() {
if srcSize == dstStat.Size() {
return nil
}
} else {
@@ -169,6 +180,12 @@ func (storage *PublishedStorage) LinkFromPool(publishedPrefix, publishedRelPath,
}
}
} else {
srcStat, err := localSourcePool.Stat(sourcePath)
if err != nil {
// source file doesn't exist? problem!
return err
}
srcSys := srcStat.Sys().(*syscall.Stat_t)
dstSys := dstStat.Sys().(*syscall.Stat_t)
@@ -223,9 +240,9 @@ func (storage *PublishedStorage) LinkFromPool(publishedPrefix, publishedRelPath,
err = dst.Close()
} else if storage.linkMethod == LinkMethodSymLink {
err = sourcePool.(aptly.LocalPackagePool).Symlink(sourcePath, filepath.Join(poolPath, baseName))
err = localSourcePool.Symlink(sourcePath, filepath.Join(poolPath, baseName))
} else {
err = sourcePool.(aptly.LocalPackagePool).Link(sourcePath, filepath.Join(poolPath, baseName))
err = localSourcePool.Link(sourcePath, filepath.Join(poolPath, baseName))
}
return err