Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[enhancement] Implement -size formats in module find, fixes #83 #88

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading