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
|
#!/usr/bin/ksh93
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
#
#
# simplefileattributetree1 - build a simple file tree (including file attributes)
#
# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
export PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
# Make sure all math stuff runs in the "C" locale to avoid problems
# with alternative # radix point representations (e.g. ',' instead of
# '.' in de_DE.*-locales). This needs to be set _before_ any
# floating-point constants are defined in this script).
if [[ "${LC_ALL}" != "" ]] ; then
export \
LC_MONETARY="${LC_ALL}" \
LC_MESSAGES="${LC_ALL}" \
LC_COLLATE="${LC_ALL}" \
LC_CTYPE="${LC_ALL}"
unset LC_ALL
fi
export LC_NUMERIC=C
function add_file_to_tree
{
typeset treename=$1
typeset filename=$2
nameref destnodename=$3
integer i
typeset nodepath # full name of compound variable
typeset -a pe # path elements
# first built an array containing the names of each path element
# (e.g. "foo/var/baz"" results in an array containing "( 'foo' 'bar' 'baz' )")
typeset IFS='/'
pe+=( ${filename} )
[[ ${pe[0]} == '' ]] && pe[0]='/'
# walk path described via the "pe" array and build nodes if
# there aren't any nodes yet
nodepath="${treename}"
for (( i=0 ; i < (${#pe[@]}-1) ; i++ )) ; do
nameref x="${nodepath}"
# [[ -v ]] does not work for arrays because [[ -v ar ]]
# is equal to [[ -v ar[0] ]]. In this case we can
# use the output of typeset +p x.nodes
[[ "${ typeset +p x.nodes ; }" == "" ]] && compound -A x.nodes
nodepath+=".nodes[${pe[i]}]"
done
# insert element
nameref node="${nodepath}"
[[ "${ typeset +p node.elements ; }" == "" ]] && compound -A node.elements
node.elements[${pe[i]}]=(
filepath="${filename}"
)
destnodename="${!node}.elements[${pe[i]}]"
return 0
}
function parse_findls
{
nameref out=$1
typeset str="$2"
# find -ls on Solaris uses the following output format by default:
#604302 3 -rw-r--r-- 1 test001 users 2678 May 9 00:46 ./httpsresdump
integer out.inodenum="${str/~(Elr)[[:space:]]*([[:digit:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alpha:]-]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alnum:]]+)[[:space:]]+([[:alnum:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alpha:]]*[[:space:]]+[[:digit:]]*[[:space:]]+[[:digit:]:]+)[[:space:]]+(.+)/\1}"
integer out.kbblocks="${str/~(Elr)[[:space:]]*([[:digit:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alpha:]-]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alnum:]]+)[[:space:]]+([[:alnum:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alpha:]]*[[:space:]]+[[:digit:]]*[[:space:]]+[[:digit:]:]+)[[:space:]]+(.+)/\2}"
typeset out.mode="${str/~(Elr)[[:space:]]*([[:digit:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alpha:]-]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alnum:]]+)[[:space:]]+([[:alnum:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alpha:]]*[[:space:]]+[[:digit:]]*[[:space:]]+[[:digit:]:]+)[[:space:]]+(.+)/\3}"
integer out.numlinks="${str/~(Elr)[[:space:]]*([[:digit:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alpha:]-]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alnum:]]+)[[:space:]]+([[:alnum:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alpha:]]*[[:space:]]+[[:digit:]]*[[:space:]]+[[:digit:]:]+)[[:space:]]+(.+)/\4}"
compound out.owner=(
typeset user="${str/~(Elr)[[:space:]]*([[:digit:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alpha:]-]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alnum:]]+)[[:space:]]+([[:alnum:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alpha:]]*[[:space:]]+[[:digit:]]*[[:space:]]+[[:digit:]:]+)[[:space:]]+(.+)/\5}"
typeset group="${str/~(Elr)[[:space:]]*([[:digit:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alpha:]-]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alnum:]]+)[[:space:]]+([[:alnum:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alpha:]]*[[:space:]]+[[:digit:]]*[[:space:]]+[[:digit:]:]+)[[:space:]]+(.+)/\6}"
)
integer out.filesize="${str/~(Elr)[[:space:]]*([[:digit:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alpha:]-]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alnum:]]+)[[:space:]]+([[:alnum:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alpha:]]*[[:space:]]+[[:digit:]]*[[:space:]]+[[:digit:]:]+)[[:space:]]+(.+)/\7}"
typeset out.date="${str/~(Elr)[[:space:]]*([[:digit:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alpha:]-]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alnum:]]+)[[:space:]]+([[:alnum:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alpha:]]*[[:space:]]+[[:digit:]]*[[:space:]]+[[:digit:]:]+)[[:space:]]+(.+)/\8}"
typeset out.filepath="${str/~(Elr)[[:space:]]*([[:digit:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alpha:]-]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alnum:]]+)[[:space:]]+([[:alnum:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:alpha:]]*[[:space:]]+[[:digit:]]*[[:space:]]+[[:digit:]:]+)[[:space:]]+(.+)/\9}"
return 0
}
function usage
{
OPTIND=0
getopts -a "${progname}" "${simplefileattributetree1_usage}" OPT '-?'
exit 2
}
# main
builtin basename
builtin dirname
set -o noglob
set -o nounset
# tree base
compound filetree
# benchmark data
compound bench=(
float start
float stop
)
compound appconfig=(
typeset do_benchmarking=false
compound do_record=(
typeset content=false
typeset filetype=false
)
)
integer i
typeset progname="${ basename "${0}" ; }"
typeset -r simplefileattributetree1_usage=$'+
[-?\n@(#)\$Id: simplefileattributetree1 (Roland Mainz) 2010-03-27 \$\n]
[-author?Roland Mainz <roland.mainz@nrubsig.org>]
[+NAME?simplefileattributetree1 - generate compound variable tree which contains file names and their attributes]
[+DESCRIPTION?\bsimplefileattributetree1\b is a simple variable tree
demo which builds a compound variable tree based on the output
of /usr/xpg4/bin/file which contains the file name, the file attributes
and optionally file type and content]
[b:benchmark?Print time needed to generate the tree.]
[c:includecontent?Include the file\'s content in the tree, split into 1kb blocks.]
[t:includefiletype?Include the file type (output of /usr/xpg4/bin/file).]
path
[+SEE ALSO?\bksh93\b(1), \bfile\b(1), \bfind\b(1)]
'
while getopts -a "${progname}" "${simplefileattributetree1_usage}" OPT ; do
# printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"
case ${OPT} in
b) appconfig.do_benchmarking="true" ;;
+b) appconfig.do_benchmarking="false" ;;
c) appconfig.do_record.content="true" ;;
+c) appconfig.do_record.content="false" ;;
t) appconfig.do_record.filetype="true" ;;
+t) appconfig.do_record.filetype="false" ;;
*) usage ;;
esac
done
shift $((OPTIND-1))
# argument prechecks
if (( $# == 0 )) ; then
print -u2 -f "%s: Missing <path> argument.\n" "${progname}"
exit 1
fi
print -u2 -f "# reading file names...\n"
while (( $# > 0 )) ; do
# "ulimit -c 0" use used to force ksh93 to use a seperate process for subshells,
# this is used to work around a bug with LC_ALL changes bleeding through subshells
IFS=$'\n' ; typeset -a findls_lines=( $(ulimit -c 0 ; LC_ALL=C find "$1" -type f -ls) ) ; IFS=$' \t\n'
shift
done
print -u2 -f "# building tree...\n"
${appconfig.do_benchmarking} && (( bench.start=SECONDS ))
for (( i=0 ; i < ${#findls_lines[@]} ; i++ )) ; do
compound parseddata
typeset treenodename
# parse "find -ls" output
parse_findls parseddata "${findls_lines[i]}"
# add node to tree and return it's absolute name in "treenodename"
add_file_to_tree filetree "${parseddata.filepath}" treenodename
# merge parsed "find -ls" output into tree node
nameref treenode="${treenodename}"
treenode+=parseddata
# extras (calculated from the existing values in "parseddata")
typeset treenode.dirname="${ dirname "${treenode.filepath}" ; }"
typeset treenode.basename="${ basename "${treenode.filepath}" ; }"
if ${appconfig.do_record.filetype} ; then
# Using /usr/(xpg4/)*/bin/file requires a |fork()|+|exec()| which makes the script a few hundred times slower... ;-(
typeset treenode.filetype="$(file "${treenode.filepath}")"
fi
if ${appconfig.do_record.content} ; then
if [[ -r "${treenode.filepath}" ]] ; then
# We use an array of compound variables here to support
# files with holes (and later alternative streams, too)
compound -a treenode.content
integer cl=0
while \
{
treenode.content[${cl}]=(
typeset type="data" # (todo: "add support for "holes" (sparse files))
typeset -b bin
)
read -n1024 treenode.content[${cl}].bin
} ; do
(( cl++ ))
done < "${treenode.filepath}"
unset treenode.content[${cl}]
typeset -A treenode.hashsum=(
[md5]="$(sum -x md5 < "${treenode.filepath}")"
[sha512]="$(sum -x sha512 < "${treenode.filepath}")"
)
# we do this for internal debugging only
if [[ "${ {
integer j
for (( j=0 ; j < ${#treenode.content[@]} ; j++ )) ; do
printf "%B" treenode.content[$j].bin
done
} | sum -x sha512 ; }" != "${treenode.hashsum[sha512]}" ]] ; then
# this should never happen...
print -u2 -f "fatal hash mismatch for %s\n" "${treenode.filepath}"
unset treenode.content treenode.hashsum
fi
fi
fi
done
${appconfig.do_benchmarking} && (( bench.stop=SECONDS ))
if ${appconfig.do_benchmarking} ; then
# print benchmark data
print -u2 -f "# time used: %f\n" $((bench.stop - bench.start))
fi
# print variable tree
print -v filetree
exit 0
# EOF.
|