[Toybox] [PATCH] Add ftruncate logic to handle non-regular files

Chris Sarra chrissarra at google.com
Tue Sep 15 10:10:20 PDT 2020


ftruncate was failing on device files, leading to whole dd.c failures.
This patch allows us to dump device files.
---
 toys/pending/dd.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/toys/pending/dd.c b/toys/pending/dd.c
index 7de91825..a7b7f5fd 100644
--- a/toys/pending/dd.c
+++ b/toys/pending/dd.c
@@ -224,7 +224,12 @@ void dd_main()
   if (!(TT.oflag & _DD_oflag_seek_bytes)) bs *= TT.out.sz;
   if (bs) {
     xlseek(TT.out.fd, bs, SEEK_CUR);
-    if (trunc && ftruncate(TT.out.fd, bs)) perror_exit("ftruncate");
+    if (trunc && ftruncate(TT.out.fd, bs)) {
+      struct stat st;
+      if (fstat(TT.out.fd, &st) < 0 || S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)) {
+        perror_exit("unexpected ftruncate failure");
+      }
+    }
   }
 
   unsigned long long bytes_left = TT.c_count;
-- 
2.28.0.618.gf4bc123cb7-goog



More information about the Toybox mailing list