From 4f1bfca2a9fd321c0330ed8218b1cc1a5f953f70 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Wed, 14 Nov 2018 14:20:52 +0100 Subject: [PATCH 1/4] don't get file when checksum of destination is valid --- client.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/client.go b/client.go index 300301c2e..47c4ba337 100644 --- a/client.go +++ b/client.go @@ -248,15 +248,24 @@ func (c *Client) Get() error { // If we're not downloading a directory, then just download the file // and return. if mode == ClientModeFile { - err := g.GetFile(dst, u) - if err != nil { - return err - } - + getFile := true if checksumHash != nil { - if err := checksum(dst, checksumHash, checksumValue); err != nil { + if err := checksum(dst, checksumHash, checksumValue); err == nil { + // don't get the file if the checksum of dst is correct + getFile = false + } + } + if getFile { + err := g.GetFile(dst, u) + if err != nil { return err } + + if checksumHash != nil { + if err := checksum(dst, checksumHash, checksumValue); err != nil { + return err + } + } } if decompressor != nil { From 48a5dcaf85410cd9383f8d97eac21bd163edfd51 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Wed, 14 Nov 2018 14:24:57 +0100 Subject: [PATCH 2/4] update README --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 86ea5e0f8..c77ec5786 100644 --- a/README.md +++ b/README.md @@ -169,6 +169,9 @@ is shown below: The checksum query parameter is never sent to the backend protocol implementation. It is used at a higher level by go-getter itself. +If the destination file exists and the checksums match: download +will be skipped. + ### Unarchiving go-getter will automatically unarchive files into a file or directory From cde3ff484983c7101e2c7b8b719372a371edeb79 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Mon, 19 Nov 2018 16:50:41 +0100 Subject: [PATCH 3/4] add test for checksum skipping --- get_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/get_test.go b/get_test.go index 123c6a314..aba1d6699 100644 --- a/get_test.go +++ b/get_test.go @@ -378,3 +378,35 @@ func TestGetFile_filename(t *testing.T) { t.Fatalf("err: %s", err) } } + +func TestGetFile_checksumSkip(t *testing.T) { + dst := tempFile(t) + u := testModule("basic-file/foo.txt") + "?checksum=md5:09f7e02f1290be211da707a266f153b3" + + getter := &MockGetter{Proxy: new(FileGetter)} + client := &Client{ + Src: u, + Dst: dst, + Dir: false, + Getters: map[string]Getter{ + "file": getter, + }, + } + + // get the file + if err := client.Get(); err != nil { + t.Fatalf("err: %s", err) + } + + if v := getter.GetFileURL.Query().Get("checksum"); v != "" { + t.Fatalf("bad: %s", v) + } + + // remove file getter as + // client does the skip + client.Getters["file"] = nil + + if err := client.Get(); err != nil { + t.Fatalf("err: %s", err) + } +} From bb46a02798bab9e42e3ecfb1b426b70b00d877f9 Mon Sep 17 00:00:00 2001 From: Chris Marchesi Date: Wed, 12 Dec 2018 19:53:24 -0800 Subject: [PATCH 4/4] Update TestGetFile_checksumSkip to fix panic Setting the file getter to nil causes a nil pointer panic. This updates things so that we remove the proxy getter and reset GetFileCalled so that we can test again whether or not it was called to attempt a re-download. --- get_test.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/get_test.go b/get_test.go index aba1d6699..f7065adc2 100644 --- a/get_test.go +++ b/get_test.go @@ -402,11 +402,15 @@ func TestGetFile_checksumSkip(t *testing.T) { t.Fatalf("bad: %s", v) } - // remove file getter as - // client does the skip - client.Getters["file"] = nil + // remove proxy file getter and reset GetFileCalled so that we can re-test. + getter.Proxy = nil + getter.GetFileCalled = false if err := client.Get(); err != nil { t.Fatalf("err: %s", err) } + + if getter.GetFileCalled { + t.Fatalf("get should not have been called") + } }