windows: fix data file update (remove before rename) (#2930)

This commit is contained in:
mmetc 2024-04-05 14:57:33 +02:00 committed by GitHub
parent 912c4bca70
commit 2682f801df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,9 +4,11 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"runtime"
"time"
"github.com/sirupsen/logrus"
@ -65,6 +67,18 @@ func downloadFile(url string, destPath string) error {
// TODO: use a better way to communicate this
fmt.Printf("updated %s\n", filepath.Base(destPath))
if runtime.GOOS == "windows" {
// On Windows, rename will fail if the destination file already exists
// so we remove it first.
err = os.Remove(destPath)
switch {
case errors.Is(err, fs.ErrNotExist):
break
case err != nil:
return err
}
}
if err = os.Rename(tmpFileName, destPath); err != nil {
return err
}