From ae394828db691c21a32fb26d5631b27379d53bbe Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Thu, 5 Oct 2023 21:41:56 +0200 Subject: [PATCH] agent: move check for EROFS/EPERM outside of saveConfig Rather than duplicating the check in saveConfig, check for EROFS or EPERM on the error returned by saveConfig. --- agent/agent.go | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index 330d2210..07a3e80b 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -102,7 +102,10 @@ func Listen(opts Options) error { port := listener.Addr().(*net.TCPAddr).Port err = saveConfig(opts, port) if err != nil { - return err + // ignore and work in remote mode only + if !errors.Is(err, syscall.EROFS) && !errors.Is(err, syscall.EPERM) { + return err + } } if opts.ShutdownCleanup { @@ -149,23 +152,12 @@ func saveConfig(opts Options, port int) error { } err := os.MkdirAll(gopsdir, os.ModePerm) - if errors.Is(err, syscall.EROFS) || errors.Is(err, syscall.EPERM) { // ignore and work in remote mode only - return nil - } if err != nil { return err } portfile = filepath.Join(gopsdir, strconv.Itoa(os.Getpid())) - err = os.WriteFile(portfile, []byte(strconv.Itoa(port)), os.ModePerm) - if errors.Is(err, syscall.EROFS) || errors.Is(err, syscall.EPERM) { // ignore and work in remote mode only - return nil - } - if err != nil { - return err - } - - return nil + return os.WriteFile(portfile, []byte(strconv.Itoa(port)), os.ModePerm) } func gracefulShutdown() {