sftp: Fix remote race on creating directories

Because there is a period of time between checking a directory needs
creating and creating it, the leads to errors where directories are
attempting to be created twice.

Add locking on a per directory basis to fix while doing mkdir.
This commit is contained in:
Nick Craig-Wood
2017-02-04 10:29:46 +00:00
parent 726cb43be9
commit 16d91246c4
3 changed files with 95 additions and 0 deletions
+4
View File
@@ -60,6 +60,7 @@ type Fs struct {
url string
sshClient *ssh.Client
sftpClient *sftp.Client
mkdirLock *stringLock
}
// Object is a remote SFTP file that has been stat'd (so it exists, but is not necessarily open for reading)
@@ -138,6 +139,7 @@ func NewFs(name, root string) (fs.Fs, error) {
sshClient: sshClient,
sftpClient: sftpClient,
url: "sftp://" + user + "@" + host + ":" + port + "/" + root,
mkdirLock: newStringLock(),
}
f.features = (&fs.Features{}).Fill(f)
if root != "" {
@@ -321,6 +323,8 @@ func (f *Fs) mkParentDir(remote string) error {
// mkdir makes the directory and parents using native paths
func (f *Fs) mkdir(path string) error {
f.mkdirLock.Lock(path)
defer f.mkdirLock.Unlock(path)
if path == "." || path == "/" {
return nil
}