dd:simplified cmd_dd code steps

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
This commit is contained in:
anjiahao 2022-11-08 15:22:11 +08:00 committed by Xiang Xiao
parent da30a87b81
commit c2f07a4d5a

View File

@ -69,7 +69,6 @@ struct dd_s
int infd; /* File descriptor of the input device */ int infd; /* File descriptor of the input device */
int outfd; /* File descriptor of the output device */ int outfd; /* File descriptor of the output device */
uint32_t nsectors; /* Number of sectors to transfer */ 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 */ uint32_t skip; /* The number of sectors skipped on input */
bool eof; /* true: The end of the input or output file has been hit */ bool eof; /* true: The end of the input or output file has been hit */
uint16_t sectsize; /* Size of one sector */ uint16_t sectsize; /* Size of one sector */
@ -202,6 +201,7 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
uint64_t elapsed; uint64_t elapsed;
uint64_t total; uint64_t total;
#endif #endif
uint32_t sector = 0;
int ret = ERROR; int ret = ERROR;
int i; int i;
@ -303,8 +303,18 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
clock_gettime(CLOCK_MONOTONIC, &ts0); clock_gettime(CLOCK_MONOTONIC, &ts0);
#endif #endif
dd.sector = 0; if (dd.skip)
while (!dd.eof && dd.nsectors > 0) {
ret = lseek(dd.infd, dd.skip * dd.sectsize, SEEK_SET);
if (ret < -1)
{
nsh_error(vtbl, g_fmtcmdfailed, g_dd, "lseek", NSH_ERRNO);
ret = ERROR;
goto errout_with_outf;
}
}
while (!dd.eof && sector < dd.nsectors)
{ {
/* Read one sector from from the input */ /* Read one sector from from the input */
@ -318,31 +328,17 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
if (!dd.eof) 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 */ /* Write one sector to the output file */
if (dd.sector >= dd.skip) ret = dd_write(&dd);
if (ret < 0)
{ {
ret = dd_write(&dd); goto errout_with_outf;
if (ret < 0)
{
goto errout_with_outf;
}
/* Decrement to show that a sector was written */
dd.nsectors--;
} }
/* Increment the sector number */ /* Increment the sector number */
dd.sector++; sector++;
} }
} }
@ -355,7 +351,7 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
elapsed -= (((uint64_t)ts0.tv_sec * NSEC_PER_SEC) + ts0.tv_nsec); elapsed -= (((uint64_t)ts0.tv_sec * NSEC_PER_SEC) + ts0.tv_nsec);
elapsed /= NSEC_PER_USEC; /* usec */ elapsed /= NSEC_PER_USEC; /* usec */
total = ((uint64_t)dd.sector * (uint64_t)dd.sectsize); total = ((uint64_t)sector * (uint64_t)dd.sectsize);
nsh_output(vtbl, "%llu bytes copied, %u usec, ", nsh_output(vtbl, "%llu bytes copied, %u usec, ",
total, (unsigned int)elapsed); total, (unsigned int)elapsed);