find: Add -uid option to filter by owning user ID

This commit is contained in:
Tim Ledbetter 2023-09-09 18:58:26 +01:00 committed by Andrew Kaster
parent d3da8f978e
commit 1bc081398e
2 changed files with 27 additions and 1 deletions

View file

@ -78,6 +78,8 @@ by the current user.
symbolic link is used.
* `-gid [-|+]number`: Checks if the file is owned by a group with an ID less
than, greater than or exactly `number`.
* `-uid [-|+]number`: Checks if the file is owned by a user with an ID less
than, greater than or exactly `number`.
* `-print`: Outputs the file path, followed by a newline. Always evaluates to
true.
* `-print0`: Outputs the file path, followed by a zero byte. Always evaluates to

View file

@ -511,7 +511,27 @@ private:
return m_gid_range.contains(stat.st_gid);
}
NumericRange<gid_t> m_gid_range;
NumericRange<gid_t> m_gid_range {};
};
class UidCommand final : public StatCommand {
public:
UidCommand(char const* arg)
{
auto uid_or_error = NumericRange<uid_t>::parse({ arg, strlen(arg) });
if (uid_or_error.is_error())
fatal_error("find: Invalid argument '{}' to '-uid'", arg);
m_uid_range = uid_or_error.release_value();
}
private:
virtual bool evaluate(struct stat const& stat) const override
{
return m_uid_range.contains(stat.st_uid);
}
NumericRange<uid_t> m_uid_range {};
};
class PrintCommand final : public Command {
@ -737,6 +757,10 @@ static OwnPtr<Command> parse_simple_command(Vector<char*>& args)
if (args.is_empty())
fatal_error("-gid: requires additional arguments");
return make<GidCommand>(args.take_first());
} else if (arg == "-uid") {
if (args.is_empty())
fatal_error("-uid: requires additional arguments");
return make<UidCommand>(args.take_first());
} else if (arg == "-print") {
g_have_seen_action_command = true;
return make<PrintCommand>();