safing-portmaster/updates/main.go
2019-07-30 13:07:33 +02:00

91 lines
1.7 KiB
Go

package updates
import (
"errors"
"os"
"path/filepath"
"runtime"
"github.com/safing/portbase/database"
"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"
)
// 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")
}
}
func init() {
modules.Register("updates", prep, start, stop, "core")
}
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
}
err = utils.EnsureDirectory(downloadTmpPath, 0700)
if err != nil {
return err
}
status.Core = info.GetInfo()
return nil
}
func start() error {
err := initUpdateStatusHook()
if err != nil {
return err
}
err = LoadIndexes()
if err != nil {
if os.IsNotExist(err) {
// download indexes
log.Infof("updates: downloading update index...")
err = UpdateIndexes()
if err != nil {
log.Errorf("updates: failed to download update index: %s", err)
}
} else {
return err
}
}
err = LoadLatest()
if err != nil {
return err
}
go updater()
go updateNotifier()
return nil
}
func stop() error {
// delete download tmp dir
return os.RemoveAll(downloadTmpPath)
}