Browse Source

find: Add `-gid` option to filter by owning group ID

Tim Ledbetter 1 year ago
parent
commit
d3da8f978e
2 changed files with 26 additions and 0 deletions
  1. 2 0
      Base/usr/share/man/man1/find.md
  2. 24 0
      Userland/Utilities/find.cpp

+ 2 - 0
Base/usr/share/man/man1/find.md

@@ -76,6 +76,8 @@ by the current user.
   the specified reference file. If `file` is a symbolic link and the `-L`
   the specified reference file. If `file` is a symbolic link and the `-L`
   option is in use, then the creation time of the file pointed to by the
   option is in use, then the creation time of the file pointed to by the
   symbolic link is used.
   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`.
 * `-print`: Outputs the file path, followed by a newline. Always evaluates to
 * `-print`: Outputs the file path, followed by a newline. Always evaluates to
   true.
   true.
 * `-print0`: Outputs the file path, followed by a zero byte. Always evaluates to
 * `-print0`: Outputs the file path, followed by a zero byte. Always evaluates to

+ 24 - 0
Userland/Utilities/find.cpp

@@ -494,6 +494,26 @@ private:
     TimestampType m_timestamp_type { TimestampType::LastModification };
     TimestampType m_timestamp_type { TimestampType::LastModification };
 };
 };
 
 
+class GidCommand final : public StatCommand {
+public:
+    GidCommand(char const* arg)
+    {
+        auto gid_or_error = NumericRange<gid_t>::parse({ arg, strlen(arg) });
+        if (gid_or_error.is_error())
+            fatal_error("find: Invalid argument '{}' to '-gid'", arg);
+
+        m_gid_range = gid_or_error.release_value();
+    }
+
+private:
+    virtual bool evaluate(struct stat const& stat) const override
+    {
+        return m_gid_range.contains(stat.st_gid);
+    }
+
+    NumericRange<gid_t> m_gid_range;
+};
+
 class PrintCommand final : public Command {
 class PrintCommand final : public Command {
 public:
 public:
     PrintCommand(char terminator = '\n')
     PrintCommand(char terminator = '\n')
@@ -713,6 +733,10 @@ static OwnPtr<Command> parse_simple_command(Vector<char*>& args)
         if (args.is_empty())
         if (args.is_empty())
             fatal_error("-cnewer: requires additional arguments");
             fatal_error("-cnewer: requires additional arguments");
         return make<NewerCommand>(args.take_first(), NewerCommand::TimestampType::Creation);
         return make<NewerCommand>(args.take_first(), NewerCommand::TimestampType::Creation);
+    } else if (arg == "-gid") {
+        if (args.is_empty())
+            fatal_error("-gid: requires additional arguments");
+        return make<GidCommand>(args.take_first());
     } else if (arg == "-print") {
     } else if (arg == "-print") {
         g_have_seen_action_command = true;
         g_have_seen_action_command = true;
         return make<PrintCommand>();
         return make<PrintCommand>();