1#!/bin/sh
2# Exercise split's new --filter option.
3
4# Copyright (C) 2011-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_ split
21require_sparse_support_ # for 'truncate --size=$LARGE'
22xz --version || skip_ 'xz required'
23
24for total_n_lines in 5 3000 20000; do
25  seq $total_n_lines > in || framework_failure_
26  for i in 2 51 598; do
27
28    # Don't create too many files/processes.
29    # Starting 10k (or even "only" 1500) processes would take a long time,
30    # and would provide little added benefit.
31    case $i:$total_n_lines in 2:5);; *) continue;; esac
32
33    split -l$i --filter='xz -1 > $FILE.xz' in out- || fail=1
34    xz -dc out-* > out || fail=1
35    compare in out || fail=1
36    rm -f out*
37  done
38  rm -f in
39done
40
41# Show how --elide-empty-files works with --filter:
42# split does not run the command (and effectively elides the file)
43# only when the output to that command would have been empty.
44split -e -n 10 --filter='xz > $FILE.xz' /dev/null || fail=1
45returns_ 1 stat x?? 2>/dev/null || fail=1
46
47# Ensure this invalid combination is flagged
48returns_ 1 split -n 1/2 --filter='true' /dev/null 2>&1 || fail=1
49
50# Ensure SIGPIPEs sent by the children don't propagate back
51# where they would result in a non zero exit from split.
52yes | head -n200K | split -b1G --filter='head -c1 >/dev/null' || fail=1
53
54# Ensure that "endless" input is ignored when all filters finish
55for mode in '' 'r/'; do
56  in_file='-'
57  in_cmd='yes'
58  if test "$mode" = ''; then
59    in_file='zero.in'
60    in_cmd='true'
61    truncate -s10T "$FILE" || continue
62  fi
63  for N in 1 2; do
64    rm -f x??.n || framework_failure_
65    $in_cmd |
66     timeout 10 split --filter='head -c1 >$FILE.n' -n $mode$N $in_file || fail=1
67    # Also ensure we get appropriate output from each filter
68    seq 1 $N | tr '0-9' 1 > stat.exp
69    stat -c%s x??.n > stat.out || framework_failure_
70    compare stat.exp stat.out || fail=1
71  done
72done
73
74# Ensure that "endless" input _is_ processed for unbounded number of filters
75for buf in 1000 1000000; do
76  returns_ 124 timeout .5 sh -c \
77    "yes | split --filter='head -c1 >/dev/null' -b $buf" || fail=1
78done
79
80Exit $fail
81