2021-03-26 18:37:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, János Tóth <toth-janos@outlook.com>
|
2023-05-31 16:54:38 +00:00
|
|
|
* Copyright (c) 2023, Karol Kosek <krkk@serenityos.org>
|
2021-03-26 18:37:19 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-03-26 18:37:19 +00:00
|
|
|
*/
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
#include <AK/ByteString.h>
|
2023-03-09 17:37:16 +00:00
|
|
|
#include <AK/CharacterTypes.h>
|
2023-05-13 14:33:15 +00:00
|
|
|
#include <AK/NumberFormat.h>
|
2021-03-26 18:37:19 +00:00
|
|
|
#include <AK/Optional.h>
|
|
|
|
#include <AK/Vector.h>
|
2023-05-13 14:33:15 +00:00
|
|
|
#include <LibCore/ElapsedTimer.h>
|
2023-05-31 16:54:38 +00:00
|
|
|
#include <LibCore/System.h>
|
2021-11-28 09:24:23 +00:00
|
|
|
#include <LibMain/Main.h>
|
2021-03-26 18:37:19 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2021-05-31 17:01:53 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2021-03-26 18:37:19 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
char const* usage = "usage:\n"
|
2021-03-26 18:37:19 +00:00
|
|
|
"\tdd <options>\n"
|
|
|
|
"options:\n"
|
|
|
|
"\tif=<file>\tinput file (default: stdin)\n"
|
|
|
|
"\tof=<file>\toutput file (default: stdout)\n"
|
2021-05-08 09:03:31 +00:00
|
|
|
"\tbs=<size>\tblocks size may be followed by multiplicate suffixes: k=1024, M=1024*1024, G=1024*1024*1024 (default: 512)\n"
|
2021-03-26 18:37:19 +00:00
|
|
|
"\tcount=<size>\t<size> blocks to copy (default: 0 (until end-of-file))\n"
|
|
|
|
"\tseek=<size>\tskip <size> blocks at start of output (default: 0)\n"
|
2021-04-18 08:30:03 +00:00
|
|
|
"\tskip=<size>\tskip <size> blocks at start of input (default: 0)\n"
|
2021-03-26 18:37:19 +00:00
|
|
|
"\tstatus=<level>\tlevel of output (default: default)\n"
|
|
|
|
"\t\t\tdefault - error messages + final statistics\n"
|
|
|
|
"\t\t\tnone - just error messages\n"
|
|
|
|
"\t\t\tnoxfer - no final statistics\n"
|
|
|
|
"\t--help\t\tshows this text\n";
|
|
|
|
|
|
|
|
enum Status {
|
|
|
|
Default,
|
|
|
|
None,
|
|
|
|
Noxfer
|
|
|
|
};
|
|
|
|
|
2023-05-31 16:54:38 +00:00
|
|
|
struct {
|
|
|
|
Status status = Default;
|
|
|
|
size_t total_bytes_copied = 0;
|
|
|
|
size_t total_blocks_in = 0, partial_blocks_in = 0;
|
|
|
|
size_t total_blocks_out = 0, partial_blocks_out = 0;
|
2024-02-26 17:52:03 +00:00
|
|
|
Core::ElapsedTimer timer { Core::TimerType::Precise };
|
2023-05-31 16:54:38 +00:00
|
|
|
} statistics;
|
|
|
|
|
|
|
|
static void closing_statistics()
|
|
|
|
{
|
2023-05-14 18:41:12 +00:00
|
|
|
if (statistics.status == None)
|
2023-05-31 16:54:38 +00:00
|
|
|
return;
|
|
|
|
warnln("{}+{} blocks in", statistics.total_blocks_in, statistics.partial_blocks_in);
|
|
|
|
warnln("{}+{} blocks out", statistics.total_blocks_out, statistics.partial_blocks_out);
|
2023-05-14 18:41:12 +00:00
|
|
|
if (statistics.status != Noxfer) {
|
2023-05-13 14:33:15 +00:00
|
|
|
auto elapsed_time = statistics.timer.elapsed_time();
|
2024-01-24 12:16:32 +00:00
|
|
|
String copy_speed = "INF B/s"_string;
|
2023-05-13 14:33:15 +00:00
|
|
|
if (!elapsed_time.is_zero()) {
|
|
|
|
auto speed = statistics.total_bytes_copied * 1000 / elapsed_time.to_milliseconds();
|
|
|
|
copy_speed = human_readable_quantity(speed, AK::HumanReadableBasedOn::Base2, "B/s"sv);
|
|
|
|
}
|
|
|
|
warnln("{} bytes copied ({}), {} ms, {}", statistics.total_bytes_copied, human_readable_size(statistics.total_bytes_copied), elapsed_time.to_milliseconds(), copy_speed);
|
2023-05-14 18:41:12 +00:00
|
|
|
}
|
2023-05-31 16:54:38 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 09:24:23 +00:00
|
|
|
static StringView split_at_equals(StringView argument)
|
2021-03-26 18:37:19 +00:00
|
|
|
{
|
2022-10-22 13:38:21 +00:00
|
|
|
auto values = argument.split_view('=', SplitBehavior::KeepEmpty);
|
2021-07-15 14:06:46 +00:00
|
|
|
if (values.size() < 2) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Unable to parse: {}", argument);
|
2021-03-26 18:37:19 +00:00
|
|
|
return {};
|
|
|
|
} else {
|
|
|
|
return values[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 09:24:23 +00:00
|
|
|
static int handle_io_file_arguments(int& fd, int flags, StringView argument)
|
2021-03-26 18:37:19 +00:00
|
|
|
{
|
|
|
|
auto value = split_at_equals(argument);
|
|
|
|
if (value.is_empty()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
fd = open(value.to_byte_string().characters(), flags, 0666);
|
2021-03-26 18:37:19 +00:00
|
|
|
if (fd == -1) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Unable to open: {}", value);
|
2021-03-26 18:37:19 +00:00
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 09:24:23 +00:00
|
|
|
static int handle_size_arguments(size_t& numeric_value, StringView argument)
|
2021-03-26 18:37:19 +00:00
|
|
|
{
|
|
|
|
auto value = split_at_equals(argument);
|
|
|
|
if (value.is_empty()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-05-08 09:03:31 +00:00
|
|
|
unsigned suffix_multiplier = 1;
|
2021-11-28 09:24:23 +00:00
|
|
|
auto suffix = value[value.length() - 1];
|
2023-03-09 17:37:16 +00:00
|
|
|
switch (to_ascii_lowercase(suffix)) {
|
2021-05-08 09:03:31 +00:00
|
|
|
case 'k':
|
|
|
|
suffix_multiplier = KiB;
|
2021-11-28 09:24:23 +00:00
|
|
|
value = value.substring_view(0, value.length() - 1);
|
2021-05-08 09:03:31 +00:00
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
suffix_multiplier = MiB;
|
2021-11-28 09:24:23 +00:00
|
|
|
value = value.substring_view(0, value.length() - 1);
|
2021-05-08 09:03:31 +00:00
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
suffix_multiplier = GiB;
|
2021-11-28 09:24:23 +00:00
|
|
|
value = value.substring_view(0, value.length() - 1);
|
2021-05-08 09:03:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-12-23 02:59:14 +00:00
|
|
|
Optional<unsigned> numeric_optional = value.to_number<unsigned>();
|
2021-03-26 18:37:19 +00:00
|
|
|
if (!numeric_optional.has_value()) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Invalid size-value: {}", value);
|
2021-03-26 18:37:19 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-05-08 09:03:31 +00:00
|
|
|
numeric_value = numeric_optional.value() * suffix_multiplier;
|
2021-03-26 18:37:19 +00:00
|
|
|
if (numeric_value < 1) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Invalid size-value: {}", numeric_value);
|
2021-03-26 18:37:19 +00:00
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 09:24:23 +00:00
|
|
|
static int handle_status_arguments(Status& status, StringView argument)
|
2021-03-26 18:37:19 +00:00
|
|
|
{
|
|
|
|
auto value = split_at_equals(argument);
|
|
|
|
if (value.is_empty()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value == "default") {
|
|
|
|
status = Default;
|
|
|
|
return 0;
|
|
|
|
} else if (value == "noxfer") {
|
|
|
|
status = Noxfer;
|
|
|
|
return 0;
|
|
|
|
} else if (value == "none") {
|
|
|
|
status = None;
|
|
|
|
return 0;
|
|
|
|
} else {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Unknown status: {}", value);
|
2021-03-26 18:37:19 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 09:24:23 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-03-26 18:37:19 +00:00
|
|
|
{
|
|
|
|
int input_fd = 0;
|
|
|
|
int input_flags = O_RDONLY;
|
|
|
|
int output_fd = 1;
|
2021-05-27 09:53:23 +00:00
|
|
|
int output_flags = O_CREAT | O_WRONLY | O_TRUNC;
|
2021-03-26 18:37:19 +00:00
|
|
|
size_t block_size = 512;
|
|
|
|
size_t count = 0;
|
|
|
|
size_t skip = 0;
|
|
|
|
size_t seek = 0;
|
|
|
|
|
|
|
|
uint8_t* buffer = nullptr;
|
|
|
|
ssize_t nread = 0, nwritten = 0;
|
|
|
|
|
2021-11-28 09:24:23 +00:00
|
|
|
for (size_t a = 1; a < arguments.strings.size(); a++) {
|
|
|
|
auto argument = arguments.strings[a];
|
|
|
|
|
|
|
|
if (argument == "--help") {
|
2021-05-31 14:43:25 +00:00
|
|
|
out("{}", usage);
|
2021-03-26 18:37:19 +00:00
|
|
|
return 0;
|
2022-07-11 17:32:29 +00:00
|
|
|
} else if (argument.starts_with("if="sv)) {
|
2021-11-28 09:24:23 +00:00
|
|
|
if (handle_io_file_arguments(input_fd, input_flags, argument) < 0) {
|
2021-03-26 18:37:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2022-07-11 17:32:29 +00:00
|
|
|
} else if (argument.starts_with("of="sv)) {
|
2021-11-28 09:24:23 +00:00
|
|
|
if (handle_io_file_arguments(output_fd, output_flags, argument) < 0) {
|
2021-03-26 18:37:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2022-07-11 17:32:29 +00:00
|
|
|
} else if (argument.starts_with("bs="sv)) {
|
2021-11-28 09:24:23 +00:00
|
|
|
if (handle_size_arguments(block_size, argument) < 0) {
|
2021-03-26 18:37:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2022-07-11 17:32:29 +00:00
|
|
|
} else if (argument.starts_with("count="sv)) {
|
2021-11-28 09:24:23 +00:00
|
|
|
if (handle_size_arguments(count, argument) < 0) {
|
2021-03-26 18:37:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2022-07-11 17:32:29 +00:00
|
|
|
} else if (argument.starts_with("seek="sv)) {
|
2021-11-28 09:24:23 +00:00
|
|
|
if (handle_size_arguments(seek, argument) < 0) {
|
2021-03-26 18:37:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2022-07-11 17:32:29 +00:00
|
|
|
} else if (argument.starts_with("skip="sv)) {
|
2021-11-28 09:24:23 +00:00
|
|
|
if (handle_size_arguments(skip, argument) < 0) {
|
2021-03-26 18:37:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2022-07-11 17:32:29 +00:00
|
|
|
} else if (argument.starts_with("status="sv)) {
|
2023-05-31 16:54:38 +00:00
|
|
|
if (handle_status_arguments(statistics.status, argument) < 0) {
|
2021-03-26 18:37:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
2021-05-31 14:43:25 +00:00
|
|
|
warn("{}", usage);
|
2021-03-26 18:37:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((buffer = (uint8_t*)malloc(block_size)) == nullptr) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Unable to allocate {} bytes for the buffer.", block_size);
|
2021-03-26 18:37:19 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (seek > 0) {
|
|
|
|
if (lseek(output_fd, seek * block_size, SEEK_SET) < 0) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Unable to seek {} bytes.", seek * block_size);
|
2021-03-26 18:37:19 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 16:54:38 +00:00
|
|
|
TRY(Core::System::signal(SIGINT, [](int status) {
|
|
|
|
closing_statistics();
|
|
|
|
exit(status);
|
|
|
|
}));
|
|
|
|
|
2023-05-13 14:33:15 +00:00
|
|
|
statistics.timer.start();
|
|
|
|
|
2021-03-26 18:37:19 +00:00
|
|
|
while (1) {
|
|
|
|
nread = read(input_fd, buffer, block_size);
|
|
|
|
if (nread < 0) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Cannot read from the input.");
|
2021-03-26 18:37:19 +00:00
|
|
|
break;
|
|
|
|
} else if (nread == 0) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if ((size_t)nread != block_size) {
|
2023-05-31 16:54:38 +00:00
|
|
|
statistics.partial_blocks_in++;
|
2021-03-26 18:37:19 +00:00
|
|
|
} else {
|
2023-05-31 16:54:38 +00:00
|
|
|
statistics.total_blocks_in++;
|
2021-03-26 18:37:19 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 16:54:38 +00:00
|
|
|
if (statistics.partial_blocks_in + statistics.total_blocks_in <= skip) {
|
2021-03-26 18:37:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nwritten = write(output_fd, buffer, nread);
|
|
|
|
if (nwritten < 0) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Cannot write to the output.");
|
2021-03-26 18:37:19 +00:00
|
|
|
break;
|
|
|
|
} else if (nwritten == 0) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if ((size_t)nwritten < block_size) {
|
2023-05-31 16:54:38 +00:00
|
|
|
statistics.partial_blocks_out++;
|
2021-03-26 18:37:19 +00:00
|
|
|
} else {
|
2023-05-31 16:54:38 +00:00
|
|
|
statistics.total_blocks_out++;
|
2021-03-26 18:37:19 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 16:54:38 +00:00
|
|
|
statistics.total_bytes_copied += nwritten;
|
2021-03-26 18:37:19 +00:00
|
|
|
|
2023-05-31 16:54:38 +00:00
|
|
|
if (count > 0 && (statistics.partial_blocks_out + statistics.total_blocks_out) >= count) {
|
2021-03-26 18:37:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 16:54:38 +00:00
|
|
|
closing_statistics();
|
2021-03-26 18:37:19 +00:00
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
|
|
|
|
if (input_fd != 0) {
|
|
|
|
close(input_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (output_fd != 1) {
|
|
|
|
close(output_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|