4d3d8fdbd1783cf6523ead0c909a6863908c58a8
[platform/upstream/coreutils.git] / tests / du / 2g
1 #!/bin/sh
2 # Ensure that du can handle a 2GB file (i.e., a file of size 2^31 bytes)
3 # Before coreutils-5.93, on systems with a signed, 32-bit stat.st_blocks
4 # one of du's computations would overflow.
5
6 if test "$VERBOSE" = yes; then
7   set -x
8   du --version
9 fi
10
11 . $srcdir/../envvar-check
12
13 # Creating a 2GB file counts as `very expensive'.
14 . $srcdir/../very-expensive
15
16 pwd=`pwd`
17 t0=`echo "$0"|sed 's,.*/,,'`.tmp; tmp=$t0/$$
18 trap 'status=$?; cd $pwd; chmod -R u+rwx $t0; rm -rf $t0 && exit $status' 0
19 trap '(exit $?); exit $?' 1 2 13 15
20
21 framework_failure=0
22 mkdir -p $tmp || framework_failure=1
23 cd $tmp || framework_failure=1
24
25 if test $framework_failure = 1; then
26   echo "$0: failure in testing framework" 1>&2
27   (exit 1); exit 1
28 fi
29
30 fail=0
31
32 # Get number of free kilobytes on current partition, so we can
33 # skip this test if there is insufficient free space.
34
35 # This technique relies on the fact that the `Available' kilobyte
36 # count is the number just before the one with a trailing `%'.
37 free_kb=`df -kP .|tail -1|sed 's/ [0-9][0-9]*%.*//;s/ *$//;s/.* //'`
38 case "$free_kb" in
39   [0-9]*) ;;
40   *) echo "invalid size from df: $free_kb" 1>&2; (exit 77); exit 77;;
41 esac
42
43 # Require about 3GB free.
44 min_kb=3000000
45 test $min_kb -lt $free_kb ||
46 {
47   echo "$0: skipping this test:"
48   echo "too little free space on current partition: $free_kb (need $min_kb KB)" \
49     1>&2;
50   (exit 77); exit 77
51 }
52
53 big=big
54 rm -f $big
55 test -t 1 || printf 'creating a 2GB file...\n'
56 for i in `seq 100`; do
57   # Note: 2147483648 == 2^31. Print floor(2^31/100) per iteration.
58   printf %21474836s x >> $big || fail=1
59   # On the final iteration, append the remaining 48 bytes.
60   test $i = 100 && { printf %48s x >> $big || fail=1; }
61   test -t 1 && printf 'creating a 2GB file: %d%% complete\r' $i
62 done
63 echo
64
65 du -k $big > out1 || fail=1
66 rm -f $big
67 sed 's/^2[0-9][0-9][0-9][0-9][0-9][0-9] '$big'$/~2M/' out1 > out
68
69 cat <<\EOF > exp || fail=1
70 ~2M
71 EOF
72
73 cmp out exp || fail=1
74 test $fail = 1 && diff out exp 2> /dev/null
75
76 (exit $fail); exit $fail