1
0
Fork 0
forked from External/grumble

freezer_windows: make ReplaceFile call fall back to os.Rename.

ReplaceFile fails because dst does not yet exist.
To remedy this, we fall back to os.Rename, which makes
everything work as it should.
This commit is contained in:
Mikkel Krautz 2018-01-13 21:29:27 +01:00
parent f53f5b7e00
commit 9922dab5fd

View file

@ -8,6 +8,7 @@ import (
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"io/ioutil" "io/ioutil"
"mumble.info/grumble/pkg/replacefile" "mumble.info/grumble/pkg/replacefile"
"os"
"path/filepath" "path/filepath"
"strconv" "strconv"
) )
@ -50,9 +51,16 @@ func (server *Server) freezeToFile() (err error) {
src := f.Name() src := f.Name()
dst := filepath.Join(Args.DataDir, "servers", strconv.FormatInt(server.Id, 10), "main.fz") dst := filepath.Join(Args.DataDir, "servers", strconv.FormatInt(server.Id, 10), "main.fz")
backup := filepath.Join(Args.DataDir, "servers", strconv.FormatInt(server.Id, 10), "backup.fz") backup := filepath.Join(Args.DataDir, "servers", strconv.FormatInt(server.Id, 10), "backup.fz")
err = replacefile.ReplaceFile(dst, src, backup, replacefile.Flag(0)) err = replacefile.ReplaceFile(dst, src, backup, replacefile.Flag(0))
if err != nil { // If the dst file does not exist (as in, on first launch)
return err // fall back to os.Rename. ReplaceFile does not work if the
// dst file is not there.
if os.IsNotExist(err) {
err = os.Rename(src, dst)
if err != nil {
return err
}
} }
return nil return nil