1#!/bin/sh
2# test dd reblocking vs. bs=
3
4# Copyright (C) 2008-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ dd
21
22# 2 short reads -> 1 full write + 1 partial write
23cat <<\EOF > exp-reblock || framework_failure_
240+2 records in
251+1 records out
264 bytes copied
27EOF
28
29# 2 short reads -> 2 partial writes
30cat <<\EOF > exp-no-reblock || framework_failure_
310+2 records in
320+2 records out
334 bytes copied
34EOF
35
36
37# Use a fifo rather than a pipe in the tests below
38# so that the producer (printf subshell) will wait
39# until the consumer (dd) opens the fifo therefore
40# increasing the chance that dd will read the data
41# from each printf separately.
42mkfifo_or_skip_ dd.fifo
43
44dd_reblock_1()
45{
46  local delay="$1"
47
48  # ensure that dd reblocks when bs= is not specified
49  dd ibs=3 obs=3 if=dd.fifo > out 2> err&
50  (printf 'ab'; sleep $delay; printf 'cd') > dd.fifo
51  wait #for dd to complete
52  sed 's/,.*//' err > k && mv k err
53  compare exp-reblock err
54}
55
56retry_delay_ dd_reblock_1 .1 6 || fail=1
57
58dd_reblock_2()
59{
60  local delay="$1"
61
62  # Demonstrate that bs=N supersedes even following ibs= and obs= settings.
63  dd bs=3 ibs=1 obs=1 if=dd.fifo > out 2> err&
64  (printf 'ab'; sleep $delay; printf 'cd') > dd.fifo
65  wait #for dd to complete
66  sed 's/,.*//' err > k && mv k err
67  compare exp-no-reblock err
68}
69
70retry_delay_ dd_reblock_2 .1 6 || fail=1
71
72Exit $fail
73