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
|
2015-07-07 21:29:08 +02:00
|
|
|
|
|
|
|
import (
|
2016-05-27 06:43:33 +02:00
|
|
|
"errors"
|
2015-07-07 21:29:08 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2016-05-27 06:43:33 +02:00
|
|
|
// File represents safe file descriptor.
|
2015-07-07 21:29:08 +02:00
|
|
|
type File struct {
|
2016-05-27 06:43:33 +02:00
|
|
|
name string
|
|
|
|
tmpfile *os.File
|
|
|
|
closed bool
|
|
|
|
aborted bool
|
2015-07-07 21:29:08 +02:00
|
|
|
}
|
|
|
|
|
2016-05-27 06:43:33 +02:00
|
|
|
// Write writes len(b) bytes to the temporary File. In case of error, the temporary file is removed.
|
|
|
|
func (file *File) Write(b []byte) (n int, err error) {
|
|
|
|
if file.aborted {
|
|
|
|
err = errors.New("write on aborted file")
|
|
|
|
return
|
2015-11-18 08:03:55 +01:00
|
|
|
}
|
2016-05-27 06:43:33 +02:00
|
|
|
if file.closed {
|
|
|
|
err = errors.New("write on closed file")
|
|
|
|
return
|
2015-11-18 01:32:20 +01:00
|
|
|
}
|
2015-11-18 08:03:55 +01:00
|
|
|
|
2016-05-27 06:43:33 +02:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(file.tmpfile.Name())
|
|
|
|
file.aborted = true
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
n, err = file.tmpfile.Write(b)
|
|
|
|
return
|
2015-07-07 21:29:08 +02:00
|
|
|
}
|
|
|
|
|
2016-05-27 06:43:33 +02:00
|
|
|
// Close closes the temporary File and renames to the named file. In case of error, the temporary file is removed.
|
|
|
|
func (file *File) Close() (err error) {
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(file.tmpfile.Name())
|
|
|
|
file.aborted = true
|
|
|
|
}
|
|
|
|
}()
|
2016-05-06 05:28:22 +02:00
|
|
|
|
2016-05-27 06:43:33 +02:00
|
|
|
if file.aborted || file.closed {
|
|
|
|
return
|
2015-07-07 21:29:08 +02:00
|
|
|
}
|
|
|
|
|
2016-05-27 06:43:33 +02:00
|
|
|
if err = file.tmpfile.Close(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Rename(file.tmpfile.Name(), file.name)
|
|
|
|
|
|
|
|
file.closed = true
|
|
|
|
return
|
2015-11-18 01:32:20 +01:00
|
|
|
}
|
|
|
|
|
2016-05-27 06:43:33 +02:00
|
|
|
// Abort aborts the temporary File by closing and removing the temporary file.
|
|
|
|
func (file *File) Abort() (err error) {
|
|
|
|
if file.aborted || file.closed {
|
|
|
|
return
|
2016-04-07 01:05:30 +02:00
|
|
|
}
|
2016-05-27 06:43:33 +02:00
|
|
|
|
|
|
|
file.tmpfile.Close()
|
|
|
|
err = os.Remove(file.tmpfile.Name())
|
|
|
|
file.aborted = true
|
|
|
|
return
|
2016-04-07 01:05:30 +02:00
|
|
|
}
|
|
|
|
|
2016-05-27 06:43:33 +02:00
|
|
|
// CreateFile creates the named file safely from unique temporary file.
|
|
|
|
// The temporary file is renamed to the named file upon successful close
|
|
|
|
// to safeguard intermediate state in the named file. The temporary file
|
|
|
|
// is created in the name of the named file with suffixed unique number
|
|
|
|
// and prefixed "$tmpfile" string. While creating the temporary file,
|
|
|
|
// missing parent directories are also created. The temporary file is
|
|
|
|
// removed if case of any intermediate failure. Not removed temporary
|
|
|
|
// files can be cleaned up by identifying them using "$tmpfile" prefix
|
|
|
|
// string.
|
|
|
|
func CreateFile(name string) (*File, error) {
|
|
|
|
// ioutil.TempFile() fails if parent directory is missing.
|
|
|
|
// Create parent directory to avoid such error.
|
|
|
|
dname := filepath.Dir(name)
|
|
|
|
if err := os.MkdirAll(dname, 0700); err != nil {
|
2015-08-04 01:17:21 +02:00
|
|
|
return nil, err
|
2015-07-07 21:35:50 +02:00
|
|
|
}
|
2016-05-27 06:43:33 +02:00
|
|
|
|
|
|
|
fname := filepath.Base(name)
|
|
|
|
tmpfile, err := ioutil.TempFile(dname, "$tmpfile."+fname+".")
|
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-05-27 06:43:33 +02:00
|
|
|
|
|
|
|
if err = os.Chmod(tmpfile.Name(), 0600); err != nil {
|
|
|
|
if rerr := os.Remove(tmpfile.Name()); rerr != nil {
|
|
|
|
err = rerr
|
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
|
|
|
}
|
2016-05-27 06:43:33 +02:00
|
|
|
|
|
|
|
return &File{name: name, tmpfile: tmpfile}, nil
|
2015-07-07 21:29:08 +02:00
|
|
|
}
|