Skip to content

Commit f396ff0

Browse files
committed
Simplify code
1 parent 1eafa31 commit f396ff0

1 file changed

Lines changed: 8 additions & 16 deletions

File tree

nxc/protocols/nfs.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,12 @@ def enum_shares(self):
250250
def get_file(self):
251251
"""Downloads a file from the NFS share"""
252252
remote_file_path = self.args.get_file[0]
253+
remote_dir_path, file_name = os.path.split(remote_file_path)
253254
local_file_path = self.args.get_file[1]
254-
windows = False
255255

256256
# Do a bit of smart handling for the local file path
257257
if local_file_path.endswith("/"):
258-
local_file_path += remote_file_path.split("/")[-1]
258+
local_file_path += file_name
259259

260260
self.logger.display(f"Downloading {remote_file_path} to {local_file_path}")
261261
try:
@@ -265,25 +265,17 @@ def get_file(self):
265265
self.nfs3.connect()
266266

267267
# Mount the NFS share
268-
mnt_info = self.mount.mnt(remote_file_path, self.auth)
269-
if mnt_info["mountinfo"] is None:
270-
windows = True
271-
remote_file_path2, remote_file_path = os.path.split(remote_file_path.rstrip("/")) # For windows. Windows wants to share name, not whole file path.
272-
mnt_info = self.mount.mnt(remote_file_path2, self.auth)
268+
mnt_info = self.mount.mnt(remote_dir_path, self.auth)
273269

274270
# Update the UID for the file
275271
attrs = self.nfs3.getattr(mnt_info["mountinfo"]["fhandle"], auth=self.auth)
276272
self.auth["uid"] = attrs["attributes"]["uid"]
277273
dir_handle = mnt_info["mountinfo"]["fhandle"]
278274

279-
# Read the file data
280-
if windows:
281-
dir_data = self.nfs3.lookup(dir_handle, remote_file_path, auth=self.auth)
282-
file_handle = dir_data["resok"]["object"]["data"]
283-
file_data = self.nfs3.read(file_handle, auth=self.auth)
284-
else:
285-
file_handle = mnt_info["mountinfo"]["fhandle"]
286-
file_data = self.nfs3.read(file_handle, auth=self.auth)
275+
# Get the file handle and read the file data
276+
dir_data = self.nfs3.lookup(dir_handle, file_name, auth=self.auth)
277+
file_handle = dir_data["resok"]["object"]["data"]
278+
file_data = self.nfs3.read(file_handle, auth=self.auth)
287279

288280
if "resfail" in file_data:
289281
raise Exception("Insufficient Permissions")
@@ -299,7 +291,7 @@ def get_file(self):
299291
# Unmount the share
300292
self.mount.umnt(self.auth)
301293
except Exception as e:
302-
self.logger.fail(f'Error writing file "{remote_file_path}" from share "{local_file_path}": {e}')
294+
self.logger.fail(f'Error retrieving file "{file_name}" from "{remote_dir_path}": {e}')
303295
if os.path.exists(local_file_path) and os.path.getsize(local_file_path) == 0:
304296
os.remove(local_file_path)
305297

0 commit comments

Comments
 (0)