summaryrefslogtreecommitdiff
path: root/test/lsbfuncs.sh
blob: 7ff8fb79676137d1849f4f51f332a107e6f85af5 (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# lsbfuncs.sh : LSB test suite specific common shell functions
#
##########################################################################
# (C) Copyright 1998-2001 The Open Group
#
# All rights reserved.  No part of this source code may be reproduced,
# stored in a retrieval system, or transmitted, in any form or by any
# means, electronic, mechanical, photocopying, recording or otherwise,
# except as stated in the end-user licence agreement, without the prior
# permission of the copyright owners.
#
# X/Open and the 'X' symbol are trademarks of X/Open Company Limited in
# the UK and other countries.
#
#	PROJECT:	LSB-FHS
#	PRODUCT:	LSB.fhs/SRC/lsblib/lsbfuncs.sh
#	AUTHOR:		Andrew Josey, The Open Group
#	DATE CREATED:	21 Dec 1998
##########################################################################

# This is $Revision: 1.6 $
#
# $Log: lsbfuncs.sh,v $
# Revision 1.6  2002/01/17 02:21:24  cyeoh
# modifies lsb_test_symlink soboth symlink "from" and "to" are followed
# for symlinks. This changes the behaviour such that if the "to" is
# a symlink but "from" does point to what "to" points to then it returns
# true.
#
# Revision 1.5  2001/07/20 11:49:47  ajosey
# add further diagnostics
#
# Revision 1.4  2001/07/20 11:27:33  ajosey
# add in more diagnostics to lsb_test_file
#
# Revision 1.3  2001/07/20 11:20:08  ajosey
# add in diagnostic to lsb_test_file
#
# Revision 1.2  2001/07/18 06:58:55  ajosey
# add header, and cvs revision stuff
#
#
#Current contents
#	lsb_setarch()
#	lsb_test_symlink()
#	lsb_test_dir()
#	lsb_test_dir_searchable()
#	lsb_test_file()
#	lsb_test_exec
#	lsb_test_device_by_name()
# These functions return the following codes
#
# 	PASS=0
# 	FAIL=1
# 	WARNING=2
# 	UNRESOLVED=3
#

# lsb_setarch: sets the $lsb_arch environment variable to the
# current architecture
lsb_setarch()
{
	lsb_arch=`uname -m`
	case $lsb_arch in 
		i?86)  lsb_arch=i386;;  
		sparc) lsb_arch=sparc;; 
		*) lsb_arch=unknown;;
	esac
}

# lsb_test_symlink symlink destination
#	Argument 1: symlink from name
#	Argument 2: to name
# Returns 
#	0 = PASS, yes symlink points to destination
#	1 = FAIL, 
#	3 = UNRESOLVED,Setup problem 
#
# On a failure or setup problem a diagnostic message is omitted
#
# Calls lsblib routines:
#	lsb_realpath, lsb_issymlink
#
lsb_test_symlink() {
	func_name=lsb_is_symlink
	# validate that we have two arguments
	if test $# -lt 2 
	then
	 	echo "$func_name: Missing argument"; return 3 # unresolved
	fi
	from="$1"
	to="$2"

	# cannot use test -L as not in POSIX.2 so call wrapper
	# that does an lstat()
	lsb_issymlink $from 
	rval=$?
	if test $rval -eq 3 # unresolved
	then
		return 3 # unresolved
	fi	
	if  ! test $rval -eq 0
	then
	    if ! test -e $from 
	    then
		echo "$from does not exist (expected symlink to $to)"
		return 1 # fail
	    else
		echo "$from expected to be  symlink to $to, returned non-symlink"
		return 1 # fail
	    fi
	else
	# its a symlink so lets validate where it points to
	# need to call realpath , which is a c program wrapping on realpath3c
		pathptr=`lsb_realpath $from`
		if test $? -ne 0
		then
			return 3 # unresolved
		fi
                pathptr_to=`lsb_realpath $to`
		if test $? -ne 0
		then
                        # Destination does not point anywhere
			return 3 # unresolved
		fi
		if test "$pathptr" = "$pathptr_to"
		then
			return 0 # pass
		else
			echo "$from expected to be  symlink to $to, pointed to $pathptr"
			return 1 # fail
		fi
	fi
}

