2014-09-13 17:00:14 +08:00
|
|
|
// Copyright 2012, Google Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package ioutil2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Write file to temp and atomically move when everything else succeeds.
|
|
|
|
func WriteFileAtomic(filename string, data []byte, perm os.FileMode) error {
|
2017-05-17 14:39:44 +08:00
|
|
|
dir, name := path.Dir(filename), path.Base(filename)
|
2014-09-13 17:00:14 +08:00
|
|
|
f, err := ioutil.TempFile(dir, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n, err := f.Write(data)
|
|
|
|
f.Close()
|
|
|
|
if err == nil && n < len(data) {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
} else {
|
|
|
|
err = os.Chmod(f.Name(), perm)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(f.Name())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.Rename(f.Name(), filename)
|
|
|
|
}
|
2014-09-17 13:11:39 +08:00
|
|
|
|
|
|
|
// Check file exists or not
|
|
|
|
func FileExists(name string) bool {
|
|
|
|
_, err := os.Stat(name)
|
|
|
|
return !os.IsNotExist(err)
|
|
|
|
}
|