From 88fea707a2889bd7305dae5308e19beb6b543436 Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Mon, 19 Sep 2022 12:27:11 +0000 Subject: [PATCH 1/2] Add atomic writes --- go.mod | 3 ++- go.sum | 2 ++ utils/bootfile/editor.go | 39 ++++++++++++++------------------------- 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/go.mod b/go.mod index 2e6c6b9..a19d36b 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/fntlnz/mountinfo v0.0.0-20171106231217-40cb42681fad github.com/getsentry/sentry-go v0.13.0 github.com/godbus/dbus/v5 v5.1.0 - github.com/pkg/errors v0.9.1 // indirect + github.com/natefinch/atomic v1.0.1 github.com/opencontainers/runtime-spec v1.0.2 + github.com/pkg/errors v0.9.1 // indirect ) diff --git a/go.sum b/go.sum index cbef9a4..9df88cf 100644 --- a/go.sum +++ b/go.sum @@ -99,6 +99,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/moul/http2curl v1.0.0/go.mod h1:8UbvGypXm98wA/IqH45anm5Y2Z6ep6O31QGOAZ3H0fQ= +github.com/natefinch/atomic v1.0.1 h1:ZPYKxkqQOx3KZ+RsbnP/YsgvxWQPGxjC0oBt2AhwV0A= +github.com/natefinch/atomic v1.0.1/go.mod h1:N/D/ELrljoqDyT3rZrsUmtsuzvHkeB/wWjHV22AZRbM= github.com/nats-io/jwt v0.3.0/go.mod h1:fRYCDE99xlTsqUzISS1Bi75UBJ6ljOJQOAAu5VglpSg= github.com/nats-io/nats.go v1.9.1/go.mod h1:ZjDU1L/7fJ09jvUSRVBR2e7+RnLiiIQyqyzEE/Zbp4w= github.com/nats-io/nkeys v0.1.0/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w= diff --git a/utils/bootfile/editor.go b/utils/bootfile/editor.go index eaa109f..a21007e 100644 --- a/utils/bootfile/editor.go +++ b/utils/bootfile/editor.go @@ -6,6 +6,8 @@ import ( "strings" logging "github.com/home-assistant/os-agent/utils/log" + + "github.com/natefinch/atomic" ) type Editor struct { @@ -43,6 +45,7 @@ func (e Editor) DisableOption(optionName string) error { logging.Error.Printf("Failed to open boot file %s: %s", e.FilePath, err) return err } + defer file.Close() // Scan over all lines fileScanner := bufio.NewScanner(file) @@ -57,23 +60,9 @@ func (e Editor) DisableOption(optionName string) error { outLines = append(outLines, line) } } - file.Close() // Write all lines back to boot config file - file, err = os.Create(e.FilePath) - if err != nil { - logging.Error.Printf("Failed to write boot file %s: %s", e.FilePath, err) - return err - } - - writter := bufio.NewWriter(file) - for _, line := range outLines { - writter.WriteString(line + "\n") - } - writter.Flush() - file.Close() - - return nil + return e.writeNewBootFile(outLines) } func (e Editor) SetOption(optionName string, value string) error { @@ -83,6 +72,7 @@ func (e Editor) SetOption(optionName string, value string) error { logging.Error.Printf("Failed to open boot file %s: %s", e.FilePath, err) return err } + defer file.Close() // Scan over all lines fileScanner := bufio.NewScanner(file) @@ -101,7 +91,6 @@ func (e Editor) SetOption(optionName string, value string) error { outLines = append(outLines, line) } } - file.Close() // No option found, add it if !found { @@ -109,18 +98,18 @@ func (e Editor) SetOption(optionName string, value string) error { } // Write all lines back to boot config file - file, err = os.Create(e.FilePath) + return e.writeNewBootFile(outLines) +} + +func (e Editor) writeNewBootFile(lines []string) error { + // Write all lines back to boot config file + reader := strings.NewReader(strings.Join(lines, "\n")) + + err := atomic.WriteFile(e.FilePath, reader) if err != nil { logging.Error.Printf("Failed to write boot file %s: %s", e.FilePath, err) return err } - writter := bufio.NewWriter(file) - for _, line := range outLines { - writter.WriteString(line + "\n") - } - writter.Flush() - file.Close() - - return nil + return err } From f7e6a365fc03464fe0ed8074127d3bf1faa7585b Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Mon, 19 Sep 2022 12:33:45 +0000 Subject: [PATCH 2/2] close explicit before we going into write function --- utils/bootfile/editor.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/bootfile/editor.go b/utils/bootfile/editor.go index a21007e..51f2053 100644 --- a/utils/bootfile/editor.go +++ b/utils/bootfile/editor.go @@ -45,7 +45,6 @@ func (e Editor) DisableOption(optionName string) error { logging.Error.Printf("Failed to open boot file %s: %s", e.FilePath, err) return err } - defer file.Close() // Scan over all lines fileScanner := bufio.NewScanner(file) @@ -60,6 +59,7 @@ func (e Editor) DisableOption(optionName string) error { outLines = append(outLines, line) } } + file.Close() // Write all lines back to boot config file return e.writeNewBootFile(outLines) @@ -72,7 +72,6 @@ func (e Editor) SetOption(optionName string, value string) error { logging.Error.Printf("Failed to open boot file %s: %s", e.FilePath, err) return err } - defer file.Close() // Scan over all lines fileScanner := bufio.NewScanner(file) @@ -91,6 +90,7 @@ func (e Editor) SetOption(optionName string, value string) error { outLines = append(outLines, line) } } + file.Close() // No option found, add it if !found {