Skip to content

Commit

Permalink
Implement -size formats in module find, fixes #83
Browse files Browse the repository at this point in the history
  • Loading branch information
p0dalirius committed Jul 8, 2024
1 parent 0b83714 commit 9c6d063
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions smbclientng/modules/Find.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,13 @@ def parseArgs(self, arguments):
def __find_callback(self, entry, fullpath, depth):
# Documentation for __find_callback function
"""
This function serves as a callback for the find operation. It applies filters based on the command line arguments
and decides whether to print, download, or list the entry in 'ls -dils' format if it matches the specified filters.
This function serves as a callback for the find operation. It applies filters based on the command line arguments and decides whether to print, download, or list the entry in 'ls -dils' format if it matches the specified filters.
Args:
entry (SMBEntry): The current file or directory entry being processed.
fullpath (str): The full path to the entry.
The function checks against filters such as file name, case sensitivity, file type, and size. If the entry matches
the filters, it will perform actions like printing the entry's details, downloading the entry, or listing the entry
based on the options provided in the command line arguments.
The function checks against filters such as file name, case sensitivity, file type, and size. If the entry matches the filters, it will perform actions like printing the entry's details, downloading the entry, or listing the entry based on the options provided in the command line arguments.
"""

# Match and print results
Expand Down Expand Up @@ -166,6 +163,24 @@ def __find_callback(self, entry, fullpath, depth):
else:
do_print_entry = (entry.get_longname().lower() == self.options.iname.lower())

# Check the size
if do_print_entry and self.options.size is not None:
size_filter = self.options.size
if (size_filter[1:].isdigit()):
size = int(size_filter[1:])
else:
size = int(size_filter[1:-1])
units = ["B","K","M","G","T"]
if size_filter[-1].upper() in units:
size = size * (1024**units.index(size_filter[-1]))
else:
pass

if size_filter[0] == '+':
do_print_entry = entry.get_filesize() >= size
elif size_filter[0] == '-':
do_print_entry = entry.get_filesize() <= size

if do_print_entry:
# Actions on matches
if self.options.download:
Expand All @@ -176,9 +191,9 @@ def __find_callback(self, entry, fullpath, depth):
# Output formats
if self.options.ls:
if entry.is_directory():
windows_ls_entry(entry, fullpath)
windows_ls_entry(entry=entry, config=self.config, pathToPrint=fullpath)
else:
windows_ls_entry(entry, fullpath)
windows_ls_entry(entry=entry, config=self.config, pathToPrint=fullpath)
else:
if entry.is_directory():
print("%s" % fullpath.replace(ntpath.sep, '/'))
Expand Down

0 comments on commit 9c6d063

Please sign in to comment.