2022-02-16 13:36:47 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Tim Schumacher <timschumi@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/LexicalPath.h>
|
2022-03-14 09:45:35 +00:00
|
|
|
#include <AK/Vector.h>
|
2022-02-16 13:36:47 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2022-04-10 18:51:01 +00:00
|
|
|
#include <LibCore/Directory.h>
|
2022-03-14 11:15:26 +00:00
|
|
|
#include <LibCore/FilePermissionsMask.h>
|
2022-02-16 13:36:47 +00:00
|
|
|
#include <LibCore/System.h>
|
2023-05-13 11:23:30 +00:00
|
|
|
#include <LibFileSystem/FileSystem.h>
|
2022-02-16 13:36:47 +00:00
|
|
|
#include <LibMain/Main.h>
|
|
|
|
|
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|
|
|
{
|
|
|
|
TRY(Core::System::pledge("stdio rpath wpath cpath fattr"));
|
|
|
|
|
|
|
|
bool create_leading_dest_components = false;
|
2022-03-14 11:15:26 +00:00
|
|
|
StringView mode = "0755"sv;
|
2022-03-14 09:45:35 +00:00
|
|
|
Vector<StringView> sources;
|
2022-02-16 13:36:47 +00:00
|
|
|
StringView destination;
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
2022-03-13 01:15:41 +00:00
|
|
|
args_parser.add_ignored(nullptr, 'c'); // "copy files" is the default, no contradicting options exist.
|
2022-02-16 13:36:47 +00:00
|
|
|
args_parser.add_option(create_leading_dest_components, "Create leading components of the destination path", nullptr, 'D');
|
2022-03-14 11:15:26 +00:00
|
|
|
args_parser.add_option(mode, "Permissions to set (instead of 0755)", "mode", 'm', "mode");
|
2022-03-14 09:45:35 +00:00
|
|
|
args_parser.add_positional_argument(sources, "Source path", "source");
|
2022-02-16 13:36:47 +00:00
|
|
|
args_parser.add_positional_argument(destination, "Destination path", "destination");
|
|
|
|
args_parser.parse(arguments);
|
|
|
|
|
2022-03-14 11:15:26 +00:00
|
|
|
auto permission_mask = TRY(Core::FilePermissionsMask::parse(mode));
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString destination_dir = (sources.size() > 1 ? DeprecatedString { destination } : LexicalPath::dirname(destination));
|
2022-03-14 09:45:35 +00:00
|
|
|
|
2022-02-16 13:36:47 +00:00
|
|
|
if (create_leading_dest_components) {
|
2023-05-13 11:23:30 +00:00
|
|
|
String destination_dir_absolute = TRY(FileSystem::absolute_path(destination_dir));
|
|
|
|
MUST(Core::Directory::create(destination_dir_absolute.to_deprecated_string(), Core::Directory::CreateDirectories::Yes));
|
2022-02-16 13:36:47 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 09:45:35 +00:00
|
|
|
for (auto const& source : sources) {
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString final_destination;
|
2022-03-14 09:45:35 +00:00
|
|
|
if (sources.size() > 1) {
|
|
|
|
final_destination = LexicalPath(destination).append(LexicalPath::basename(source)).string();
|
|
|
|
} else {
|
|
|
|
final_destination = destination;
|
|
|
|
}
|
|
|
|
|
2023-05-13 11:23:30 +00:00
|
|
|
TRY(FileSystem::copy_file_or_directory(final_destination, source, FileSystem::RecursionMode::Allowed,
|
|
|
|
FileSystem::LinkMode::Disallowed, FileSystem::AddDuplicateFileMarker::No,
|
|
|
|
FileSystem::PreserveMode::Nothing));
|
2022-03-14 11:15:26 +00:00
|
|
|
|
|
|
|
auto current_access = TRY(Core::System::stat(final_destination));
|
|
|
|
TRY(Core::System::chmod(final_destination, permission_mask.apply(current_access.st_mode)));
|
2022-03-14 09:45:35 +00:00
|
|
|
}
|
2022-02-16 13:36:47 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|