From 35b9f965f155832c3db165a2153e88325ab60da7 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 17 Nov 2015 16:32:20 -0800 Subject: [PATCH] fs: use new atomic package - use FileCreateWithPrefix() API --- pkg/fs/fs-multipart.go | 6 ++---- pkg/fs/fs-object.go | 3 +-- .../github.com/minio/minio-xl/pkg/atomic/atomic.go | 13 ++++++++++++- vendor/github.com/minio/minio-xl/pkg/quick/quick.go | 11 ++++++++++- vendor/vendor.json | 8 ++++---- 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/pkg/fs/fs-multipart.go b/pkg/fs/fs-multipart.go index fd94bbff2..8072af7b8 100644 --- a/pkg/fs/fs-multipart.go +++ b/pkg/fs/fs-multipart.go @@ -276,7 +276,7 @@ func (fs Filesystem) CreateObjectPart(bucket, object, uploadID, expectedMD5Sum s objectPath := filepath.Join(bucketPath, object) partPath := objectPath + fmt.Sprintf("$%d", partID) - partFile, err := atomic.FileCreate(partPath) + partFile, err := atomic.FileCreateWithPrefix(partPath, "") if err != nil { return "", probe.NewError(err) } @@ -307,7 +307,6 @@ func (fs Filesystem) CreateObjectPart(bucket, object, uploadID, expectedMD5Sum s return "", probe.NewError(SignatureDoesNotMatch{}) } } - partFile.File.Sync() partFile.Close() fi, err := os.Stat(partPath) @@ -374,7 +373,7 @@ func (fs Filesystem) CompleteMultipartUpload(bucket, object, uploadID string, da } objectPath := filepath.Join(bucketPath, object) - file, err := atomic.FileCreate(objectPath) + file, err := atomic.FileCreateWithPrefix(objectPath, "") if err != nil { return ObjectMetadata{}, probe.NewError(err) } @@ -430,7 +429,6 @@ func (fs Filesystem) CompleteMultipartUpload(bucket, object, uploadID string, da file.CloseAndPurge() return ObjectMetadata{}, err.Trace() } - file.File.Sync() file.Close() st, err := os.Stat(objectPath) diff --git a/pkg/fs/fs-object.go b/pkg/fs/fs-object.go index 98b04797a..1369db05b 100644 --- a/pkg/fs/fs-object.go +++ b/pkg/fs/fs-object.go @@ -207,7 +207,7 @@ func (fs Filesystem) CreateObject(bucket, object, expectedMD5Sum string, size in } // write object - file, err := atomic.FileCreate(objectPath) + file, err := atomic.FileCreateWithPrefix(objectPath, "") if err != nil { return ObjectMetadata{}, probe.NewError(err) } @@ -250,7 +250,6 @@ func (fs Filesystem) CreateObject(bucket, object, expectedMD5Sum string, size in return ObjectMetadata{}, probe.NewError(SignatureDoesNotMatch{}) } } - file.File.Sync() file.Close() st, err := os.Stat(objectPath) diff --git a/vendor/github.com/minio/minio-xl/pkg/atomic/atomic.go b/vendor/github.com/minio/minio-xl/pkg/atomic/atomic.go index 2e6d23ed3..e6969a690 100644 --- a/vendor/github.com/minio/minio-xl/pkg/atomic/atomic.go +++ b/vendor/github.com/minio/minio-xl/pkg/atomic/atomic.go @@ -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 } diff --git a/vendor/github.com/minio/minio-xl/pkg/quick/quick.go b/vendor/github.com/minio/minio-xl/pkg/quick/quick.go index 65c7ca3de..92bb27b9a 100644 --- a/vendor/github.com/minio/minio-xl/pkg/quick/quick.go +++ b/vendor/github.com/minio/minio-xl/pkg/quick/quick.go @@ -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) } diff --git a/vendor/vendor.json b/vendor/vendor.json index fede15a10..508195db1 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -39,8 +39,8 @@ }, { "path": "github.com/minio/minio-xl/pkg/atomic", - "revision": "a5fc6d2430ba2ebcab31b938ab02a42bac85dc2e", - "revisionTime": "2015-10-20T11:16:42-07:00" + "revision": "008404af67dcf66bdde580245cd43951b425ed39", + "revisionTime": "2015-11-17T16:21:42-08:00" }, { "path": "github.com/minio/minio-xl/pkg/cpu", @@ -69,8 +69,8 @@ }, { "path": "github.com/minio/minio-xl/pkg/quick", - "revision": "a5fc6d2430ba2ebcab31b938ab02a42bac85dc2e", - "revisionTime": "2015-10-20T11:16:42-07:00" + "revision": "008404af67dcf66bdde580245cd43951b425ed39", + "revisionTime": "2015-11-17T16:21:42-08:00" }, { "path": "github.com/rs/cors",