mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:38:11 +00:00
find: Add the -empty
option
This predicate returns true for empty regular files or directories.
This commit is contained in:
parent
db2701f2e2
commit
889e6ab43d
2 changed files with 30 additions and 0 deletions
|
@ -46,6 +46,8 @@ space rounded up to the nearest whole unit.
|
||||||
|
|
||||||
* `-name pattern`: Checks if the file name matches the given global-style
|
* `-name pattern`: Checks if the file name matches the given global-style
|
||||||
pattern (case sensitive).
|
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
|
* `-iname pattern`: Checks if the file name matches the given global-style
|
||||||
pattern (case insensitive).
|
pattern (case insensitive).
|
||||||
* `-readable`: Checks if the file is readable by the current user.
|
* `-readable`: Checks if the file is readable by the current user.
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
#include <AK/OwnPtr.h>
|
#include <AK/OwnPtr.h>
|
||||||
#include <AK/Time.h>
|
#include <AK/Time.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
|
#include <LibCore/DirIterator.h>
|
||||||
#include <LibCore/System.h>
|
#include <LibCore/System.h>
|
||||||
#include <LibMain/Main.h>
|
#include <LibMain/Main.h>
|
||||||
#include <LibRegex/Regex.h>
|
#include <LibRegex/Regex.h>
|
||||||
|
@ -274,6 +275,31 @@ private:
|
||||||
off_t m_unit_size { 512 };
|
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 {
|
class NameCommand : public Command {
|
||||||
public:
|
public:
|
||||||
NameCommand(char const* pattern, CaseSensitivity case_sensitivity)
|
NameCommand(char const* pattern, CaseSensitivity case_sensitivity)
|
||||||
|
@ -507,6 +533,8 @@ static OwnPtr<Command> parse_simple_command(Vector<char*>& args)
|
||||||
if (args.is_empty())
|
if (args.is_empty())
|
||||||
fatal_error("-size: requires additional arguments");
|
fatal_error("-size: requires additional arguments");
|
||||||
return make<SizeCommand>(args.take_first());
|
return make<SizeCommand>(args.take_first());
|
||||||
|
} else if (arg == "-empty") {
|
||||||
|
return make<EmptyCommand>();
|
||||||
} else if (arg == "-name") {
|
} else if (arg == "-name") {
|
||||||
if (args.is_empty())
|
if (args.is_empty())
|
||||||
fatal_error("-name: requires additional arguments");
|
fatal_error("-name: requires additional arguments");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue