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

@@ -13,7 +13,7 @@ import (
)
func checkAndCreateInstanceLock(name string) (pid int32, err error) {
lockFilePath := filepath.Join(databaseRootDir, fmt.Sprintf("%s-lock.pid", name))
lockFilePath := filepath.Join(dataRoot.Path, fmt.Sprintf("%s-lock.pid", name))
// read current pid file
data, err := ioutil.ReadFile(lockFilePath)
@@ -49,10 +49,10 @@ func checkAndCreateInstanceLock(name string) (pid int32, err error) {
}
func createInstanceLock(lockFilePath string) error {
// create database dir
err := os.MkdirAll(databaseRootDir, 0777)
// check data root dir
err := dataRoot.Ensure()
if err != nil {
log.Printf("failed to create base folder: %s\n", err)
log.Printf("failed to check data root dir: %s\n", err)
}
// create lock file
@@ -65,6 +65,6 @@ func createInstanceLock(lockFilePath string) error {
}
func deleteInstanceLock(name string) error {
lockFilePath := filepath.Join(databaseRootDir, fmt.Sprintf("%s-lock.pid", name))
lockFilePath := filepath.Join(dataRoot.Path, fmt.Sprintf("%s-lock.pid", name))
return os.Remove(lockFilePath)
}

View File

@@ -18,7 +18,7 @@ import (
)
func initializeLogFile(logFilePath string, identifier string, updateFile *updates.File) *os.File {
logFile, err := os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE, 0644)
logFile, err := os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE, 0444)
if err != nil {
log.Printf("failed to create log file %s: %s\n", logFilePath, err)
return nil
@@ -74,12 +74,11 @@ func finalizeLogFile(logFile *os.File, logFilePath string) {
}
func initControlLogFile() *os.File {
// create logging dir
logFileBasePath := filepath.Join(databaseRootDir, "logs", "fstree", "control")
err := os.MkdirAll(logFileBasePath, 0777)
// check logging dir
logFileBasePath := filepath.Join(logsRoot.Path, "fstree", "control")
err := logsRoot.EnsureAbsPath(logFileBasePath)
if err != nil {
log.Printf("failed to create log file folder %s: %s\n", logFileBasePath, err)
return nil
log.Printf("failed to check/create log file folder %s: %s\n", logFileBasePath, err)
}
// open log file
@@ -93,11 +92,11 @@ func logControlError(cErr error) {
return
}
// create dir
logFileBasePath := filepath.Join(databaseRootDir, "logs", "fstree", "control")
err := os.MkdirAll(logFileBasePath, 0777)
// check logging dir
logFileBasePath := filepath.Join(logsRoot.Path, "fstree", "control")
err := logsRoot.EnsureAbsPath(logFileBasePath)
if err != nil {
log.Printf("failed to create log file folder %s: %s\n", logFileBasePath, err)
log.Printf("failed to check/create log file folder %s: %s\n", logFileBasePath, err)
}
// open log file
@@ -113,11 +112,11 @@ func logControlError(cErr error) {
}
func logControlStack() {
// create dir
logFileBasePath := filepath.Join(databaseRootDir, "logs", "fstree", "control")
err := os.MkdirAll(logFileBasePath, 0777)
// check logging dir
logFileBasePath := filepath.Join(logsRoot.Path, "fstree", "control")
err := logsRoot.EnsureAbsPath(logFileBasePath)
if err != nil {
log.Printf("failed to create log file folder %s: %s\n", logFileBasePath, err)
log.Printf("failed to check/create log file folder %s: %s\n", logFileBasePath, err)
}
// open log file

View File

@@ -10,14 +10,22 @@ import (
"strings"
"syscall"
"github.com/safing/portmaster/core/structure"
"github.com/safing/portmaster/updates"
"github.com/safing/portbase/utils"
"github.com/safing/portbase/info"
portlog "github.com/safing/portbase/log"
"github.com/safing/portmaster/updates"
"github.com/spf13/cobra"
)
var (
databaseRootDir string
dataDir string
databaseDir string
dataRoot *utils.DirStructure
logsRoot *utils.DirStructure
showShortVersion bool
showFullVersion bool
@@ -43,7 +51,8 @@ func init() {
// Let cobra ignore if we are running as "GUI" or not
cobra.MousetrapHelpText = ""
rootCmd.PersistentFlags().StringVar(&databaseRootDir, "db", "", "set database directory")
rootCmd.PersistentFlags().StringVar(&dataDir, "data", "", "set data directory")
rootCmd.PersistentFlags().StringVar(&databaseDir, "db", "", "alias to --data (deprecated)")
rootCmd.Flags().BoolVar(&showFullVersion, "version", false, "print version")
rootCmd.Flags().BoolVar(&showShortVersion, "ver", false, "print version number only")
}
@@ -123,14 +132,30 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) {
portlog.SetLogLevel(portlog.CriticalLevel)
if !showShortVersion && !showFullVersion {
// set database root
if databaseRootDir != "" {
// remove redundant escape characters and quotes
databaseRootDir = strings.Trim(databaseRootDir, `\"`)
// set updates path
updates.SetDatabaseRoot(databaseRootDir)
} else {
return errors.New("please supply the database directory using the --db flag")
// set data root
// backwards compatibility
if dataDir == "" {
dataDir = databaseDir
}
// check data dir
if dataDir == "" {
return errors.New("please set the data directory using --data=/path/to/data/dir")
}
// remove redundant escape characters and quotes
dataDir = strings.Trim(dataDir, `\"`)
// initialize structure
err = structure.Initialize(dataDir, 0755)
if err != nil {
return fmt.Errorf("failed to initialize data root: %s", err)
}
dataRoot = structure.Root()
// manually set updates root (no modules)
updates.SetDataRoot(structure.Root())
// set up logs root
logsRoot = structure.NewRootDir("logs", 0777)
err = logsRoot.Ensure()
if err != nil {
return fmt.Errorf("failed to initialize logs root: %s", err)
}
// warn about CTRL-C on windows

View File

@@ -207,10 +207,10 @@ func execute(opts *Options, args []string) (cont bool, err error) {
// log files
var logFile, errorFile *os.File
logFileBasePath := filepath.Join(databaseRootDir, "logs", "fstree", opts.ShortIdentifier)
err = os.MkdirAll(logFileBasePath, 0777)
logFileBasePath := filepath.Join(logsRoot.Path, "fstree", opts.ShortIdentifier)
err = logsRoot.EnsureAbsPath(logFileBasePath)
if err != nil {
log.Printf("failed to create log file folder %s: %s\n", logFileBasePath, err)
log.Printf("failed to check/create log file dir %s: %s\n", logFileBasePath, err)
} else {
// open log file
logFilePath := filepath.Join(logFileBasePath, fmt.Sprintf("%s.log", time.Now().UTC().Format("2006-02-01-15-04-05")))