Be more atomic during writing of files -- this prevents other processes from seeing empty or impartial files

This commit is contained in:
Eugene Bujak 2018-09-06 02:03:03 +03:00
parent 0ce40fd46e
commit 859f1590dd
2 changed files with 18 additions and 3 deletions

View File

@ -108,11 +108,16 @@ func writeConfig() error {
log.Printf("Couldn't generate YAML file: %s", err) log.Printf("Couldn't generate YAML file: %s", err)
return err return err
} }
err = ioutil.WriteFile(configfile, yamlText, 0644) err = ioutil.WriteFile(configfile+".tmp", yamlText, 0644)
if err != nil { if err != nil {
log.Printf("Couldn't write YAML config: %s", err) log.Printf("Couldn't write YAML config: %s", err)
return err return err
} }
err = os.Rename(configfile+".tmp", configfile)
if err != nil {
log.Printf("Couldn't rename YAML config: %s", err)
return err
}
return nil return nil
} }
@ -127,10 +132,14 @@ func writeCoreDNSConfig() error {
log.Printf("Couldn't generate DNS config: %s", err) log.Printf("Couldn't generate DNS config: %s", err)
return err return err
} }
err = ioutil.WriteFile(corefile, []byte(configtext), 0644) err = ioutil.WriteFile(corefile+".tmp", []byte(configtext), 0644)
if err != nil { if err != nil {
log.Printf("Couldn't write DNS config: %s", err) log.Printf("Couldn't write DNS config: %s", err)
} }
err = os.Rename(corefile+".tmp", corefile)
if err != nil {
log.Printf("Couldn't rename DNS config: %s", err)
}
return err return err
} }

View File

@ -929,11 +929,17 @@ func writeFilterFile() error {
data = append(data, []byte(rule)...) data = append(data, []byte(rule)...)
data = append(data, '\n') data = append(data, '\n')
} }
err := ioutil.WriteFile(filterpath, data, 0644) err := ioutil.WriteFile(filterpath+".tmp", data, 0644)
if err != nil { if err != nil {
log.Printf("Couldn't write filter file: %s", err) log.Printf("Couldn't write filter file: %s", err)
return err return err
} }
err = os.Rename(filterpath+".tmp", filterpath)
if err != nil {
log.Printf("Couldn't rename filter file: %s", err)
return err
}
return nil return nil
} }