2018-09-26 17:39:21 +00:00
|
|
|
// +build !windows
|
|
|
|
|
2019-02-12 11:24:11 +00:00
|
|
|
package fs
|
2018-09-26 17:39:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2019-02-12 11:39:13 +00:00
|
|
|
// SyncDir flushes any file renames to the filesystem.
|
2018-09-26 17:39:21 +00:00
|
|
|
func SyncDir(dirName string) error {
|
|
|
|
// fsync the dir to flush the rename
|
|
|
|
dir, err := os.OpenFile(dirName, os.O_RDONLY, os.ModeDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dir.Close()
|
|
|
|
|
|
|
|
// While we're on unix, we may be running in a Docker container that is
|
|
|
|
// pointed at a Windows volume over samba. That doesn't support fsyncs
|
|
|
|
// on directories. This shows itself as an EINVAL, so we ignore that
|
|
|
|
// error.
|
|
|
|
err = dir.Sync()
|
|
|
|
if pe, ok := err.(*os.PathError); ok && pe.Err == syscall.EINVAL {
|
|
|
|
err = nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir.Close()
|
|
|
|
}
|
|
|
|
|
2019-02-12 11:39:13 +00:00
|
|
|
// RenameFileWithReplacement will replace any existing file at newpath with the contents
|
|
|
|
// of oldpath.
|
|
|
|
//
|
|
|
|
// If no file already exists at newpath, newpath will be created using the contents
|
|
|
|
// of oldpath. If this function returns successfully, the contents of newpath will
|
|
|
|
// be identical to oldpath, and oldpath will be removed.
|
|
|
|
func RenameFileWithReplacement(oldpath, newpath string) error {
|
2018-09-26 17:39:21 +00:00
|
|
|
return os.Rename(oldpath, newpath)
|
|
|
|
}
|
2019-02-12 12:02:06 +00:00
|
|
|
|
|
|
|
// RenameFile renames oldpath to newpath, returning an error if newpath already
|
|
|
|
// exists. If this function returns successfully, the contents of newpath will
|
|
|
|
// be identical to oldpath, and oldpath will be removed.
|
|
|
|
func RenameFile(oldpath, newpath string) error {
|
|
|
|
if _, err := os.Stat(newpath); err == nil {
|
|
|
|
return newFileExistsError(newpath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.Rename(oldpath, newpath)
|
|
|
|
}
|
2019-07-08 17:01:42 +00:00
|
|
|
|
|
|
|
// CreateFileWithReplacement will create a new file at any path, removing the
|
|
|
|
// contents of the old file
|
|
|
|
func CreateFileWithReplacement(newpath string) (*os.File, error) {
|
|
|
|
return os.Create(newpath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateFile creates a new file at newpath, returning an error if newpath already
|
|
|
|
// exists
|
|
|
|
func CreateFile(newpath string) (*os.File, error) {
|
|
|
|
if _, err := os.Stat(newpath); err == nil {
|
|
|
|
return nil, newFileExistsError(newpath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.Create(newpath)
|
|
|
|
}
|