mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 18:02:05 -05:00
chmod: Implement the --recursive
flag
This commit is contained in:
parent
0329a644f8
commit
a031b7a174
1 changed files with 29 additions and 2 deletions
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
|
#include <LibCore/DirIterator.h>
|
||||||
#include <LibCore/FilePermissionsMask.h>
|
#include <LibCore/FilePermissionsMask.h>
|
||||||
#include <LibCore/System.h>
|
#include <LibCore/System.h>
|
||||||
#include <LibMain/Main.h>
|
#include <LibMain/Main.h>
|
||||||
|
@ -18,17 +19,43 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
|
|
||||||
StringView mode;
|
StringView mode;
|
||||||
Vector<StringView> paths;
|
Vector<StringView> paths;
|
||||||
|
bool recursive = false;
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
Core::ArgsParser args_parser;
|
||||||
|
args_parser.add_option(recursive, "Change file modes recursively", "recursive", 'R');
|
||||||
args_parser.add_positional_argument(mode, "File mode in octal or symbolic notation", "mode");
|
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.add_positional_argument(paths, "Paths to file", "paths");
|
||||||
args_parser.parse(arguments);
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
auto mask = TRY(Core::FilePermissionsMask::parse(mode));
|
auto mask = TRY(Core::FilePermissionsMask::parse(mode));
|
||||||
|
|
||||||
|
Function<ErrorOr<void>(StringView const&)> update_path_permissions = [&](StringView const& path) -> ErrorOr<void> {
|
||||||
|
auto stat = TRY(Core::System::lstat(path));
|
||||||
|
|
||||||
|
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))
|
||||||
|
return {};
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
stat = TRY(Core::System::stat(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
TRY(Core::System::chmod(path, mask.apply(stat.st_mode)));
|
||||||
|
|
||||||
|
if (recursive && S_ISDIR(stat.st_mode)) {
|
||||||
|
Core::DirIterator it(path, Core::DirIterator::Flags::SkipParentAndBaseDir);
|
||||||
|
|
||||||
|
while (it.has_next())
|
||||||
|
TRY(update_path_permissions(it.next_full_path()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
};
|
||||||
|
|
||||||
for (auto const& path : paths) {
|
for (auto const& path : paths) {
|
||||||
auto current_access = TRY(Core::System::stat(path));
|
TRY(update_path_permissions(path));
|
||||||
TRY(Core::System::chmod(path, mask.apply(current_access.st_mode)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Add table
Reference in a new issue