github.com/prysmaticlabs/prysm@v1.4.4/shared/cmd/defaults.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser 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  // The go-ethereum library 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 Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package cmd
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"path/filepath"
    23  	"runtime"
    24  
    25  	"github.com/prysmaticlabs/prysm/shared/fileutil"
    26  )
    27  
    28  // DefaultDataDir is the default data directory to use for the databases and other
    29  // persistence requirements.
    30  func DefaultDataDir() string {
    31  	// Try to place the data folder in the user's home dir
    32  	home := fileutil.HomeDir()
    33  	if home != "" {
    34  		if runtime.GOOS == "darwin" {
    35  			return filepath.Join(home, "Library", "Eth2")
    36  		} else if runtime.GOOS == "windows" {
    37  			return filepath.Join(home, "AppData", "Local", "Eth2")
    38  		} else {
    39  			return filepath.Join(home, ".eth2")
    40  		}
    41  	}
    42  	// As we cannot guess a stable location, return empty and handle later
    43  	return ""
    44  }
    45  
    46  // FixDefaultDataDir checks if previous data directory is found and can be migrated to a new path.
    47  // This is used to resolve issue with weak default path (for Windows users) in existing installations.
    48  // For full details see: https://github.com/prysmaticlabs/prysm/issues/5660.
    49  func FixDefaultDataDir(prevDataDir, curDataDir string) error {
    50  	if runtime.GOOS != "windows" {
    51  		return nil
    52  	}
    53  
    54  	// Clean paths.
    55  	prevDataDir, err := fileutil.ExpandPath(prevDataDir)
    56  	if err != nil {
    57  		return err
    58  	}
    59  	curDataDir, err = fileutil.ExpandPath(curDataDir)
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	// See if shared directory is found (if it is -- we need to move it to non-shared destination).
    65  	prevDataDirExists, err := fileutil.HasDir(prevDataDir)
    66  	if err != nil {
    67  		return err
    68  	}
    69  	if !prevDataDirExists {
    70  		// If no previous "%APPDATA%\Eth2" found, nothing to patch and move to new default location.
    71  		return nil
    72  	}
    73  
    74  	if curDataDir == "" {
    75  		curDataDir = DefaultDataDir()
    76  	}
    77  	selectedDirExists, err := fileutil.HasDir(curDataDir)
    78  	if err != nil {
    79  		return err
    80  	}
    81  	if selectedDirExists {
    82  		// No need not move anything, destination directory already exists.
    83  		log.Warnf("Outdated data directory is found: %s! The current data folder %s is not empty, "+
    84  			"so can not copy files automatically. Either remove outdated data directory, or "+
    85  			"consider specifying non-existent new data directory (files will be moved automatically).\n"+
    86  			"For full details see: https://github.com/prysmaticlabs/prysm/issues/5660.",
    87  			prevDataDir, curDataDir)
    88  		return nil
    89  	}
    90  
    91  	if curDataDir == prevDataDir {
    92  		return nil
    93  	}
    94  
    95  	log.Warnf("Outdated data directory is found: %s. "+
    96  		"Copying its contents to the new data folder: %s", prevDataDir, curDataDir)
    97  	if err := fileutil.CopyDir(prevDataDir, curDataDir); err != nil {
    98  		return err
    99  	}
   100  	log.Infof("All files from the outdated data directory %s have been moved to %s.", prevDataDir, curDataDir)
   101  
   102  	// If directories match, previous data directory can be safely deleted.
   103  	actionText := "The outdated directory is copied and not needed anymore, so should be deleted. " +
   104  		"Directory %s and its contents will be removed - do you want to proceed? (Y/N)"
   105  	deniedText := "Outdated directory will not be deleted. No changes have been made."
   106  	removeConfirmed, err := ConfirmAction(fmt.Sprintf(actionText, prevDataDir), deniedText)
   107  	if err != nil {
   108  		return err
   109  	}
   110  	if removeConfirmed && fileutil.DirsEqual(prevDataDir, curDataDir) {
   111  		if err := os.RemoveAll(prevDataDir); err != nil {
   112  			return fmt.Errorf("cannot remove outdated directory or one of its files: %w", err)
   113  		}
   114  		log.Infof("Successfully removed %s", prevDataDir)
   115  	}
   116  	return nil
   117  }