close tmpfile before removing to avoid fd leak

This commit is contained in:
Leo Antunes 2018-08-25 22:27:38 +02:00
parent 2c93f8b233
commit 06d16b7b51

View file

@ -32,12 +32,12 @@ func writeToEtcHosts(ipsToNames ipsToNamesMap, config ConfigSpec) error {
} }
// remove tempfile; this might fail if we managed to move it, which is ok // remove tempfile; this might fail if we managed to move it, which is ok
defer func(path string) { defer func(file *os.File) {
err := os.Remove(path) file.Close()
if err != nil && !os.IsNotExist(err) { if err := os.Remove(file.Name()); err != nil && !os.IsNotExist(err) {
log.Warnf("unexpected error trying to remove temp file %s: %s", path, err) log.Warnf("unexpected error trying to remove temp file %s: %s", file.Name(), err)
} }
}(tmp.Name()) }(tmp)
// go through file and update existing entries/prune nonexistent entries // go through file and update existing entries/prune nonexistent entries
managedLine := false managedLine := false