Locking around osutil.Rename and some descriptive text
This commit is contained in:
@@ -9,9 +9,22 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Try to keep this entire operation atomic-like. We shouldn't be doing this
|
||||
// often enough that there is any contention on this lock.
|
||||
var renameLock sync.Mutex
|
||||
|
||||
// Rename renames a file, while trying hard to succeed on various systems by
|
||||
// temporarily tweaking directory permissions and removing the destination
|
||||
// file when necessary. Will make sure to delete the from file if the
|
||||
// operation fails, so use only for situations like committing a temp file to
|
||||
// it's final location.
|
||||
func Rename(from, to string) error {
|
||||
renameLock.Lock()
|
||||
defer renameLock.Unlock()
|
||||
|
||||
// Make sure the destination directory is writeable
|
||||
toDir := filepath.Dir(to)
|
||||
if info, err := os.Stat(toDir); err == nil {
|
||||
|
||||
Reference in New Issue
Block a user