forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dd.cpp
282 lines (250 loc) Β· 8.31 KB
/
dd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* Copyright (c) 2021, JΓ‘nos TΓ³th <[email protected]>
* Copyright (c) 2023, Karol Kosek <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/ByteString.h>
#include <AK/CharacterTypes.h>
#include <AK/NumberFormat.h>
#include <AK/Optional.h>
#include <AK/Vector.h>
#include <LibCore/ElapsedTimer.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
char const* usage = "usage:\n"
"\tdd <options>\n"
"options:\n"
"\tif=<file>\tinput file (default: stdin)\n"
"\tof=<file>\toutput file (default: stdout)\n"
"\tbs=<size>\tblocks size may be followed by multiplicate suffixes: k=1024, M=1024*1024, G=1024*1024*1024 (default: 512)\n"
"\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"
"\tskip=<size>\tskip <size> blocks at start of input (default: 0)\n"
"\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
};
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;
Core::ElapsedTimer timer { Core::TimerType::Precise };
} statistics;
static void closing_statistics()
{
if (statistics.status == None)
return;
warnln("{}+{} blocks in", statistics.total_blocks_in, statistics.partial_blocks_in);
warnln("{}+{} blocks out", statistics.total_blocks_out, statistics.partial_blocks_out);
if (statistics.status != Noxfer) {
auto elapsed_time = statistics.timer.elapsed_time();
String copy_speed = "INF B/s"_string;
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);
}
}
static StringView split_at_equals(StringView argument)
{
auto values = argument.split_view('=', SplitBehavior::KeepEmpty);
if (values.size() < 2) {
warnln("Unable to parse: {}", argument);
return {};
} else {
return values[1];
}
}
static int handle_io_file_arguments(int& fd, int flags, StringView argument)
{
auto value = split_at_equals(argument);
if (value.is_empty()) {
return -1;
}
fd = open(value.to_byte_string().characters(), flags, 0666);
if (fd == -1) {
warnln("Unable to open: {}", value);
return -1;
} else {
return 0;
}
}
static int handle_size_arguments(size_t& numeric_value, StringView argument)
{
auto value = split_at_equals(argument);
if (value.is_empty()) {
return -1;
}
unsigned suffix_multiplier = 1;
auto suffix = value[value.length() - 1];
switch (to_ascii_lowercase(suffix)) {
case 'k':
suffix_multiplier = KiB;
value = value.substring_view(0, value.length() - 1);
break;
case 'm':
suffix_multiplier = MiB;
value = value.substring_view(0, value.length() - 1);
break;
case 'g':
suffix_multiplier = GiB;
value = value.substring_view(0, value.length() - 1);
break;
}
Optional<unsigned> numeric_optional = value.to_number<unsigned>();
if (!numeric_optional.has_value()) {
warnln("Invalid size-value: {}", value);
return -1;
}
numeric_value = numeric_optional.value() * suffix_multiplier;
if (numeric_value < 1) {
warnln("Invalid size-value: {}", numeric_value);
return -1;
} else {
return 0;
}
}
static int handle_status_arguments(Status& status, StringView argument)
{
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 {
warnln("Unknown status: {}", value);
return -1;
}
}
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
int input_fd = 0;
int input_flags = O_RDONLY;
int output_fd = 1;
int output_flags = O_CREAT | O_WRONLY | O_TRUNC;
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;
for (size_t a = 1; a < arguments.strings.size(); a++) {
auto argument = arguments.strings[a];
if (argument == "--help") {
out("{}", usage);
return 0;
} else if (argument.starts_with("if="sv)) {
if (handle_io_file_arguments(input_fd, input_flags, argument) < 0) {
return 1;
}
} else if (argument.starts_with("of="sv)) {
if (handle_io_file_arguments(output_fd, output_flags, argument) < 0) {
return 1;
}
} else if (argument.starts_with("bs="sv)) {
if (handle_size_arguments(block_size, argument) < 0) {
return 1;
}
} else if (argument.starts_with("count="sv)) {
if (handle_size_arguments(count, argument) < 0) {
return 1;
}
} else if (argument.starts_with("seek="sv)) {
if (handle_size_arguments(seek, argument) < 0) {
return 1;
}
} else if (argument.starts_with("skip="sv)) {
if (handle_size_arguments(skip, argument) < 0) {
return 1;
}
} else if (argument.starts_with("status="sv)) {
if (handle_status_arguments(statistics.status, argument) < 0) {
return 1;
}
} else {
warn("{}", usage);
return 1;
}
}
if ((buffer = (uint8_t*)malloc(block_size)) == nullptr) {
warnln("Unable to allocate {} bytes for the buffer.", block_size);
return -1;
}
if (seek > 0) {
if (lseek(output_fd, seek * block_size, SEEK_SET) < 0) {
warnln("Unable to seek {} bytes.", seek * block_size);
return -1;
}
}
TRY(Core::System::signal(SIGINT, [](int status) {
closing_statistics();
exit(status);
}));
statistics.timer.start();
while (1) {
nread = read(input_fd, buffer, block_size);
if (nread < 0) {
warnln("Cannot read from the input.");
break;
} else if (nread == 0) {
break;
} else {
if ((size_t)nread != block_size) {
statistics.partial_blocks_in++;
} else {
statistics.total_blocks_in++;
}
if (statistics.partial_blocks_in + statistics.total_blocks_in <= skip) {
continue;
}
nwritten = write(output_fd, buffer, nread);
if (nwritten < 0) {
warnln("Cannot write to the output.");
break;
} else if (nwritten == 0) {
break;
} else {
if ((size_t)nwritten < block_size) {
statistics.partial_blocks_out++;
} else {
statistics.total_blocks_out++;
}
statistics.total_bytes_copied += nwritten;
if (count > 0 && (statistics.partial_blocks_out + statistics.total_blocks_out) >= count) {
break;
}
}
}
}
closing_statistics();
free(buffer);
if (input_fd != 0) {
close(input_fd);
}
if (output_fd != 1) {
close(output_fd);
}
return 0;
}