xref: /onnv-gate/usr/src/lib/libshell/common/scripts/simplefiletree1.sh (revision 12068:08a39a083754)
110898Sroland.mainz@nrubsig.org#!/usr/bin/ksh93
210898Sroland.mainz@nrubsig.org
310898Sroland.mainz@nrubsig.org#
410898Sroland.mainz@nrubsig.org# CDDL HEADER START
510898Sroland.mainz@nrubsig.org#
610898Sroland.mainz@nrubsig.org# The contents of this file are subject to the terms of the
710898Sroland.mainz@nrubsig.org# Common Development and Distribution License (the "License").
810898Sroland.mainz@nrubsig.org# You may not use this file except in compliance with the License.
910898Sroland.mainz@nrubsig.org#
1010898Sroland.mainz@nrubsig.org# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1110898Sroland.mainz@nrubsig.org# or http://www.opensolaris.org/os/licensing.
1210898Sroland.mainz@nrubsig.org# See the License for the specific language governing permissions
1310898Sroland.mainz@nrubsig.org# and limitations under the License.
1410898Sroland.mainz@nrubsig.org#
1510898Sroland.mainz@nrubsig.org# When distributing Covered Code, include this CDDL HEADER in each
1610898Sroland.mainz@nrubsig.org# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1710898Sroland.mainz@nrubsig.org# If applicable, add the following below this CDDL HEADER, with the
1810898Sroland.mainz@nrubsig.org# fields enclosed by brackets "[]" replaced with your own identifying
1910898Sroland.mainz@nrubsig.org# information: Portions Copyright [yyyy] [name of copyright owner]
2010898Sroland.mainz@nrubsig.org#
2110898Sroland.mainz@nrubsig.org# CDDL HEADER END
2210898Sroland.mainz@nrubsig.org#
2310898Sroland.mainz@nrubsig.org
2410898Sroland.mainz@nrubsig.org#
25*12068SRoger.Faulkner@Oracle.COM# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
2610898Sroland.mainz@nrubsig.org#
2710898Sroland.mainz@nrubsig.org
2810898Sroland.mainz@nrubsig.org#
2910898Sroland.mainz@nrubsig.org# simplefiletree1 - build a simple file tree
3010898Sroland.mainz@nrubsig.org#
3110898Sroland.mainz@nrubsig.org
3210898Sroland.mainz@nrubsig.org# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
3310898Sroland.mainz@nrubsig.orgexport PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
3410898Sroland.mainz@nrubsig.org
3510898Sroland.mainz@nrubsig.org# Make sure all math stuff runs in the "C" locale to avoid problems
3610898Sroland.mainz@nrubsig.org# with alternative # radix point representations (e.g. ',' instead of
3710898Sroland.mainz@nrubsig.org# '.' in de_DE.*-locales). This needs to be set _before_ any
3810898Sroland.mainz@nrubsig.org# floating-point constants are defined in this script).
3910898Sroland.mainz@nrubsig.orgif [[ "${LC_ALL}" != "" ]] ; then
4010898Sroland.mainz@nrubsig.org    export \
4110898Sroland.mainz@nrubsig.org        LC_MONETARY="${LC_ALL}" \
4210898Sroland.mainz@nrubsig.org        LC_MESSAGES="${LC_ALL}" \
4310898Sroland.mainz@nrubsig.org        LC_COLLATE="${LC_ALL}" \
4410898Sroland.mainz@nrubsig.org        LC_CTYPE="${LC_ALL}"
4510898Sroland.mainz@nrubsig.org        unset LC_ALL
4610898Sroland.mainz@nrubsig.orgfi
4710898Sroland.mainz@nrubsig.orgexport LC_NUMERIC=C
4810898Sroland.mainz@nrubsig.org
4910898Sroland.mainz@nrubsig.org
5010898Sroland.mainz@nrubsig.orgfunction add_file_to_tree
5110898Sroland.mainz@nrubsig.org{
5210898Sroland.mainz@nrubsig.org	typeset treename=$1
5310898Sroland.mainz@nrubsig.org	typeset filename=$2
5410898Sroland.mainz@nrubsig.org	integer i
5510898Sroland.mainz@nrubsig.org	typeset nodepath # full name of compound variable
5610898Sroland.mainz@nrubsig.org	typeset -a pe # path elements
5710898Sroland.mainz@nrubsig.org
5810898Sroland.mainz@nrubsig.org	# first built an array containing the names of each path element
5910898Sroland.mainz@nrubsig.org	# (e.g. "foo/var/baz"" results in an array containing "( 'foo' 'bar' 'baz' )")
6010898Sroland.mainz@nrubsig.org	typeset IFS='/'
6110898Sroland.mainz@nrubsig.org	pe+=( ${filename} )
6210898Sroland.mainz@nrubsig.org
6310898Sroland.mainz@nrubsig.org	[[ ${pe[0]} == '' ]] && pe[0]='/'
6410898Sroland.mainz@nrubsig.org
6510898Sroland.mainz@nrubsig.org	# walk path described via the "pe" array and build nodes if
6610898Sroland.mainz@nrubsig.org	# there aren't any nodes yet
6710898Sroland.mainz@nrubsig.org	nodepath="${treename}"
6810898Sroland.mainz@nrubsig.org	for (( i=0 ; i < (${#pe[@]}-1) ; i++ )) ; do
6910898Sroland.mainz@nrubsig.org		nameref x="${nodepath}"
70*12068SRoger.Faulkner@Oracle.COM
71*12068SRoger.Faulkner@Oracle.COM		# [[ -v ]] does not work for arrays because [[ -v ar ]]
72*12068SRoger.Faulkner@Oracle.COM		# is equal to [[ -v ar[0] ]]. In this case we can
73*12068SRoger.Faulkner@Oracle.COM		# use the output of typeset +p x.nodes
74*12068SRoger.Faulkner@Oracle.COM		[[ "${ typeset +p x.nodes ; }" == "" ]] && compound -A x.nodes
7510898Sroland.mainz@nrubsig.org
7610898Sroland.mainz@nrubsig.org		nodepath+=".nodes[${pe[i]}]"
7710898Sroland.mainz@nrubsig.org	done
7810898Sroland.mainz@nrubsig.org
7910898Sroland.mainz@nrubsig.org	# insert element
8010898Sroland.mainz@nrubsig.org	nameref node="${nodepath}"
81*12068SRoger.Faulkner@Oracle.COM	[[ "${ typeset +p node.elements ; }" == "" ]] && typeset -a node.elements
8210898Sroland.mainz@nrubsig.org	node.elements+=( "${pe[i]}" )
8310898Sroland.mainz@nrubsig.org
8410898Sroland.mainz@nrubsig.org	return 0
8510898Sroland.mainz@nrubsig.org}
8610898Sroland.mainz@nrubsig.org
8710898Sroland.mainz@nrubsig.org# main
8810898Sroland.mainz@nrubsig.orgbuiltin rev
8910898Sroland.mainz@nrubsig.org
9010898Sroland.mainz@nrubsig.org# tree base
9110898Sroland.mainz@nrubsig.orgcompound filetree
9210898Sroland.mainz@nrubsig.org
9310898Sroland.mainz@nrubsig.org# benchmark data
9410898Sroland.mainz@nrubsig.orgcompound bench=(
9510898Sroland.mainz@nrubsig.org	float start
9610898Sroland.mainz@nrubsig.org	float stop
9710898Sroland.mainz@nrubsig.org)
9810898Sroland.mainz@nrubsig.org
9910898Sroland.mainz@nrubsig.orgtypeset i
10010898Sroland.mainz@nrubsig.org
10110898Sroland.mainz@nrubsig.org# argument prechecks
10210898Sroland.mainz@nrubsig.orgif (( $# == 0 )) ; then
10310898Sroland.mainz@nrubsig.org	print -u2 -f "%s: Missing <path> argument." "$0"
10410898Sroland.mainz@nrubsig.org	exit 1
10510898Sroland.mainz@nrubsig.orgfi
10610898Sroland.mainz@nrubsig.org
10710898Sroland.mainz@nrubsig.orgprint -u2 "# reading file names"
10810898Sroland.mainz@nrubsig.orgwhile (( $# > 0 )) ; do
10910898Sroland.mainz@nrubsig.org	IFS=$'\n' ; typeset -a filenames=( $(find "$1" -type f) ) ; IFS=$' \t\n'
11010898Sroland.mainz@nrubsig.org	shift
11110898Sroland.mainz@nrubsig.orgdone
11210898Sroland.mainz@nrubsig.orgprint -u2 "# building tree..."
11310898Sroland.mainz@nrubsig.org
11410898Sroland.mainz@nrubsig.org(( bench.start=SECONDS ))
11510898Sroland.mainz@nrubsig.org
11610898Sroland.mainz@nrubsig.orgfor ((i=0 ; i < ${#filenames[@]} ; i++ )) ; do
11710898Sroland.mainz@nrubsig.org	add_file_to_tree filetree "${filenames[i]}"
11810898Sroland.mainz@nrubsig.orgdone
11910898Sroland.mainz@nrubsig.org
12010898Sroland.mainz@nrubsig.org(( bench.stop=SECONDS ))
12110898Sroland.mainz@nrubsig.org
12210898Sroland.mainz@nrubsig.org# print benchmark data
12310898Sroland.mainz@nrubsig.orgprint -u2 -f "# time used: %f\n" $((bench.stop - bench.start))
12410898Sroland.mainz@nrubsig.org
12510898Sroland.mainz@nrubsig.org# print tree
12610898Sroland.mainz@nrubsig.orgprint -v filetree
12710898Sroland.mainz@nrubsig.org
12810898Sroland.mainz@nrubsig.orgexit 0
12910898Sroland.mainz@nrubsig.org# EOF.
130