2015-07-07 21:29:08 +02:00
|
|
|
/*
|
2016-02-11 01:40:09 +01:00
|
|
|
* Minio Cloud Storage (C) 2015-2016 Minio, Inc.
|
2015-07-07 21:29:08 +02: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.
|
|
|
|
*/
|
|
|
|
|
2016-04-07 01:05:30 +02:00
|
|
|
// NOTE - Rename() not guaranteed to be safe on all filesystems which are not fully POSIX compatible
|
2015-07-07 21:29:08 +02:00
|
|
|
|
2016-04-07 01:05:30 +02:00
|
|
|
// Package safe provides safe file write semantics by leveraging Rename's() safeity.
|
|
|
|
package safe
|
2015-07-07 21:29:08 +02:00
|
|
|
|
|
|
|
import (
|
2016-03-28 06:52:38 +02:00
|
|
|
"io"
|
2015-07-07 21:29:08 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2016-03-28 06:52:38 +02:00
|
|
|
// Vault - vault is an interface for different implementations of safe
|
|
|
|
// i/o semantics.
|
|
|
|
type Vault interface {
|
|
|
|
io.ReadWriteCloser
|
|
|
|
SyncClose() error
|
|
|
|
CloseAndRemove() error
|
|
|
|
}
|
|
|
|
|
2016-04-07 01:05:30 +02:00
|
|
|
// File provides for safe file writes.
|
2015-07-07 21:29:08 +02:00
|
|
|
type File struct {
|
|
|
|
*os.File
|
|
|
|
file string
|
|
|
|
}
|
|
|
|
|
2016-04-07 01:05:30 +02:00
|
|
|
// SyncClose sync file to disk and close, returns an error if any
|
|
|
|
func (f *File) SyncClose() error {
|
2015-11-18 01:32:20 +01:00
|
|
|
// sync to the disk
|
2015-11-18 08:03:55 +01:00
|
|
|
if err := f.File.Sync(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-28 06:52:38 +02:00
|
|
|
// Close the fd.
|
2015-11-18 08:03:55 +01:00
|
|
|
if err := f.Close(); err != nil {
|
2015-11-18 01:32:20 +01:00
|
|
|
return err
|
|
|
|
}
|
2015-11-18 08:03:55 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the file, returns an error if any
|
|
|
|
func (f *File) Close() error {
|
2016-03-28 06:52:38 +02:00
|
|
|
// Close the embedded fd.
|
2015-07-07 21:29:08 +02:00
|
|
|
if err := f.File.Close(); err != nil {
|
2015-08-04 01:17:21 +02:00
|
|
|
return err
|
2015-07-07 21:29:08 +02:00
|
|
|
}
|
2016-03-28 06:52:38 +02:00
|
|
|
// Safe rename to final destination
|
2015-07-07 21:29:08 +02:00
|
|
|
if err := os.Rename(f.Name(), f.file); err != nil {
|
2015-08-04 01:17:21 +02:00
|
|
|
return err
|
2015-07-07 21:29:08 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-07 01:05:30 +02:00
|
|
|
// CloseAndRemove closes the temp file, and safely removes it. Returns
|
|
|
|
// error if any.
|
|
|
|
func (f *File) CloseAndRemove() error {
|
2015-07-07 21:29:08 +02:00
|
|
|
// close the embedded fd
|
2016-05-06 05:28:22 +02:00
|
|
|
f.File.Close()
|
|
|
|
|
2016-03-28 06:52:38 +02:00
|
|
|
// Remove the temp file.
|
2015-07-07 21:29:08 +02:00
|
|
|
if err := os.Remove(f.Name()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-07 01:05:30 +02:00
|
|
|
// CreateFile creates a new file at filePath for safe writes, it also
|
|
|
|
// creates parent directories if they don't exist.
|
|
|
|
func CreateFile(filePath string) (*File, error) {
|
|
|
|
return CreateFileWithPrefix(filePath, "$deleteme.")
|
2015-11-18 01:32:20 +01:00
|
|
|
}
|
|
|
|
|
2016-04-07 01:05:30 +02:00
|
|
|
// CreateFileWithSuffix is similar to CreateFileWithPrefix, but the
|
|
|
|
// second argument is treated as suffix for the temporary files.
|
|
|
|
func CreateFileWithSuffix(filePath string, suffix 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), filepath.Base(filePath)+suffix)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateFileWithPrefix creates a new file at filePath for safe
|
|
|
|
// 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 CreateFileWithPrefix(filePath string, prefix string) (*File, error) {
|
|
|
|
// If parent directories do not exist, ioutil.TempFile doesn't create them
|
2015-07-07 21:35:50 +02:00
|
|
|
// handle such a case with os.MkdirAll()
|
|
|
|
if err := os.MkdirAll(filepath.Dir(filePath), 0700); err != nil {
|
2015-08-04 01:17:21 +02:00
|
|
|
return nil, err
|
2015-07-07 21:35:50 +02:00
|
|
|
}
|
2015-11-18 01:32:20 +01:00
|
|
|
f, err := ioutil.TempFile(filepath.Dir(filePath), prefix+filepath.Base(filePath))
|
2015-07-07 21:29:08 +02:00
|
|
|
if err != nil {
|
2015-08-04 01:17:21 +02:00
|
|
|
return nil, err
|
2015-07-07 21:29:08 +02:00
|
|
|
}
|
2016-02-19 02:16:41 +01:00
|
|
|
if err = os.Chmod(f.Name(), 0600); err != nil {
|
|
|
|
if err = os.Remove(f.Name()); err != nil {
|
2015-08-04 01:17:21 +02:00
|
|
|
return nil, err
|
2015-07-07 21:29:08 +02:00
|
|
|
}
|
2015-08-04 01:17:21 +02:00
|
|
|
return nil, err
|
2015-07-07 21:29:08 +02:00
|
|
|
}
|
|
|
|
return &File{File: f, file: filePath}, nil
|
|
|
|
}
|