Merge pull request #1440 from KnicKnic/master

do not rename inuse files
This commit is contained in:
Erik Wilson 2020-02-24 15:00:13 -07:00 committed by GitHub
commit b8d8fc3eff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -133,19 +133,23 @@ func (p *Passwd) Write(passwdFile string) error {
}
func writePasswords(passwdFile string, records [][]string) error {
out, err := os.Create(passwdFile + ".tmp")
err := func() error {
// ensure to close tmp file before rename for filesystems like NTFS
out, err := os.Create(passwdFile + ".tmp")
if err != nil {
return err
}
defer out.Close()
if err := out.Chmod(0600); err != nil {
return err
}
return csv.NewWriter(out).WriteAll(records)
}()
if err != nil {
return err
}
defer out.Close()
if err := out.Chmod(0600); err != nil {
return err
}
if err := csv.NewWriter(out).WriteAll(records); err != nil {
return err
}
return os.Rename(passwdFile+".tmp", passwdFile)
}