2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2021-06-20 19:44:10 +01:00
|
|
|
#include <AK/LexicalPath.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2021-11-26 22:35:16 +01:00
|
|
|
#include <LibCore/System.h>
|
2023-03-22 02:35:30 +11:00
|
|
|
#include <LibFileSystem/FileSystem.h>
|
2021-11-26 22:35:16 +01:00
|
|
|
#include <LibMain/Main.h>
|
2019-01-27 07:18:26 +01:00
|
|
|
#include <stdio.h>
|
2019-06-07 11:49:31 +02:00
|
|
|
#include <unistd.h>
|
2019-01-27 07:18:26 +01:00
|
|
|
|
2021-11-26 22:35:16 +01:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-01-27 07:18:26 +01:00
|
|
|
{
|
2021-11-27 14:26:34 -08:00
|
|
|
TRY(Core::System::pledge("stdio rpath wpath cpath fattr chown"));
|
2020-01-13 12:30:07 +01:00
|
|
|
|
2021-01-06 10:39:52 -05:00
|
|
|
bool link = false;
|
2023-05-13 13:31:09 +02:00
|
|
|
auto preserve = FileSystem::PreserveMode::Nothing;
|
2020-01-27 20:25:36 +03:00
|
|
|
bool recursion_allowed = false;
|
2020-11-16 20:39:03 -05:00
|
|
|
bool verbose = false;
|
2021-11-26 22:32:37 +01:00
|
|
|
Vector<StringView> sources;
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString destination;
|
2020-01-27 20:25:36 +03:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
Core::ArgsParser args_parser;
|
2021-01-06 10:39:52 -05:00
|
|
|
args_parser.add_option(link, "Link files instead of copying", "link", 'l');
|
2022-07-12 23:57:14 +02:00
|
|
|
args_parser.add_option({
|
|
|
|
|
Core::ArgsParser::OptionArgumentMode::Optional,
|
|
|
|
|
"Preserve a selection of mode, ownership and timestamps. Defaults to all three if the option is present but no list is given.",
|
|
|
|
|
"preserve",
|
|
|
|
|
'p',
|
|
|
|
|
"attributes",
|
2023-02-21 15:14:41 +03:30
|
|
|
[&preserve](StringView s) {
|
|
|
|
|
if (s.is_empty()) {
|
2023-05-13 13:31:09 +02:00
|
|
|
preserve = FileSystem::PreserveMode::Permissions | FileSystem::PreserveMode::Ownership | FileSystem::PreserveMode::Timestamps;
|
2022-07-12 23:57:14 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool values_ok = true;
|
|
|
|
|
|
2023-02-21 15:14:41 +03:30
|
|
|
s.for_each_split_view(',', SplitBehavior::Nothing, [&](StringView value) {
|
2022-07-12 23:57:14 +02:00
|
|
|
if (value == "mode"sv) {
|
2023-05-13 13:31:09 +02:00
|
|
|
preserve |= FileSystem::PreserveMode::Permissions;
|
2022-07-12 23:57:14 +02:00
|
|
|
} else if (value == "ownership"sv) {
|
2023-05-13 13:31:09 +02:00
|
|
|
preserve |= FileSystem::PreserveMode::Ownership;
|
2022-07-12 23:57:14 +02:00
|
|
|
} else if (value == "timestamps"sv) {
|
2023-05-13 13:31:09 +02:00
|
|
|
preserve |= FileSystem::PreserveMode::Timestamps;
|
2022-07-12 23:57:14 +02:00
|
|
|
} else {
|
|
|
|
|
warnln("cp: Unknown or unimplemented --preserve attribute: '{}'", value);
|
|
|
|
|
values_ok = false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return values_ok;
|
|
|
|
|
},
|
|
|
|
|
Core::ArgsParser::OptionHideMode::None,
|
|
|
|
|
});
|
2021-01-06 18:32:51 -05: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-16 20:39:03 -05:00
|
|
|
args_parser.add_option(verbose, "Verbose", "verbose", 'v');
|
2021-06-20 19:44:10 +01: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-26 22:35:16 +01:00
|
|
|
args_parser.parse(arguments);
|
2019-09-07 11:03:16 +10:00
|
|
|
|
2023-05-13 13:31:09 +02:00
|
|
|
if (has_flag(preserve, FileSystem::PreserveMode::Permissions)) {
|
2021-08-15 12:24:37 +02:00
|
|
|
umask(0);
|
|
|
|
|
} else {
|
2021-11-27 14:26:34 -08:00
|
|
|
TRY(Core::System::pledge("stdio rpath wpath cpath fattr"));
|
2021-08-15 12:24:37 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-22 02:35:30 +11:00
|
|
|
bool destination_is_existing_dir = FileSystem::is_directory(destination);
|
2021-06-20 19:44:10 +01:00
|
|
|
|
2020-01-27 20:25:36 +03:00
|
|
|
for (auto& source : sources) {
|
2021-06-20 19:44:10 +01:00
|
|
|
auto destination_path = destination_is_existing_dir
|
2022-12-04 18:02:33 +00:00
|
|
|
? DeprecatedString::formatted("{}/{}", destination, LexicalPath::basename(source))
|
2021-06-20 19:44:10 +01:00
|
|
|
: destination;
|
|
|
|
|
|
2023-05-13 13:31:09 +02:00
|
|
|
auto result = FileSystem::copy_file_or_directory(
|
2021-06-20 19:44:10 +01:00
|
|
|
destination_path, source,
|
2023-05-13 13:31:09 +02:00
|
|
|
recursion_allowed ? FileSystem::RecursionMode::Allowed : FileSystem::RecursionMode::Disallowed,
|
|
|
|
|
link ? FileSystem::LinkMode::Allowed : FileSystem::LinkMode::Disallowed,
|
|
|
|
|
FileSystem::AddDuplicateFileMarker::No,
|
2022-07-12 23:57:14 +02:00
|
|
|
preserve);
|
2021-02-21 02:53:30 +02:00
|
|
|
|
|
|
|
|
if (result.is_error()) {
|
2023-05-13 13:31:09 +02:00
|
|
|
if (result.error().code() == EISDIR)
|
2021-02-21 02:53:30 +02:00
|
|
|
warnln("cp: -R not specified; omitting directory '{}'", source);
|
|
|
|
|
else
|
2021-12-20 04:22:26 +01: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 02:53:30 +02:00
|
|
|
}
|
|
|
|
|
|
2020-11-16 20:39:03 -05:00
|
|
|
if (verbose)
|
2021-06-20 19:44:10 +01:00
|
|
|
outln("'{}' -> '{}'", source, destination_path);
|
2019-01-27 07:18:26 +01:00
|
|
|
}
|
2020-01-27 20:25:36 +03:00
|
|
|
return 0;
|
2019-09-07 11:49:28 +10:00
|
|
|
}
|