Skip to content

Commit b5f9e13

Browse files
committed
Add error handling for upload, fix permissions for upload and fix upload for windows
1 parent f396ff0 commit b5f9e13

2 files changed

Lines changed: 11 additions & 15 deletions

File tree

nxc/protocols/nfs.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from nxc.connection import connection
22
from nxc.logger import NXCAdapter
3-
from pyNfsClient import Portmap, Mount, NFSv3, NFS_PROGRAM, NFS_V3, ACCESS3_READ, ACCESS3_MODIFY, ACCESS3_EXECUTE
3+
from pyNfsClient import Portmap, Mount, NFSv3, NFS_PROGRAM, NFS_V3, ACCESS3_READ, ACCESS3_MODIFY, ACCESS3_EXECUTE, NFSSTAT3
44
import re
55
import uuid
66
import math
@@ -323,19 +323,17 @@ def put_file(self):
323323

324324
# Create file
325325
self.logger.display(f"Trying to create {remote_file_path}{file_name}")
326-
self.nfs3.create(dir_handle, file_name, 1, auth=self.auth)
327-
self.logger.success(f"{file_name} successfully created.")
326+
res = self.nfs3.create(dir_handle, file_name, create_mode=1, mode=0o777, auth=self.auth)
327+
if res["status"] != 0:
328+
raise Exception(NFSSTAT3[res["status"]])
329+
else:
330+
file_handle = res["resok"]["obj"]["handle"]["data"]
331+
self.logger.success(f"{file_name} successfully created")
328332
except Exception as e:
329-
self.logger.fail(f"{file_name} was not created.")
330-
self.logger.debug(f"Error while creating remote file: {e}")
331-
exit(-1)
333+
self.logger.fail(f"{file_name} was not created: {e}")
334+
return
332335

333336
try:
334-
# Mount the NFS share to write the file
335-
mnt_info = self.mount.mnt(remote_file_path + "/" + file_name, self.auth)
336-
file_handle = mnt_info["mountinfo"]["fhandle"]
337-
attrs = self.nfs3.getattr(file_handle, auth=self.auth)
338-
self.auth["uid"] = attrs["attributes"]["uid"]
339337
with open(local_file_path, "rb") as file:
340338
file_data = file.read().decode()
341339

@@ -344,9 +342,7 @@ def put_file(self):
344342
self.nfs3.write(file_handle, 0, len(file_data), file_data, 1, auth=self.auth)
345343
self.logger.success(f"Data from {local_file_path} successfully written to {remote_file_path}")
346344
except Exception as e:
347-
self.logger.fail(f"{local_file_path} was not writed.")
348-
self.logger.debug(f"Error while creating remote file: {e}")
349-
exit(-1)
345+
self.logger.fail(f"Could not write to {local_file_path}: {e}")
350346

351347
# Unmount the share
352348
self.mount.umnt(self.auth)

nxc/protocols/nfs/proto_args.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ def proto_args(parser, parents):
77
dgroup.add_argument("--shares", action="store_true", help="List NFS shares")
88
dgroup.add_argument("--enum-shares", nargs="?", type=int, const=3, help="Authenticate and enumerate exposed shares recursively (default depth: %(const)s)")
99
dgroup.add_argument("--get-file", nargs=2, metavar="FILE", help="Download remote NFS file.\n--get-file remote_file local_file")
10-
dgroup.add_argument("--put-file", nargs=2, metavar="FILE", help="Upload remote NFS file.\n--put-file local_file remote_file")
10+
dgroup.add_argument("--put-file", nargs=2, metavar="FILE", help="Upload remote NFS file with chmod 777 permissions.\n--put-file local_file remote_file")
1111

1212
return parser

0 commit comments

Comments
 (0)