Utilities: Add support for multiple sources to install

This commit is contained in:
Tim Schumacher 2022-03-14 10:45:35 +01:00 committed by Brian Gianforcaro
parent 62f0fa818d
commit 9de742e321
Notes: sideshowbarker 2024-07-17 17:06:52 +09:00

View file

@ -5,6 +5,7 @@
*/
#include <AK/LexicalPath.h>
#include <AK/Vector.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <LibCore/System.h>
@ -15,24 +16,35 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
TRY(Core::System::pledge("stdio rpath wpath cpath fattr"));
bool create_leading_dest_components = false;
StringView source;
Vector<StringView> sources;
StringView destination;
Core::ArgsParser args_parser;
args_parser.add_ignored(nullptr, 'c'); // "copy files" is the default, no contradicting options exist.
args_parser.add_option(create_leading_dest_components, "Create leading components of the destination path", nullptr, 'D');
args_parser.add_positional_argument(source, "Source path", "source");
args_parser.add_positional_argument(sources, "Source path", "source");
args_parser.add_positional_argument(destination, "Destination path", "destination");
args_parser.parse(arguments);
String destination_dir = (sources.size() > 1 ? String { destination } : LexicalPath::dirname(destination));
if (create_leading_dest_components) {
String destination_absolute = Core::File::absolute_path(destination);
Core::File::ensure_parent_directories(destination_absolute);
String destination_dir_absolute = Core::File::absolute_path(destination_dir);
Core::File::ensure_directories(destination_dir_absolute);
}
TRY(Core::File::copy_file_or_directory(destination, source, Core::File::RecursionMode::Allowed,
Core::File::LinkMode::Disallowed, Core::File::AddDuplicateFileMarker::No,
Core::File::PreserveMode::Nothing));
for (auto const& source : sources) {
String final_destination;
if (sources.size() > 1) {
final_destination = LexicalPath(destination).append(LexicalPath::basename(source)).string();
} else {
final_destination = destination;
}
TRY(Core::File::copy_file_or_directory(final_destination, source, Core::File::RecursionMode::Allowed,
Core::File::LinkMode::Disallowed, Core::File::AddDuplicateFileMarker::No,
Core::File::PreserveMode::Nothing));
}
return 0;
}