mirror of
https://github.com/minio/minio.git
synced 2025-11-07 12:52:58 -05:00
Move atomic file writes into its own package, use them inside quick and disk packages
This commit is contained in:
@@ -18,7 +18,6 @@ package disk
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
@@ -27,6 +26,7 @@ import (
|
||||
"syscall"
|
||||
|
||||
"github.com/minio/minio/pkg/iodine"
|
||||
"github.com/minio/minio/pkg/utils/atomic"
|
||||
)
|
||||
|
||||
// Disk container for disk parameters
|
||||
@@ -36,36 +36,6 @@ type Disk struct {
|
||||
fsInfo map[string]string
|
||||
}
|
||||
|
||||
// File container provided by disk for atomic file writes
|
||||
type File struct {
|
||||
*os.File
|
||||
path string
|
||||
}
|
||||
|
||||
// Close the file replacing the configured file.
|
||||
func (f *File) Close() error {
|
||||
if err := f.File.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Rename(f.Name(), f.path); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Abort closes the file and removes it instead of replacing the configured
|
||||
// file. This is useful if after starting to write to the file you decide you
|
||||
// don't want it anymore.
|
||||
func (f *File) Abort() error {
|
||||
if err := f.File.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Remove(f.Name()); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// New - instantiate new disk
|
||||
func New(diskPath string) (Disk, error) {
|
||||
if diskPath == "" {
|
||||
@@ -175,7 +145,7 @@ func (disk Disk) ListFiles(dirname string) ([]os.FileInfo, error) {
|
||||
}
|
||||
|
||||
// CreateFile - create a file inside disk root path, replies with custome disk.File which provides atomic writes
|
||||
func (disk Disk) CreateFile(filename string) (*File, error) {
|
||||
func (disk Disk) CreateFile(filename string) (*atomic.File, error) {
|
||||
disk.lock.Lock()
|
||||
defer disk.lock.Unlock()
|
||||
|
||||
@@ -188,16 +158,13 @@ func (disk Disk) CreateFile(filename string) (*File, error) {
|
||||
if err := os.MkdirAll(filepath.Dir(filePath), 0700); err != nil {
|
||||
return nil, iodine.New(err, nil)
|
||||
}
|
||||
f, err := ioutil.TempFile(filepath.Dir(filePath), filepath.Base(filePath))
|
||||
|
||||
f, err := atomic.FileCreate(filePath)
|
||||
if err != nil {
|
||||
return nil, iodine.New(err, nil)
|
||||
}
|
||||
if err := os.Chmod(f.Name(), 0600); err != nil {
|
||||
os.Remove(f.Name())
|
||||
return nil, iodine.New(err, nil)
|
||||
}
|
||||
|
||||
return &File{File: f, path: filePath}, nil
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// OpenFile - read a file inside disk root path
|
||||
|
||||
Reference in New Issue
Block a user