From 204f8c56d69a858eb3455da798c63f6477e1f3b7 Mon Sep 17 00:00:00 2001 From: Chris Sarra Date: Tue, 15 Sep 2020 10:10:20 -0700 Subject: Add ftruncate logic to handle non-regular files 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(-) (limited to 'toys') 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; -- cgit v1.2.3