-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsh_ddcmd.c
408 lines (330 loc) · 10.8 KB
/
nsh_ddcmd.c
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/****************************************************************************
* apps/nshlib/nsh_ddcmd.c
*
* Copyright (C) 2008-2009, 2012, 2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <debug.h>
#include <errno.h>
#include <time.h>
#include "nsh.h"
#include "nsh_console.h"
#ifndef CONFIG_NSH_DISABLE_DD
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* If no sector size is specified with BS=, then the following default value
* is used.
*/
#define DEFAULT_SECTSIZE 512
/* At present, piping of input and output are not support, i.e., both of=
* and if= arguments are required.
*/
#undef CAN_PIPE_FROM_STD
/****************************************************************************
* Private Types
****************************************************************************/
struct dd_s
{
FAR struct nsh_vtbl_s *vtbl;
int infd; /* File descriptor of the input device */
int outfd; /* File descriptor of the output device */
uint32_t nsectors; /* Number of sectors to transfer */
uint32_t sector; /* The current sector number */
uint32_t skip; /* The number of sectors skipped on input */
bool eof; /* true: The end of the input or output file has been hit */
uint16_t sectsize; /* Size of one sector */
uint16_t nbytes; /* Number of valid bytes in the buffer */
uint8_t *buffer; /* Buffer of data to write to the output file */
};
/****************************************************************************
* Private Data
****************************************************************************/
static const char g_dd[] = "dd";
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: dd_write
****************************************************************************/
static int dd_write(FAR struct dd_s *dd)
{
FAR uint8_t *buffer = dd->buffer;
uint16_t written ;
ssize_t nbytes;
/* Is the out buffer full (or is this the last one)? */
written = 0;
do
{
nbytes = write(dd->outfd, buffer, dd->sectsize - written);
if (nbytes < 0)
{
FAR struct nsh_vtbl_s *vtbl = dd->vtbl;
nsh_error(vtbl, g_fmtcmdfailed, g_dd, "write", NSH_ERRNO_OF(-nbytes));
return ERROR;
}
written += nbytes;
buffer += nbytes;
}
while (written < dd->sectsize);
return OK;
}
/****************************************************************************
* Name: dd_read
****************************************************************************/
static int dd_read(FAR struct dd_s *dd)
{
FAR uint8_t *buffer = dd->buffer;
ssize_t nbytes;
dd->nbytes = 0;
do
{
nbytes = read(dd->infd, buffer, dd->sectsize - dd->nbytes);
if (nbytes < 0)
{
FAR struct nsh_vtbl_s *vtbl = dd->vtbl;
nsh_error(vtbl, g_fmtcmdfailed, g_dd, "read", NSH_ERRNO_OF(-nbytes));
return ERROR;
}
dd->nbytes += nbytes;
buffer += nbytes;
}
while (dd->nbytes < dd->sectsize && nbytes > 0);
dd->eof |= (dd->nbytes == 0);
return OK;
}
/****************************************************************************
* Name: dd_infopen
****************************************************************************/
static inline int dd_infopen(FAR const char *name, FAR struct dd_s *dd)
{
dd->infd = open(name, O_RDONLY);
if (dd->infd < 0)
{
FAR struct nsh_vtbl_s *vtbl = dd->vtbl;
nsh_error(vtbl, g_fmtcmdfailed, g_dd, "open", NSH_ERRNO);
return ERROR;
}
return OK;
}
/****************************************************************************
* Name: dd_outfopen
****************************************************************************/
static inline int dd_outfopen(FAR const char *name, FAR struct dd_s *dd)
{
dd->outfd = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (dd->outfd < 0)
{
FAR struct nsh_vtbl_s *vtbl = dd->vtbl;
nsh_error(vtbl, g_fmtcmdfailed, g_dd, "open", NSH_ERRNO);
return ERROR;
}
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: cmd_dd
****************************************************************************/
int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
struct dd_s dd;
FAR char *infile = NULL;
FAR char *outfile = NULL;
#ifdef CONFIG_NSH_CMDOPT_DD_STATS
struct timespec ts0;
struct timespec ts1;
uint64_t elapsed;
uint64_t total;
#endif
int ret = ERROR;
int i;
/* Initialize the dd structure */
memset(&dd, 0, sizeof(struct dd_s));
dd.vtbl = vtbl; /* For nsh_output */
dd.sectsize = DEFAULT_SECTSIZE; /* Sector size if 'bs=' not provided */
dd.nsectors = 0xffffffff; /* MAX_UINT32 */
/* If no IF= option is provided on the command line, then read
* from stdin.
*/
#ifdef CAN_PIPE_FROM_STD
dd->infd = 0; /* stdin */
#endif
/* If no OF= option is provided on the command line, then write
* to stdout.
*/
#ifdef CAN_PIPE_FROM_STD
dd->outfd = 1; /* stdout */
#endif
/* Parse command line parameters */
for (i = 1; i < argc; i++)
{
if (strncmp(argv[i], "if=", 3) == 0)
{
if (infile != NULL)
{
free(infile);
}
infile = nsh_getfullpath(vtbl, &argv[i][3]);
}
else if (strncmp(argv[i], "of=", 3) == 0)
{
if (outfile != NULL)
{
free(outfile);
}
outfile = nsh_getfullpath(vtbl, &argv[i][3]);
}
else if (strncmp(argv[i], "bs=", 3) == 0)
{
dd.sectsize = atoi(&argv[i][3]);
}
else if (strncmp(argv[i], "count=", 6) == 0)
{
dd.nsectors = atoi(&argv[i][6]);
}
else if (strncmp(argv[i], "skip=", 5) == 0)
{
dd.skip = atoi(&argv[i][5]);
}
}
#ifndef CAN_PIPE_FROM_STD
if (infile == NULL || outfile == NULL)
{
nsh_error(vtbl, g_fmtargrequired, g_dd);
goto errout_with_paths;
}
#endif
/* Allocate the I/O buffer */
dd.buffer = malloc(dd.sectsize);
if (!dd.buffer)
{
nsh_error(vtbl, g_fmtcmdoutofmemory, g_dd);
goto errout_with_paths;
}
/* Open the input file */
ret = dd_infopen(infile, &dd);
if (ret < 0)
{
goto errout_with_paths;
}
/* Open the output file */
ret = dd_outfopen(outfile, &dd);
if (ret < 0)
{
goto errout_with_inf;
}
/* Then perform the data transfer */
#ifdef CONFIG_NSH_CMDOPT_DD_STATS
#ifdef CONFIG_CLOCK_MONOTONIC
clock_gettime(CLOCK_MONOTONIC, &ts0);
#else
clock_gettime(CLOCK_REALTIME, &ts0);
#endif
#endif
dd.sector = 0;
while (!dd.eof && dd.nsectors > 0)
{
/* Read one sector from from the input */
ret = dd_read(&dd);
if (ret < 0)
{
goto errout_with_outf;
}
/* Has the incoming data stream ended? */
if (!dd.eof)
{
/* Pad with zero if necessary (at the end of file only) */
for (i = dd.nbytes; i < dd.sectsize; i++)
{
dd.buffer[i] = 0;
}
/* Write one sector to the output file */
if (dd.sector >= dd.skip)
{
ret = dd_write(&dd);
if (ret < 0)
{
goto errout_with_outf;
}
/* Decrement to show that a sector was written */
dd.nsectors--;
}
/* Increment the sector number */
dd.sector++;
}
}
ret = OK;
#ifdef CONFIG_NSH_CMDOPT_DD_STATS
#ifdef CONFIG_CLOCK_MONOTONIC
clock_gettime(CLOCK_MONOTONIC, &ts1);
#else
clock_gettime(CLOCK_REALTIME, &ts1);
#endif
elapsed = (((uint64_t)ts1.tv_sec * NSEC_PER_SEC) + ts1.tv_nsec);
elapsed -= (((uint64_t)ts0.tv_sec * NSEC_PER_SEC) + ts0.tv_nsec);
elapsed /= NSEC_PER_MSEC; /* msec */
total = ((uint64_t)dd.sector * (uint64_t)dd.sectsize);
nsh_output(vtbl, "%llu bytes copied, %u msec, ",
total, (unsigned int)elapsed);
nsh_output(vtbl, "%u KB/s\n" ,
(unsigned int)((double)total / (double)elapsed));
#endif
errout_with_outf:
close(dd.outfd);
errout_with_inf:
close(dd.infd);
free(dd.buffer);
errout_with_paths:
if (infile)
{
nsh_freefullpath(infile);
}
if (outfile)
{
nsh_freefullpath(outfile);
}
return ret;
}
#endif /* !CONFIG_NSH_DISABLE_DD */