[WIP] Updater support for windows

This commit is contained in:
Vladimir Stoilov
2024-09-11 18:52:36 +03:00
parent 8c6eb04292
commit 83ec18f552
11 changed files with 371 additions and 115 deletions

View File

@@ -12,6 +12,7 @@ import (
"net/http"
"os"
"path/filepath"
"runtime"
"time"
"github.com/safing/portmaster/base/log"
@@ -19,6 +20,8 @@ import (
const MaxUnpackSize = 1 << 30 // 2^30 == 1GB
const current_platform = runtime.GOOS + "_" + runtime.GOARCH
type Artifact struct {
Filename string `json:"Filename"`
SHA256 string `json:"SHA256"`
@@ -107,6 +110,11 @@ func checkIfFileIsValid(filename string, artifact Artifact) (bool, error) {
}
func processArtifact(client *http.Client, artifact Artifact, filePath string) error {
// Skip artifacts not meant for this machine.
if artifact.Platform != "" && artifact.Platform != current_platform {
return nil
}
providedHash, err := hex.DecodeString(artifact.SHA256)
if err != nil || len(providedHash) != sha256.Size {
return fmt.Errorf("invalid provided hash %s: %w", artifact.SHA256, err)
@@ -149,6 +157,7 @@ func processArtifact(client *http.Client, artifact Artifact, filePath string) er
if err != nil {
return fmt.Errorf("failed to write to file: %w", err)
}
file.Close()
// Rename
err = os.Rename(tmpFilename, filePath)

View File

@@ -125,7 +125,7 @@ func (reg *Registry) DownloadUpdates() error {
// ApplyUpdates removes the current binary folder and replaces it with the downloaded one.
func (reg *Registry) ApplyUpdates() error {
// Create purge dir.
err := os.MkdirAll(filepath.Dir(reg.updateIndex.PurgeDirectory), defaultDirMode)
err := os.MkdirAll(reg.updateIndex.PurgeDirectory, defaultDirMode)
if err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}
@@ -198,7 +198,7 @@ func deleteUnfinishedDownloads(rootDir string) error {
// Check if the current file has the specified extension
if !info.IsDir() && strings.HasSuffix(info.Name(), ".download") {
log.Warningf("updates deleting unfinished: %s\n", path)
log.Warningf("updates: deleting unfinished: %s\n", path)
err := os.Remove(path)
if err != nil {
return fmt.Errorf("failed to delete file %s: %w", path, err)