Skip to content

Commit

Permalink
update get-file method to get large files
Browse files Browse the repository at this point in the history
  • Loading branch information
ledrypotato committed Oct 9, 2024
1 parent c1780a1 commit 7a86d0f
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions nxc/protocols/nfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,19 +273,38 @@ def get_file(self):
self.auth["uid"] = attrs["attributes"]["uid"]
dir_handle = mnt_info["mountinfo"]["fhandle"]

# Get the file handle and read the file data
# Get the file handle
dir_data = self.nfs3.lookup(dir_handle, file_name, auth=self.auth)
file_handle = dir_data["resok"]["object"]["data"]
file_data = self.nfs3.read(file_handle, auth=self.auth)

if "resfail" in file_data:
raise Exception("Insufficient Permissions")
else:
data = file_data["resok"]["data"]
# Get the file attributes (size)
file_attrs = self.nfs3.getattr(file_handle, auth=self.auth)
file_size = file_attrs["attributes"]["size"]

# Handle files over 32768 bytes
offset = 0
chunk_size = 32768 # 32KB
eof = False
file_data_total = b''

# Loop until we have read the entire file
while offset < file_size:
file_data = self.nfs3.read(file_handle, offset, chunk_size, auth=self.auth)

# Write the data to the local file
if "resfail" in file_data:
raise Exception("Insufficient Permissions")

else:
# Get the data and append it to the total file data
data = file_data["resok"]["data"]
file_data_total += data

# Update the offset to read the next chunk
offset += len(data)

# Write the complete file data to the local file
with open(local_file_path, "wb+") as local_file:
local_file.write(data)
local_file.write(file_data_total)

self.logger.highlight(f"File successfully downloaded to {local_file_path} from {remote_file_path}")

Expand Down

0 comments on commit 7a86d0f

Please sign in to comment.