# lsb_test_dir filename
# test for presence of a directory called filename
# Argument: filename to test if its a directory
# Returns: 0 PASS , it's a directory
#          1 FAIL, its  not a directory or a symlink
#          2 WARNING its a symlink 
#          3 UNRESOLVED error
lsb_test_dir() {
	func_name=lsb_test_dir
	# validate that we have one arguments
	if test $# -lt 1 
	then
		echo "$func_name: Missing argument"
		return 3 # unresolved
	fi
	
	_fname=$1
	# if it does not exist then fail
	if ! test -e $_fname
	then
		tet_infoline "$_fname: directory not found"
		return 1 # fail
	fi

	# since test -d will follow the symlink, we should check
	# for a symlink using the lstat wrapper first
	lsb_issymlink $_fname 
	rval=$?
	if test $rval -eq 3 # unresolved
	then
		return 3 # unresolved
	fi	
	if  ! test $rval -eq 0
	then
		# not a symlink 
		if test -d  $_fname
		then
			# success
			return 0 # pass
		else
			# not a symlink and not a directory
			tet_infoline "$_fname: not a directory or a symlink"
			return 1 # fail
		fi
	else
		# warning , its a symlink when we expected a directory
		return 2 # warning
	fi
}
# lsb_test_dir_searchable filename
# test for presence of a directory and that it can be searched
# Argument: filename 
# Returns: 0 PASS , it's a file and it exists
#          1 FAIL, its  not a file or a symlink
#          3 UNRESOLVED error
lsb_test_dir_searchable() {
	func_name=lsb_test_dir_searchable
	# validate that we have one arguments
	if test $# -lt 1 
	then
		echo "$func_name: Missing argument"
		return 3
	fi
	lsb_test_dir $1
	funcret=$?
	if test $funcret -eq 0 -o $funcret -eq 2
	then
		( ls $1 ) > /dev/null 2> _lsb.stderr
		if test $? -ne 0
		then
			echo "$func_name: expected be able to search directory $1, got an error"
			if test -s _lsb.stderr
			then
				cat _lsb.stderr
				rm _lsb.stderr
			fi
		else
			rm _lsb.stderr
			return 0
		fi

	else
		return $funcret
	fi
	

	
}
# lsb_test_file filename
# test for presence of a filename
# Argument: filename 
# Returns: 0 PASS , it's a file and it exists
#          1 FAIL, its  not a file or a symlink
#          2 WARNING its a symlink 
#          3 UNRESOLVED error
lsb_test_file() {
	func_name=lsb_test_file
	# validate that we have one arguments
	if test $# -lt 1 
	then
		echo "$func_name: Missing argument"
		return 3
	fi
	_fname=$1
	# if it does not exist then fail
	if ! test -e $_fname
	then
		tet_infoline "$_fname: file not found"
		return 1
	fi
	# since test -f will follow the symlink, we should check
	# for a symlink using the lstat wrapper first
	lsb_issymlink $_fname 
	rval=$?
	if test $rval -eq 3
	then
		return 3
	fi	
	if  ! test $rval -eq 0
	then
		# not a symlink 
		if test -f  $_fname
		then
			# success
			return 0
		else
			# not a symlink and not a file
			tet_infoline "$_fname: not a file or a symlink"
			return 1
		fi
	else
		# warning , its a symlink when we expected a file
		tet_infoline "$_fname: found a symlink"
		return 2
	fi
}
	

# lsb_test_exec:
# check that a utility can be executed
# if privilege is needed then an lsb_execwithpriv tool is used.
#
# if a utility needs an argument such as a file to parse that should be
# supplied.
# Returns:
# 	0 - PASS
# 	1 - Fail
#	3 - Unresolved
# 

lsb_test_exec()
{

test  $# -eq 0  && return 3 # unresolved

_ExecWithPriv=""
if test "$1" = "lsb_execwithpriv"
then
        _ExecWithPriv="$1" ; shift
        case "$1"
        in
                *) _ExecWithPriv="$_ExecWithPriv IGNORED_ARG" ; shift ;;
        esac
fi

rm -f _ltexec.stderr > /dev/null 2>&1
#( $_ExecWithPriv exec "$@" ) > /dev/null 2> _ltexec.stderr
( $_ExecWithPriv exec "$@" ) 
return $?

#if test -s _ltexec.stderr 
#then
#	cat _ltexec.stderr
#        rm -f _ltexec.stderr
#        return 1 # fail
#else
#        rm -f _ltexec.stderr
#        return 0 # pass
#fi
}

# lsb_test_device_by_name  name b|c major#  minor#
#
# test whether a device exists, the type and the expected major and
# minor number
#
# Calls :
#	lsb_devstat a wrapper to stat that when called with a device
#	name as the first argument outputs four space separated strings
#	device-name type majorno minorno
#
# Returns:
#	0 PASS
#	1 FAIL
#	3 Unresolved

lsb_test_device_by_name() {
	func_name=lsb_test_device_by_name
	# validate that we have one arguments
	if test $# -lt 4 
	then
		echo "$func_name: Missing argument(s)"
		return 3 # unresolved
	fi
	_devname=$1
	_devtype=$2
	_major=$3
	_minor=$4
	
	case $_devtype in
	b)	
		if ! test -b $_devname
		then
			return 1 # fail
		fi ;;
	c)
		if ! test -c $_devname
		then
			return 1 # fail
		fi ;;
	*)	
		echo "$func_name: unknown device type argument ($_devtype)"
		return 3 # unresolved 
		;;
	esac

	_retval=`lsb_devstat $_devname`
	set $_retval
	if test "$1" = "$_devname" -a  "$2" = "$_devtype" -a  "$3" = "$_major" -a  "$4" = "$_minor"
	then
		return 0 # pass
	else
		if ! test "$1" = "$_devname"
		then
			echo "$func_name: expected device name:\"$_devname\" got \"$1\""
		fi
		if ! test "$2" = "$_devtype"
		then
			echo "$func_name: expected device type: \"$_devtype\" got \"$2\""
		fi
		if ! test "$3" = "$_major"
		then
			echo "$func_name: expected major number: \"$_major\" got \"$3\""
		fi
		if ! test "$4" = "$_minor"
		then
			echo "$func_name: expected minor number: \"$_minor\" got \"$4\""
		fi
		return 1 # fail
	fi
		
}