Skip to content

Commit

Permalink
Reduce required usage of LocalPackagePool
Browse files Browse the repository at this point in the history
Several sections of the code *required* a LocalPackagePool, but they
could still perform their operations with a standard PackagePool.

Signed-off-by: Ryan Gonzalez <[email protected]>
  • Loading branch information
refi64 authored and neolynx committed Jun 17, 2024
1 parent 1ebd37f commit 19255de
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
14 changes: 13 additions & 1 deletion api/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package api

import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion cmd/mirror_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"
"strings"
"sync"

Expand Down Expand Up @@ -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
Expand Down
35 changes: 26 additions & 9 deletions files/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 19255de

Please sign in to comment.