Skip to content

Commit

Permalink
sftp: move -> use atomic copying
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr. Outis committed Nov 1, 2019
1 parent 8f70000 commit 16164e6
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions dvc/remote/ssh/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,24 +185,28 @@ def download(self, src, dest, no_progress_bar=False, progress_title=None):
self.sftp.get(src, dest, callback=pbar.update_to)

def move(self, src, dst):
"""Atomically move src to dst.
Moving is performed in two stages to make the whole operation atomic in
case src and dst are on different filesystems and actual physical
copying of data is happening.
"""Rename src to dst, if it is not possible (in case src and dst are
on different filesystems) and actual physical copying of data is
happening.
"""
self.makedirs(posixpath.dirname(dst))

tmp = tmp_fname(dst)

try:
self.sftp.rename(src, tmp)
self.sftp.rename(src, dst)
except OSError:
self.copy(src, tmp)
self._atomic_copy(src, dst)

self.sftp.rename(tmp, dst)
self.remove(src)

def _atomic_copy(self, src, dst):
tmp = tmp_fname(dst)

try:
self.copy(src, tmp)
self.sftp.rename(tmp, dst)
finally:
self.remove(tmp)

def upload(self, src, dest, no_progress_bar=False, progress_title=None):
self.makedirs(posixpath.dirname(dest))
tmp_file = tmp_fname(dest)
Expand Down

0 comments on commit 16164e6

Please sign in to comment.