summaryrefslogtreecommitdiff
diff options
authorMichael Vogt <mvo@ubuntu.com>2017-11-23 11:05:57 +0100
committerMichael Vogt <mvo@ubuntu.com>2017-11-23 11:05:57 +0100
commitadfbf1dd7d46242648b64b9e86b7d7d49ac1c4ed (patch)
treecb67ddcb57acb8a8467cc3550e8f0fa0ed0175a4
parent718675cf070e652f17ca4466b00abd6786e4b64d (diff)
snap: use existing files in `snap download` if digest/size matchesdownload-not-again
When `snap download` sees an existing file it will only re-download if the digest/size does not match. Otherwise the local file is used and not downloaded from the store.
-rw-r--r--image/helpers.go12
-rw-r--r--tests/main/snap-download/task.yaml3
2 files changed, 15 insertions, 0 deletions
diff --git a/image/helpers.go b/image/helpers.go
index 828887ee89..46cb34cf1b 100644
--- a/image/helpers.go
+++ b/image/helpers.go
@@ -22,6 +22,7 @@ package image
// TODO: put these in appropriate package(s) once they are clarified a bit more
import (
+ "crypto"
"encoding/json"
"fmt"
"os"
@@ -31,6 +32,8 @@ import (
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/asserts/snapasserts"
+ "github.com/snapcore/snapd/logger"
+ "github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/progress"
"github.com/snapcore/snapd/release"
@@ -144,6 +147,15 @@ func (tsto *ToolingStore) DownloadSnap(name string, revision snap.Revision, opts
baseName := filepath.Base(snap.MountFile())
targetFn = filepath.Join(targetDir, baseName)
+ // check if we already have the right file
+ if osutil.FileExists(targetFn) {
+ sha3_384Dgst, size, err := osutil.FileDigest(targetFn, crypto.SHA3_384)
+ if err == nil && size == uint64(snap.DownloadInfo.Size) && fmt.Sprintf("%x", sha3_384Dgst) == snap.DownloadInfo.Sha3_384 {
+ logger.Debugf("not downloading, using existing file %s", targetFn)
+ return targetFn, snap, nil
+ }
+ }
+
pb := progress.MakeProgressBar()
defer pb.Finished()
diff --git a/tests/main/snap-download/task.yaml b/tests/main/snap-download/task.yaml
index 9381a8c4da..57540e916e 100644
--- a/tests/main/snap-download/task.yaml
+++ b/tests/main/snap-download/task.yaml
@@ -11,6 +11,9 @@ execute: |
ls test-snapd-control-consumer_*.snap
verify_asserts test-snapd-control-consumer_*.assert
+ echo "Snap will use existing files"
+ SNAPD_DEBUG=1 snap download test-snapd-control-consumer 2>&1 | MATCH "not downloading, using existing file"
+
echo "Snap download understand --edge"
snap download --edge test-snapd-tools
ls test-snapd-tools_*.snap