Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add atomic writes #99

Merged
merged 2 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
35 changes: 12 additions & 23 deletions utils/bootfile/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"

logging "github.com/home-assistant/os-agent/utils/log"

"github.com/natefinch/atomic"
)

type Editor struct {
Expand Down Expand Up @@ -60,20 +62,7 @@ func (e Editor) DisableOption(optionName string) error {
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 {
Expand Down Expand Up @@ -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
}