1
0
mirror of https://github.com/golang/go synced 2024-11-22 07:34:40 -07:00

godoc: atomically update filter file

R=rsc
CC=golang-dev
https://golang.org/cl/2206041
This commit is contained in:
Robert Griesemer 2010-09-14 16:54:38 -07:00
parent bce8f51b2b
commit 4398768b84
2 changed files with 24 additions and 4 deletions

View File

@ -170,10 +170,8 @@ func updateFilterFile() {
}) })
// update filter file // update filter file
// TODO(gri) should write a tmp file and atomically rename if err := writeFileAtomically(*filter, buf.Bytes()); err != nil {
err := ioutil.WriteFile(*filter, buf.Bytes(), 0666) log.Stderrf("writeFileAtomically(%s): %s", *filter, err)
if err != nil {
log.Stderrf("ioutil.Writefile(%s): %s", *filter, err)
filterDelay.backoff(24 * 60) // back off exponentially, but try at least once a day filterDelay.backoff(24 * 60) // back off exponentially, but try at least once a day
} else { } else {
filterDelay.set(*filterMin) // revert to regular filter update schedule filterDelay.set(*filterMin) // revert to regular filter update schedule

View File

@ -7,6 +7,8 @@
package main package main
import ( import (
"io"
"io/ioutil"
"os" "os"
pathutil "path" pathutil "path"
"sort" "sort"
@ -85,3 +87,23 @@ func canonicalizePaths(list []string, filter func(path string) bool) []string {
return list[0:i] return list[0:i]
} }
// writeFileAtomically writes data to a temporary file and then
// atomically renames that file to the file named by filename.
//
func writeFileAtomically(filename string, data []byte) os.Error {
f, err := ioutil.TempFile(cwd, filename)
if err != nil {
return err
}
n, err := f.Write(data)
f.Close()
if err != nil {
return err
}
if n < len(data) {
return io.ErrShortWrite
}
return os.Rename(f.Name(), filename)
}