mirror of
https://github.com/minio/minio.git
synced 2025-11-09 13:39:46 -05:00
xl: Moved to minio/minio - fixes #1112
This commit is contained in:
97
pkg/atomic/atomic.go
Normal file
97
pkg/atomic/atomic.go
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Minio Cloud Storage (C) 2015-2016 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"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// File container provided for atomic file writes
|
||||
type File struct {
|
||||
*os.File
|
||||
file string
|
||||
}
|
||||
|
||||
// CloseAndSync sync file to disk and close, returns an error if any
|
||||
func (f *File) CloseAndSync() error {
|
||||
// sync to the disk
|
||||
if err := f.File.Sync(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := f.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close the file, returns an error if any
|
||||
func (f *File) Close() error {
|
||||
// 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
|
||||
}
|
||||
prefix = strings.TrimSpace(prefix)
|
||||
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
|
||||
}
|
||||
66
pkg/atomic/atomic_test.go
Normal file
66
pkg/atomic/atomic_test.go
Normal file
@@ -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(os.TempDir(), "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))
|
||||
}
|
||||
Reference in New Issue
Block a user