mirror of
https://github.com/restic/restic.git
synced 2025-08-23 17:13:10 +00:00
Merge pull request #4163 from MichaelEischer/fix-windows-self-upgrade
self-upgrade: Fix handling of `--output` on windows
This commit is contained in:
44
internal/selfupdate/download_test.go
Normal file
44
internal/selfupdate/download_test.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package selfupdate
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
rtest "github.com/restic/restic/internal/test"
|
||||
)
|
||||
|
||||
func TestExtractToFileZip(t *testing.T) {
|
||||
printf := func(string, ...interface{}) {}
|
||||
dir := t.TempDir()
|
||||
|
||||
ext := "zip"
|
||||
data := []byte("Hello World!")
|
||||
|
||||
// create dummy archive
|
||||
var archive bytes.Buffer
|
||||
zw := zip.NewWriter(&archive)
|
||||
w, err := zw.CreateHeader(&zip.FileHeader{
|
||||
Name: "example.exe",
|
||||
UncompressedSize64: uint64(len(data)),
|
||||
})
|
||||
rtest.OK(t, err)
|
||||
_, err = w.Write(data[:])
|
||||
rtest.OK(t, err)
|
||||
rtest.OK(t, zw.Close())
|
||||
|
||||
// run twice to test creating a new file and overwriting
|
||||
for i := 0; i < 2; i++ {
|
||||
outfn := filepath.Join(dir, ext+"-out")
|
||||
rtest.OK(t, extractToFile(archive.Bytes(), "src."+ext, outfn, printf))
|
||||
|
||||
outdata, err := os.ReadFile(outfn)
|
||||
rtest.OK(t, err)
|
||||
rtest.Assert(t, bytes.Equal(data[:], outdata), "%v contains wrong data", outfn)
|
||||
|
||||
// overwrite to test the file is properly overwritten
|
||||
rtest.OK(t, os.WriteFile(outfn, []byte{1, 2, 3}, 0))
|
||||
}
|
||||
}
|
@@ -7,11 +7,18 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/restic/restic/internal/errors"
|
||||
)
|
||||
|
||||
// Rename (rather than remove) the running version. The running binary will be locked
|
||||
// on Windows and cannot be removed while still executing.
|
||||
func removeResticBinary(dir, target string) error {
|
||||
// nothing to do if the target does not exist
|
||||
if _, err := os.Stat(target); errors.Is(err, os.ErrNotExist) {
|
||||
return nil
|
||||
}
|
||||
|
||||
backup := filepath.Join(dir, filepath.Base(target)+".bak")
|
||||
if _, err := os.Stat(backup); err == nil {
|
||||
_ = os.Remove(backup)
|
||||
|
Reference in New Issue
Block a user