2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2020-01-12 13:00:14 +02:00
|
|
|
#include <AK/String.h>
|
2020-08-05 22:37:59 +02:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2021-11-27 15:24:25 +01:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2020-01-12 13:00:14 +02:00
|
|
|
#include <grp.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2021-11-27 15:24:25 +01:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-01-12 13:00:14 +02:00
|
|
|
{
|
2022-04-03 16:16:06 -07:00
|
|
|
TRY(Core::System::pledge("stdio rpath chown"));
|
2020-01-12 13:00:14 +02:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
char const* gid_arg = nullptr;
|
2022-07-11 20:42:03 +00:00
|
|
|
StringView path {};
|
2021-12-31 19:28:24 +01:00
|
|
|
bool dont_follow_symlinks = false;
|
2020-08-05 22:37:59 +02:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
2020-12-05 16:22:58 +01:00
|
|
|
args_parser.set_general_help("Change the owning group for a file or directory.");
|
2021-12-31 19:28:24 +01:00
|
|
|
args_parser.add_option(dont_follow_symlinks, "Don't follow symlinks", "no-dereference", 'h');
|
2020-08-05 22:37:59 +02:00
|
|
|
args_parser.add_positional_argument(gid_arg, "Group ID", "gid");
|
|
|
|
args_parser.add_positional_argument(path, "Path to file", "path");
|
2021-11-27 15:24:25 +01:00
|
|
|
args_parser.parse(arguments);
|
2020-01-12 13:00:14 +02:00
|
|
|
|
|
|
|
gid_t new_gid = -1;
|
|
|
|
|
2020-08-05 22:37:59 +02:00
|
|
|
if (String(gid_arg).is_empty()) {
|
2021-05-31 15:43:25 +01:00
|
|
|
warnln("Empty gid option");
|
2020-01-12 13:00:14 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-08-05 22:37:59 +02:00
|
|
|
auto number = String(gid_arg).to_uint();
|
2020-06-12 21:07:52 +02:00
|
|
|
if (number.has_value()) {
|
|
|
|
new_gid = number.value();
|
|
|
|
} else {
|
2020-08-05 22:37:59 +02:00
|
|
|
auto* group = getgrnam(gid_arg);
|
2020-04-04 19:29:30 +02:00
|
|
|
if (!group) {
|
2021-05-31 15:43:25 +01:00
|
|
|
warnln("Unknown group '{}'", gid_arg);
|
2020-01-12 13:00:14 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2020-04-04 19:29:30 +02:00
|
|
|
new_gid = group->gr_gid;
|
2020-01-12 13:00:14 +02:00
|
|
|
}
|
|
|
|
|
2021-12-31 19:28:24 +01:00
|
|
|
if (dont_follow_symlinks) {
|
|
|
|
TRY(Core::System::lchown(path, -1, new_gid));
|
|
|
|
} else {
|
|
|
|
TRY(Core::System::chown(path, -1, new_gid));
|
|
|
|
}
|
2020-01-12 13:00:14 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|