Skip to content

Commit

Permalink
Log error and continue if nat settings file cannot be loaded
Browse files Browse the repository at this point in the history
If the nat settings cannot be loaded from file, relocate the file
and continue with cleared settings. Log the original error along
with the relocation path for inspection.
  • Loading branch information
chrisroberts committed Aug 10, 2024
1 parent c42bad4 commit 7f7818d
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions go_src/vagrant-vmware-utility/settings/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"sync"
Expand Down Expand Up @@ -110,14 +109,20 @@ func (n *NAT) Reload() error {
return nil
}
var info NatInfo
data, err := ioutil.ReadFile(n.Path)
data, err := os.ReadFile(n.Path)
if err != nil {
n.logger.Error("failed to read nat file", "error", err, "path", n.Path)
return err
}
if err := json.Unmarshal(data, &info); err != nil {
n.logger.Error("failed to load nat data", "error", err)
return err
if renameErr := os.Rename(n.Path, n.Path+".invalid"); renameErr != nil {
n.logger.Error("failed to move invalid nat settings file", "error", renameErr)
return err
}
n.logger.Warn("moved invalid nat settings file, clearing", "invalid-path", n.Path+".invalid")

return nil
}
n.info = info
return nil
Expand All @@ -137,7 +142,7 @@ func (n *NAT) Save() error {
n.logger.Error("failed to dump nat data", "error", err)
return err
}
f, err := ioutil.TempFile(path.Dir(n.Path), "nat")
f, err := os.CreateTemp(path.Dir(n.Path), "nat")
if err != nil {
n.logger.Error("failed to create file", "error", err, "path", f.Name())
return err
Expand Down

0 comments on commit 7f7818d

Please sign in to comment.