Commit 522e0887 authored by Knic Knic's avatar Knic Knic

do not rename inuse files

parent 95760fff
...@@ -133,17 +133,21 @@ func (p *Passwd) Write(passwdFile string) error { ...@@ -133,17 +133,21 @@ func (p *Passwd) Write(passwdFile string) error {
} }
func writePasswords(passwdFile string, records [][]string) error { func writePasswords(passwdFile string, records [][]string) error {
out, err := os.Create(passwdFile + ".tmp") err := func() error {
if err != nil { // ensure to close tmp file before rename for filesystems like NTFS
return err out, err := os.Create(passwdFile + ".tmp")
} if err != nil {
defer out.Close() return err
}
defer out.Close()
if err := out.Chmod(0600); err != nil { if err := out.Chmod(0600); err != nil {
return err return err
} }
if err := csv.NewWriter(out).WriteAll(records); err != nil { return csv.NewWriter(out).WriteAll(records)
}()
if err != nil {
return err return err
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment