aboutsummaryrefslogtreecommitdiff
path: root/tofu/tofu.go
diff options
context:
space:
mode:
authorAdnan Maolood <[email protected]>2021-01-14 16:35:54 -0500
committerAdnan Maolood <[email protected]>2021-01-14 16:35:54 -0500
commitda3e9ac0fe3cf8f4c3c78fb35d79e8961f7bae73 (patch)
treed8e3ef9936b69cd569e6117c95166ff753de355c /tofu/tofu.go
parenttofu: Refactor known hosts (diff)
downloadgo-gemini-da3e9ac0fe3cf8f4c3c78fb35d79e8961f7bae73.tar.xz
go-gemini-da3e9ac0fe3cf8f4c3c78fb35d79e8961f7bae73.zip
tofu: Protect HostWriter with a mutex
Diffstat (limited to 'tofu/tofu.go')
-rw-r--r--tofu/tofu.go12
1 files changed, 8 insertions, 4 deletions
diff --git a/tofu/tofu.go b/tofu/tofu.go
index 7dd5069..e2f694a 100644
--- a/tofu/tofu.go
+++ b/tofu/tofu.go
@@ -121,6 +121,7 @@ func (k *KnownHosts) TOFU(hostname string, cert *x509.Certificate) error {
// HostWriter writes host entries to an io.Writer.
type HostWriter struct {
bw *bufio.Writer
+ mu sync.Mutex
}
// NewHostsWriter returns a new host writer that writes to the provided writer.
@@ -131,11 +132,14 @@ func NewHostsWriter(w io.Writer) *HostWriter {
}
// WriteHost writes the host to the underlying io.Writer.
-func (f *HostWriter) WriteHost(host Host) error {
- f.bw.WriteString(host.String())
- f.bw.WriteByte('\n')
+func (h *HostWriter) WriteHost(host Host) error {
+ h.mu.Lock()
+ defer h.mu.Unlock()
- if err := f.bw.Flush(); err != nil {
+ h.bw.WriteString(host.String())
+ h.bw.WriteByte('\n')
+
+ if err := h.bw.Flush(); err != nil {
return fmt.Errorf("failed to write to hosts file: %w", err)
}
return nil