11from nxc .connection import connection
22from nxc .logger import NXCAdapter
3+ from nxc .helpers .logger import highlight
34from pyNfsClient import Portmap , Mount , NFSv3 , NFS_PROGRAM , NFS_V3 , ACCESS3_READ , ACCESS3_MODIFY , ACCESS3_EXECUTE , NFSSTAT3
45import re
56import uuid
@@ -301,6 +302,11 @@ def put_file(self):
301302 remote_file_path = self .args .put_file [1 ]
302303 file_name = ""
303304
305+ # Check if local file is exist
306+ if not os .path .isfile (local_file_path ):
307+ self .logger .fail (f"{ local_file_path } does not exist." )
308+ return
309+
304310 # Do a bit of smart handling for the file paths
305311 file_name = local_file_path .split ("/" )[- 1 ] if "/" in local_file_path else local_file_path
306312 if not remote_file_path .endswith ("/" ):
@@ -313,14 +319,18 @@ def put_file(self):
313319 self .nfs3 = NFSv3 (self .host , nfs_port , self .args .nfs_timeout , self .auth )
314320 self .nfs3 .connect ()
315321
316- try :
317- # Mount the NFS share to create the file
318- mnt_info = self .mount .mnt (remote_file_path , self .auth )
319- dir_handle = mnt_info ["mountinfo" ]["fhandle" ]
320- # Update the UID from the directory
321- attrs = self .nfs3 .getattr (dir_handle , auth = self .auth )
322- self .auth ["uid" ] = attrs ["attributes" ]["uid" ]
322+ # Mount the NFS share to create the file
323+ mnt_info = self .mount .mnt (remote_file_path , self .auth )
324+ dir_handle = mnt_info ["mountinfo" ]["fhandle" ]
325+ # Update the UID from the directory
326+ attrs = self .nfs3 .getattr (dir_handle , auth = self .auth )
327+ self .auth ["uid" ] = attrs ["attributes" ]["uid" ]
328+
329+ # Checking if file_name is already exist on remote file path
330+ lookup_response = self .nfs3 .lookup (dir_handle , file_name , auth = self .auth )
323331
332+ # If success, file_name does not exist on remote machine. Else, trying to overwrite it.
333+ if lookup_response ["resok" ] is None :
324334 # Create file
325335 self .logger .display (f"Trying to create { remote_file_path } { file_name } " )
326336 res = self .nfs3 .create (dir_handle , file_name , create_mode = 1 , mode = 0o777 , auth = self .auth )
@@ -329,9 +339,16 @@ def put_file(self):
329339 else :
330340 file_handle = res ["resok" ]["obj" ]["handle" ]["data" ]
331341 self .logger .success (f"{ file_name } successfully created" )
332- except Exception as e :
333- self .logger .fail (f"{ file_name } was not created: { e } " )
334- return
342+ else :
343+ # Asking to user overwriting.
344+ ans = input (highlight (f"[!] { file_name } is already exist on { remote_file_path } . Do you want to overwrite it? [Y/n] " , "red" ))
345+ if ans .lower () in ["y" , "yes" , "" ]:
346+ # Overwriting
347+ self .logger .display (f"{ file_name } is already exist on { remote_file_path } . Trying to overwrite it..." )
348+ file_handle = lookup_response ["resok" ]["object" ]["data" ]
349+ else :
350+ self .logger .fail (f"Uploading was not successful. The { file_name } is exist on { remote_file_path } " )
351+ return
335352
336353 try :
337354 with open (local_file_path , "rb" ) as file :
0 commit comments