Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
Auto detect data dir and switch to safe exec dir
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed May 8, 2020
1 parent a6b258b commit b03555c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
31 changes: 31 additions & 0 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"fmt"
"os"
"os/signal"
"path/filepath"
"runtime"
"strings"
"syscall"

"github.com/safing/portbase/info"
Expand Down Expand Up @@ -62,6 +65,11 @@ func main() {
dataDir = databaseDir
}

// auto detect
if dataDir == "" {
dataDir = detectDataDir()
}

// check data dir
if dataDir == "" {
fmt.Fprintln(os.Stderr, "please set the data directory using --data=/path/to/data/dir")
Expand All @@ -71,6 +79,12 @@ func main() {
// backwards compatibility
databaseDir = dataDir

// switch to safe exec dir
err = os.Chdir(filepath.Join(dataDir, "exec"))
if err != nil {
fmt.Fprintf(os.Stderr, "warning: failed to switch to safe exec dir: %s\n", err)
}

// set custom url for development
if urlFlag != "" {
url = urlFlag
Expand Down Expand Up @@ -150,3 +164,20 @@ func shutdownHandler(wv webview.WebView) {
// exit
wv.Dispatch(wv.Exit)
}

func detectDataDir() string {
// get path of executable
binPath, err := os.Executable()
if err != nil {
return ""
}
// get directory
binDir := filepath.Dir(binPath)
// check if we in the updates directory
identifierDir := filepath.Join("updates", runtime.GOOS+"_"+runtime.GOARCH, "app")
// check if there is a match and return data dir
if strings.HasSuffix(binDir, identifierDir) {
return filepath.Clean(strings.TrimSuffix(binDir, identifierDir))
}
return ""
}
30 changes: 30 additions & 0 deletions notifier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"fmt"
"os"
"os/signal"
"path/filepath"
"runtime"
"runtime/pprof"
"strings"
"sync"
"syscall"
"time"
Expand Down Expand Up @@ -70,6 +72,11 @@ func main() {
dataDir = databaseDir
}

// auto detect
if dataDir == "" {
dataDir = detectDataDir()
}

// check data dir
if dataDir == "" {
fmt.Fprintln(os.Stderr, "please set the data directory using --data=/path/to/data/dir")
Expand All @@ -79,6 +86,12 @@ func main() {
// backwards compatibility
databaseDir = dataDir

// switch to safe exec dir
err = os.Chdir(filepath.Join(dataDir, "exec"))
if err != nil {
fmt.Fprintf(os.Stderr, "warning: failed to switch to safe exec dir: %s\n", err)
}

// start log writer
err = log.Start()
if err != nil {
Expand Down Expand Up @@ -141,3 +154,20 @@ func main() {

os.Exit(0)
}

func detectDataDir() string {
// get path of executable
binPath, err := os.Executable()
if err != nil {
return ""
}
// get directory
binDir := filepath.Dir(binPath)
// check if we in the updates directory
identifierDir := filepath.Join("updates", runtime.GOOS+"_"+runtime.GOARCH, "notifier")
// check if there is a match and return data dir
if strings.HasSuffix(binDir, identifierDir) {
return filepath.Clean(strings.TrimSuffix(binDir, identifierDir))
}
return ""
}

0 comments on commit b03555c

Please sign in to comment.