github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/node/options_directory.go (about)

     1  /*
     2   * Copyright (C) 2018 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package node
    19  
    20  import (
    21  	"os"
    22  	"path/filepath"
    23  
    24  	"github.com/pkg/errors"
    25  	"github.com/rs/zerolog/log"
    26  
    27  	"github.com/mysteriumnetwork/node/config"
    28  )
    29  
    30  // OptionsDirectory describes data structure holding directories as parameters.
    31  type OptionsDirectory struct {
    32  	// Data directory stores persistent data like keystore, cli history, etc.
    33  	Data string
    34  	// Data directory stores database
    35  	Storage string
    36  	// Data directory stores identity keys
    37  	Keystore string
    38  	// Script directory stores all data needed for runtime, e.g. DNS scripts.
    39  	Script string
    40  	// Runtime directory for various temp file - usually current working dir
    41  	Runtime string
    42  	// NodeUI directory for nodeUI releases
    43  	NodeUI string
    44  }
    45  
    46  const (
    47  	// NetworkSubDirMainnet represents mainnet subdir
    48  	NetworkSubDirMainnet = "mainnet"
    49  
    50  	// NetworkSubDirLocalnet represents localnet subdir
    51  	NetworkSubDirLocalnet = "localnet"
    52  
    53  	// NetworkSubDirTestnet represents localnet subdir
    54  	NetworkSubDirTestnet = "testnet"
    55  )
    56  
    57  // GetOptionsDirectory retrieves directory configuration from app configuration.
    58  func GetOptionsDirectory(network *OptionsNetwork) *OptionsDirectory {
    59  	dataDir := config.GetString(config.FlagDataDir)
    60  	networkSubdir := NetworkSubDirMainnet // Matches DefaultNetworkDefinition
    61  	switch {
    62  	case network.Network.IsMainnet():
    63  		networkSubdir = NetworkSubDirMainnet
    64  	case network.Network.IsTestnet():
    65  		networkSubdir = NetworkSubDirTestnet
    66  	case network.Network.IsLocalnet():
    67  		networkSubdir = NetworkSubDirLocalnet
    68  	}
    69  	return &OptionsDirectory{
    70  		Data:     dataDir,
    71  		Storage:  GetOptionsDirectoryDB(networkSubdir),
    72  		Keystore: GetOptionsDirectoryKeystore(dataDir),
    73  		Script:   config.GetString(config.FlagScriptDir),
    74  		Runtime:  config.GetString(config.FlagRuntimeDir),
    75  		NodeUI:   config.GetString(config.FlagNodeUIDir),
    76  	}
    77  }
    78  
    79  // GetOptionsDirectoryKeystore given a dataDir returns a path for keystore.
    80  func GetOptionsDirectoryKeystore(dataDir string) string {
    81  	return filepath.Join(dataDir, "keystore")
    82  }
    83  
    84  // GetOptionsDirectoryDB returns a database directory given a networkSubdir.
    85  func GetOptionsDirectoryDB(networkSubdir string) string {
    86  	dataDir := config.GetString(config.FlagDataDir)
    87  	return filepath.Join(dataDir, networkSubdir, "db")
    88  }
    89  
    90  // Check checks that configured dirs exist (which should contain info) and runtime dirs are created (if not exist)
    91  func (options *OptionsDirectory) Check() error {
    92  	if err := ensureOrCreateDir(options.Runtime); err != nil {
    93  		return err
    94  	}
    95  	if err := ensureOrCreateDir(options.Storage); err != nil {
    96  		return err
    97  	}
    98  	return ensureOrCreateDir(options.Data)
    99  }
   100  
   101  func ensureOrCreateDir(dir string) error {
   102  	err := ensureDirExists(dir)
   103  	if os.IsNotExist(err) {
   104  		log.Info().Msg("Directory does not exist, creating a new one: " + dir)
   105  		return os.MkdirAll(dir, 0700)
   106  	}
   107  	return err
   108  }
   109  
   110  func ensureDirExists(dir string) error {
   111  	fileStat, err := os.Stat(dir)
   112  	if err != nil {
   113  		return err
   114  	}
   115  	if isDir := fileStat.IsDir(); !isDir {
   116  		return errors.Errorf("directory expected: %s", dir)
   117  	}
   118  	return nil
   119  }