mirror of
https://github.com/safing/portmaster
synced 2025-04-22 20:09:09 +00:00
* Move portbase into monorepo * Add new simple module mgr * [WIP] Switch to new simple module mgr * Add StateMgr and more worker variants * [WIP] Switch more modules * [WIP] Switch more modules * [WIP] swtich more modules * [WIP] switch all SPN modules * [WIP] switch all service modules * [WIP] Convert all workers to the new module system * [WIP] add new task system to module manager * [WIP] Add second take for scheduling workers * [WIP] Add FIXME for bugs in new scheduler * [WIP] Add minor improvements to scheduler * [WIP] Add new worker scheduler * [WIP] Fix more bug related to new module system * [WIP] Fix start handing of the new module system * [WIP] Improve startup process * [WIP] Fix minor issues * [WIP] Fix missing subsystem in settings * [WIP] Initialize managers in constructor * [WIP] Move module event initialization to constrictors * [WIP] Fix setting for enabling and disabling the SPN module * [WIP] Move API registeration into module construction * [WIP] Update states mgr for all modules * [WIP] Add CmdLine operation support * Add state helper methods to module group and instance * Add notification and module status handling to status package * Fix starting issues * Remove pilot widget and update security lock to new status data * Remove debug logs * Improve http server shutdown * Add workaround for cleanly shutting down firewall+netquery * Improve logging * Add syncing states with notifications for new module system * Improve starting, stopping, shutdown; resolve FIXMEs/TODOs * [WIP] Fix most unit tests * Review new module system and fix minor issues * Push shutdown and restart events again via API * Set sleep mode via interface * Update example/template module * [WIP] Fix spn/cabin unit test * Remove deprecated UI elements * Make log output more similar for the logging transition phase * Switch spn hub and observer cmds to new module system * Fix log sources * Make worker mgr less error prone * Fix tests and minor issues * Fix observation hub * Improve shutdown and restart handling * Split up big connection.go source file * Move varint and dsd packages to structures repo * Improve expansion test * Fix linter warnings * Fix interception module on windows * Fix linter errors --------- Co-authored-by: Vladimir Stoilov <vladimir@safing.io>
195 lines
4.9 KiB
Go
195 lines
4.9 KiB
Go
package updater
|
|
|
|
import (
|
|
"archive/zip"
|
|
"compress/gzip"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
"github.com/safing/portmaster/base/log"
|
|
"github.com/safing/portmaster/base/utils"
|
|
)
|
|
|
|
// MaxUnpackSize specifies the maximum size that will be unpacked.
|
|
const MaxUnpackSize = 1000000000 // 1GB
|
|
|
|
// UnpackGZIP unpacks a GZIP compressed reader r
|
|
// and returns a new reader. It's suitable to be
|
|
// used with registry.GetPackedFile.
|
|
func UnpackGZIP(r io.Reader) (io.Reader, error) {
|
|
return gzip.NewReader(r)
|
|
}
|
|
|
|
// UnpackResources unpacks all resources defined in the AutoUnpack list.
|
|
func (reg *ResourceRegistry) UnpackResources() error {
|
|
reg.RLock()
|
|
defer reg.RUnlock()
|
|
|
|
var multierr *multierror.Error
|
|
for _, res := range reg.resources {
|
|
if utils.StringInSlice(reg.AutoUnpack, res.Identifier) {
|
|
err := res.UnpackArchive()
|
|
if err != nil {
|
|
multierr = multierror.Append(
|
|
multierr,
|
|
fmt.Errorf("%s: %w", res.Identifier, err),
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
return multierr.ErrorOrNil()
|
|
}
|
|
|
|
const (
|
|
zipSuffix = ".zip"
|
|
)
|
|
|
|
// UnpackArchive unpacks the archive the resource refers to. The contents are
|
|
// unpacked into a directory with the same name as the file, excluding the
|
|
// suffix. If the destination folder already exists, it is assumed that the
|
|
// contents have already been correctly unpacked.
|
|
func (res *Resource) UnpackArchive() error {
|
|
res.Lock()
|
|
defer res.Unlock()
|
|
|
|
// Only unpack selected versions.
|
|
if res.SelectedVersion == nil {
|
|
return nil
|
|
}
|
|
|
|
switch {
|
|
case strings.HasSuffix(res.Identifier, zipSuffix):
|
|
return res.unpackZipArchive()
|
|
default:
|
|
return fmt.Errorf("unsupported file type for unpacking")
|
|
}
|
|
}
|
|
|
|
func (res *Resource) unpackZipArchive() error {
|
|
// Get file and directory paths.
|
|
archiveFile := res.SelectedVersion.storagePath()
|
|
destDir := strings.TrimSuffix(archiveFile, zipSuffix)
|
|
tmpDir := filepath.Join(
|
|
res.registry.tmpDir.Path,
|
|
filepath.FromSlash(strings.TrimSuffix(
|
|
path.Base(res.SelectedVersion.versionedPath()),
|
|
zipSuffix,
|
|
)),
|
|
)
|
|
|
|
// Check status of destination.
|
|
dstStat, err := os.Stat(destDir)
|
|
switch {
|
|
case errors.Is(err, fs.ErrNotExist):
|
|
// The destination does not exist, continue with unpacking.
|
|
case err != nil:
|
|
return fmt.Errorf("cannot access destination for unpacking: %w", err)
|
|
case !dstStat.IsDir():
|
|
return fmt.Errorf("destination for unpacking is blocked by file: %s", dstStat.Name())
|
|
default:
|
|
// Archive already seems to be unpacked.
|
|
return nil
|
|
}
|
|
|
|
// Create the tmp directory for unpacking.
|
|
err = res.registry.tmpDir.EnsureAbsPath(tmpDir)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create tmp dir for unpacking: %w", err)
|
|
}
|
|
|
|
// Defer clean up of directories.
|
|
defer func() {
|
|
// Always clean up the tmp dir.
|
|
_ = os.RemoveAll(tmpDir)
|
|
// Cleanup the destination in case of an error.
|
|
if err != nil {
|
|
_ = os.RemoveAll(destDir)
|
|
}
|
|
}()
|
|
|
|
// Open the archive for reading.
|
|
var archiveReader *zip.ReadCloser
|
|
archiveReader, err = zip.OpenReader(archiveFile)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open zip reader: %w", err)
|
|
}
|
|
defer func() {
|
|
_ = archiveReader.Close()
|
|
}()
|
|
|
|
// Save all files to the tmp dir.
|
|
for _, file := range archiveReader.File {
|
|
err = copyFromZipArchive(
|
|
file,
|
|
filepath.Join(tmpDir, filepath.FromSlash(file.Name)),
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to extract archive file %s: %w", file.Name, err)
|
|
}
|
|
}
|
|
|
|
// Make the final move.
|
|
err = os.Rename(tmpDir, destDir)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to move the extracted archive from %s to %s: %w", tmpDir, destDir, err)
|
|
}
|
|
|
|
// Fix permissions on the destination dir.
|
|
err = res.registry.storageDir.EnsureAbsPath(destDir)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to apply directory permissions on %s: %w", destDir, err)
|
|
}
|
|
|
|
log.Infof("%s: unpacked %s", res.registry.Name, res.SelectedVersion.versionedPath())
|
|
return nil
|
|
}
|
|
|
|
func copyFromZipArchive(archiveFile *zip.File, dstPath string) error {
|
|
// If file is a directory, create it and continue.
|
|
if archiveFile.FileInfo().IsDir() {
|
|
err := os.Mkdir(dstPath, archiveFile.Mode())
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create directory %s: %w", dstPath, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Open archived file for reading.
|
|
fileReader, err := archiveFile.Open()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open file in archive: %w", err)
|
|
}
|
|
defer func() {
|
|
_ = fileReader.Close()
|
|
}()
|
|
|
|
// Open destination file for writing.
|
|
dstFile, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, archiveFile.Mode())
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open destination file %s: %w", dstPath, err)
|
|
}
|
|
defer func() {
|
|
_ = dstFile.Close()
|
|
}()
|
|
|
|
// Copy full file from archive to dst.
|
|
if _, err := io.CopyN(dstFile, fileReader, MaxUnpackSize); err != nil {
|
|
// EOF is expected here as the archive is likely smaller
|
|
// thane MaxUnpackSize
|
|
if errors.Is(err, io.EOF) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|