Skip to content

Commit

Permalink
[sftp] Check full path for existance in ._save() and ._mkdir()
Browse files Browse the repository at this point in the history
  • Loading branch information
pjpetersik committed Mar 22, 2024
1 parent e7f1911 commit 4ccc2c1
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions storages/backends/sftpstorage.py
Expand Up @@ -104,6 +104,13 @@ def _read(self, name):
remote_path = self._remote_path(name)
return self.sftp.open(remote_path, "rb")

def _exists(self, path):
try:
self.sftp.stat(path)
return True
except FileNotFoundError:
return False

def _chown(self, path, uid=None, gid=None):
"""Set uid and/or gid for file at path."""
# Paramiko's chown requires both uid and gid, so look them up first if
Expand All @@ -118,7 +125,7 @@ def _mkdir(self, path):
"""Create directory, recursing up to create parent dirs if
necessary."""
parent = posixpath.dirname(path)
if not self.exists(parent):
if not self._exists(parent):
self._mkdir(parent)
self.sftp.mkdir(path)

Expand All @@ -134,7 +141,7 @@ def _save(self, name, content):
content.seek(0, os.SEEK_SET)
path = self._remote_path(name)
dirname = posixpath.dirname(path)
if not self.exists(dirname):
if not self._exists(dirname):
self._mkdir(dirname)

self.sftp.putfo(content, path)
Expand All @@ -153,11 +160,7 @@ def delete(self, name):
pass

def exists(self, name):
try:
self.sftp.stat(self._remote_path(name))
return True
except FileNotFoundError:
return False
return self._exists(self._remote_path(name))

def _isdir_attr(self, item):
# Return whether an item in sftp.listdir_attr results is a directory
Expand Down

0 comments on commit 4ccc2c1

Please sign in to comment.