Use ioutil.TempFile, not some nasty homebrew crap

This commit is contained in:
Jakob Borg 2014-09-22 15:54:36 +02:00
parent 4ddd87e773
commit 9797f62cb8

View File

@ -10,7 +10,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"math/rand" "io/ioutil"
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
@ -617,13 +617,12 @@ func (m *Model) SetIgnores(repo string, content []string) error {
return fmt.Errorf("Repo %s does not exist", repo) return fmt.Errorf("Repo %s does not exist", repo)
} }
tmpFileName := filepath.Join(cfg.Directory, fmt.Sprintf(".stignore.%d", rand.Int63())) fd, err := ioutil.TempFile(cfg.Directory, "stignore-"+repo)
fd, err := os.Create(tmpFileName)
if err != nil { if err != nil {
l.Warnln("Saving .stignore:", err) l.Warnln("Saving .stignore:", err)
return err return err
} }
defer os.Remove(tmpFileName) defer os.Remove(fd.Name())
writer := bufio.NewWriter(fd) writer := bufio.NewWriter(fd)
for _, line := range content { for _, line := range content {
@ -637,7 +636,7 @@ func (m *Model) SetIgnores(repo string, content []string) error {
} }
file := filepath.Join(cfg.Directory, ".stignore") file := filepath.Join(cfg.Directory, ".stignore")
err = osutil.Rename(tmpFileName, file) err = osutil.Rename(fd.Name(), file)
if err != nil { if err != nil {
l.Warnln("Saving .stignore:", err) l.Warnln("Saving .stignore:", err)
return err return err