Skip to content

Commit

Permalink
find: Add the -empty option
Browse files Browse the repository at this point in the history
This predicate returns true for empty regular files or directories.
  • Loading branch information
tcl3 authored and gmta committed Sep 1, 2023
1 parent db2701f commit 889e6ab
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Base/usr/share/man/man1/find.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions Userland/Utilities/find.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -274,6 +275,31 @@ class SizeCommand final : public StatCommand {
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)
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 889e6ab

Please sign in to comment.