Implement XL layer - preliminary work.

This commit is contained in:
Harshavardhana
2016-03-27 21:52:38 -07:00
parent bf8a9702a4
commit a98a7fb1ad
24 changed files with 2990 additions and 21 deletions

View File

@@ -20,11 +20,20 @@
package safe
import (
"io"
"io/ioutil"
"os"
"path/filepath"
)
// Vault - vault is an interface for different implementations of safe
// i/o semantics.
type Vault interface {
io.ReadWriteCloser
SyncClose() error
CloseAndRemove() error
}
// File provides for safe file writes.
type File struct {
*os.File
@@ -37,6 +46,7 @@ func (f *File) SyncClose() error {
if err := f.File.Sync(); err != nil {
return err
}
// Close the fd.
if err := f.Close(); err != nil {
return err
}
@@ -45,11 +55,11 @@ func (f *File) SyncClose() error {
// Close the file, returns an error if any
func (f *File) Close() error {
// close the embedded fd
// Close the embedded fd.
if err := f.File.Close(); err != nil {
return err
}
// safe rename to final destination
// Safe rename to final destination
if err := os.Rename(f.Name(), f.file); err != nil {
return err
}
@@ -63,6 +73,7 @@ func (f *File) CloseAndRemove() error {
if err := f.File.Close(); err != nil {
return err
}
// Remove the temp file.
if err := os.Remove(f.Name()); err != nil {
return err
}

View File

@@ -54,7 +54,7 @@ func (s *MySuite) TestSafe(c *C) {
c.Assert(err, IsNil)
}
func (s *MySuite) TestSafePurge(c *C) {
func (s *MySuite) TestSafeRemove(c *C) {
f, err := CreateFile(filepath.Join(s.root, "purgefile"))
c.Assert(err, IsNil)
_, err = os.Stat(filepath.Join(s.root, "purgefile"))