Skip to content

Commit 97a33e7

Browse files
Donny9xiaoxiang781216
authored andcommitted
nshlib: support dd with conv=notrunc,nocreat
refs to: https://man7.org/linux/man-pages/man1/dd.1.html Signed-off-by: dongjiuzhu1 <[email protected]>
1 parent 28aa8a9 commit 97a33e7

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

nshlib/nsh_command.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ static const struct cmdmap_s g_cmdmap[] =
188188
#ifndef CONFIG_NSH_DISABLE_DD
189189
CMD_MAP("dd", cmd_dd, 3, 7,
190190
"if=<infile> of=<outfile> [bs=<sectsize>] [count=<sectors>] "
191-
"[skip=<sectors>] [seek=<sectors>] [verify]"),
191+
"[skip=<sectors>] [seek=<sectors>] [verify] [conv=<nocreat,notrunc>]"),
192192
#endif
193193

194194
#if defined(CONFIG_NET) && defined(CONFIG_NET_ROUTE) && !defined(CONFIG_NSH_DISABLE_DELROUTE)

nshlib/nsh_ddcmd.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ struct dd_s
7676
uint32_t nsectors; /* Number of sectors to transfer */
7777
uint32_t skip; /* The number of sectors skipped on input */
7878
uint32_t seek; /* The number of bytes skipped on output */
79+
int oflags; /* The open flags on output deivce */
7980
bool eof; /* true: The end of the input or output file has been hit */
80-
bool verify; /* true: Verify infile and outfile correctness */
8181
size_t sectsize; /* Size of one sector */
8282
size_t nbytes; /* Number of valid bytes in the buffer */
8383
FAR uint8_t *buffer; /* Buffer of data to write to the output file */
@@ -170,8 +170,7 @@ static inline int dd_infopen(FAR const char *name, FAR struct dd_s *dd)
170170

171171
static inline int dd_outfopen(FAR const char *name, FAR struct dd_s *dd)
172172
{
173-
dd->outfd = open(name, (dd->verify ? O_RDWR : O_WRONLY) |
174-
O_CREAT | O_TRUNC, 0644);
173+
dd->outfd = open(name, dd->oflags, 0644);
175174
if (dd->outfd < 0)
176175
{
177176
FAR struct nsh_vtbl_s *vtbl = dd->vtbl;
@@ -282,6 +281,7 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
282281
dd.vtbl = vtbl; /* For nsh_output */
283282
dd.sectsize = DEFAULT_SECTSIZE; /* Sector size if 'bs=' not provided */
284283
dd.nsectors = 0xffffffff; /* MAX_UINT32 */
284+
dd.oflags = O_WRONLY | O_CREAT | O_TRUNC;
285285

286286
/* If no IF= option is provided on the command line, then read
287287
* from stdin.
@@ -339,7 +339,18 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
339339
}
340340
else if (strncmp(argv[i], "verify", 6) == 0)
341341
{
342-
dd.verify = true;
342+
dd.oflags |= O_RDONLY;
343+
}
344+
else if (strncmp(argv[i], "conv=", 5) == 0)
345+
{
346+
if (strstr(argv[i], "nocreat") != NULL)
347+
{
348+
dd.oflags &= ~(O_CREAT | O_TRUNC);
349+
}
350+
else if (strstr(argv[i], "notrunc") != NULL)
351+
{
352+
dd.oflags &= ~O_TRUNC;
353+
}
343354
}
344355
}
345356

@@ -450,7 +461,7 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
450461
/ ((double)elapsed / USEC_PER_SEC)));
451462
#endif
452463

453-
if (ret == 0 && dd.verify)
464+
if (ret == 0 && (dd.oflags & O_RDONLY) != 0)
454465
{
455466
ret = dd_verify(infile, outfile, &dd);
456467
}

0 commit comments

Comments
 (0)