fs: use new atomic package - use FileCreateWithPrefix() API

This commit is contained in:
Harshavardhana
2015-11-17 16:32:20 -08:00
parent 26f83f108a
commit 35b9f965f1
5 changed files with 29 additions and 12 deletions

View File

@@ -33,6 +33,11 @@ type File struct {
// Close the file replacing, returns an error if any
func (f *File) Close() error {
// sync to the disk
err := f.Sync()
if err != nil {
return err
}
// close the embedded fd
if err := f.File.Close(); err != nil {
return err
@@ -58,12 +63,18 @@ func (f *File) CloseAndPurge() error {
// FileCreate creates a new file at filePath for atomic writes, it also creates parent directories if they don't exist
func FileCreate(filePath string) (*File, error) {
return FileCreateWithPrefix(filePath, "$deleteme.")
}
// FileCreateWithPrefix creates a new file at filePath for atomic writes, it also creates parent directories if they don't exist
// prefix specifies the prefix of the temporary files so that cleaning stale temp files is easy
func FileCreateWithPrefix(filePath string, prefix string) (*File, error) {
// if parent directories do not exist, ioutil.TempFile doesn't create them
// handle such a case with os.MkdirAll()
if err := os.MkdirAll(filepath.Dir(filePath), 0700); err != nil {
return nil, err
}
f, err := ioutil.TempFile(filepath.Dir(filePath), filepath.Base(filePath))
f, err := ioutil.TempFile(filepath.Dir(filePath), prefix+filepath.Base(filePath))
if err != nil {
return nil, err
}

View File

@@ -30,6 +30,7 @@ import (
"sync"
"github.com/fatih/structs"
"github.com/minio/minio-xl/pkg/atomic"
"github.com/minio/minio-xl/pkg/probe"
)
@@ -193,7 +194,15 @@ func (d config) Save(filename string) *probe.Error {
jsonData = []byte(strings.Replace(string(jsonData), "\n", "\r\n", -1))
}
err = ioutil.WriteFile(filename, jsonData, 0600)
atomicFile, err := atomic.FileCreate(filename)
if err != nil {
return probe.NewError(err)
}
_, err = atomicFile.Write(jsonData)
if err != nil {
return probe.NewError(err)
}
err = atomicFile.Close()
if err != nil {
return probe.NewError(err)
}