From 229d6671b99a4b90858de07d62d57fdd4754709e Mon Sep 17 00:00:00 2001
From: BitBaseBit <vardygreg23@gmail.com>
Date: Mon, 14 Jun 2021 07:35:01 +0100
Subject: [PATCH] Added temp file in crashreports because git is stupid (#2157)

* added temp file in crashreports because git is stupid and wont add empty folders

* added proper creating of crash report dir

* changed crashreports dir to one directory up from datadir

* implemented suggestions

* switched back to 5d651381daf9d12cc663a7326b3b16d7bb4c1aa0 for tests/testdata
---
 common/debug/log_panic.go | 28 ++++++++++++----------------
 crashreports/.hold        |  0
 node/node.go              |  2 +-
 3 files changed, 13 insertions(+), 17 deletions(-)
 create mode 100644 crashreports/.hold

diff --git a/common/debug/log_panic.go b/common/debug/log_panic.go
index f1d361d03c..eec6e2b07b 100644
--- a/common/debug/log_panic.go
+++ b/common/debug/log_panic.go
@@ -13,6 +13,7 @@ import (
 )
 
 var sigc chan os.Signal
+var crashReportDir string
 
 func GetSigC(sig *chan os.Signal) {
 	sigc = *sig
@@ -23,22 +24,22 @@ func prettyTime() string {
 	return strings.Replace(time[:19], " ", "-", 1)
 }
 
-func CheckForCrashes() {
-	ex, err := os.Executable()
-	if err != nil {
-		log.Warn(err.Error())
+func CheckForCrashes(datadir string) {
+	crashReportDir = filepath.Join(datadir, "crashreports")
+	if _, err := os.Stat(crashReportDir); os.IsNotExist(err) {
+		os.Mkdir(crashReportDir, 0755)
+	} else if err != nil {
+		log.Error("log_panig.go: CheckForCrashes", "error", err)
 		return
 	}
-	binPath := filepath.Dir(ex)
-	crashReportDir := filepath.Join(binPath[:len(binPath)-10], "crashreports")
 	f, err := os.Open(crashReportDir)
 	if err != nil {
-		log.Error(err.Error())
+		log.Error("log_panig.go: CheckForCrashes", "error", err)
 		return
 	}
 	fileInfo, err := f.ReadDir(-1)
 	if err != nil {
-		log.Error(err.Error())
+		log.Error("log_panig.go: CheckForCrashes", "error", err)
 		return
 	}
 	for _, v := range fileInfo {
@@ -46,6 +47,7 @@ func CheckForCrashes() {
 			msg := fmt.Sprintf("Crashes From Previous Boots Detected. Find the stack trace in %v",
 				crashReportDir)
 			log.Warn(msg)
+			f.Close()
 			return
 		}
 	}
@@ -71,16 +73,10 @@ func LogPanic(err error, stopErigon bool, panicResult interface{}) error {
 }
 
 func WriteStackTraceOnPanic(stack string) {
-	ex, err := os.Executable()
-	if err != nil {
-		log.Warn(err.Error())
-		return
-	}
-	binPath := filepath.Dir(ex)
-	fileName := filepath.Join(binPath[:len(binPath)-10], "crashreports", prettyTime()+".txt")
+	fileName := filepath.Join(crashReportDir, prettyTime()+".txt")
 	f, errFs := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
 	if errFs != nil {
-		log.Error(errFs.Error())
+		log.Error("log_panic.go:WriteStackTraceOnPanic", "error", errFs)
 		f.Close()
 		return
 	}
diff --git a/crashreports/.hold b/crashreports/.hold
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/node/node.go b/node/node.go
index 565337c02d..d0f842dd44 100644
--- a/node/node.go
+++ b/node/node.go
@@ -123,7 +123,7 @@ func New(conf *Config) (*Node, error) {
 	node.ipc = newIPCServer(node.log, conf.IPCEndpoint())
 	// Check for uncaught crashes from the previous boot and notify the user if
 	// there are any
-	debug.CheckForCrashes()
+	debug.CheckForCrashes(conf.DataDir)
 
 	return node, nil
 }
-- 
GitLab