Adapt modules to new core module and dir structure handling

This commit is contained in:
Daniel
2019-07-31 22:36:09 +02:00
parent 7a6189143c
commit 328fc9087f
15 changed files with 122 additions and 91 deletions

View File

@@ -15,7 +15,6 @@ import (
"github.com/google/renameio"
"github.com/safing/portbase/log"
"github.com/safing/portbase/utils"
)
var (
@@ -38,13 +37,13 @@ func fetchFile(realFilepath, updateFilepath string, tries int) error {
// check destination dir
dirPath := filepath.Dir(realFilepath)
err = utils.EnsureDirectory(dirPath, 0755)
err = updateStorage.EnsureAbsPath(dirPath)
if err != nil {
return fmt.Errorf("could not create updates folder: %s", dirPath)
}
// open file for writing
atomicFile, err := renameio.TempFile(downloadTmpPath, realFilepath)
atomicFile, err := renameio.TempFile(tmpStorage.Path, realFilepath)
if err != nil {
return fmt.Errorf("could not create temp file for download: %s", err)
}

View File

@@ -9,7 +9,6 @@ import (
"runtime"
"github.com/safing/portbase/log"
"github.com/safing/portbase/utils"
)
// Errors
@@ -75,7 +74,7 @@ func loadOrFetchFile(identifier string, fetch bool) (*File, error) {
}
// build final filepath
realFilePath := filepath.Join(updateStoragePath, filepath.FromSlash(versionedFilePath))
realFilePath := filepath.Join(updateStorage.Path, filepath.FromSlash(versionedFilePath))
if _, err := os.Stat(realFilePath); err == nil {
// file exists
updateUsedStatus(identifier, version)
@@ -83,7 +82,7 @@ func loadOrFetchFile(identifier string, fetch bool) (*File, error) {
}
// check download dir
err := utils.EnsureDirectory(downloadTmpPath, 0700)
err := tmpStorage.Ensure()
if err != nil {
return nil, fmt.Errorf("could not prepare tmp directory for download: %s", err)
}

View File

@@ -26,14 +26,14 @@ func LoadLatest() error {
// all
prefix := "all"
new, err1 := ScanForLatest(filepath.Join(updateStoragePath, prefix), false)
new, err1 := ScanForLatest(filepath.Join(updateStorage.Path, prefix), false)
for key, val := range new {
newLocalUpdates[filepath.ToSlash(filepath.Join(prefix, key))] = val
}
// os_platform
prefix = fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH)
new, err2 := ScanForLatest(filepath.Join(updateStoragePath, prefix), false)
new, err2 := ScanForLatest(filepath.Join(updateStorage.Path, prefix), false)
for key, val := range new {
newLocalUpdates[filepath.ToSlash(filepath.Join(prefix, key))] = val
}
@@ -120,7 +120,7 @@ func ScanForLatest(baseDir string, hardFail bool) (latest map[string]string, las
// LoadIndexes loads the current update indexes from disk.
func LoadIndexes() error {
data, err := ioutil.ReadFile(filepath.Join(updateStoragePath, "stable.json"))
data, err := ioutil.ReadFile(filepath.Join(updateStorage.Path, "stable.json"))
if err != nil {
return err
}

View File

@@ -3,27 +3,30 @@ package updates
import (
"errors"
"os"
"path/filepath"
"runtime"
"github.com/safing/portbase/database"
"github.com/safing/portmaster/core/structure"
"github.com/safing/portbase/info"
"github.com/safing/portbase/log"
"github.com/safing/portbase/modules"
"github.com/safing/portbase/utils"
)
var (
updateStoragePath string
downloadTmpPath string
isWindows = runtime.GOOS == "windows"
const (
isWindows = runtime.GOOS == "windows"
)
// SetDatabaseRoot tells the updates module where the database is - and where to put its stuff.
func SetDatabaseRoot(path string) {
if updateStoragePath == "" {
updateStoragePath = filepath.Join(path, "updates")
downloadTmpPath = filepath.Join(updateStoragePath, "tmp")
var (
updateStorage *utils.DirStructure
tmpStorage *utils.DirStructure
)
// SetDataRoot sets the data root from which the updates module derives its paths.
func SetDataRoot(root *utils.DirStructure) {
if root != nil && updateStorage == nil {
updateStorage = root.ChildDir("updates", 0755)
tmpStorage = updateStorage.ChildDir("tmp", 0777)
}
}
@@ -32,19 +35,12 @@ func init() {
}
func prep() error {
dbRoot := database.GetDatabaseRoot()
if dbRoot == "" {
return errors.New("database root is not set")
}
updateStoragePath = filepath.Join(dbRoot, "updates")
downloadTmpPath = filepath.Join(updateStoragePath, "tmp")
err := utils.EnsureDirectory(updateStoragePath, 0755)
if err != nil {
return err
SetDataRoot(structure.Root())
if updateStorage == nil {
return errors.New("update storage path is not set")
}
err = utils.EnsureDirectory(downloadTmpPath, 0700)
err := updateStorage.Ensure()
if err != nil {
return err
}
@@ -87,5 +83,5 @@ func start() error {
func stop() error {
// delete download tmp dir
return os.RemoveAll(downloadTmpPath)
return os.RemoveAll(tmpStorage.Path)
}

View File

@@ -33,7 +33,7 @@ func updateNotifier() {
Message: fmt.Sprintf("There is an update available for the Portmaster core (v%s), please restart the Portmaster to apply the update.", file.Version()),
Type: notifications.Info,
Expires: time.Now().Add(1 * time.Minute).Unix(),
}).Init().Save()
}).Save()
}
}

View File

@@ -12,7 +12,6 @@ import (
"time"
"github.com/safing/portbase/log"
"github.com/safing/portbase/utils"
)
func updater() {
@@ -84,13 +83,13 @@ func UpdateIndexes() (err error) {
updatesLock.Unlock()
// check dir
err = utils.EnsureDirectory(updateStoragePath, 0755)
err = updateStorage.Ensure()
if err != nil {
return err
}
// save stable index
err = ioutil.WriteFile(filepath.Join(updateStoragePath, "stable.json"), data, 0644)
err = ioutil.WriteFile(filepath.Join(updateStorage.Path, "stable.json"), data, 0644)
if err != nil {
log.Warningf("updates: failed to save new version of stable.json: %s", err)
}
@@ -125,6 +124,12 @@ func DownloadUpdates() (err error) {
}
updatesLock.Unlock()
// check download dir
err = tmpStorage.Ensure()
if err != nil {
return fmt.Errorf("could not prepare tmp directory for download: %s", err)
}
// RLock for the remaining function
updatesLock.RLock()
defer updatesLock.RUnlock()
@@ -137,7 +142,7 @@ func DownloadUpdates() (err error) {
log.Tracef("updates: updating %s to %s", identifier, newVersion)
filePath := GetVersionedPath(identifier, newVersion)
realFilePath := filepath.Join(updateStoragePath, filePath)
realFilePath := filepath.Join(updateStorage.Path, filePath)
for tries := 0; tries < 3; tries++ {
err = fetchFile(realFilePath, filePath, tries)
if err == nil {
@@ -153,9 +158,9 @@ func DownloadUpdates() (err error) {
log.Tracef("updates: finished updating existing files")
// remove tmp folder after we are finished
err = os.RemoveAll(downloadTmpPath)
err = os.RemoveAll(tmpStorage.Path)
if err != nil {
log.Tracef("updates: failed to remove tmp dir %s after downloading updates: %s", updateStoragePath, err)
log.Tracef("updates: failed to remove tmp dir %s after downloading updates: %s", updateStorage.Path, err)
}
return nil

View File

@@ -11,7 +11,6 @@ import (
"time"
"github.com/google/renameio"
"github.com/safing/portbase/utils"
"github.com/safing/portbase/log"
@@ -35,7 +34,7 @@ func runFileUpgrades() error {
}
// update portmaster-control in data root
rootControlPath := filepath.Join(filepath.Dir(updateStoragePath), filename)
rootControlPath := filepath.Join(filepath.Dir(updateStorage.Path), filename)
err = upgradeFile(rootControlPath, newFile)
if err != nil {
return err
@@ -76,6 +75,12 @@ func upgradeFile(fileToUpgrade string, file *File) error {
fileExists = true
}
// ensure that the tmp dir exists
err = tmpStorage.Ensure()
if err != nil {
return fmt.Errorf("unable to create directory for upgrade process: %s", err)
}
if fileExists {
// get current version
var currentVersion string
@@ -103,14 +108,8 @@ func upgradeFile(fileToUpgrade string, file *File) error {
err = os.Remove(fileToUpgrade)
if err != nil {
// maybe we're on windows and it's in use, try moving
// create dir
err = utils.EnsureDirectory(downloadTmpPath, 0700)
if err != nil {
return fmt.Errorf("unable to create directory for upgrade process: %s", err)
}
// move
err = os.Rename(fileToUpgrade, filepath.Join(
downloadTmpPath,
tmpStorage.Path,
fmt.Sprintf(
"%s-%d%s",
GetVersionedPath(filepath.Base(fileToUpgrade), currentVersion),
@@ -154,7 +153,7 @@ func upgradeFile(fileToUpgrade string, file *File) error {
func copyFile(srcPath, dstPath string) (err error) {
// open file for writing
atomicDstFile, err := renameio.TempFile(downloadTmpPath, dstPath)
atomicDstFile, err := renameio.TempFile(tmpStorage.Path, dstPath)
if err != nil {
return fmt.Errorf("could not create temp file for atomic copy: %s", err)
}
@@ -183,5 +182,5 @@ func copyFile(srcPath, dstPath string) (err error) {
}
func cleanOldUpgradedFiles() error {
return os.RemoveAll(downloadTmpPath)
return os.RemoveAll(tmpStorage.Path)
}