Skip to content

Commit 8518826

Browse files
umbynoscmaglie
authored andcommitted
crashreports are now created in .arduino-create/logs to preserve macos bundle integrity
1 parent 7ab1736 commit 8518826

File tree

2 files changed

+25
-30
lines changed

2 files changed

+25
-30
lines changed

main.go

+8-12
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"io/ioutil"
2525
"os"
2626
"os/user"
27-
"path/filepath"
2827
"runtime"
2928
"runtime/debug"
3029
"strconv"
@@ -239,9 +238,10 @@ func loop() {
239238

240239
// Instantiate Tools
241240
usr, _ := user.Current()
242-
directory := filepath.Join(usr.HomeDir, ".arduino-create")
241+
usrDir := paths.New(usr.HomeDir)
242+
agentDir := usrDir.Join(".arduino-create")
243243
Tools = tools.Tools{
244-
Directory: directory,
244+
Directory: agentDir.String(),
245245
IndexURL: *indexURL,
246246
Logger: func(msg string) {
247247
mapD := map[string]string{"DownloadStatus": "Pending", "Msg": msg}
@@ -331,16 +331,12 @@ func loop() {
331331
// save crashreport to file
332332
if *crashreport {
333333
logFilename := "crashreport_" + time.Now().Format("20060102150405") + ".log"
334-
currDir, err := os.Getwd()
335-
if err != nil {
336-
panic(err)
337-
}
338334
// handle logs directory creation
339-
logsDir := filepath.Join(currDir, "logs")
340-
if _, err := os.Stat(logsDir); os.IsNotExist(err) {
341-
os.Mkdir(logsDir, 0700)
335+
logsDir := agentDir.Join("logs")
336+
if logsDir.NotExist() {
337+
logsDir.Mkdir()
342338
}
343-
logFile, err := os.OpenFile(filepath.Join(logsDir, logFilename), os.O_WRONLY|os.O_CREATE|os.O_SYNC|os.O_APPEND, 0644)
339+
logFile, err := os.OpenFile(logsDir.Join(logFilename).String(), os.O_WRONLY|os.O_CREATE|os.O_SYNC|os.O_APPEND, 0644)
344340
if err != nil {
345341
log.Print("Cannot create file used for crash-report")
346342
} else {
@@ -399,7 +395,7 @@ func loop() {
399395
r.POST("/update", updateHandler)
400396

401397
// Mount goa handlers
402-
goa := v2.Server(directory)
398+
goa := v2.Server(agentDir.String())
403399
r.Any("/v2/*path", gin.WrapH(goa))
404400

405401
go func() {

systray/systray_real.go

+17-18
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ package systray
2222
import (
2323
"fmt"
2424
"os"
25+
"os/user"
2526
"path/filepath"
2627

2728
log "github.com/sirupsen/logrus"
2829

2930
"github.com/arduino/arduino-create-agent/icon"
31+
"github.com/arduino/go-paths-helper"
3032
"github.com/getlantern/systray"
3133
"github.com/go-ini/ini"
3234
"github.com/skratchdot/open-golang/open"
@@ -101,32 +103,29 @@ func (s *Systray) updateMenuItem(item *systray.MenuItem, disable bool) {
101103

102104
// CrashesIsEmpty checks if the folder containing crash-reports is empty
103105
func (s *Systray) CrashesIsEmpty() bool {
104-
currDir, err := os.Getwd()
105-
if err != nil {
106-
log.Error("Cannot determine executable path: ", err)
107-
}
108-
logsDir := filepath.Join(currDir, "logs")
109-
if _, err := os.Stat(string(logsDir)); os.IsNotExist(err) {
110-
return true
111-
}
112-
return false
106+
logsDir := getLogsDir()
107+
return logsDir.NotExist() // if the logs directory is empty we assume there are no crashreports
113108
}
114109

115110
// RemoveCrashes removes the crash-reports from `logs` folder
116111
func (s *Systray) RemoveCrashes() {
117-
currDir, err := os.Getwd()
118-
if err != nil {
119-
log.Error("Cannot determine executable path: ", err)
120-
}
121-
logsDir := filepath.Join(currDir, "logs")
122-
pathErr := os.RemoveAll(logsDir)
112+
logsDir := getLogsDir()
113+
pathErr := logsDir.RemoveAll()
123114
if pathErr != nil {
124-
log.Error("Cannot remove crashreports: ", pathErr)
115+
log.Errorf("Cannot remove crashreports: %s", pathErr)
125116
} else {
126-
log.Info("Removed crashreports inside: ", logsDir)
117+
log.Infof("Removed crashreports inside: %s", logsDir)
127118
}
128119
}
129120

121+
// getLogsDir simply returns the folder containing the logs
122+
func getLogsDir() *paths.Path {
123+
usr, _ := user.Current()
124+
usrDir := paths.New(usr.HomeDir) // The user folder, on linux/macos /home/<usr>/
125+
agentDir := usrDir.Join(".arduino-create")
126+
return agentDir.Join("logs")
127+
}
128+
130129
// starthibernate creates a systray icon with menu options to resume/quit the agent
131130
func (s *Systray) startHibernate() {
132131
systray.SetIcon(icon.GetIconHiber())
@@ -189,7 +188,7 @@ type configIni struct {
189188
// getconfigs parses all config files in the executable folder
190189
func getConfigs() []configIni {
191190
// config.ini must be there, so call it Default
192-
src, _ := os.Executable()
191+
src, _ := os.Executable() // TODO change path
193192
dest := filepath.Dir(src)
194193

195194
var configs []configIni

0 commit comments

Comments
 (0)