2015-07-07 15:29:08 -04:00
|
|
|
/*
|
2016-02-10 19:40:09 -05:00
|
|
|
* Minio Cloud Storage (C) 2015-2016 Minio, Inc.
|
2015-07-07 15:29:08 -04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
}
|
|
|
|
|
2015-11-18 02:03:55 -05:00
|
|
|
// CloseAndSync sync file to disk and close, returns an error if any
|
|
|
|
func (f *File) CloseAndSync() error {
|
2015-11-17 19:32:20 -05:00
|
|
|
// sync to the disk
|
2015-11-18 02:03:55 -05:00
|
|
|
if err := f.File.Sync(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
2015-11-17 19:32:20 -05:00
|
|
|
return err
|
|
|
|
}
|
2015-11-18 02:03:55 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the file, returns an error if any
|
|
|
|
func (f *File) Close() error {
|
2015-07-07 15:29:08 -04:00
|
|
|
// close the embedded fd
|
|
|
|
if err := f.File.Close(); err != nil {
|
2015-08-03 19:17:21 -04:00
|
|
|
return err
|
2015-07-07 15:29:08 -04:00
|
|
|
}
|
|
|
|
// atomic rename to final destination
|
|
|
|
if err := os.Rename(f.Name(), f.file); err != nil {
|
2015-08-03 19:17:21 -04:00
|
|
|
return err
|
2015-07-07 15:29:08 -04:00
|
|
|
}
|
|
|
|
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 {
|
2015-08-03 19:17:21 -04:00
|
|
|
return err
|
2015-07-07 15:29:08 -04:00
|
|
|
}
|
|
|
|
if err := os.Remove(f.Name()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-07 15:35:50 -04:00
|
|
|
// FileCreate creates a new file at filePath for atomic writes, it also creates parent directories if they don't exist
|
2015-07-07 15:29:08 -04:00
|
|
|
func FileCreate(filePath string) (*File, error) {
|
2015-11-17 19:32:20 -05:00
|
|
|
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) {
|
2015-07-07 15:35:50 -04:00
|
|
|
// 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 {
|
2015-08-03 19:17:21 -04:00
|
|
|
return nil, err
|
2015-07-07 15:35:50 -04:00
|
|
|
}
|
2015-11-17 19:32:20 -05:00
|
|
|
f, err := ioutil.TempFile(filepath.Dir(filePath), prefix+filepath.Base(filePath))
|
2015-07-07 15:29:08 -04:00
|
|
|
if err != nil {
|
2015-08-03 19:17:21 -04:00
|
|
|
return nil, err
|
2015-07-07 15:29:08 -04:00
|
|
|
}
|
2016-02-18 20:16:41 -05:00
|
|
|
if err = os.Chmod(f.Name(), 0600); err != nil {
|
|
|
|
if err = os.Remove(f.Name()); err != nil {
|
2015-08-03 19:17:21 -04:00
|
|
|
return nil, err
|
2015-07-07 15:29:08 -04:00
|
|
|
}
|
2015-08-03 19:17:21 -04:00
|
|
|
return nil, err
|
2015-07-07 15:29:08 -04:00
|
|
|
}
|
|
|
|
return &File{File: f, file: filePath}, nil
|
|
|
|
}
|