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