2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-06-30 14:19:03 +00:00
|
|
|
* Copyright (c) 2021, Kenneth Myhra <kennethmyhra@serenityos.org>
|
2021-12-25 10:02:56 +00:00
|
|
|
* Copyright (c) 2021, Xavier Defrang <xavier.defrang@gmail.com>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2021-11-27 14:55:40 +00:00
|
|
|
#include <AK/Vector.h>
|
2022-07-23 19:31:49 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2022-07-24 16:59:14 +00:00
|
|
|
#include <LibCore/DirIterator.h>
|
2021-12-25 10:02:56 +00:00
|
|
|
#include <LibCore/FilePermissionsMask.h>
|
2021-11-27 14:55:40 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2019-01-29 03:55:08 +00:00
|
|
|
|
2021-11-27 14:55:40 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-01-29 03:55:08 +00:00
|
|
|
{
|
2021-12-25 10:02:56 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath fattr"));
|
2020-01-12 12:25:02 +00:00
|
|
|
|
2022-07-23 19:31:49 +00:00
|
|
|
StringView mode;
|
|
|
|
Vector<StringView> paths;
|
2022-07-24 16:59:14 +00:00
|
|
|
bool recursive = false;
|
2022-07-23 19:31:49 +00:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
2022-07-24 16:59:14 +00:00
|
|
|
args_parser.add_option(recursive, "Change file modes recursively", "recursive", 'R');
|
2022-07-23 19:31:49 +00:00
|
|
|
args_parser.add_positional_argument(mode, "File mode in octal or symbolic notation", "mode");
|
|
|
|
args_parser.add_positional_argument(paths, "Paths to file", "paths");
|
|
|
|
args_parser.parse(arguments);
|
2019-01-29 03:55:08 +00:00
|
|
|
|
2022-07-23 19:31:49 +00:00
|
|
|
auto mask = TRY(Core::FilePermissionsMask::parse(mode));
|
2019-07-13 17:36:02 +00:00
|
|
|
|
2023-06-17 21:34:00 +00:00
|
|
|
Function<bool(StringView)> update_path_permissions = [&](StringView const& path) {
|
|
|
|
auto stat_or_error = Core::System::lstat(path);
|
|
|
|
if (stat_or_error.is_error()) {
|
|
|
|
warnln("Could not stat '{}': {}", path, stat_or_error.release_error());
|
|
|
|
return false;
|
|
|
|
}
|
2022-07-24 16:59:14 +00:00
|
|
|
|
2023-06-17 21:34:00 +00:00
|
|
|
auto stat = stat_or_error.release_value();
|
2022-07-24 16:59:14 +00:00
|
|
|
if (S_ISLNK(stat.st_mode)) {
|
|
|
|
// Symlinks don't get processed unless they are explicitly listed on the command line.
|
|
|
|
if (!paths.contains_slow(path))
|
2023-06-17 21:34:00 +00:00
|
|
|
return false;
|
2022-07-24 16:59:14 +00:00
|
|
|
|
|
|
|
// The chmod syscall changes the file that a link points to, so we will have to get
|
|
|
|
// the correct mode to base our modifications on.
|
2023-06-17 21:34:00 +00:00
|
|
|
stat_or_error = Core::System::stat(path);
|
|
|
|
if (stat_or_error.is_error()) {
|
|
|
|
warnln("Could not stat '{}': {}", path, stat_or_error.release_error());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
stat = stat_or_error.release_value();
|
2022-07-24 16:59:14 +00:00
|
|
|
}
|
|
|
|
|
2023-06-17 21:34:00 +00:00
|
|
|
auto success = true;
|
|
|
|
auto maybe_error = Core::System::chmod(path, mask.apply(stat.st_mode));
|
|
|
|
if (maybe_error.is_error()) {
|
|
|
|
warnln("Failed to change permissions of '{}': {}", path, maybe_error.release_error());
|
|
|
|
success = false;
|
|
|
|
}
|
2022-07-24 16:59:14 +00:00
|
|
|
|
|
|
|
if (recursive && S_ISDIR(stat.st_mode)) {
|
|
|
|
Core::DirIterator it(path, Core::DirIterator::Flags::SkipParentAndBaseDir);
|
|
|
|
|
|
|
|
while (it.has_next())
|
2023-06-17 21:34:00 +00:00
|
|
|
success &= update_path_permissions(it.next_full_path());
|
2022-07-24 16:59:14 +00:00
|
|
|
}
|
|
|
|
|
2023-06-17 21:34:00 +00:00
|
|
|
return success;
|
2022-07-24 16:59:14 +00:00
|
|
|
};
|
|
|
|
|
2023-06-17 21:34:00 +00:00
|
|
|
auto success = true;
|
2022-07-23 19:31:49 +00:00
|
|
|
for (auto const& path : paths) {
|
2023-06-17 21:34:00 +00:00
|
|
|
success &= update_path_permissions(path);
|
2019-01-29 03:55:08 +00:00
|
|
|
}
|
|
|
|
|
2023-06-17 21:34:00 +00:00
|
|
|
return success ? 0 : 1;
|
2019-01-29 03:55:08 +00:00
|
|
|
}
|