summaryrefslogtreecommitdiff
path: root/tests/generic/001
blob: c59344e475d26d2b57cadfe208f2cefd5570a95d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
#
# FS QA Test No. 001
#
# Random file copier to produce chains of identical files so the head
# and the tail can be diff'd at the end of each iteration.
#
# Exercises creat, write and unlink for a variety of directory sizes, and
# checks for data corruption.
#
# run [config]
#
# config has one line per file with filename and byte size, else use
# the default one below.
#
. ./common/preamble
_begin_fstest rw dir udf auto quick

# Import common functions.
. ./common/filter

status=1
done_cleanup=false
_register_cleanup "_cleanup; rm -f $tmp.*"

# real QA test starts here
_supported_fs generic
_require_test

verbose=true
verify=$here/verify_fill

if [ $# -eq 0 ]
then
    # use the default config
    #
    cat <<End-of-File >$tmp.config
# pathname	size in bytes
#
small		10
big		102400
sub/small	10
sub/big		102400
#
sub/a		1
sub/b		2
sub/c		4
sub/d		8
sub/e		16
sub/f		32
sub/g		64
sub/h		128
sub/i		256
sub/j		512
sub/k		1024
sub/l		2048
sub/m		4096
sub/n		8192
#
sub/a00		100
sub/b00		200
sub/c00		400
sub/d00		800
sub/e00		1600
sub/f00		3200
sub/g00		6400
sub/h00		12800
sub/i00		25600
sub/j00		51200
sub/k00		102400
sub/l00		204800
sub/m00		409600
sub/n00		819200
#
sub/a000	1000
sub/e000	16000
sub/h000	128000
sub/k000	1024000
End-of-File
elif [ $# -eq 1 ]
then
    if [ -f $1 ]
    then
	cp $1 $tmp.config
    else
	echo "Error: cannot open config \"$1\""
	exit 1
    fi
else
    echo "Usage: run [config]"
    exit 1
fi

ncopy=200		# number of file copies in the chain step
udf_fsize=20240		# number of sectors for UDF

_setup()
{
    if mkdir -p $TEST_DIR/$$
    then
	:
    else
	echo "Error: cannot mkdir \"$TEST_DIR/$$\""
	exit 1
    fi
    cd $TEST_DIR/$$

    $verbose && echo -n "setup "
    sed -e '/^#/d' $tmp.config \
    | while read file nbytes
    do
	dir=`dirname $file`
	if [ "$dir" != "." ]
	then
	    if [ ! -d $dir ]
	    then
		if mkdir $dir
		then
		    :
		else
		    $verbose && echo
		    echo "Error: cannot mkdir \"$dir\""
		    exit 1
		fi
	    fi
	fi
	rm -f $file
	if $here/src/fill $file $file $nbytes
	then
	    :
	else
	    $verbose && echo
	    echo "Error: cannot create \"$file\""
	    exit 1
	fi
	$verbose && echo -n "."
    done
    $verbose && echo
}

_mark_iteration()
{
    $verbose && echo -n "mark_iteration "
    sed -e '/^#/d' $tmp.config \
    | while read file nbytes
    do
	if [ ! -f $file ]
	then
	    $verbose && echo
	    echo "Error: $file vanished!"
	    touch $tmp.bad
	    continue
	fi
	sed -e "s/ [0-9][0-9]* / $1 /" <$file >$file.tmp
	mv $file.tmp $file
	$verbose && echo -n "."
    done
    $verbose && echo
}

# for each file, make a number of copies forming a chain like foo.0,
# foo.1, foo.2, ... foo.N
#
# files are chosen at random, so the lengths of the chains are different
#
# then rename foo.N to foo.last and remove all of the other files in
# the chain
#
_chain()
{
    $AWK_PROG -v full_file=$seqres.full -v verify=$verify <$tmp.config '
BEGIN	{ nfile = 0 }
/^#/	{ next }
	{ file[nfile] = $1
	  size[nfile] = $2
	  link[nfile] = 0
	  nfile++
	  total_size += $2
	}
END	{ srand('$iter')
	  for (i=0; i < '$ncopy'; i++) {
	    # choose a file at random, and add one copy to that chain
	    j = -1
	    while (j < 0 || j >= nfile)
		j = int(rand() * nfile)
	    if (link[j] == 0) {
		# previous should already exist and next one should not exist
		printf "if [ ! -f %s ]; then echo \"%s missing!\"; exit; fi\n",file[j],file[j]
		printf "if [ -f %s.0 ]; then echo \"%s.0 already present!\"; exit; fi\n",file[j],file[j]
		printf "cp %s %s.0 || exit 1\n",file[j],file[j]
		printf "ls -i %s.0\n", file[j] >full_file;
		total_size += size[j]
		printf "# total size = %d\n", total_size 
	    }
	    else {
		# previous should already exist and next one should not exist
		printf "if [ ! -f %s.%d ]; then echo \"%s.%d missing!\"; exit; fi\n",file[j],link[j]-1,file[j],link[j]-1
		printf "if [ -f %s.%d ]; then echo \"%s.%d already present!\"; exit; fi\n",file[j],link[j],file[j],link[j]
		printf "cp %s.%d %s.%d || exit 1\n",file[j],link[j]-1,file[j],link[j]
		printf "ls -i %s.%d\n", file[j], link[j] >full_file;
		total_size += size[j]
		printf "# total size = %d\n", total_size 
	    }
	    link[j]++
	  }
	  # close all the chains, 
	  # if have at least one copy then move the last copy to "file[j].last"
	  # and remove all of the other files except the head of the chain
	  for (j=0; j<nfile; j++) {
	    if (link[j] > 0) {
		printf "mv %s.%d %s.last\n",file[j],link[j]-1,file[j]
		printf "ls -i %s.last\n", file[j] >full_file;
	    }
	    for (i=0; i<link[j]-1; i++) {
		printf "rm -f %s.%d\n",file[j],i
	    }
	  }
	}' \
	| tee -a $seqres.full | sh
}

_check()
{
    rm -f $tmp.bad
    $verbose && echo -n "check "
    sed -e '/^#/d' $tmp.config \
    | while read file nbytes
    do
	# the file is never removed so it should exist
	if [ ! -f $file ]
	then
	    $verbose && echo
	    echo "Error: $file vanished!"
	    touch $tmp.bad
	    continue
	fi
	# checks that the file and its last copy are the same
	if [ -f $file.last ]
	then
	    if cmp $file $file.last >/dev/null 2>&1
	    then
		$verbose && echo -n "."
	    else
		$verbose && echo
		echo "Error: corruption for $file ..."
		diff -c $file $file.last
		touch $tmp.bad
	    fi
	else
	    $verbose && echo -n "."
	fi
    done
    $verbose && echo
}

# Override the default cleanup function.
_cleanup()
{
    # cleanup
    #
    if $done_cleanup
    then
	:
    elif [ $status -eq 0 ]
    then
	$verbose && echo "cleanup"
	cd /
	rm -rf $TEST_DIR/$$
	done_cleanup=true
    fi
}

status=0
_cleanup
status=1
done_cleanup=false

_setup

# do the test
#
for iter in 1 2 3 4 5
do
    echo -n "iter $iter chain ... "
    echo "iter $iter" >> $seqres.full
    _chain
    _check
    if [ -f $tmp.bad ]
    then
	echo "Fatal error: test abandoned without changes"
	exit 1
    fi
done

status=0
exit