2019-02-19 06:17:27 +00:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-12-15 20:01:51 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-02-19 06:17:27 +00:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2020-08-10 18:54:40 +00:00
|
|
|
|
2024-02-15 05:46:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v5/io/file"
|
2019-02-19 06:17:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultDataDir is the default data directory to use for the databases and other
|
|
|
|
// persistence requirements.
|
|
|
|
func DefaultDataDir() string {
|
|
|
|
// Try to place the data folder in the user's home dir
|
2021-09-17 21:55:24 +00:00
|
|
|
home := file.HomeDir()
|
2019-02-19 06:17:27 +00:00
|
|
|
if home != "" {
|
2024-01-15 14:46:54 +00:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "darwin":
|
2019-03-18 15:45:28 +00:00
|
|
|
return filepath.Join(home, "Library", "Eth2")
|
2024-01-15 14:46:54 +00:00
|
|
|
case "windows":
|
2020-12-15 20:01:51 +00:00
|
|
|
return filepath.Join(home, "AppData", "Local", "Eth2")
|
2024-01-15 14:46:54 +00:00
|
|
|
default:
|
2019-03-18 15:45:28 +00:00
|
|
|
return filepath.Join(home, ".eth2")
|
2019-02-19 06:17:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// As we cannot guess a stable location, return empty and handle later
|
|
|
|
return ""
|
|
|
|
}
|
2020-12-15 20:01:51 +00:00
|
|
|
|
|
|
|
// FixDefaultDataDir checks if previous data directory is found and can be migrated to a new path.
|
|
|
|
// This is used to resolve issue with weak default path (for Windows users) in existing installations.
|
|
|
|
// For full details see: https://github.com/prysmaticlabs/prysm/issues/5660.
|
|
|
|
func FixDefaultDataDir(prevDataDir, curDataDir string) error {
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean paths.
|
2021-09-17 21:55:24 +00:00
|
|
|
prevDataDir, err := file.ExpandPath(prevDataDir)
|
2020-12-15 20:01:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-17 21:55:24 +00:00
|
|
|
curDataDir, err = file.ExpandPath(curDataDir)
|
2020-12-15 20:01:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if shared directory is found (if it is -- we need to move it to non-shared destination).
|
2021-09-17 21:55:24 +00:00
|
|
|
prevDataDirExists, err := file.HasDir(prevDataDir)
|
2020-12-15 20:01:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !prevDataDirExists {
|
|
|
|
// If no previous "%APPDATA%\Eth2" found, nothing to patch and move to new default location.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if curDataDir == "" {
|
|
|
|
curDataDir = DefaultDataDir()
|
|
|
|
}
|
2021-09-17 21:55:24 +00:00
|
|
|
selectedDirExists, err := file.HasDir(curDataDir)
|
2020-12-15 20:01:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if selectedDirExists {
|
|
|
|
// No need not move anything, destination directory already exists.
|
|
|
|
log.Warnf("Outdated data directory is found: %s! The current data folder %s is not empty, "+
|
|
|
|
"so can not copy files automatically. Either remove outdated data directory, or "+
|
|
|
|
"consider specifying non-existent new data directory (files will be moved automatically).\n"+
|
|
|
|
"For full details see: https://github.com/prysmaticlabs/prysm/issues/5660.",
|
|
|
|
prevDataDir, curDataDir)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if curDataDir == prevDataDir {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Warnf("Outdated data directory is found: %s. "+
|
|
|
|
"Copying its contents to the new data folder: %s", prevDataDir, curDataDir)
|
2021-09-17 21:55:24 +00:00
|
|
|
if err := file.CopyDir(prevDataDir, curDataDir); err != nil {
|
2020-12-15 20:01:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Infof("All files from the outdated data directory %s have been moved to %s.", prevDataDir, curDataDir)
|
|
|
|
|
|
|
|
// If directories match, previous data directory can be safely deleted.
|
|
|
|
actionText := "The outdated directory is copied and not needed anymore, so should be deleted. " +
|
|
|
|
"Directory %s and its contents will be removed - do you want to proceed? (Y/N)"
|
|
|
|
deniedText := "Outdated directory will not be deleted. No changes have been made."
|
|
|
|
removeConfirmed, err := ConfirmAction(fmt.Sprintf(actionText, prevDataDir), deniedText)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-17 21:55:24 +00:00
|
|
|
if removeConfirmed && file.DirsEqual(prevDataDir, curDataDir) {
|
2020-12-15 20:01:51 +00:00
|
|
|
if err := os.RemoveAll(prevDataDir); err != nil {
|
|
|
|
return fmt.Errorf("cannot remove outdated directory or one of its files: %w", err)
|
|
|
|
}
|
|
|
|
log.Infof("Successfully removed %s", prevDataDir)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|