|
|
@ -16,115 +16,109 @@ |
|
|
|
|
|
|
|
|
|
|
|
// NOTE - Rename() not guaranteed to be safe on all filesystems which are not fully POSIX compatible
|
|
|
|
// NOTE - Rename() not guaranteed to be safe on all filesystems which are not fully POSIX compatible
|
|
|
|
|
|
|
|
|
|
|
|
// Package safe provides safe file write semantics by leveraging Rename's() safeity.
|
|
|
|
|
|
|
|
package safe |
|
|
|
package safe |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"io" |
|
|
|
"errors" |
|
|
|
"io/ioutil" |
|
|
|
"io/ioutil" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
"path/filepath" |
|
|
|
"path/filepath" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// Vault - vault is an interface for different implementations of safe
|
|
|
|
// File represents safe file descriptor.
|
|
|
|
// i/o semantics.
|
|
|
|
|
|
|
|
type Vault interface { |
|
|
|
|
|
|
|
io.ReadWriteCloser |
|
|
|
|
|
|
|
SyncClose() error |
|
|
|
|
|
|
|
CloseAndRemove() error |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// File provides for safe file writes.
|
|
|
|
|
|
|
|
type File struct { |
|
|
|
type File struct { |
|
|
|
*os.File |
|
|
|
name string |
|
|
|
file string |
|
|
|
tmpfile *os.File |
|
|
|
|
|
|
|
closed bool |
|
|
|
|
|
|
|
aborted bool |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// SyncClose sync file to disk and close, returns an error if any
|
|
|
|
// Write writes len(b) bytes to the temporary File. In case of error, the temporary file is removed.
|
|
|
|
func (f *File) SyncClose() error { |
|
|
|
func (file *File) Write(b []byte) (n int, err error) { |
|
|
|
// sync to the disk
|
|
|
|
if file.aborted { |
|
|
|
if err := f.File.Sync(); err != nil { |
|
|
|
err = errors.New("write on aborted file") |
|
|
|
return err |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
// Close the fd.
|
|
|
|
if file.closed { |
|
|
|
if err := f.Close(); err != nil { |
|
|
|
err = errors.New("write on closed file") |
|
|
|
return err |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Close the file, returns an error if any
|
|
|
|
defer func() { |
|
|
|
func (f *File) Close() error { |
|
|
|
if err != nil { |
|
|
|
// Close the embedded fd.
|
|
|
|
os.Remove(file.tmpfile.Name()) |
|
|
|
if err := f.File.Close(); err != nil { |
|
|
|
file.aborted = true |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
}() |
|
|
|
// Safe rename to final destination
|
|
|
|
|
|
|
|
if err := os.Rename(f.Name(), f.file); err != nil { |
|
|
|
n, err = file.tmpfile.Write(b) |
|
|
|
return err |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// CloseAndRemove closes the temp file, and safely removes it. Returns
|
|
|
|
// Close closes the temporary File and renames to the named file. In case of error, the temporary file is removed.
|
|
|
|
// error if any.
|
|
|
|
func (file *File) Close() (err error) { |
|
|
|
func (f *File) CloseAndRemove() error { |
|
|
|
defer func() { |
|
|
|
// close the embedded fd
|
|
|
|
if err != nil { |
|
|
|
f.File.Close() |
|
|
|
os.Remove(file.tmpfile.Name()) |
|
|
|
|
|
|
|
file.aborted = true |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
|
|
// Remove the temp file.
|
|
|
|
if file.aborted || file.closed { |
|
|
|
if err := os.Remove(f.Name()); err != nil { |
|
|
|
return |
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// CreateFile creates a new file at filePath for safe writes, it also
|
|
|
|
if err = file.tmpfile.Close(); err != nil { |
|
|
|
// creates parent directories if they don't exist.
|
|
|
|
return |
|
|
|
func CreateFile(filePath string) (*File, error) { |
|
|
|
} |
|
|
|
return CreateFileWithPrefix(filePath, "$deleteme.") |
|
|
|
|
|
|
|
|
|
|
|
err = os.Rename(file.tmpfile.Name(), file.name) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file.closed = true |
|
|
|
|
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// CreateFileWithSuffix is similar to CreateFileWithPrefix, but the
|
|
|
|
// Abort aborts the temporary File by closing and removing the temporary file.
|
|
|
|
// second argument is treated as suffix for the temporary files.
|
|
|
|
func (file *File) Abort() (err error) { |
|
|
|
func CreateFileWithSuffix(filePath string, suffix string) (*File, error) { |
|
|
|
if file.aborted || file.closed { |
|
|
|
// If parent directories do not exist, ioutil.TempFile doesn't create them
|
|
|
|
return |
|
|
|
// 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 { |
|
|
|
file.tmpfile.Close() |
|
|
|
return nil, err |
|
|
|
err = os.Remove(file.tmpfile.Name()) |
|
|
|
} |
|
|
|
file.aborted = true |
|
|
|
return nil, err |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
return &File{File: f, file: filePath}, nil |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// CreateFileWithPrefix creates a new file at filePath for safe
|
|
|
|
// CreateFile creates the named file safely from unique temporary file.
|
|
|
|
// writes, it also creates parent directories if they don't exist.
|
|
|
|
// The temporary file is renamed to the named file upon successful close
|
|
|
|
// prefix specifies the prefix of the temporary files so that cleaning
|
|
|
|
// to safeguard intermediate state in the named file. The temporary file
|
|
|
|
// stale temp files is easy.
|
|
|
|
// is created in the name of the named file with suffixed unique number
|
|
|
|
func CreateFileWithPrefix(filePath string, prefix string) (*File, error) { |
|
|
|
// and prefixed "$tmpfile" string. While creating the temporary file,
|
|
|
|
// If parent directories do not exist, ioutil.TempFile doesn't create them
|
|
|
|
// missing parent directories are also created. The temporary file is
|
|
|
|
// handle such a case with os.MkdirAll()
|
|
|
|
// removed if case of any intermediate failure. Not removed temporary
|
|
|
|
if err := os.MkdirAll(filepath.Dir(filePath), 0700); err != nil { |
|
|
|
// 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 { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
f, err := ioutil.TempFile(filepath.Dir(filePath), prefix+filepath.Base(filePath)) |
|
|
|
|
|
|
|
|
|
|
|
fname := filepath.Base(name) |
|
|
|
|
|
|
|
tmpfile, err := ioutil.TempFile(dname, "$tmpfile."+fname+".") |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
if err = os.Chmod(f.Name(), 0600); err != nil { |
|
|
|
|
|
|
|
if err = os.Remove(f.Name()); err != nil { |
|
|
|
if err = os.Chmod(tmpfile.Name(), 0600); err != nil { |
|
|
|
return nil, err |
|
|
|
if rerr := os.Remove(tmpfile.Name()); rerr != nil { |
|
|
|
|
|
|
|
err = rerr |
|
|
|
} |
|
|
|
} |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
return &File{File: f, file: filePath}, nil |
|
|
|
|
|
|
|
|
|
|
|
return &File{name: name, tmpfile: tmpfile}, nil |
|
|
|
} |
|
|
|
} |
|
|
|