From d0be49cdbf454f55fecf1f4532e2eac90403705d Mon Sep 17 00:00:00 2001 From: "Remi GASCOU (Podalirius)" <79218792+p0dalirius@users.noreply.github.com> Date: Fri, 21 Jun 2024 17:47:56 +0200 Subject: [PATCH] Putting folder in remote subdirectory results in error and local files deletion, Fixed #48 --- smbclientng/core/SMBSession.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/smbclientng/core/SMBSession.py b/smbclientng/core/SMBSession.py index 9928d61..5a03421 100644 --- a/smbclientng/core/SMBSession.py +++ b/smbclientng/core/SMBSession.py @@ -866,12 +866,15 @@ def put_file(self, localpath=None): callback=f.read ) f.close() + except (BrokenPipeError, KeyboardInterrupt) as err: print("[!] Interrupted.") self.close_smb_session() self.init_smb_session() - except Exception as err: - print("[!] Failed to upload '%s': %s" % (localfile, err)) + + except (Exception, PermissionError) as err: + f.set_error(message="[bold red]Failed uploading '%s': %s" % (f.path, err)) + f.close(remove=False) if self.config.debug: traceback.print_exc() else: @@ -926,13 +929,16 @@ def put_file_recursively(self, localpath=None): ) f.close() - except (BrokenPipeError, PermissionError) as err: + except (BrokenPipeError, KeyboardInterrupt) as err: + print("[!] Interrupted.") + self.close_smb_session() + self.init_smb_session() + + except (Exception, PermissionError) as err: f.set_error(message="[bold red]Failed uploading '%s': %s" % (f.path, err)) - f.close(remove=True) - break - except Exception as err: - f.set_error(message="[bold red]Failed uploading '%s': %s" % (f.path, err)) - f.close(remove=True) + f.close(remove=False) + if self.config.debug: + traceback.print_exc() else: print("[!] The specified localpath is a file. Use 'put ' instead.") else: