Skip to content

Commit

Permalink
Fixed -download option of find module
Browse files Browse the repository at this point in the history
  • Loading branch information
p0dalirius committed May 31, 2024
1 parent 463ddcc commit 7034d18
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
16 changes: 9 additions & 7 deletions smbclientng/core/LocalFileIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@ class LocalFileIO(object):
read(self, size): Reads data from the file up to the specified size and updates the progress bar if expected size is provided.
"""

def __init__(self, mode, path=None, expected_size=None, debug=False):
def __init__(self, mode, path=None, expected_size=None, keepRemotePath=False, debug=False):
super(LocalFileIO, self).__init__()

self.mode = mode
self.path = path.replace(ntpath.sep, '/')
# Convert remote path format to local operating system path format
self.path = path.replace(ntpath.sep, os.path.sep)
self.dir = None
self.debug = False
self.expected_size = expected_size
self.keepRemotePath = keepRemotePath

# Write to local (read remote)
if self.mode in ["wb"]:
self.dir = './' + os.path.dirname(self.path)
self.dir = '.' + os.path.sep
if keepRemotePath:
self.dir += os.path.dirname(self.path)

if not os.path.exists(self.dir):
if self.debug:
Expand All @@ -47,8 +50,8 @@ def __init__(self, mode, path=None, expected_size=None, debug=False):

if self.debug:
print("[debug] Openning local '%s' with mode '%s'" % (self.path, self.mode))

self.fd = open(self.path, self.mode)
self.fd = open(self.dir + os.path.sep + os.path.basename(self.path), self.mode)

# Write to remote (read local)
elif self.mode in ["rb"]:
Expand All @@ -57,7 +60,6 @@ def __init__(self, mode, path=None, expected_size=None, debug=False):

if self.debug:
print("[debug] Openning local '%s' with mode '%s'" % (self.path, self.mode))

self.fd = open(self.path, self.mode)

if self.expected_size is None:
Expand Down
12 changes: 9 additions & 3 deletions smbclientng/core/SMBSession.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def close_smb_session(self):

# Operations

def get_file(self, path=None):
def get_file(self, path=None, keepRemotePath=False):
"""
Retrieves a file from the specified path on the SMB share.
Expand All @@ -170,16 +170,22 @@ def get_file(self, path=None):
shareName=self.smb_share,
path=tmp_file_path
)

for entry in matches:
if entry.is_directory():
print("[>] Skipping '%s' because it is a directory." % tmp_file_path)
else:
try:
if ntpath.sep in path:
outputfile = ntpath.dirname(path) + ntpath.sep + entry.get_longname()
else:
outputfile = entry.get_longname()
f = LocalFileIO(
mode="wb",
path=entry.get_longname(),
path=outputfile,
expected_size=entry.get_filesize(),
debug=self.config.debug
debug=self.config.debug,
keepRemotePath=keepRemotePath
)
self.smbClient.getFile(
shareName=self.smb_share,
Expand Down
2 changes: 1 addition & 1 deletion smbclientng/modules/Find.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def __recurse_action(self, paths=[], depth=0):
if entry.is_directory():
self.smbSession.get_file_recursively(path=(path + entry.get_longname() + ntpath.sep))
else:
self.smbSession.get_file(path=(path + entry.get_longname() + ntpath.sep))
self.smbSession.get_file(path=(path + entry.get_longname() + ntpath.sep), keepRemotePath=True)
# Output formats
if self.options.ls:
if entry.is_directory():
Expand Down

0 comments on commit 7034d18

Please sign in to comment.