From 3b87f4ff017fc3b7680e0acdd10beacebe175d49 Mon Sep 17 00:00:00 2001 From: Fabio Erculiani Date: Mon, 4 Jan 2010 14:08:35 +0100 Subject: [PATCH] [entropy.transceivers] EntropySshUriHandler: download/upload functions are now atomic --- .../plugins/interfaces/ssh_plugin.py | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/libraries/entropy/transceivers/uri_handlers/plugins/interfaces/ssh_plugin.py b/libraries/entropy/transceivers/uri_handlers/plugins/interfaces/ssh_plugin.py index 78a551f2b..e546423e3 100644 --- a/libraries/entropy/transceivers/uri_handlers/plugins/interfaces/ssh_plugin.py +++ b/libraries/entropy/transceivers/uri_handlers/plugins/interfaces/ssh_plugin.py @@ -210,18 +210,39 @@ class EntropySshUriHandler(EntropyUriHandler): return args, remote_str def download(self, remote_path, save_path): + args = [EntropySshUriHandler._TXC_CMD] c_args, remote_str = self._setup_common_args(remote_path) + tmp_save_path = save_path + EntropyUriHandler.TMP_TXC_FILE_EXT args.extend(c_args) - args += ["-B", "-P", str(self.__port), remote_str, save_path] - return self._fork_cmd(args) == 0 + args += ["-B", "-P", str(self.__port), remote_str, tmp_save_path] + + down_sts = self._fork_cmd(args) == 0 + if not down_sts: + try: + os.remove(tmp_save_path) + except OSError: + return False + return False + + os.rename(tmp_save_path, save_path) + return True def upload(self, load_path, remote_path): + args = [EntropySshUriHandler._TXC_CMD] - c_args, remote_str = self._setup_common_args(remote_path) + tmp_remote_path = remote_path + EntropyUriHandler.TMP_TXC_FILE_EXT + c_args, remote_str = self._setup_common_args(tmp_remote_path) args.extend(c_args) args += ["-B", "-P", str(self.__port), load_path, remote_str] - return self._fork_cmd(args) == 0 + + upload_sts = self._fork_cmd(args) == 0 + if not upload_sts: + self.delete(tmp_remote_path) + return False + + # atomic rename + return self.rename(tmp_remote_path, remote_path) def _setup_fs_args(self): args = [EntropySshUriHandler._SSH_CMD]