From 99512b90dba9c671c2b799c50cde5c5a31c7677e Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Wed, 10 Jan 2018 07:24:24 -0600 Subject: [PATCH] apps/nshlib: truncate command will now use ftruncate (vs. truncate) if it had to create the file with creat(). --- nshlib/nsh_fscmds.c | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/nshlib/nsh_fscmds.c b/nshlib/nsh_fscmds.c index fe60d9d75..1542fead6 100644 --- a/nshlib/nsh_fscmds.c +++ b/nshlib/nsh_fscmds.c @@ -1834,35 +1834,53 @@ int cmd_truncate(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) { nsh_output(vtbl, g_fmtcmdfailed, argv[0], "stat", NSH_ERRNO); ret = ERROR; - goto errout_with_fullpath; } + else + { + /* We successfully performed the create and now have a zero- + * length file. Perform the truncation to extend the allocation + * (unless we really wanted a zero-length file). + */ - close(fd); + ret = OK; + if (length > 0) + { + /* Extend the file to length */ + + ret = ftruncate(fd, length); + if (ret < 0) + { + nsh_output(vtbl, g_fmtcmdfailed, argv[0], "ftruncate", + NSH_ERRNO); + } + } + + close(fd); + } } else { nsh_output(vtbl, g_fmtcmdfailed, argv[0], "stat", NSH_ERRNO_OF(errval)); ret = ERROR; - goto errout_with_fullpath; } } else if (!S_ISREG(buf.st_mode)) { nsh_output(vtbl, g_fmtarginvalid, argv[0]); ret = ERROR; - goto errout_with_fullpath; } - - /* Everything looks good... perform the truncation */ - - ret = truncate(fullpath, length); - if (ret < 0) + else { - nsh_output(vtbl, g_fmtcmdfailed, argv[0], "truncate", NSH_ERRNO); + /* Everything looks good... perform the truncation */ + + ret = truncate(fullpath, length); + if (ret < 0) + { + nsh_output(vtbl, g_fmtcmdfailed, argv[0], "truncate", NSH_ERRNO); + } } -errout_with_fullpath: nsh_freefullpath(fullpath); return ret; }