2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2021-06-20 21:44:10 +03:00
|
|
|
#include <AK/LexicalPath.h>
|
2020-02-06 17:04:03 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2021-02-21 03:53:30 +03:00
|
|
|
#include <LibCore/File.h>
|
2021-11-27 00:35:16 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2019-01-27 09:18:26 +03:00
|
|
|
#include <stdio.h>
|
2019-06-07 12:49:31 +03:00
|
|
|
#include <unistd.h>
|
2019-01-27 09:18:26 +03:00
|
|
|
|
2021-11-27 00:35:16 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-01-27 09:18:26 +03:00
|
|
|
{
|
2021-11-28 01:26:34 +03:00
|
|
|
TRY(Core::System::pledge("stdio rpath wpath cpath fattr chown"));
|
2020-01-13 14:30:07 +03:00
|
|
|
|
2021-01-06 18:39:52 +03:00
|
|
|
bool link = false;
|
2021-08-15 13:24:37 +03:00
|
|
|
bool preserve = false;
|
2020-01-27 20:25:36 +03:00
|
|
|
bool recursion_allowed = false;
|
2020-11-17 04:39:03 +03:00
|
|
|
bool verbose = false;
|
2021-11-27 00:32:37 +03:00
|
|
|
Vector<StringView> sources;
|
2021-06-20 21:44:10 +03:00
|
|
|
String destination;
|
2020-01-27 20:25:36 +03:00
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
Core::ArgsParser args_parser;
|
2021-01-06 18:39:52 +03:00
|
|
|
args_parser.add_option(link, "Link files instead of copying", "link", 'l');
|
2021-08-15 13:24:37 +03:00
|
|
|
args_parser.add_option(preserve, "Preserve time, UID/GID and file permissions", nullptr, 'p');
|
2021-01-07 02:32:51 +03:00
|
|
|
args_parser.add_option(recursion_allowed, "Copy directories recursively", "recursive", 'R');
|
|
|
|
args_parser.add_option(recursion_allowed, "Same as -R", nullptr, 'r');
|
2020-11-17 04:39:03 +03:00
|
|
|
args_parser.add_option(verbose, "Verbose", "verbose", 'v');
|
2021-06-20 21:44:10 +03:00
|
|
|
args_parser.add_positional_argument(sources, "Source file paths", "source");
|
2020-01-27 20:25:36 +03:00
|
|
|
args_parser.add_positional_argument(destination, "Destination file path", "destination");
|
2021-11-27 00:35:16 +03:00
|
|
|
args_parser.parse(arguments);
|
2019-09-07 04:03:16 +03:00
|
|
|
|
2021-08-15 13:24:37 +03:00
|
|
|
if (preserve) {
|
|
|
|
umask(0);
|
|
|
|
} else {
|
2021-11-28 01:26:34 +03:00
|
|
|
TRY(Core::System::pledge("stdio rpath wpath cpath fattr"));
|
2021-08-15 13:24:37 +03:00
|
|
|
}
|
|
|
|
|
2021-06-20 21:44:10 +03:00
|
|
|
bool destination_is_existing_dir = Core::File::is_directory(destination);
|
|
|
|
|
2020-01-27 20:25:36 +03:00
|
|
|
for (auto& source : sources) {
|
2021-06-20 21:44:10 +03:00
|
|
|
auto destination_path = destination_is_existing_dir
|
2021-06-29 17:46:16 +03:00
|
|
|
? String::formatted("{}/{}", destination, LexicalPath::basename(source))
|
2021-06-20 21:44:10 +03:00
|
|
|
: destination;
|
|
|
|
|
2021-02-21 03:53:30 +03:00
|
|
|
auto result = Core::File::copy_file_or_directory(
|
2021-06-20 21:44:10 +03:00
|
|
|
destination_path, source,
|
2021-02-21 03:53:30 +03:00
|
|
|
recursion_allowed ? Core::File::RecursionMode::Allowed : Core::File::RecursionMode::Disallowed,
|
|
|
|
link ? Core::File::LinkMode::Allowed : Core::File::LinkMode::Disallowed,
|
2021-08-15 13:24:37 +03:00
|
|
|
Core::File::AddDuplicateFileMarker::No,
|
|
|
|
preserve ? Core::File::PreserveMode::PermissionsOwnershipTimestamps : Core::File::PreserveMode::Nothing);
|
2021-02-21 03:53:30 +03:00
|
|
|
|
|
|
|
if (result.is_error()) {
|
|
|
|
if (result.error().tried_recursing)
|
|
|
|
warnln("cp: -R not specified; omitting directory '{}'", source);
|
|
|
|
else
|
2021-12-20 06:22:26 +03:00
|
|
|
warnln("cp: unable to copy '{}' to '{}': {}", source, destination_path, strerror(result.error().code()));
|
2020-01-27 20:25:36 +03:00
|
|
|
return 1;
|
2021-02-21 03:53:30 +03:00
|
|
|
}
|
|
|
|
|
2020-11-17 04:39:03 +03:00
|
|
|
if (verbose)
|
2021-06-20 21:44:10 +03:00
|
|
|
outln("'{}' -> '{}'", source, destination_path);
|
2019-01-27 09:18:26 +03:00
|
|
|
}
|
2020-01-27 20:25:36 +03:00
|
|
|
return 0;
|
2019-09-07 04:49:28 +03:00
|
|
|
}
|