find: Add the -empty option

This predicate returns true for empty regular files or directories.
This commit is contained in:
Tim Ledbetter 2023-09-01 20:22:20 +01:00 committed by Jelle Raaijmakers
parent db2701f2e2
commit 889e6ab43d
2 changed files with 30 additions and 0 deletions

View file

@ -46,6 +46,8 @@ space rounded up to the nearest whole unit.
* `-name pattern`: Checks if the file name matches the given global-style
pattern (case sensitive).
* `-empty`: File is either an empty regular file or a directory containing no
files.
* `-iname pattern`: Checks if the file name matches the given global-style
pattern (case insensitive).
* `-readable`: Checks if the file is readable by the current user.

View file

@ -11,6 +11,7 @@
#include <AK/OwnPtr.h>
#include <AK/Time.h>
#include <AK/Vector.h>
#include <LibCore/DirIterator.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <LibRegex/Regex.h>
@ -274,6 +275,31 @@ private:
off_t m_unit_size { 512 };
};
class EmptyCommand final : public Command {
public:
EmptyCommand()
{
}
private:
virtual bool evaluate(FileData& file_data) const override
{
struct stat const* stat = file_data.ensure_stat();
if (!stat)
return false;
if (S_ISREG(stat->st_mode))
return stat->st_size == 0;
if (S_ISDIR(stat->st_mode)) {
auto dir_iterator = Core::DirIterator(file_data.full_path.string(), Core::DirIterator::SkipDots);
return !dir_iterator.has_next();
}
return false;
}
};
class NameCommand : public Command {
public:
NameCommand(char const* pattern, CaseSensitivity case_sensitivity)
@ -507,6 +533,8 @@ static OwnPtr<Command> parse_simple_command(Vector<char*>& args)
if (args.is_empty())
fatal_error("-size: requires additional arguments");
return make<SizeCommand>(args.take_first());
} else if (arg == "-empty") {
return make<EmptyCommand>();
} else if (arg == "-name") {
if (args.is_empty())
fatal_error("-name: requires additional arguments");