From b745676a24f48c636359b9ecfc6c6d90de3d9e2d Mon Sep 17 00:00:00 2001 From: Krishna Srinivas Date: Tue, 17 Nov 2015 16:23:32 -0800 Subject: [PATCH] quick users atomic package to Save() #1390 --- .../minio/minio-xl/pkg/atomic/atomic.go | 88 +++++++++++++++++++ .../minio/minio-xl/pkg/atomic/atomic_test.go | 66 ++++++++++++++ .../minio/minio-xl/pkg/quick/quick.go | 11 ++- vendor/vendor.json | 9 +- 4 files changed, 171 insertions(+), 3 deletions(-) create mode 100644 vendor/github.com/minio/minio-xl/pkg/atomic/atomic.go create mode 100644 vendor/github.com/minio/minio-xl/pkg/atomic/atomic_test.go diff --git a/vendor/github.com/minio/minio-xl/pkg/atomic/atomic.go b/vendor/github.com/minio/minio-xl/pkg/atomic/atomic.go new file mode 100644 index 00000000..e6969a69 --- /dev/null +++ b/vendor/github.com/minio/minio-xl/pkg/atomic/atomic.go @@ -0,0 +1,88 @@ +/* + * Minio Client (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// NOTE - Rename() not guaranteed to be atomic on all filesystems which are not fully POSIX compatible + +// Package atomic provides atomic file write semantics by leveraging Rename's() atomicity. +package atomic + +import ( + "io/ioutil" + "os" + "path/filepath" +) + +// File container provided for atomic file writes +type File struct { + *os.File + file string +} + +// 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 + } + // atomic rename to final destination + if err := os.Rename(f.Name(), f.file); err != nil { + return err + } + return nil +} + +// CloseAndPurge removes the temp file, closes the transaction and returns an error if any +func (f *File) CloseAndPurge() error { + // close the embedded fd + if err := f.File.Close(); err != nil { + return err + } + if err := os.Remove(f.Name()); err != nil { + return err + } + return nil +} + +// 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), prefix+filepath.Base(filePath)) + if err != nil { + return nil, err + } + if err := os.Chmod(f.Name(), 0600); err != nil { + if err := os.Remove(f.Name()); err != nil { + return nil, err + } + return nil, err + } + return &File{File: f, file: filePath}, nil +} diff --git a/vendor/github.com/minio/minio-xl/pkg/atomic/atomic_test.go b/vendor/github.com/minio/minio-xl/pkg/atomic/atomic_test.go new file mode 100644 index 00000000..1f026e2a --- /dev/null +++ b/vendor/github.com/minio/minio-xl/pkg/atomic/atomic_test.go @@ -0,0 +1,66 @@ +/* + * Minio Client (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package atomic + +import ( + "io/ioutil" + "os" + "path/filepath" + "testing" + + . "gopkg.in/check.v1" +) + +func Test(t *testing.T) { TestingT(t) } + +type MySuite struct { + root string +} + +var _ = Suite(&MySuite{}) + +func (s *MySuite) SetUpSuite(c *C) { + root, err := ioutil.TempDir("/tmp", "atomic-") + c.Assert(err, IsNil) + s.root = root +} + +func (s *MySuite) TearDownSuite(c *C) { + os.RemoveAll(s.root) +} + +func (s *MySuite) TestAtomic(c *C) { + f, err := FileCreate(filepath.Join(s.root, "testfile")) + c.Assert(err, IsNil) + _, err = os.Stat(filepath.Join(s.root, "testfile")) + c.Assert(err, Not(IsNil)) + err = f.Close() + c.Assert(err, IsNil) + _, err = os.Stat(filepath.Join(s.root, "testfile")) + c.Assert(err, IsNil) +} + +func (s *MySuite) TestAtomicPurge(c *C) { + f, err := FileCreate(filepath.Join(s.root, "purgefile")) + c.Assert(err, IsNil) + _, err = os.Stat(filepath.Join(s.root, "purgefile")) + c.Assert(err, Not(IsNil)) + err = f.CloseAndPurge() + c.Assert(err, IsNil) + err = f.Close() + c.Assert(err, Not(IsNil)) +} 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 65c7ca3d..92bb27b9 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 0dc4c5e2..c0ab77a8 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -32,6 +32,11 @@ "revision": "8264db82e69176b2a0f5b37fd8ffd232bdea6ad1", "revisionTime": "2015-11-16T12:27:52-08:00" }, + { + "path": "github.com/minio/minio-xl/pkg/atomic", + "revision": "008404af67dcf66bdde580245cd43951b425ed39", + "revisionTime": "2015-11-17T16:21:42-08:00" + }, { "path": "github.com/minio/minio-xl/pkg/probe", "revision": "a7b8623fd546965505f18172717393f5de4139a2", @@ -39,8 +44,8 @@ }, { "path": "github.com/minio/minio-xl/pkg/quick", - "revision": "c6e98ee0f2bd750e516e468b04dffe6419654aa6", - "revisionTime": "2015-10-16T11:34:25-07:00" + "revision": "008404af67dcf66bdde580245cd43951b425ed39", + "revisionTime": "2015-11-17T16:21:42-08:00" }, { "path": "github.com/minio/pb",