xref: /titanic_52/usr/src/tools/scripts/wsdiff.py (revision a4f922faa6d47d839acf0ff78f1d901c231354ba)
193be19b9SAndy Fiddaman#!@TOOLS_PYTHON@
296ccc8cbSesaxe#
396ccc8cbSesaxe# CDDL HEADER START
496ccc8cbSesaxe#
596ccc8cbSesaxe# The contents of this file are subject to the terms of the
696ccc8cbSesaxe# Common Development and Distribution License (the "License").
796ccc8cbSesaxe# You may not use this file except in compliance with the License.
896ccc8cbSesaxe#
996ccc8cbSesaxe# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1096ccc8cbSesaxe# or http://www.opensolaris.org/os/licensing.
1196ccc8cbSesaxe# See the License for the specific language governing permissions
1296ccc8cbSesaxe# and limitations under the License.
1396ccc8cbSesaxe#
1496ccc8cbSesaxe# When distributing Covered Code, include this CDDL HEADER in each
1596ccc8cbSesaxe# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1696ccc8cbSesaxe# If applicable, add the following below this CDDL HEADER, with the
1796ccc8cbSesaxe# fields enclosed by brackets "[]" replaced with your own identifying
1896ccc8cbSesaxe# information: Portions Copyright [yyyy] [name of copyright owner]
1996ccc8cbSesaxe#
2096ccc8cbSesaxe# CDDL HEADER END
2196ccc8cbSesaxe#
22598cc7dfSVladimir Kotal# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23*a4f922faSAndy Fiddaman# Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
2496ccc8cbSesaxe#
2596ccc8cbSesaxe
2696ccc8cbSesaxe#
2796ccc8cbSesaxe# wsdiff(1) is a tool that can be used to determine which compiled objects
2896ccc8cbSesaxe# have changed as a result of a given source change. Developers backporting
2996ccc8cbSesaxe# new features, RFEs and bug fixes need to be able to identify the set of
3096ccc8cbSesaxe# patch deliverables necessary for feature/fix realization on a patched system.
3196ccc8cbSesaxe#
3296ccc8cbSesaxe# The tool works by comparing objects in two trees/proto areas (one build with,
3396ccc8cbSesaxe# and without the source changes.
3496ccc8cbSesaxe#
3596ccc8cbSesaxe# Using wsdiff(1) is fairly simple:
3696ccc8cbSesaxe#	- Bringover to a fresh workspace
3796ccc8cbSesaxe#	- Perform a full non-debug build (clobber if workspace isn't fresh)
3896ccc8cbSesaxe#	- Move the proto area aside, call it proto.old, or something.
3996ccc8cbSesaxe#	- Integrate your changes to the workspace
4096ccc8cbSesaxe#	- Perform another full non-debug clobber build.
4196ccc8cbSesaxe#	- Use wsdiff(1) to see what changed:
4296ccc8cbSesaxe#		$ wsdiff proto.old proto
4396ccc8cbSesaxe#
4496ccc8cbSesaxe# By default, wsdiff will print the list of changed objects / deliverables to
4596ccc8cbSesaxe# stdout. If a results file is specified via -r, the list of differing objects,
4696ccc8cbSesaxe# and details about why wsdiff(1) thinks they are different will be logged to
4796ccc8cbSesaxe# the results file.
4896ccc8cbSesaxe#
49a62941d8SAndy Fiddaman# By invoking nightly(1) with the -w option to NIGHTLY_FLAGS, nightly(1) will
50a62941d8SAndy Fiddaman# use wsdiff(1) to report on what objects changed since the last build.
5196ccc8cbSesaxe#
5296ccc8cbSesaxe# For patch deliverable purposes, it's advised to have nightly do a clobber,
5396ccc8cbSesaxe# non-debug build.
5496ccc8cbSesaxe#
5596ccc8cbSesaxe# Think about the results. Was something flagged that you don't expect? Go look
5696ccc8cbSesaxe# at the results file to see details about the differences.
5796ccc8cbSesaxe#
58a62941d8SAndy Fiddaman# Use the -i option in conjunction with -v and -V to dive deeper and have
59a62941d8SAndy Fiddaman# wsdiff(1) report with more verbosity.
6096ccc8cbSesaxe#
61*a4f922faSAndy Fiddaman# Usage: wsdiff [-dstuvV] [-U lines] [-r results ] [-i filelist ] old new
6296ccc8cbSesaxe#
6396ccc8cbSesaxe# Where "old" is the path to the proto area build without the changes, and
6496ccc8cbSesaxe# "new" is the path to the proto area built with the changes. The following
6596ccc8cbSesaxe# options are supported:
6696ccc8cbSesaxe#
67*a4f922faSAndy Fiddaman#       -d      Print debug messages about the progress
68*a4f922faSAndy Fiddaman#       -i      Tell wsdiff which objects to compare via an input file list
69*a4f922faSAndy Fiddaman#       -r      Log results and observed differences
70*a4f922faSAndy Fiddaman#       -s      Produce sorted list of differences
71*a4f922faSAndy Fiddaman#       -t      Use onbld tools in $SRC/tools
72*a4f922faSAndy Fiddaman#       -u      Produce unified diff output
73*a4f922faSAndy Fiddaman#       -U      Produce unified diff output with <lines> lines of context
7496ccc8cbSesaxe#       -v      Do not truncate observed diffs in results
7596ccc8cbSesaxe#       -V      Log *all* ELF sect diffs vs. logging the first diff found
7696ccc8cbSesaxe
7793be19b9SAndy Fiddamanfrom __future__ import print_function
78ea7dde8fSAndy Fiddamanimport datetime, fnmatch, getopt, os, profile, io, subprocess
79598cc7dfSVladimir Kotalimport re, resource, select, shutil, signal, string, struct, sys, tempfile
80598cc7dfSVladimir Kotalimport time, threading
8196ccc8cbSesaxefrom stat import *
82a62941d8SAndy Fiddaman
83a62941d8SAndy FiddamanPY3 = sys.version_info[0] == 3
84a62941d8SAndy Fiddaman
8596ccc8cbSesaxe# Human readable diffs truncated by default if longer than this
8696ccc8cbSesaxe# Specifying -v on the command line will override
8796ccc8cbSesaxediffs_sz_thresh = 4096
88*a4f922faSAndy Fiddamandiff_args = ''
8996ccc8cbSesaxe
90598cc7dfSVladimir Kotal# Lock name	 Provides exclusive access to
91598cc7dfSVladimir Kotal# --------------+------------------------------------------------
92598cc7dfSVladimir Kotal# output_lock	 standard output or temporary file (difference())
93598cc7dfSVladimir Kotal# log_lock	 the results file (log_difference())
94598cc7dfSVladimir Kotal# wset_lock	 changedFiles list (workerThread())
95598cc7dfSVladimir Kotaloutput_lock = threading.Lock()
96598cc7dfSVladimir Kotallog_lock = threading.Lock()
97598cc7dfSVladimir Kotalwset_lock = threading.Lock()
98598cc7dfSVladimir Kotal
99598cc7dfSVladimir Kotal# Variable for thread control
100598cc7dfSVladimir Kotalkeep_processing = True
101598cc7dfSVladimir Kotal
10296ccc8cbSesaxe# Default search path for wsdiff
10396ccc8cbSesaxewsdiff_path = [ "/usr/bin",
10496ccc8cbSesaxe		"/usr/ccs/bin",
10596ccc8cbSesaxe		"/lib/svc/bin",
10696ccc8cbSesaxe		"/opt/onbld/bin" ]
10796ccc8cbSesaxe
10896ccc8cbSesaxe# These are objects that wsdiff will notice look different, but will not report.
10996ccc8cbSesaxe# Existence of an exceptions list, and adding things here is *dangerous*,
11096ccc8cbSesaxe# and therefore the *only* reasons why anything would be listed here is because
11196ccc8cbSesaxe# the objects do not build deterministically, yet we *cannot* fix this.
11296ccc8cbSesaxe#
11393be19b9SAndy Fiddamanwsdiff_exceptions = [
11496ccc8cbSesaxe]
11596ccc8cbSesaxe
116*a4f922faSAndy Fiddaman# Path to genunix, used for CTF diff
117*a4f922faSAndy Fiddamangenunix = "/kernel/amd64/genunix"
118*a4f922faSAndy Fiddaman
119a62941d8SAndy Fiddamanif PY3:
120895ef1d5SAlexander Pyhalov	def getoutput(cmd):
121895ef1d5SAlexander Pyhalov		import shlex, tempfile
122895ef1d5SAlexander Pyhalov		f, fpath = tempfile.mkstemp()
123895ef1d5SAlexander Pyhalov		status = os.system("{ " + cmd + "; } >" +
124895ef1d5SAlexander Pyhalov			shlex.quote(fpath) + " 2>&1")
125895ef1d5SAlexander Pyhalov		returncode = os.WEXITSTATUS(status)
126*a4f922faSAndy Fiddaman		with os.fdopen(f, mode="r", errors="ignore") as tfile:
127895ef1d5SAlexander Pyhalov			output = tfile.read()
128895ef1d5SAlexander Pyhalov		os.unlink(fpath)
129895ef1d5SAlexander Pyhalov		if output[-1:] == '\n':
130895ef1d5SAlexander Pyhalov			output = output[:-1]
131895ef1d5SAlexander Pyhalov		return returncode, output
132a62941d8SAndy Fiddamanelse:
133*a4f922faSAndy Fiddaman	import commands
134895ef1d5SAlexander Pyhalov	getoutput = commands.getstatusoutput
135ea7dde8fSAndy Fiddaman
13696ccc8cbSesaxe#####
13796ccc8cbSesaxe# Logging routines
13896ccc8cbSesaxe#
13996ccc8cbSesaxe
140598cc7dfSVladimir Kotal# Debug message to be printed to the screen, and the log file
141598cc7dfSVladimir Kotaldef debug(msg) :
142598cc7dfSVladimir Kotal
143598cc7dfSVladimir Kotal	# Add prefix to highlight debugging message
144598cc7dfSVladimir Kotal	msg = "## " + msg
145598cc7dfSVladimir Kotal	if debugon :
146598cc7dfSVladimir Kotal		output_lock.acquire()
14793be19b9SAndy Fiddaman		print(msg)
148598cc7dfSVladimir Kotal		sys.stdout.flush()
149598cc7dfSVladimir Kotal		output_lock.release()
150598cc7dfSVladimir Kotal		if logging :
151598cc7dfSVladimir Kotal			log_lock.acquire()
15293be19b9SAndy Fiddaman			print(msg, file=log)
153598cc7dfSVladimir Kotal			log.flush()
154598cc7dfSVladimir Kotal			log_lock.release()
155598cc7dfSVladimir Kotal
15696ccc8cbSesaxe# Informational message to be printed to the screen, and the log file
15796ccc8cbSesaxedef info(msg) :
15896ccc8cbSesaxe
159598cc7dfSVladimir Kotal	output_lock.acquire()
16093be19b9SAndy Fiddaman	print(msg)
16196ccc8cbSesaxe	sys.stdout.flush()
162598cc7dfSVladimir Kotal	output_lock.release()
163598cc7dfSVladimir Kotal	if logging :
164598cc7dfSVladimir Kotal		log_lock.acquire()
16593be19b9SAndy Fiddaman		print(msg, file=log)
166598cc7dfSVladimir Kotal		log.flush()
167598cc7dfSVladimir Kotal		log_lock.release()
16896ccc8cbSesaxe
16996ccc8cbSesaxe# Error message to be printed to the screen, and the log file
17096ccc8cbSesaxedef error(msg) :
17196ccc8cbSesaxe
172598cc7dfSVladimir Kotal	output_lock.acquire()
17393be19b9SAndy Fiddaman	print("ERROR: " + msg, file=sys.stderr)
17496ccc8cbSesaxe	sys.stderr.flush()
175598cc7dfSVladimir Kotal	output_lock.release()
17696ccc8cbSesaxe	if logging :
177598cc7dfSVladimir Kotal		log_lock.acquire()
17893be19b9SAndy Fiddaman		print("ERROR: " + msg, file=log)
17996ccc8cbSesaxe		log.flush()
180598cc7dfSVladimir Kotal		log_lock.release()
18196ccc8cbSesaxe
18296ccc8cbSesaxe# Informational message to be printed only to the log, if there is one.
18396ccc8cbSesaxedef v_info(msg) :
18496ccc8cbSesaxe
18596ccc8cbSesaxe	if logging :
186598cc7dfSVladimir Kotal		log_lock.acquire()
18793be19b9SAndy Fiddaman		print(msg, file=log)
18896ccc8cbSesaxe		log.flush()
189598cc7dfSVladimir Kotal		log_lock.release()
19096ccc8cbSesaxe
19196ccc8cbSesaxe#
19296ccc8cbSesaxe# Flag a detected file difference
19396ccc8cbSesaxe# Display the fileName to stdout, and log the difference
19496ccc8cbSesaxe#
19596ccc8cbSesaxedef difference(f, dtype, diffs) :
19696ccc8cbSesaxe
19796ccc8cbSesaxe	if f in wsdiff_exceptions :
19896ccc8cbSesaxe		return
19996ccc8cbSesaxe
200598cc7dfSVladimir Kotal	output_lock.acquire()
201*a4f922faSAndy Fiddaman	if o_sorted :
202598cc7dfSVladimir Kotal		differentFiles.append(f)
203598cc7dfSVladimir Kotal	else:
20493be19b9SAndy Fiddaman		print(f)
20596ccc8cbSesaxe		sys.stdout.flush()
206598cc7dfSVladimir Kotal	output_lock.release()
20796ccc8cbSesaxe
20896ccc8cbSesaxe	log_difference(f, dtype, diffs)
20996ccc8cbSesaxe
21096ccc8cbSesaxe#
21196ccc8cbSesaxe# Do the actual logging of the difference to the results file
21296ccc8cbSesaxe#
21396ccc8cbSesaxedef log_difference(f, dtype, diffs) :
214598cc7dfSVladimir Kotal
21596ccc8cbSesaxe	if logging :
216598cc7dfSVladimir Kotal		log_lock.acquire()
21793be19b9SAndy Fiddaman		print(f, file=log)
21893be19b9SAndy Fiddaman		print("NOTE: " + dtype + " difference detected.", file=log)
21996ccc8cbSesaxe
22096ccc8cbSesaxe		difflen = len(diffs)
22196ccc8cbSesaxe		if difflen > 0 :
22293be19b9SAndy Fiddaman			print('', file=log)
22396ccc8cbSesaxe
22496ccc8cbSesaxe			if not vdiffs and difflen > diffs_sz_thresh :
22593be19b9SAndy Fiddaman				print(diffs[:diffs_sz_thresh], file=log)
22693be19b9SAndy Fiddaman				print("... truncated due to length: " +
22793be19b9SAndy Fiddaman				      "use -v to override ...", file=log)
22896ccc8cbSesaxe			else :
22993be19b9SAndy Fiddaman				print(diffs, file=log)
23093be19b9SAndy Fiddaman			print('\n', file=log)
23196ccc8cbSesaxe		log.flush()
232598cc7dfSVladimir Kotal		log_lock.release()
23396ccc8cbSesaxe
23496ccc8cbSesaxe
23596ccc8cbSesaxe#####
23696ccc8cbSesaxe# diff generating routines
23796ccc8cbSesaxe#
23896ccc8cbSesaxe
23996ccc8cbSesaxe#
240a62941d8SAndy Fiddaman# Return human readable diffs from two files
24196ccc8cbSesaxe#
24296ccc8cbSesaxedef diffFileData(tmpf1, tmpf2) :
24396ccc8cbSesaxe
244598cc7dfSVladimir Kotal	binaries = False
245598cc7dfSVladimir Kotal
24696ccc8cbSesaxe	# Filter the data through od(1) if the data is detected
24796ccc8cbSesaxe	# as being binary
24896ccc8cbSesaxe	if isBinary(tmpf1) or isBinary(tmpf2) :
249598cc7dfSVladimir Kotal		binaries = True
25096ccc8cbSesaxe		tmp_od1 = tmpf1 + ".od"
25196ccc8cbSesaxe		tmp_od2 = tmpf2 + ".od"
25296ccc8cbSesaxe
25396ccc8cbSesaxe		cmd = od_cmd + " -c -t x4" + " " + tmpf1 + " > " + tmp_od1
25496ccc8cbSesaxe		os.system(cmd)
25596ccc8cbSesaxe		cmd = od_cmd + " -c -t x4" + " " + tmpf2 + " > " + tmp_od2
25696ccc8cbSesaxe		os.system(cmd)
25796ccc8cbSesaxe
25896ccc8cbSesaxe		tmpf1 = tmp_od1
25996ccc8cbSesaxe		tmpf2 = tmp_od2
26096ccc8cbSesaxe
261*a4f922faSAndy Fiddaman	dcmd = "{} {} {} {}".format(diff_cmd, diff_args, tmpf1, tmpf2)
262598cc7dfSVladimir Kotal	try:
263*a4f922faSAndy Fiddaman		rc, data = getoutput(dcmd)
264*a4f922faSAndy Fiddaman		if rc == 0:
265*a4f922faSAndy Fiddaman			# No differences found
266*a4f922faSAndy Fiddaman			data = ''
267*a4f922faSAndy Fiddaman		# If producing unified output, strip the first two lines
268*a4f922faSAndy Fiddaman		# which just show the temporary file names.
269*a4f922faSAndy Fiddaman		if diff_args:
270*a4f922faSAndy Fiddaman			data = data.split("\n", 2)[-1]
271*a4f922faSAndy Fiddaman
272598cc7dfSVladimir Kotal		# Remove the temp files as we no longer need them.
273598cc7dfSVladimir Kotal		if binaries :
274598cc7dfSVladimir Kotal			try:
275598cc7dfSVladimir Kotal				os.unlink(tmp_od1)
27693be19b9SAndy Fiddaman			except OSError as e:
277598cc7dfSVladimir Kotal				error("diffFileData: unlink failed %s" % e)
278598cc7dfSVladimir Kotal			try:
279598cc7dfSVladimir Kotal				os.unlink(tmp_od2)
28093be19b9SAndy Fiddaman			except OSError as e:
281598cc7dfSVladimir Kotal				error("diffFileData: unlink failed %s" % e)
282598cc7dfSVladimir Kotal	except:
283*a4f922faSAndy Fiddaman		error("failed to get output of command: " + dcmd)
284598cc7dfSVladimir Kotal
285598cc7dfSVladimir Kotal		# Send exception for the failed command up
286598cc7dfSVladimir Kotal		raise
287598cc7dfSVladimir Kotal		return
28896ccc8cbSesaxe
28996ccc8cbSesaxe	return data
29096ccc8cbSesaxe
29196ccc8cbSesaxe#####
29296ccc8cbSesaxe# Misc utility functions
29396ccc8cbSesaxe#
29496ccc8cbSesaxe
29596ccc8cbSesaxe# Prune off the leading prefix from string s
29696ccc8cbSesaxedef str_prefix_trunc(s, prefix) :
29796ccc8cbSesaxe	snipLen = len(prefix)
29896ccc8cbSesaxe	return s[snipLen:]
29996ccc8cbSesaxe
30096ccc8cbSesaxe#
30196ccc8cbSesaxe# Prune off leading proto path goo (if there is one) to yield
30296ccc8cbSesaxe# the deliverable's eventual path relative to root
30396ccc8cbSesaxe# e.g. proto.base/root_sparc/usr/src/cmd/prstat => usr/src/cmd/prstat
30496ccc8cbSesaxe#
30596ccc8cbSesaxedef fnFormat(fn) :
30696ccc8cbSesaxe	root_arch_str = "root_" + arch
30796ccc8cbSesaxe
30896ccc8cbSesaxe	pos = fn.find(root_arch_str)
30996ccc8cbSesaxe	if pos == -1 :
31096ccc8cbSesaxe		return fn
31196ccc8cbSesaxe
31296ccc8cbSesaxe	pos = fn.find("/", pos)
31396ccc8cbSesaxe	if pos == -1 :
31496ccc8cbSesaxe		return fn
31596ccc8cbSesaxe
31696ccc8cbSesaxe	return fn[pos + 1:]
31796ccc8cbSesaxe
318*a4f922faSAndy Fiddaman#
319*a4f922faSAndy Fiddaman# Find the path to a proto root given the name of a file or directory under it
320*a4f922faSAndy Fiddaman# e.g. proto.base/root_i386-nd/usr/bin => proto.base/root_i386-nd
321*a4f922faSAndy Fiddaman#
322*a4f922faSAndy Fiddamandef protoroot(fn):
323*a4f922faSAndy Fiddaman	root_arch_str = "root_" + arch
324*a4f922faSAndy Fiddaman
325*a4f922faSAndy Fiddaman	pos = fn.find(root_arch_str)
326*a4f922faSAndy Fiddaman	if pos == -1:
327*a4f922faSAndy Fiddaman		return None
328*a4f922faSAndy Fiddaman
329*a4f922faSAndy Fiddaman	pos = fn.find("/", pos)
330*a4f922faSAndy Fiddaman	if pos == -1:
331*a4f922faSAndy Fiddaman		return fn
332*a4f922faSAndy Fiddaman
333*a4f922faSAndy Fiddaman	return fn[:pos]
334*a4f922faSAndy Fiddaman
335*a4f922faSAndy Fiddaman
33696ccc8cbSesaxe#####
33796ccc8cbSesaxe# Usage / argument processing
33896ccc8cbSesaxe#
33996ccc8cbSesaxe
34096ccc8cbSesaxe#
34196ccc8cbSesaxe# Display usage message
34296ccc8cbSesaxe#
34396ccc8cbSesaxedef usage() :
34496ccc8cbSesaxe	sys.stdout.flush()
345*a4f922faSAndy Fiddaman	print("""Usage: wsdiff [-dstuvV] [-U lines] [-r results ] [-i filelist ] old new
346598cc7dfSVladimir Kotal        -d      Print debug messages about the progress
347*a4f922faSAndy Fiddaman        -i      Tell wsdiff which objects to compare via an input file list
34896ccc8cbSesaxe        -r      Log results and observed differences
349598cc7dfSVladimir Kotal        -s      Produce sorted list of differences
350*a4f922faSAndy Fiddaman        -t      Use onbld tools in $SRC/tools
351*a4f922faSAndy Fiddaman        -u      Produce unified diff output
352*a4f922faSAndy Fiddaman        -U      Produce unified diff output with <lines> lines of context
353*a4f922faSAndy Fiddaman        -v      Do not truncate observed diffs in results
354*a4f922faSAndy Fiddaman        -V      Log *all* ELF sect diffs vs. logging the first diff found""",
35593be19b9SAndy Fiddaman	    file=sys.stderr)
35696ccc8cbSesaxe	sys.exit(1)
35796ccc8cbSesaxe
35896ccc8cbSesaxe#
35996ccc8cbSesaxe# Process command line options
36096ccc8cbSesaxe#
36196ccc8cbSesaxedef args() :
36296ccc8cbSesaxe
363598cc7dfSVladimir Kotal	global debugon
36496ccc8cbSesaxe	global logging
36596ccc8cbSesaxe	global vdiffs
36696ccc8cbSesaxe	global reportAllSects
367*a4f922faSAndy Fiddaman	global o_sorted
368*a4f922faSAndy Fiddaman	global diff_args
36996ccc8cbSesaxe
370*a4f922faSAndy Fiddaman	validOpts = 'di:r:uU:vVst?'
37196ccc8cbSesaxe
37296ccc8cbSesaxe	baseRoot = ""
37396ccc8cbSesaxe	ptchRoot = ""
37496ccc8cbSesaxe	fileNamesFile = ""
37596ccc8cbSesaxe	results = ""
37696ccc8cbSesaxe	localTools = False
37796ccc8cbSesaxe
37896ccc8cbSesaxe	# getopt.getopt() returns:
37996ccc8cbSesaxe	#	an option/value tuple
38096ccc8cbSesaxe	#	a list of remaining non-option arguments
38196ccc8cbSesaxe	#
38296ccc8cbSesaxe	# A correct wsdiff invocation will have exactly two non option
38396ccc8cbSesaxe	# arguments, the paths to the base (old), ptch (new) proto areas
38496ccc8cbSesaxe	try:
38596ccc8cbSesaxe		optlist, args = getopt.getopt(sys.argv[1:], validOpts)
38693be19b9SAndy Fiddaman	except getopt.error as val:
38796ccc8cbSesaxe		usage()
38896ccc8cbSesaxe
38996ccc8cbSesaxe	if len(args) != 2 :
39096ccc8cbSesaxe		usage();
39196ccc8cbSesaxe
39296ccc8cbSesaxe	for opt,val in optlist :
393598cc7dfSVladimir Kotal		if opt == '-d' :
394598cc7dfSVladimir Kotal			debugon = True
395598cc7dfSVladimir Kotal		elif opt == '-i' :
39696ccc8cbSesaxe			fileNamesFile = val
39796ccc8cbSesaxe		elif opt == '-r' :
39896ccc8cbSesaxe			results = val
39996ccc8cbSesaxe			logging = True
400598cc7dfSVladimir Kotal		elif opt == '-s' :
401*a4f922faSAndy Fiddaman			o_sorted = True
402*a4f922faSAndy Fiddaman		elif opt == '-u' :
403*a4f922faSAndy Fiddaman			diff_args = '-u'
404*a4f922faSAndy Fiddaman		elif opt == '-U' :
405*a4f922faSAndy Fiddaman			diff_args = '-U' + str(val)
40696ccc8cbSesaxe		elif opt == '-v' :
40796ccc8cbSesaxe			vdiffs = True
40896ccc8cbSesaxe		elif opt == '-V' :
40996ccc8cbSesaxe			reportAllSects = True
41096ccc8cbSesaxe		elif opt == '-t':
41196ccc8cbSesaxe			localTools = True
41296ccc8cbSesaxe		else:
41396ccc8cbSesaxe			usage()
41496ccc8cbSesaxe
41596ccc8cbSesaxe	baseRoot = args[0]
41696ccc8cbSesaxe	ptchRoot = args[1]
41796ccc8cbSesaxe
41896ccc8cbSesaxe	if len(baseRoot) == 0 or len(ptchRoot) == 0 :
41996ccc8cbSesaxe		usage()
42096ccc8cbSesaxe
42196ccc8cbSesaxe	if logging and len(results) == 0 :
42296ccc8cbSesaxe		usage()
42396ccc8cbSesaxe
42496ccc8cbSesaxe	if vdiffs and not logging :
42596ccc8cbSesaxe		error("The -v option requires a results file (-r)")
42696ccc8cbSesaxe		sys.exit(1)
42796ccc8cbSesaxe
42896ccc8cbSesaxe	if reportAllSects and not logging :
42996ccc8cbSesaxe		error("The -V option requires a results file (-r)")
43096ccc8cbSesaxe		sys.exit(1)
43196ccc8cbSesaxe
43296ccc8cbSesaxe	# alphabetical order
43396ccc8cbSesaxe	return	baseRoot, fileNamesFile, localTools, ptchRoot, results
43496ccc8cbSesaxe
43596ccc8cbSesaxe#####
43696ccc8cbSesaxe# File identification
43796ccc8cbSesaxe#
43896ccc8cbSesaxe
43996ccc8cbSesaxe#
44096ccc8cbSesaxe# Identify the file type.
44196ccc8cbSesaxe# If it's not ELF, use the file extension to identify
44296ccc8cbSesaxe# certain file types that require special handling to
44396ccc8cbSesaxe# compare. Otherwise just return a basic "ASCII" type.
44496ccc8cbSesaxe#
44596ccc8cbSesaxedef getTheFileType(f) :
44696ccc8cbSesaxe
44796ccc8cbSesaxe	extensions = { 'a'	:	'ELF Object Archive',
44896ccc8cbSesaxe		       'jar'	:	'Java Archive',
44996ccc8cbSesaxe		       'html'	:	'HTML',
45096ccc8cbSesaxe		       'ln'	:	'Lint Library',
45196ccc8cbSesaxe		       'db'	:	'Sqlite Database' }
45296ccc8cbSesaxe
453619b4598Srotondo	try:
45496ccc8cbSesaxe		if os.stat(f)[ST_SIZE] == 0 :
45596ccc8cbSesaxe			return 'ASCII'
456619b4598Srotondo	except:
457619b4598Srotondo		error("failed to stat " + f)
458619b4598Srotondo		return 'Error'
45996ccc8cbSesaxe
46096ccc8cbSesaxe	if isELF(f) == 1 :
46196ccc8cbSesaxe		return 'ELF'
46296ccc8cbSesaxe
46396ccc8cbSesaxe	fnamelist = f.split('.')
46496ccc8cbSesaxe	if len(fnamelist) > 1 :	# Test the file extension
46596ccc8cbSesaxe		extension = fnamelist[-1]
46696ccc8cbSesaxe		if extension in extensions.keys():
46796ccc8cbSesaxe			return extensions[extension]
46896ccc8cbSesaxe
46996ccc8cbSesaxe	return 'ASCII'
47096ccc8cbSesaxe
47196ccc8cbSesaxe#
47296ccc8cbSesaxe# Return non-zero if "f" is an ELF file
47396ccc8cbSesaxe#
474ea7dde8fSAndy Fiddamanelfmagic = b'\177ELF'
47596ccc8cbSesaxedef isELF(f) :
47696ccc8cbSesaxe	try:
477a62941d8SAndy Fiddaman		with open(f, mode='rb') as fd:
47896ccc8cbSesaxe			magic = fd.read(len(elfmagic))
47996ccc8cbSesaxe
48096ccc8cbSesaxe		if magic == elfmagic :
48196ccc8cbSesaxe			return 1
482ea7dde8fSAndy Fiddaman	except:
483ea7dde8fSAndy Fiddaman		pass
48496ccc8cbSesaxe	return 0
48596ccc8cbSesaxe
48696ccc8cbSesaxe#
48796ccc8cbSesaxe# Return non-zero is "f" is binary.
48896ccc8cbSesaxe# Consider the file to be binary if it contains any null characters
48996ccc8cbSesaxe#
49096ccc8cbSesaxedef isBinary(f) :
49196ccc8cbSesaxe	try:
492a62941d8SAndy Fiddaman		with open(f, mode='rb') as fd:
49396ccc8cbSesaxe			s = fd.read()
49496ccc8cbSesaxe
495ea7dde8fSAndy Fiddaman		if s.find(b'\0') == -1 :
49696ccc8cbSesaxe			return 0
497ea7dde8fSAndy Fiddaman	except:
498ea7dde8fSAndy Fiddaman		pass
49996ccc8cbSesaxe	return 1
50096ccc8cbSesaxe
50196ccc8cbSesaxe#####
50296ccc8cbSesaxe# Directory traversal and file finding
50396ccc8cbSesaxe#
50496ccc8cbSesaxe
50596ccc8cbSesaxe#
50696ccc8cbSesaxe# Return a sorted list of files found under the specified directory
50796ccc8cbSesaxe#
50896ccc8cbSesaxedef findFiles(d) :
50996ccc8cbSesaxe	for path, subdirs, files in os.walk(d) :
51096ccc8cbSesaxe		files.sort()
51196ccc8cbSesaxe		for name in files :
51296ccc8cbSesaxe			yield os.path.join(path, name)
51396ccc8cbSesaxe
51496ccc8cbSesaxe#
51596ccc8cbSesaxe# Examine all files in base, ptch
51696ccc8cbSesaxe#
51796ccc8cbSesaxe# Return a list of files appearing in both proto areas,
51896ccc8cbSesaxe# a list of new files (files found only in ptch) and
51996ccc8cbSesaxe# a list of deleted files (files found only in base)
52096ccc8cbSesaxe#
52196ccc8cbSesaxedef protoCatalog(base, ptch) :
522598cc7dfSVladimir Kotal
52396ccc8cbSesaxe	compFiles = []		# List of files in both proto areas
52496ccc8cbSesaxe	ptchList = []		# List of file in patch proto area
52596ccc8cbSesaxe
52696ccc8cbSesaxe	newFiles = []		# New files detected
52796ccc8cbSesaxe	deletedFiles = []	# Deleted files
52896ccc8cbSesaxe
529598cc7dfSVladimir Kotal	debug("Getting the list of files in the base area");
53096ccc8cbSesaxe	baseFilesList = list(findFiles(base))
53196ccc8cbSesaxe	baseStringLength = len(base)
532598cc7dfSVladimir Kotal	debug("Found " + str(len(baseFilesList)) + " files")
53396ccc8cbSesaxe
534598cc7dfSVladimir Kotal	debug("Getting the list of files in the patch area");
53596ccc8cbSesaxe	ptchFilesList = list(findFiles(ptch))
53696ccc8cbSesaxe	ptchStringLength = len(ptch)
537598cc7dfSVladimir Kotal	debug("Found " + str(len(ptchFilesList)) + " files")
53896ccc8cbSesaxe
53996ccc8cbSesaxe	# Inventory files in the base proto area
540598cc7dfSVladimir Kotal	debug("Determining the list of regular files in the base area");
54196ccc8cbSesaxe	for fn in baseFilesList :
54296ccc8cbSesaxe		if os.path.islink(fn) :
54396ccc8cbSesaxe			continue
54496ccc8cbSesaxe
54596ccc8cbSesaxe		fileName = fn[baseStringLength:]
54696ccc8cbSesaxe		compFiles.append(fileName)
547598cc7dfSVladimir Kotal	debug("Found " + str(len(compFiles)) + " files")
54896ccc8cbSesaxe
54996ccc8cbSesaxe	# Inventory files in the patch proto area
550598cc7dfSVladimir Kotal	debug("Determining the list of regular files in the patch area");
55196ccc8cbSesaxe	for fn in ptchFilesList :
55296ccc8cbSesaxe		if os.path.islink(fn) :
55396ccc8cbSesaxe			continue
55496ccc8cbSesaxe
55596ccc8cbSesaxe		fileName = fn[ptchStringLength:]
55696ccc8cbSesaxe		ptchList.append(fileName)
557598cc7dfSVladimir Kotal	debug("Found " + str(len(ptchList)) + " files")
55896ccc8cbSesaxe
55996ccc8cbSesaxe	# Deleted files appear in the base area, but not the patch area
560598cc7dfSVladimir Kotal	debug("Searching for deleted files by comparing the lists")
56196ccc8cbSesaxe	for fileName in compFiles :
56296ccc8cbSesaxe		if not fileName in ptchList :
56396ccc8cbSesaxe			deletedFiles.append(fileName)
564598cc7dfSVladimir Kotal	debug("Found " + str(len(deletedFiles)) + " deleted files")
56596ccc8cbSesaxe
56696ccc8cbSesaxe	# Eliminate "deleted" files from the list of objects appearing
56796ccc8cbSesaxe	# in both the base and patch proto areas
568598cc7dfSVladimir Kotal	debug("Eliminating deleted files from the list of objects")
56996ccc8cbSesaxe	for fileName in deletedFiles :
57096ccc8cbSesaxe		try:
57196ccc8cbSesaxe			compFiles.remove(fileName)
57296ccc8cbSesaxe		except:
57396ccc8cbSesaxe			error("filelist.remove() failed")
57493be19b9SAndy Fiddaman	debug("List for comparison reduced to " + str(len(compFiles))
575598cc7dfSVladimir Kotal	    + " files")
57696ccc8cbSesaxe
57796ccc8cbSesaxe	# New files appear in the patch area, but not the base
578598cc7dfSVladimir Kotal	debug("Getting the list of newly added files")
57996ccc8cbSesaxe	for fileName in ptchList :
58096ccc8cbSesaxe		if not fileName in compFiles :
58196ccc8cbSesaxe			newFiles.append(fileName)
582598cc7dfSVladimir Kotal	debug("Found " + str(len(newFiles)) + " new files")
58396ccc8cbSesaxe
58496ccc8cbSesaxe	return compFiles, newFiles, deletedFiles
58596ccc8cbSesaxe
58696ccc8cbSesaxe#
58796ccc8cbSesaxe# Examine the files listed in the input file list
58896ccc8cbSesaxe#
58996ccc8cbSesaxe# Return a list of files appearing in both proto areas,
59096ccc8cbSesaxe# a list of new files (files found only in ptch) and
59196ccc8cbSesaxe# a list of deleted files (files found only in base)
59296ccc8cbSesaxe#
59396ccc8cbSesaxedef flistCatalog(base, ptch, flist) :
59496ccc8cbSesaxe	compFiles = []		# List of files in both proto areas
59596ccc8cbSesaxe	newFiles = []		# New files detected
59696ccc8cbSesaxe	deletedFiles = []	# Deleted files
59796ccc8cbSesaxe
59896ccc8cbSesaxe	try:
59996ccc8cbSesaxe		fd = open(flist, "r")
60096ccc8cbSesaxe	except:
60196ccc8cbSesaxe		error("could not open: " + flist)
60296ccc8cbSesaxe		cleanup(1)
60396ccc8cbSesaxe
60496ccc8cbSesaxe	files = []
60596ccc8cbSesaxe	files = fd.readlines()
606598cc7dfSVladimir Kotal	fd.close()
60796ccc8cbSesaxe
60896ccc8cbSesaxe	for f in files :
60996ccc8cbSesaxe		ptch_present = True
61096ccc8cbSesaxe		base_present = True
61196ccc8cbSesaxe
61296ccc8cbSesaxe		if f == '\n' :
61396ccc8cbSesaxe			continue
61496ccc8cbSesaxe
61596ccc8cbSesaxe		# the fileNames have a trailing '\n'
61696ccc8cbSesaxe		f = f.rstrip()
61796ccc8cbSesaxe
61896ccc8cbSesaxe		# The objects in the file list have paths relative
61996ccc8cbSesaxe		# to $ROOT or to the base/ptch directory specified on
62096ccc8cbSesaxe		# the command line.
62196ccc8cbSesaxe		# If it's relative to $ROOT, we'll need to add back the
62296ccc8cbSesaxe		# root_`uname -p` goo we stripped off in fnFormat()
62396ccc8cbSesaxe		if os.path.exists(base + f) :
62496ccc8cbSesaxe			fn = f;
62596ccc8cbSesaxe		elif os.path.exists(base + "root_" + arch + "/" + f) :
62696ccc8cbSesaxe			fn = "root_" + arch + "/" + f
62796ccc8cbSesaxe		else :
62896ccc8cbSesaxe			base_present = False
62996ccc8cbSesaxe
63096ccc8cbSesaxe		if base_present :
63196ccc8cbSesaxe			if not os.path.exists(ptch + fn) :
63296ccc8cbSesaxe				ptch_present = False
63396ccc8cbSesaxe		else :
63496ccc8cbSesaxe			if os.path.exists(ptch + f) :
63596ccc8cbSesaxe				fn = f
63696ccc8cbSesaxe			elif os.path.exists(ptch + "root_" + arch + "/" + f) :
63796ccc8cbSesaxe				fn = "root_" + arch + "/" + f
63896ccc8cbSesaxe			else :
63996ccc8cbSesaxe				ptch_present = False
64096ccc8cbSesaxe
64196ccc8cbSesaxe		if os.path.islink(base + fn) :	# ignore links
64296ccc8cbSesaxe			base_present = False
64396ccc8cbSesaxe		if os.path.islink(ptch + fn) :
64496ccc8cbSesaxe			ptch_present = False
64596ccc8cbSesaxe
64696ccc8cbSesaxe		if base_present and ptch_present :
64796ccc8cbSesaxe			compFiles.append(fn)
64896ccc8cbSesaxe		elif base_present :
64996ccc8cbSesaxe			deletedFiles.append(fn)
65096ccc8cbSesaxe		elif ptch_present :
65196ccc8cbSesaxe			newFiles.append(fn)
65296ccc8cbSesaxe		else :
65393be19b9SAndy Fiddaman			if (os.path.islink(base + fn) and
65493be19b9SAndy Fiddaman			    os.path.islink(ptch + fn)) :
65596ccc8cbSesaxe				continue
65693be19b9SAndy Fiddaman			error(f + " in file list, but not in either tree. " +
657598cc7dfSVladimir Kotal			    "Skipping...")
65896ccc8cbSesaxe
65996ccc8cbSesaxe	return compFiles, newFiles, deletedFiles
66096ccc8cbSesaxe
66196ccc8cbSesaxe
66296ccc8cbSesaxe#
66396ccc8cbSesaxe# Build a fully qualified path to an external tool/utility.
66496ccc8cbSesaxe# Consider the default system locations. For onbld tools, if
66596ccc8cbSesaxe# the -t option was specified, we'll try to use built tools in $SRC tools,
66696ccc8cbSesaxe# and otherwise, we'll fall back on /opt/onbld/
66796ccc8cbSesaxe#
66896ccc8cbSesaxedef find_tool(tool) :
66996ccc8cbSesaxe
67096ccc8cbSesaxe	# First, check what was passed
67196ccc8cbSesaxe	if os.path.exists(tool) :
67296ccc8cbSesaxe		return tool
67396ccc8cbSesaxe
67496ccc8cbSesaxe	# Next try in wsdiff path
67596ccc8cbSesaxe	for pdir in wsdiff_path :
67696ccc8cbSesaxe		location = pdir + "/" + tool
67796ccc8cbSesaxe		if os.path.exists(location) :
67896ccc8cbSesaxe			return location + " "
67996ccc8cbSesaxe
68096ccc8cbSesaxe		location = pdir + "/" + arch + "/" + tool
68196ccc8cbSesaxe		if os.path.exists(location) :
68296ccc8cbSesaxe			return location + " "
68396ccc8cbSesaxe
68496ccc8cbSesaxe	error("Could not find path to: " + tool);
68596ccc8cbSesaxe	sys.exit(1);
68696ccc8cbSesaxe
68796ccc8cbSesaxe
68896ccc8cbSesaxe#####
68996ccc8cbSesaxe# ELF file comparison helper routines
69096ccc8cbSesaxe#
69196ccc8cbSesaxe
69296ccc8cbSesaxe#
69396ccc8cbSesaxe# Return a dictionary of ELF section types keyed by section name
69496ccc8cbSesaxe#
69596ccc8cbSesaxedef get_elfheader(f) :
69696ccc8cbSesaxe
69796ccc8cbSesaxe	header = {}
69896ccc8cbSesaxe
699ea7dde8fSAndy Fiddaman	rc, hstring = getoutput(elfdump_cmd + " -c " + f)
70096ccc8cbSesaxe
70196ccc8cbSesaxe	if len(hstring) == 0 :
70296ccc8cbSesaxe		error("Failed to dump ELF header for " + f)
703598cc7dfSVladimir Kotal		raise
70496ccc8cbSesaxe		return
70596ccc8cbSesaxe
70696ccc8cbSesaxe	# elfdump(1) dumps the section headers with the section name
70796ccc8cbSesaxe	# following "sh_name:", and the section type following "sh_type:"
70896ccc8cbSesaxe	sections = hstring.split("Section Header")
70996ccc8cbSesaxe	for sect in sections :
71096ccc8cbSesaxe		datap = sect.find("sh_name:");
71196ccc8cbSesaxe		if datap == -1 :
71296ccc8cbSesaxe			continue
71396ccc8cbSesaxe		section = sect[datap:].split()[1]
71496ccc8cbSesaxe		datap = sect.find("sh_type:");
71596ccc8cbSesaxe		if datap == -1 :
71693be19b9SAndy Fiddaman			error("Could not get type for sect: " + section +
71796ccc8cbSesaxe			      " in " + f)
71896ccc8cbSesaxe		sh_type = sect[datap:].split()[2]
71996ccc8cbSesaxe		header[section] = sh_type
72096ccc8cbSesaxe
72196ccc8cbSesaxe	return header
72296ccc8cbSesaxe
72396ccc8cbSesaxe#
72496ccc8cbSesaxe# Extract data in the specified ELF section from the given file
72596ccc8cbSesaxe#
72696ccc8cbSesaxedef extract_elf_section(f, section) :
72796ccc8cbSesaxe
728ea7dde8fSAndy Fiddaman	rc, data = getoutput(dump_cmd + " -sn " + section + " " + f)
72996ccc8cbSesaxe
73096ccc8cbSesaxe	if len(data) == 0 :
73193be19b9SAndy Fiddaman		error(dump_cmd + "yielded no data on section " + section +
732598cc7dfSVladimir Kotal		    " of " + f)
733598cc7dfSVladimir Kotal		raise
73496ccc8cbSesaxe		return
73596ccc8cbSesaxe
73696ccc8cbSesaxe	# dump(1) displays the file name to start...
73796ccc8cbSesaxe	# get past it to the data itself
73896ccc8cbSesaxe	dbegin = data.find(":") + 1
73996ccc8cbSesaxe	data = data[dbegin:];
74096ccc8cbSesaxe
74196ccc8cbSesaxe	return (data)
74296ccc8cbSesaxe
74396ccc8cbSesaxe#
74496ccc8cbSesaxe# Return a (hopefully meaningful) human readable set of diffs
74596ccc8cbSesaxe# for the specified ELF section between f1 and f2
74696ccc8cbSesaxe#
74796ccc8cbSesaxe# Depending on the section, various means for dumping and diffing
74896ccc8cbSesaxe# the data may be employed.
74996ccc8cbSesaxe#
750*a4f922faSAndy Fiddaman
75196ccc8cbSesaxetext_sections = [ '.text', '.init', '.fini' ]
752*a4f922faSAndy Fiddaman
753*a4f922faSAndy Fiddaman# Helper to generate the requireed commands for diffing two .SUNW_ctf
754*a4f922faSAndy Fiddaman# sections.
755*a4f922faSAndy Fiddamandef diff_ctf(f1, f2):
756*a4f922faSAndy Fiddaman
757*a4f922faSAndy Fiddaman	# Find genunix so that it can be used for parent CTF data when
758*a4f922faSAndy Fiddaman	# appropriate.
759*a4f922faSAndy Fiddaman	if diff_ctf.genunix1 is None:
760*a4f922faSAndy Fiddaman		global genunix, baseRoot, ptchRoot
761*a4f922faSAndy Fiddaman
762*a4f922faSAndy Fiddaman		d1 = protoroot(baseRoot)
763*a4f922faSAndy Fiddaman		d2 = protoroot(ptchRoot)
764*a4f922faSAndy Fiddaman		if (d1 and d2 and os.path.isfile(d1 + genunix) and
765*a4f922faSAndy Fiddaman		    os.path.isfile(d2 + genunix)):
766*a4f922faSAndy Fiddaman			diff_ctf.genunix1 = d1 + genunix
767*a4f922faSAndy Fiddaman			diff_ctf.genunix2 = d2 + genunix
768*a4f922faSAndy Fiddaman			debug("CTF: Found {}".format(diff_ctf.genunix1))
769*a4f922faSAndy Fiddaman			debug("CTF: Found {}".format(diff_ctf.genunix2))
770*a4f922faSAndy Fiddaman		else:
771*a4f922faSAndy Fiddaman			# Could not find genunix, do the best we can.
772*a4f922faSAndy Fiddaman			error("diff_ctf: Could not find genunix. " +
773*a4f922faSAndy Fiddaman			    "CTF diffs will be less useful.")
774*a4f922faSAndy Fiddaman			diff_ctf.genunix1 = diff_ctf.genunix2 = False
775*a4f922faSAndy Fiddaman
776*a4f922faSAndy Fiddaman	# Determine if this is a merged file from genunix by looking
777*a4f922faSAndy Fiddaman	# at the parent
778*a4f922faSAndy Fiddaman	rc, data = getoutput("{} -h {}".format(ctfdump_cmd, f1))
779*a4f922faSAndy Fiddaman	if rc != 0:
780*a4f922faSAndy Fiddaman		error("Could not read CTF header: {}".format(data))
781*a4f922faSAndy Fiddaman		return (None, None)
782*a4f922faSAndy Fiddaman
783*a4f922faSAndy Fiddaman	parent = None
784*a4f922faSAndy Fiddaman	for line in data.split('\n'):
785*a4f922faSAndy Fiddaman		if line.strip().startswith('cth_parname'):
786*a4f922faSAndy Fiddaman			try:
787*a4f922faSAndy Fiddaman				parent = line.split('=')[1].strip()
788*a4f922faSAndy Fiddaman				break
789*a4f922faSAndy Fiddaman			except:
790*a4f922faSAndy Fiddaman				pass
791*a4f922faSAndy Fiddaman
792*a4f922faSAndy Fiddaman	cmd1 = cmd2 = "{} -c ".format(ctfdump_cmd)
793*a4f922faSAndy Fiddaman	if parent == "genunix":
794*a4f922faSAndy Fiddaman		if diff_ctf.genunix1 and diff_ctf.genunix2:
795*a4f922faSAndy Fiddaman			cmd1 += "-p {} ".format(diff_ctf.genunix1)
796*a4f922faSAndy Fiddaman			cmd2 += "-p {} ".format(diff_ctf.genunix2)
797*a4f922faSAndy Fiddaman	elif parent is None or (len(parent) > 0 and parent != "(anon)"):
798*a4f922faSAndy Fiddaman		error("Unknown CTF Parent: {}".format(parent))
799*a4f922faSAndy Fiddaman		return (None, None)
800*a4f922faSAndy Fiddaman
801*a4f922faSAndy Fiddaman	cmd1 += f1
802*a4f922faSAndy Fiddaman	cmd2 += f2
803*a4f922faSAndy Fiddaman
804*a4f922faSAndy Fiddaman	return (cmd1, cmd2)
805*a4f922faSAndy Fiddaman
806*a4f922faSAndy Fiddamandiff_ctf.genunix1 = None
807*a4f922faSAndy Fiddamandiff_ctf.genunix2 = None
808*a4f922faSAndy Fiddaman
80996ccc8cbSesaxedef diff_elf_section(f1, f2, section, sh_type) :
81096ccc8cbSesaxe
811598cc7dfSVladimir Kotal	t = threading.currentThread()
812598cc7dfSVladimir Kotal	tmpFile1 = tmpDir1 + os.path.basename(f1) + t.getName()
813598cc7dfSVladimir Kotal	tmpFile2 = tmpDir2 + os.path.basename(f2) + t.getName()
814598cc7dfSVladimir Kotal
81596ccc8cbSesaxe	if (sh_type == "SHT_RELA") : # sh_type == SHT_RELA
81696ccc8cbSesaxe		cmd1 = elfdump_cmd + " -r " + f1 + " > " + tmpFile1
81796ccc8cbSesaxe		cmd2 = elfdump_cmd + " -r " + f2 + " > " + tmpFile2
81896ccc8cbSesaxe	elif (section == ".group") :
81996ccc8cbSesaxe		cmd1 = elfdump_cmd + " -g " + f1 + " > " + tmpFile1
82096ccc8cbSesaxe		cmd2 = elfdump_cmd + " -g " + f2 + " > " + tmpFile2
82196ccc8cbSesaxe	elif (section == ".hash") :
82296ccc8cbSesaxe		cmd1 = elfdump_cmd + " -h " + f1 + " > " + tmpFile1
82396ccc8cbSesaxe		cmd2 = elfdump_cmd + " -h " + f2 + " > " + tmpFile2
82496ccc8cbSesaxe	elif (section == ".dynamic") :
82596ccc8cbSesaxe		cmd1 = elfdump_cmd + " -d " + f1 + " > " + tmpFile1
82696ccc8cbSesaxe		cmd2 = elfdump_cmd + " -d " + f2 + " > " + tmpFile2
82796ccc8cbSesaxe	elif (section == ".got") :
82896ccc8cbSesaxe		cmd1 = elfdump_cmd + " -G " + f1 + " > " + tmpFile1
82996ccc8cbSesaxe		cmd2 = elfdump_cmd + " -G " + f2 + " > " + tmpFile2
83096ccc8cbSesaxe	elif (section == ".SUNW_cap") :
83196ccc8cbSesaxe		cmd1 = elfdump_cmd + " -H " + f1 + " > " + tmpFile1
83296ccc8cbSesaxe		cmd2 = elfdump_cmd + " -H " + f2 + " > " + tmpFile2
83396ccc8cbSesaxe	elif (section == ".interp") :
83496ccc8cbSesaxe		cmd1 = elfdump_cmd + " -i " + f1 + " > " + tmpFile1
83596ccc8cbSesaxe		cmd2 = elfdump_cmd + " -i " + f2 + " > " + tmpFile2
83696ccc8cbSesaxe	elif (section == ".symtab" or section == ".dynsym") :
83793be19b9SAndy Fiddaman		cmd1 = (elfdump_cmd + " -s -N " + section + " " + f1 +
83893be19b9SAndy Fiddaman		    " > " + tmpFile1)
83993be19b9SAndy Fiddaman		cmd2 = (elfdump_cmd + " -s -N " + section + " " + f2 +
84093be19b9SAndy Fiddaman		    " > " + tmpFile2)
84196ccc8cbSesaxe	elif (section in text_sections) :
84296ccc8cbSesaxe		# dis sometimes complains when it hits something it doesn't
84396ccc8cbSesaxe		# know how to disassemble. Just ignore it, as the output
84496ccc8cbSesaxe		# being generated here is human readable, and we've already
84596ccc8cbSesaxe		# correctly flagged the difference.
84693be19b9SAndy Fiddaman		cmd1 = (dis_cmd + " -t " + section + " " + f1 +
84793be19b9SAndy Fiddaman		       " 2>/dev/null | grep -v disassembly > " + tmpFile1)
84893be19b9SAndy Fiddaman		cmd2 = (dis_cmd + " -t " + section + " " + f2 +
84993be19b9SAndy Fiddaman		       " 2>/dev/null | grep -v disassembly > " + tmpFile2)
850*a4f922faSAndy Fiddaman	elif (section == ".SUNW_ctf"):
851*a4f922faSAndy Fiddaman		(cmd1, cmd2) = diff_ctf(f1, f2)
852*a4f922faSAndy Fiddaman		if not cmd1:
853*a4f922faSAndy Fiddaman			return ""
854*a4f922faSAndy Fiddaman		cmd1 += " > {}".format(tmpFile1)
855*a4f922faSAndy Fiddaman		cmd2 += " > {}".format(tmpFile2)
856*a4f922faSAndy Fiddaman
85796ccc8cbSesaxe	else :
85893be19b9SAndy Fiddaman		cmd1 = (elfdump_cmd + " -w " + tmpFile1 + " -N " +
85993be19b9SAndy Fiddaman		       section + " " + f1)
86093be19b9SAndy Fiddaman		cmd2 = (elfdump_cmd + " -w " + tmpFile2 + " -N " +
86193be19b9SAndy Fiddaman		       section + " " + f2)
86296ccc8cbSesaxe
86396ccc8cbSesaxe	os.system(cmd1)
86496ccc8cbSesaxe	os.system(cmd2)
86596ccc8cbSesaxe
86696ccc8cbSesaxe	data = diffFileData(tmpFile1, tmpFile2)
86796ccc8cbSesaxe
868598cc7dfSVladimir Kotal	# remove temp files as we no longer need them
869598cc7dfSVladimir Kotal	try:
870598cc7dfSVladimir Kotal		os.unlink(tmpFile1)
87193be19b9SAndy Fiddaman	except OSError as e:
872598cc7dfSVladimir Kotal		error("diff_elf_section: unlink failed %s" % e)
873598cc7dfSVladimir Kotal	try:
874598cc7dfSVladimir Kotal		os.unlink(tmpFile2)
87593be19b9SAndy Fiddaman	except OSError as e:
876598cc7dfSVladimir Kotal		error("diff_elf_section: unlink failed %s" % e)
877598cc7dfSVladimir Kotal
87896ccc8cbSesaxe	return (data)
87996ccc8cbSesaxe
88096ccc8cbSesaxe#
88196ccc8cbSesaxe# compare the relevant sections of two ELF binaries
88296ccc8cbSesaxe# and report any differences
88396ccc8cbSesaxe#
88496ccc8cbSesaxe# Returns: 1 if any differenes found
88596ccc8cbSesaxe#          0 if no differences found
88696ccc8cbSesaxe#	  -1 on error
88796ccc8cbSesaxe#
88896ccc8cbSesaxe
88996ccc8cbSesaxe# Sections deliberately not considered when comparing two ELF
89096ccc8cbSesaxe# binaries. Differences observed in these sections are not considered
89196ccc8cbSesaxe# significant where patch deliverable identification is concerned.
89296ccc8cbSesaxesections_to_skip = [ ".SUNW_signature",
89396ccc8cbSesaxe		     ".comment",
89496ccc8cbSesaxe		     ".debug",
89596ccc8cbSesaxe		     ".plt",
89696ccc8cbSesaxe		     ".rela.bss",
89796ccc8cbSesaxe		     ".rela.plt",
89896ccc8cbSesaxe		     ".line",
89996ccc8cbSesaxe		     ".note",
900f6a1d796Sesaxe		     ".compcom",
901*a4f922faSAndy Fiddaman		     ".SUNW_dof",
90296ccc8cbSesaxe		     ]
90396ccc8cbSesaxe
904*a4f922faSAndy Fiddamansections_preferred = [ ".SUNW_ctf",
905*a4f922faSAndy Fiddaman		       ".rodata.str1.8",
90696ccc8cbSesaxe		       ".rodata.str1.1",
90796ccc8cbSesaxe		       ".rodata",
90896ccc8cbSesaxe		       ".data1",
90996ccc8cbSesaxe		       ".data",
91096ccc8cbSesaxe		       ".text",
91196ccc8cbSesaxe		       ]
91296ccc8cbSesaxe
913*a4f922faSAndy Fiddaman# Some sections must always be extracted and diffed to check that there are
914*a4f922faSAndy Fiddaman# real differences.
915*a4f922faSAndy Fiddamansections_to_always_diff = [ ".SUNW_ctf" ]
916*a4f922faSAndy Fiddaman
91796ccc8cbSesaxedef compareElfs(base, ptch, quiet) :
91896ccc8cbSesaxe
91996ccc8cbSesaxe	global logging
92096ccc8cbSesaxe
921598cc7dfSVladimir Kotal	try:
92296ccc8cbSesaxe		base_header = get_elfheader(base)
923598cc7dfSVladimir Kotal	except:
924598cc7dfSVladimir Kotal		return
925ea7dde8fSAndy Fiddaman	sections = list(base_header.keys())
926*a4f922faSAndy Fiddaman	sections.sort()
92796ccc8cbSesaxe
928598cc7dfSVladimir Kotal	try:
92996ccc8cbSesaxe		ptch_header = get_elfheader(ptch)
930598cc7dfSVladimir Kotal	except:
931598cc7dfSVladimir Kotal		return
932ea7dde8fSAndy Fiddaman	e2_only_sections = list(ptch_header.keys())
933*a4f922faSAndy Fiddaman	e2_only_sections.sort()
93496ccc8cbSesaxe
93596ccc8cbSesaxe	e1_only_sections = []
93696ccc8cbSesaxe
93796ccc8cbSesaxe	fileName = fnFormat(base)
93896ccc8cbSesaxe
93996ccc8cbSesaxe	# Derive the list of ELF sections found only in
94096ccc8cbSesaxe	# either e1 or e2.
94196ccc8cbSesaxe	for sect in sections :
94296ccc8cbSesaxe		if not sect in e2_only_sections :
94396ccc8cbSesaxe			e1_only_sections.append(sect)
94496ccc8cbSesaxe		else :
94596ccc8cbSesaxe			e2_only_sections.remove(sect)
94696ccc8cbSesaxe
94796ccc8cbSesaxe	if len(e1_only_sections) > 0 :
94896ccc8cbSesaxe		if quiet :
94996ccc8cbSesaxe			return 1
95096ccc8cbSesaxe
951598cc7dfSVladimir Kotal		data = ""
952598cc7dfSVladimir Kotal		if logging :
95396ccc8cbSesaxe			slist = ""
95496ccc8cbSesaxe			for sect in e1_only_sections :
95596ccc8cbSesaxe				slist = slist + sect + "\t"
95693be19b9SAndy Fiddaman			data = ("ELF sections found in " +
95793be19b9SAndy Fiddaman				base + " but not in " + ptch +
95893be19b9SAndy Fiddaman				"\n\n" + slist)
959598cc7dfSVladimir Kotal
960598cc7dfSVladimir Kotal		difference(fileName, "ELF", data)
96196ccc8cbSesaxe		return 1
96296ccc8cbSesaxe
96396ccc8cbSesaxe	if len(e2_only_sections) > 0 :
96496ccc8cbSesaxe		if quiet :
96596ccc8cbSesaxe			return 1
96696ccc8cbSesaxe
967598cc7dfSVladimir Kotal		data = ""
968598cc7dfSVladimir Kotal		if logging :
96996ccc8cbSesaxe			slist = ""
97096ccc8cbSesaxe			for sect in e2_only_sections :
97196ccc8cbSesaxe				slist = slist + sect + "\t"
97293be19b9SAndy Fiddaman			data = ("ELF sections found in " +
97393be19b9SAndy Fiddaman				ptch + " but not in " + base +
97493be19b9SAndy Fiddaman				"\n\n" + slist)
975598cc7dfSVladimir Kotal
976598cc7dfSVladimir Kotal		difference(fileName, "ELF", data)
97796ccc8cbSesaxe		return 1
97896ccc8cbSesaxe
97996ccc8cbSesaxe	# Look for preferred sections, and put those at the
98096ccc8cbSesaxe	# top of the list of sections to compare
98196ccc8cbSesaxe	for psect in sections_preferred :
98296ccc8cbSesaxe		if psect in sections :
98396ccc8cbSesaxe			sections.remove(psect)
98496ccc8cbSesaxe			sections.insert(0, psect)
98596ccc8cbSesaxe
98696ccc8cbSesaxe	# Compare ELF sections
98796ccc8cbSesaxe	first_section = True
98896ccc8cbSesaxe	for sect in sections :
98996ccc8cbSesaxe
99096ccc8cbSesaxe		if sect in sections_to_skip :
99196ccc8cbSesaxe			continue
99296ccc8cbSesaxe
993598cc7dfSVladimir Kotal		try:
99496ccc8cbSesaxe			s1 = extract_elf_section(base, sect);
995598cc7dfSVladimir Kotal		except:
996598cc7dfSVladimir Kotal			return
997598cc7dfSVladimir Kotal
998598cc7dfSVladimir Kotal		try:
99996ccc8cbSesaxe			s2 = extract_elf_section(ptch, sect);
1000598cc7dfSVladimir Kotal		except:
1001598cc7dfSVladimir Kotal			return
100296ccc8cbSesaxe
100396ccc8cbSesaxe		if len(s1) != len (s2) or s1 != s2:
1004*a4f922faSAndy Fiddaman			if not quiet or sect in sections_to_always_diff:
100596ccc8cbSesaxe				sh_type = base_header[sect]
100693be19b9SAndy Fiddaman				data = diff_elf_section(base, ptch,
1007598cc7dfSVladimir Kotal							sect, sh_type)
100896ccc8cbSesaxe
1009*a4f922faSAndy Fiddaman				if len(data) == 0:
1010*a4f922faSAndy Fiddaman					continue # No differences
1011*a4f922faSAndy Fiddaman
1012*a4f922faSAndy Fiddaman			if not quiet:
101396ccc8cbSesaxe				# If all ELF sections are being reported, then
101496ccc8cbSesaxe				# invoke difference() to flag the file name to
1015*a4f922faSAndy Fiddaman				# stdout only once. Any other section
1016*a4f922faSAndy Fiddaman				# differences should be logged to the results
1017*a4f922faSAndy Fiddaman				# file directly
101896ccc8cbSesaxe				if not first_section :
101993be19b9SAndy Fiddaman					log_difference(fileName,
1020598cc7dfSVladimir Kotal					    "ELF " + sect, data)
102196ccc8cbSesaxe				else :
102293be19b9SAndy Fiddaman					difference(fileName, "ELF " + sect,
1023598cc7dfSVladimir Kotal					    data)
102496ccc8cbSesaxe
102596ccc8cbSesaxe			if not reportAllSects :
102696ccc8cbSesaxe				return 1
102796ccc8cbSesaxe			first_section = False
1028598cc7dfSVladimir Kotal
102996ccc8cbSesaxe	return 0
103096ccc8cbSesaxe
103196ccc8cbSesaxe#####
1032598cc7dfSVladimir Kotal# recursively remove 2 directories
1033598cc7dfSVladimir Kotal#
1034598cc7dfSVladimir Kotal# Used for removal of temporary directory strucures (ignores any errors).
1035598cc7dfSVladimir Kotal#
1036598cc7dfSVladimir Kotaldef clearTmpDirs(dir1, dir2) :
1037598cc7dfSVladimir Kotal
1038598cc7dfSVladimir Kotal	if os.path.isdir(dir1) > 0 :
1039598cc7dfSVladimir Kotal		shutil.rmtree(dir1, True)
1040598cc7dfSVladimir Kotal
1041598cc7dfSVladimir Kotal	if os.path.isdir(dir2) > 0 :
1042598cc7dfSVladimir Kotal		shutil.rmtree(dir2, True)
1043598cc7dfSVladimir Kotal
1044598cc7dfSVladimir Kotal
1045598cc7dfSVladimir Kotal#####
104696ccc8cbSesaxe# Archive object comparison
104796ccc8cbSesaxe#
104896ccc8cbSesaxe# Returns 1 if difference detected
104996ccc8cbSesaxe#         0 if no difference detected
105096ccc8cbSesaxe#        -1 on error
105196ccc8cbSesaxe#
105296ccc8cbSesaxedef compareArchives(base, ptch, fileType) :
105396ccc8cbSesaxe
105496ccc8cbSesaxe	fileName = fnFormat(base)
1055598cc7dfSVladimir Kotal	t = threading.currentThread()
1056598cc7dfSVladimir Kotal	ArchTmpDir1 = tmpDir1 + os.path.basename(base) + t.getName()
1057598cc7dfSVladimir Kotal	ArchTmpDir2 = tmpDir2 + os.path.basename(base) + t.getName()
105896ccc8cbSesaxe
105996ccc8cbSesaxe	#
106096ccc8cbSesaxe	# Be optimistic and first try a straight file compare
106196ccc8cbSesaxe	# as it will allow us to finish up quickly.
1062598cc7dfSVladimir Kotal	#
106396ccc8cbSesaxe	if compareBasic(base, ptch, True, fileType) == 0 :
106496ccc8cbSesaxe		return 0
106596ccc8cbSesaxe
1066598cc7dfSVladimir Kotal	try:
1067598cc7dfSVladimir Kotal		os.makedirs(ArchTmpDir1)
106893be19b9SAndy Fiddaman	except OSError as e:
1069598cc7dfSVladimir Kotal		error("compareArchives: makedir failed %s" % e)
1070598cc7dfSVladimir Kotal		return -1
1071598cc7dfSVladimir Kotal	try:
1072598cc7dfSVladimir Kotal		os.makedirs(ArchTmpDir2)
107393be19b9SAndy Fiddaman	except OSError as e:
1074598cc7dfSVladimir Kotal		error("compareArchives: makedir failed %s" % e)
1075598cc7dfSVladimir Kotal		return -1
1076598cc7dfSVladimir Kotal
107796ccc8cbSesaxe	# copy over the objects to the temp areas, and
107896ccc8cbSesaxe	# unpack them
1079598cc7dfSVladimir Kotal	baseCmd = "cp -fp " + base + " " + ArchTmpDir1
1080ea7dde8fSAndy Fiddaman	rc, output = getoutput(baseCmd)
1081ea7dde8fSAndy Fiddaman	if rc != 0:
108296ccc8cbSesaxe		error(baseCmd + " failed: " + output)
1083598cc7dfSVladimir Kotal		clearTmpDirs(ArchTmpDir1, ArchTmpDir2)
108496ccc8cbSesaxe		return -1
108596ccc8cbSesaxe
1086598cc7dfSVladimir Kotal	ptchCmd = "cp -fp " + ptch + " " + ArchTmpDir2
1087ea7dde8fSAndy Fiddaman	rc, output = getoutput(ptchCmd)
1088ea7dde8fSAndy Fiddaman	if rc != 0:
108996ccc8cbSesaxe		error(ptchCmd + " failed: " + output)
1090598cc7dfSVladimir Kotal		clearTmpDirs(ArchTmpDir1, ArchTmpDir2)
109196ccc8cbSesaxe		return -1
109296ccc8cbSesaxe
1093ea7dde8fSAndy Fiddaman	bname = fileName.split('/')[-1]
109496ccc8cbSesaxe	if fileType == "Java Archive" :
109593be19b9SAndy Fiddaman		baseCmd = ("cd " + ArchTmpDir1 + "; " + "jar xf " + bname +
109693be19b9SAndy Fiddaman			  "; rm -f " + bname + " META-INF/MANIFEST.MF")
109793be19b9SAndy Fiddaman		ptchCmd = ("cd " + ArchTmpDir2 + "; " + "jar xf " + bname +
109893be19b9SAndy Fiddaman			  "; rm -f " + bname + " META-INF/MANIFEST.MF")
109996ccc8cbSesaxe	elif fileType == "ELF Object Archive" :
110093be19b9SAndy Fiddaman		baseCmd = ("cd " + ArchTmpDir1 + "; " + "/usr/ccs/bin/ar x " +
110193be19b9SAndy Fiddaman			  bname + "; rm -f " + bname)
110293be19b9SAndy Fiddaman		ptchCmd = ("cd " + ArchTmpDir2 + "; " + "/usr/ccs/bin/ar x " +
110393be19b9SAndy Fiddaman			  bname + "; rm -f " + bname)
110496ccc8cbSesaxe	else :
110596ccc8cbSesaxe		error("unexpected file type: " + fileType)
1106598cc7dfSVladimir Kotal		clearTmpDirs(ArchTmpDir1, ArchTmpDir2)
110796ccc8cbSesaxe		return -1
110896ccc8cbSesaxe
110996ccc8cbSesaxe	os.system(baseCmd)
111096ccc8cbSesaxe	os.system(ptchCmd)
111196ccc8cbSesaxe
1112598cc7dfSVladimir Kotal	baseFlist = list(findFiles(ArchTmpDir1))
1113598cc7dfSVladimir Kotal	ptchFlist = list(findFiles(ArchTmpDir2))
111496ccc8cbSesaxe
111596ccc8cbSesaxe	# Trim leading path off base/ptch file lists
111696ccc8cbSesaxe	flist = []
111796ccc8cbSesaxe	for fn in baseFlist :
1118598cc7dfSVladimir Kotal		flist.append(str_prefix_trunc(fn, ArchTmpDir1))
111996ccc8cbSesaxe	baseFlist = flist
112096ccc8cbSesaxe
112196ccc8cbSesaxe	flist = []
112296ccc8cbSesaxe	for fn in ptchFlist :
1123598cc7dfSVladimir Kotal		flist.append(str_prefix_trunc(fn, ArchTmpDir2))
112496ccc8cbSesaxe	ptchFlist = flist
112596ccc8cbSesaxe
112696ccc8cbSesaxe	for fn in ptchFlist :
112796ccc8cbSesaxe		if not fn in baseFlist :
112893be19b9SAndy Fiddaman			difference(fileName, fileType,
112996ccc8cbSesaxe				   fn + " added to " + fileName)
1130598cc7dfSVladimir Kotal			clearTmpDirs(ArchTmpDir1, ArchTmpDir2)
113196ccc8cbSesaxe			return 1
113296ccc8cbSesaxe
113396ccc8cbSesaxe	for fn in baseFlist :
113496ccc8cbSesaxe		if not fn in ptchFlist :
113593be19b9SAndy Fiddaman			difference(fileName, fileType,
113696ccc8cbSesaxe				   fn + " removed from " + fileName)
1137598cc7dfSVladimir Kotal			clearTmpDirs(ArchTmpDir1, ArchTmpDir2)
113896ccc8cbSesaxe			return 1
113996ccc8cbSesaxe
114093be19b9SAndy Fiddaman		differs = compareOneFile((ArchTmpDir1 + fn),
1141598cc7dfSVladimir Kotal		    (ArchTmpDir2 + fn), True)
114296ccc8cbSesaxe		if differs :
114393be19b9SAndy Fiddaman			difference(fileName, fileType,
114496ccc8cbSesaxe				   fn + " in " + fileName + " differs")
1145598cc7dfSVladimir Kotal			clearTmpDirs(ArchTmpDir1, ArchTmpDir2)
114696ccc8cbSesaxe			return 1
1147598cc7dfSVladimir Kotal
1148598cc7dfSVladimir Kotal	clearTmpDirs(ArchTmpDir1, ArchTmpDir2)
114996ccc8cbSesaxe	return 0
115096ccc8cbSesaxe
115196ccc8cbSesaxe#####
115296ccc8cbSesaxe# (Basic) file comparison
115396ccc8cbSesaxe#
115496ccc8cbSesaxe# Returns 1 if difference detected
115596ccc8cbSesaxe#         0 if no difference detected
115696ccc8cbSesaxe#        -1 on error
115796ccc8cbSesaxe#
115896ccc8cbSesaxedef compareBasic(base, ptch, quiet, fileType) :
115996ccc8cbSesaxe
116096ccc8cbSesaxe	fileName = fnFormat(base);
116196ccc8cbSesaxe
116296ccc8cbSesaxe	if quiet and os.stat(base)[ST_SIZE] != os.stat(ptch)[ST_SIZE] :
116396ccc8cbSesaxe		return 1
116496ccc8cbSesaxe
116596ccc8cbSesaxe	try:
1166a62941d8SAndy Fiddaman		with open(base, 'rb') as fh:
1167a62941d8SAndy Fiddaman			baseData = fh.read()
116896ccc8cbSesaxe	except:
116996ccc8cbSesaxe		error("could not open " + base)
117096ccc8cbSesaxe		return -1
1171a62941d8SAndy Fiddaman
117296ccc8cbSesaxe	try:
1173a62941d8SAndy Fiddaman		with open(ptch, 'rb') as fh:
1174a62941d8SAndy Fiddaman			ptchData = fh.read()
117596ccc8cbSesaxe	except:
117696ccc8cbSesaxe		error("could not open " + ptch)
117796ccc8cbSesaxe		return -1
117896ccc8cbSesaxe
117996ccc8cbSesaxe	if quiet :
118096ccc8cbSesaxe		if baseData != ptchData :
118196ccc8cbSesaxe			return 1
118296ccc8cbSesaxe	else :
118396ccc8cbSesaxe		if len(baseData) != len(ptchData) or baseData != ptchData :
1184a62941d8SAndy Fiddaman			diffs = diffFileData(base, ptch)
118596ccc8cbSesaxe			difference(fileName, fileType, diffs)
118696ccc8cbSesaxe			return 1
118796ccc8cbSesaxe	return 0
118896ccc8cbSesaxe
118996ccc8cbSesaxe
119096ccc8cbSesaxe#####
119196ccc8cbSesaxe# Compare two objects by producing a data dump from
119296ccc8cbSesaxe# each object, and then comparing the dump data
119396ccc8cbSesaxe#
119496ccc8cbSesaxe# Returns: 1 if a difference is detected
119596ccc8cbSesaxe#          0 if no difference detected
119696ccc8cbSesaxe#         -1 upon error
119796ccc8cbSesaxe#
119896ccc8cbSesaxedef compareByDumping(base, ptch, quiet, fileType) :
119996ccc8cbSesaxe
120096ccc8cbSesaxe	fileName = fnFormat(base);
1201598cc7dfSVladimir Kotal	t = threading.currentThread()
1202598cc7dfSVladimir Kotal	tmpFile1 = tmpDir1 + os.path.basename(base) + t.getName()
1203598cc7dfSVladimir Kotal	tmpFile2 = tmpDir2 + os.path.basename(ptch) + t.getName()
120496ccc8cbSesaxe
120596ccc8cbSesaxe	if fileType == "Lint Library" :
120693be19b9SAndy Fiddaman		baseCmd = (lintdump_cmd + " -ir " + base +
120793be19b9SAndy Fiddaman			  " | egrep -v '(LINTOBJ|LINTMOD):'" +
120893be19b9SAndy Fiddaman			  " | grep -v PASS[1-3]:" +
120993be19b9SAndy Fiddaman			  " > " + tmpFile1)
121093be19b9SAndy Fiddaman		ptchCmd = (lintdump_cmd + " -ir " + ptch +
121193be19b9SAndy Fiddaman			  " | egrep -v '(LINTOBJ|LINTMOD):'" +
121293be19b9SAndy Fiddaman			  " | grep -v PASS[1-3]:" +
121393be19b9SAndy Fiddaman			  " > " + tmpFile2)
121496ccc8cbSesaxe	elif fileType == "Sqlite Database" :
121593be19b9SAndy Fiddaman		baseCmd = ("echo .dump | " + sqlite_cmd + base + " > " +
121693be19b9SAndy Fiddaman			  tmpFile1)
121793be19b9SAndy Fiddaman		ptchCmd = ("echo .dump | " + sqlite_cmd + ptch + " > " +
121893be19b9SAndy Fiddaman			  tmpFile2)
121996ccc8cbSesaxe
122096ccc8cbSesaxe	os.system(baseCmd)
122196ccc8cbSesaxe	os.system(ptchCmd)
122296ccc8cbSesaxe
122396ccc8cbSesaxe	try:
1224a62941d8SAndy Fiddaman		with open(tmpFile1, 'rb') as fh:
1225a62941d8SAndy Fiddaman			baseData = fh.read()
122696ccc8cbSesaxe	except:
122796ccc8cbSesaxe		error("could not open: " + tmpFile1)
1228598cc7dfSVladimir Kotal		return
1229a62941d8SAndy Fiddaman
123096ccc8cbSesaxe	try:
1231a62941d8SAndy Fiddaman		with open(tmpFile2, 'rb') as fh:
1232a62941d8SAndy Fiddaman			ptchData = fh.read()
123396ccc8cbSesaxe	except:
123496ccc8cbSesaxe		error("could not open: " + tmpFile2)
1235598cc7dfSVladimir Kotal		return
123696ccc8cbSesaxe
1237a62941d8SAndy Fiddaman	ret = 0
123896ccc8cbSesaxe
123996ccc8cbSesaxe	if len(baseData) != len(ptchData) or baseData != ptchData :
124096ccc8cbSesaxe		if not quiet :
124196ccc8cbSesaxe			data = diffFileData(tmpFile1, tmpFile2);
1242a62941d8SAndy Fiddaman		ret = 1
1243598cc7dfSVladimir Kotal
1244598cc7dfSVladimir Kotal	# Remove the temporary files now.
1245598cc7dfSVladimir Kotal	try:
1246598cc7dfSVladimir Kotal		os.unlink(tmpFile1)
124793be19b9SAndy Fiddaman	except OSError as e:
1248598cc7dfSVladimir Kotal		error("compareByDumping: unlink failed %s" % e)
1249598cc7dfSVladimir Kotal	try:
1250598cc7dfSVladimir Kotal		os.unlink(tmpFile2)
125193be19b9SAndy Fiddaman	except OSError as e:
1252598cc7dfSVladimir Kotal		error("compareByDumping: unlink failed %s" % e)
1253598cc7dfSVladimir Kotal
1254a62941d8SAndy Fiddaman	return ret
125596ccc8cbSesaxe
125696ccc8cbSesaxe#####
1257619b4598Srotondo#
1258598cc7dfSVladimir Kotal# SIGINT signal handler. Changes thread control variable to tell the threads
1259598cc7dfSVladimir Kotal# to finish their current job and exit.
1260619b4598Srotondo#
1261598cc7dfSVladimir Kotaldef discontinue_processing(signl, frme):
1262598cc7dfSVladimir Kotal	global keep_processing
1263619b4598Srotondo
126493be19b9SAndy Fiddaman	print("Caught Ctrl-C, stopping the threads", file=sys.stderr)
1265598cc7dfSVladimir Kotal	keep_processing = False
1266619b4598Srotondo
1267598cc7dfSVladimir Kotal	return 0
1268619b4598Srotondo
1269598cc7dfSVladimir Kotal#####
1270598cc7dfSVladimir Kotal#
1271598cc7dfSVladimir Kotal# worker thread for changedFiles processing
1272598cc7dfSVladimir Kotal#
1273598cc7dfSVladimir Kotalclass workerThread(threading.Thread) :
1274598cc7dfSVladimir Kotal	def run(self):
1275598cc7dfSVladimir Kotal		global wset_lock
1276598cc7dfSVladimir Kotal		global changedFiles
1277598cc7dfSVladimir Kotal		global baseRoot
1278598cc7dfSVladimir Kotal		global ptchRoot
1279598cc7dfSVladimir Kotal		global keep_processing
1280619b4598Srotondo
1281598cc7dfSVladimir Kotal		while (keep_processing) :
1282598cc7dfSVladimir Kotal			# grab the lock to changedFiles and remove one member
1283598cc7dfSVladimir Kotal			# and process it
1284598cc7dfSVladimir Kotal			wset_lock.acquire()
1285598cc7dfSVladimir Kotal			try :
1286598cc7dfSVladimir Kotal				fn = changedFiles.pop()
1287598cc7dfSVladimir Kotal			except IndexError :
1288598cc7dfSVladimir Kotal				# there is nothing more to do
1289598cc7dfSVladimir Kotal				wset_lock.release()
1290598cc7dfSVladimir Kotal				return
1291598cc7dfSVladimir Kotal			wset_lock.release()
1292598cc7dfSVladimir Kotal
1293598cc7dfSVladimir Kotal			base = baseRoot + fn
1294598cc7dfSVladimir Kotal			ptch = ptchRoot + fn
1295598cc7dfSVladimir Kotal
1296598cc7dfSVladimir Kotal			compareOneFile(base, ptch, False)
1297598cc7dfSVladimir Kotal
1298619b4598Srotondo
1299619b4598Srotondo#####
130096ccc8cbSesaxe# Compare two objects. Detect type changes.
130196ccc8cbSesaxe# Vector off to the appropriate type specific
130296ccc8cbSesaxe# compare routine based on the type.
130396ccc8cbSesaxe#
130496ccc8cbSesaxedef compareOneFile(base, ptch, quiet) :
130596ccc8cbSesaxe
130696ccc8cbSesaxe	# Verify the file types.
130796ccc8cbSesaxe	# If they are different, indicate this and move on
130896ccc8cbSesaxe	btype = getTheFileType(base)
130996ccc8cbSesaxe	ptype = getTheFileType(ptch)
131096ccc8cbSesaxe
1311619b4598Srotondo	if btype == 'Error' or ptype == 'Error' :
1312619b4598Srotondo		return -1
1313619b4598Srotondo
131496ccc8cbSesaxe	fileName = fnFormat(base)
131596ccc8cbSesaxe
131696ccc8cbSesaxe	if (btype != ptype) :
1317619b4598Srotondo		if not quiet :
1318*a4f922faSAndy Fiddaman			difference(fileName, "file type",
1319*a4f922faSAndy Fiddaman			    btype + " to " + ptype)
132096ccc8cbSesaxe		return 1
132196ccc8cbSesaxe	else :
132296ccc8cbSesaxe		fileType = btype
132396ccc8cbSesaxe
132496ccc8cbSesaxe	if (fileType == 'ELF') :
132596ccc8cbSesaxe		return compareElfs(base, ptch, quiet)
132696ccc8cbSesaxe
132796ccc8cbSesaxe	elif (fileType == 'Java Archive' or fileType == 'ELF Object Archive') :
132896ccc8cbSesaxe		return compareArchives(base, ptch, fileType)
132996ccc8cbSesaxe
133096ccc8cbSesaxe	elif (fileType == 'HTML') :
133196ccc8cbSesaxe		return compareBasic(base, ptch, quiet, fileType)
133296ccc8cbSesaxe
133396ccc8cbSesaxe	elif ( fileType == 'Lint Library' ) :
133496ccc8cbSesaxe		return compareByDumping(base, ptch, quiet, fileType)
133596ccc8cbSesaxe
133696ccc8cbSesaxe	elif ( fileType == 'Sqlite Database' ) :
133796ccc8cbSesaxe		return compareByDumping(base, ptch, quiet, fileType)
1338619b4598Srotondo
133996ccc8cbSesaxe	else :
134096ccc8cbSesaxe		# it has to be some variety of text file
134196ccc8cbSesaxe		return compareBasic(base, ptch, quiet, fileType)
134296ccc8cbSesaxe
134396ccc8cbSesaxe# Cleanup and self-terminate
134496ccc8cbSesaxedef cleanup(ret) :
134596ccc8cbSesaxe
1346598cc7dfSVladimir Kotal	debug("Performing cleanup (" + str(ret) + ")")
1347598cc7dfSVladimir Kotal	if os.path.isdir(tmpDir1) > 0 :
1348598cc7dfSVladimir Kotal		shutil.rmtree(tmpDir1)
134996ccc8cbSesaxe
1350598cc7dfSVladimir Kotal	if os.path.isdir(tmpDir2) > 0 :
1351598cc7dfSVladimir Kotal		shutil.rmtree(tmpDir2)
135296ccc8cbSesaxe
135396ccc8cbSesaxe	if logging :
135496ccc8cbSesaxe		log.close()
135596ccc8cbSesaxe
135696ccc8cbSesaxe	sys.exit(ret)
135796ccc8cbSesaxe
135896ccc8cbSesaxedef main() :
135996ccc8cbSesaxe
136096ccc8cbSesaxe	# Log file handle
136196ccc8cbSesaxe	global log
136296ccc8cbSesaxe
136396ccc8cbSesaxe	# Globals relating to command line options
136496ccc8cbSesaxe	global logging, vdiffs, reportAllSects
136596ccc8cbSesaxe
136696ccc8cbSesaxe	# Named temporary files / directories
1367598cc7dfSVladimir Kotal	global tmpDir1, tmpDir2
136896ccc8cbSesaxe
136996ccc8cbSesaxe	# Command paths
1370*a4f922faSAndy Fiddaman	global lintdump_cmd, elfdump_cmd, dump_cmd, dis_cmd, od_cmd, \
1371*a4f922faSAndy Fiddaman	    diff_cmd, sqlite_cmd, ctfdump_cmd
137296ccc8cbSesaxe
137396ccc8cbSesaxe	# Default search path
137496ccc8cbSesaxe	global wsdiff_path
137596ccc8cbSesaxe
137696ccc8cbSesaxe	# Essentially "uname -p"
137796ccc8cbSesaxe	global arch
137896ccc8cbSesaxe
1379598cc7dfSVladimir Kotal	# changed files for worker thread processing
1380598cc7dfSVladimir Kotal	global changedFiles
1381598cc7dfSVladimir Kotal	global baseRoot
1382598cc7dfSVladimir Kotal	global ptchRoot
1383598cc7dfSVladimir Kotal
1384598cc7dfSVladimir Kotal	# Sort the list of files from a temporary file
1385*a4f922faSAndy Fiddaman	global o_sorted
1386598cc7dfSVladimir Kotal	global differentFiles
1387598cc7dfSVladimir Kotal
1388598cc7dfSVladimir Kotal	# Debugging indicator
1389598cc7dfSVladimir Kotal	global debugon
1390598cc7dfSVladimir Kotal
139196ccc8cbSesaxe	# Some globals need to be initialized
1392*a4f922faSAndy Fiddaman	debugon = logging = vdiffs = reportAllSects = o_sorted = False
139396ccc8cbSesaxe
139496ccc8cbSesaxe	# Process command line arguments
139596ccc8cbSesaxe	# Return values are returned from args() in alpha order
139696ccc8cbSesaxe	# (Yes, python functions can return multiple values (ewww))
139796ccc8cbSesaxe	# Note that args() also set the globals:
139896ccc8cbSesaxe	#	logging to True if verbose logging (to a file) was enabled
139996ccc8cbSesaxe	#	vdiffs to True if logged differences aren't to be truncated
1400*a4f922faSAndy Fiddaman	#	reportAllSects to True if all ELF section differences are to
1401*a4f922faSAndy Fiddaman	#	be reported
140296ccc8cbSesaxe	#
140396ccc8cbSesaxe	baseRoot, fileNamesFile, localTools, ptchRoot, results = args()
140496ccc8cbSesaxe
140596ccc8cbSesaxe	#
140696ccc8cbSesaxe	# Set up the results/log file
140796ccc8cbSesaxe	#
140896ccc8cbSesaxe	if logging :
140996ccc8cbSesaxe		try:
141096ccc8cbSesaxe			log = open(results, "w")
141196ccc8cbSesaxe		except:
141296ccc8cbSesaxe			logging = False
141396ccc8cbSesaxe			error("failed to open log file: " + log)
141496ccc8cbSesaxe			sys.exit(1)
141596ccc8cbSesaxe
1416*a4f922faSAndy Fiddaman		dateTimeStr= "# %04d-%02d-%02d at %02d:%02d:%02d" % \
1417*a4f922faSAndy Fiddaman		    time.localtime()[:6]
141896ccc8cbSesaxe		v_info("# This file was produced by wsdiff")
141996ccc8cbSesaxe		v_info(dateTimeStr)
142096ccc8cbSesaxe
1421598cc7dfSVladimir Kotal	# Changed files (used only for the sorted case)
1422*a4f922faSAndy Fiddaman	if o_sorted :
1423598cc7dfSVladimir Kotal		differentFiles = []
1424598cc7dfSVladimir Kotal
142596ccc8cbSesaxe	#
142696ccc8cbSesaxe	# Build paths to the tools required tools
142796ccc8cbSesaxe	#
142896ccc8cbSesaxe	# Try to look for tools in $SRC/tools if the "-t" option
142996ccc8cbSesaxe	# was specified
143096ccc8cbSesaxe	#
1431ea7dde8fSAndy Fiddaman	rc, arch = getoutput("uname -p")
1432ea7dde8fSAndy Fiddaman	arch = arch.rstrip()
143396ccc8cbSesaxe	if localTools :
143496ccc8cbSesaxe		try:
143596ccc8cbSesaxe			src = os.environ['SRC']
143696ccc8cbSesaxe		except:
1437*a4f922faSAndy Fiddaman			error("-t specified, but $SRC not set. " +
1438*a4f922faSAndy Fiddaman			    "Cannot find $SRC/tools")
143996ccc8cbSesaxe			src = ""
144096ccc8cbSesaxe		if len(src) > 0 :
1441*a4f922faSAndy Fiddaman			wsdiff_path.insert(0,
1442*a4f922faSAndy Fiddaman			    src + "/tools/proto/opt/onbld/bin")
144396ccc8cbSesaxe
144496ccc8cbSesaxe	lintdump_cmd = find_tool("lintdump")
144596ccc8cbSesaxe	elfdump_cmd = find_tool("elfdump")
144696ccc8cbSesaxe	dump_cmd = find_tool("dump")
144796ccc8cbSesaxe	od_cmd = find_tool("od")
144896ccc8cbSesaxe	dis_cmd = find_tool("dis")
144996ccc8cbSesaxe	diff_cmd = find_tool("diff")
145096ccc8cbSesaxe	sqlite_cmd = find_tool("sqlite")
1451*a4f922faSAndy Fiddaman	ctfdump_cmd = find_tool("ctfdump")
145296ccc8cbSesaxe
145396ccc8cbSesaxe	#
1454598cc7dfSVladimir Kotal	# Set resource limit for number of open files as high as possible.
1455598cc7dfSVladimir Kotal	# This might get handy with big number of threads.
1456598cc7dfSVladimir Kotal	#
1457598cc7dfSVladimir Kotal	(nofile_soft, nofile_hard) = resource.getrlimit(resource.RLIMIT_NOFILE)
1458598cc7dfSVladimir Kotal	try:
1459598cc7dfSVladimir Kotal		resource.setrlimit(resource.RLIMIT_NOFILE,
1460598cc7dfSVladimir Kotal		    (nofile_hard, nofile_hard))
1461598cc7dfSVladimir Kotal	except:
1462598cc7dfSVladimir Kotal		error("cannot set resource limits for number of open files")
1463598cc7dfSVladimir Kotal		sys.exit(1)
1464598cc7dfSVladimir Kotal
1465598cc7dfSVladimir Kotal	#
146696ccc8cbSesaxe	# validate the base and patch paths
146796ccc8cbSesaxe	#
146896ccc8cbSesaxe	if baseRoot[-1] != '/' :
146996ccc8cbSesaxe		baseRoot += '/'
147096ccc8cbSesaxe
147196ccc8cbSesaxe	if ptchRoot[-1] != '/' :
147296ccc8cbSesaxe		ptchRoot += '/'
147396ccc8cbSesaxe
147496ccc8cbSesaxe	if not os.path.exists(baseRoot) :
147596ccc8cbSesaxe		error("old proto area: " + baseRoot + " does not exist")
147696ccc8cbSesaxe		sys.exit(1)
147796ccc8cbSesaxe
147896ccc8cbSesaxe	if not os.path.exists(ptchRoot) :
147993be19b9SAndy Fiddaman		error("new proto area: " + ptchRoot + " does not exist")
148096ccc8cbSesaxe		sys.exit(1)
148196ccc8cbSesaxe
148296ccc8cbSesaxe	#
148396ccc8cbSesaxe	# log some information identifying the run
148496ccc8cbSesaxe	#
148596ccc8cbSesaxe	v_info("Old proto area: " + baseRoot)
148696ccc8cbSesaxe	v_info("New proto area: " + ptchRoot)
148796ccc8cbSesaxe	v_info("Results file: " + results + "\n")
148896ccc8cbSesaxe
148996ccc8cbSesaxe	#
149096ccc8cbSesaxe	# Set up the temporary directories / files
149196ccc8cbSesaxe	# Could use python's tmpdir routines, but these should
149296ccc8cbSesaxe	# be easier to identify / keep around for debugging
149396ccc8cbSesaxe	pid = os.getpid()
149496ccc8cbSesaxe	tmpDir1 = "/tmp/wsdiff_tmp1_" + str(pid) + "/"
149596ccc8cbSesaxe	tmpDir2 = "/tmp/wsdiff_tmp2_" + str(pid) + "/"
1496598cc7dfSVladimir Kotal	try:
149796ccc8cbSesaxe		os.makedirs(tmpDir1)
149893be19b9SAndy Fiddaman	except OSError as e:
1499598cc7dfSVladimir Kotal		error("main: makedir failed %s" % e)
1500598cc7dfSVladimir Kotal	try:
150196ccc8cbSesaxe		os.makedirs(tmpDir2)
150293be19b9SAndy Fiddaman	except OSError as e:
1503598cc7dfSVladimir Kotal		error("main: makedir failed %s" % e)
150496ccc8cbSesaxe
150596ccc8cbSesaxe	# Derive a catalog of new, deleted, and to-be-compared objects
150696ccc8cbSesaxe	# either from the specified base and patch proto areas, or from
150796ccc8cbSesaxe	# from an input file list
150896ccc8cbSesaxe	newOrDeleted = False
150996ccc8cbSesaxe
151096ccc8cbSesaxe	if fileNamesFile != "" :
151196ccc8cbSesaxe		changedFiles, newFiles, deletedFiles = \
151296ccc8cbSesaxe			      flistCatalog(baseRoot, ptchRoot, fileNamesFile)
151396ccc8cbSesaxe	else :
1514598cc7dfSVladimir Kotal		changedFiles, newFiles, deletedFiles = \
1515598cc7dfSVladimir Kotal				protoCatalog(baseRoot, ptchRoot)
151696ccc8cbSesaxe
151796ccc8cbSesaxe	if len(newFiles) > 0 :
151896ccc8cbSesaxe		newOrDeleted = True
151996ccc8cbSesaxe		info("\nNew objects found: ")
152096ccc8cbSesaxe
1521*a4f922faSAndy Fiddaman		if o_sorted :
1522598cc7dfSVladimir Kotal			newFiles.sort()
152396ccc8cbSesaxe		for fn in newFiles :
152496ccc8cbSesaxe			info(fnFormat(fn))
152596ccc8cbSesaxe
152696ccc8cbSesaxe	if len(deletedFiles) > 0 :
152796ccc8cbSesaxe		newOrDeleted = True
152896ccc8cbSesaxe		info("\nObjects removed: ")
152996ccc8cbSesaxe
1530*a4f922faSAndy Fiddaman		if o_sorted :
1531598cc7dfSVladimir Kotal			deletedFiles.sort()
153296ccc8cbSesaxe		for fn in deletedFiles :
153396ccc8cbSesaxe			info(fnFormat(fn))
153496ccc8cbSesaxe
153596ccc8cbSesaxe	if newOrDeleted :
1536598cc7dfSVladimir Kotal		info("\nChanged objects: ")
1537*a4f922faSAndy Fiddaman	if o_sorted :
1538598cc7dfSVladimir Kotal		debug("The list will appear after the processing is done")
153996ccc8cbSesaxe
154096ccc8cbSesaxe	# Here's where all the heavy lifting happens
154196ccc8cbSesaxe	# Perform a comparison on each object appearing in
154296ccc8cbSesaxe	# both proto areas. compareOneFile will examine the
154396ccc8cbSesaxe	# file types of each object, and will vector off to
154496ccc8cbSesaxe	# the appropriate comparison routine, where the compare
154596ccc8cbSesaxe	# will happen, and any differences will be reported / logged
154696ccc8cbSesaxe
1547598cc7dfSVladimir Kotal	# determine maximum number of worker threads by using
1548598cc7dfSVladimir Kotal	# DMAKE_MAX_JOBS environment variable set by nightly(1)
1549598cc7dfSVladimir Kotal	# or get number of CPUs in the system
1550598cc7dfSVladimir Kotal	try:
1551598cc7dfSVladimir Kotal		max_threads = int(os.environ['DMAKE_MAX_JOBS'])
1552598cc7dfSVladimir Kotal	except:
1553598cc7dfSVladimir Kotal		max_threads = os.sysconf("SC_NPROCESSORS_ONLN")
1554598cc7dfSVladimir Kotal		# If we cannot get number of online CPUs in the system
1555598cc7dfSVladimir Kotal		# run unparallelized otherwise bump the number up 20%
1556598cc7dfSVladimir Kotal		# to achieve best results.
1557598cc7dfSVladimir Kotal		if max_threads == -1 :
1558598cc7dfSVladimir Kotal			max_threads = 1
1559598cc7dfSVladimir Kotal		else :
1560a62941d8SAndy Fiddaman			max_threads += int(max_threads/5)
1561598cc7dfSVladimir Kotal
1562598cc7dfSVladimir Kotal	# Set signal handler to attempt graceful exit
1563598cc7dfSVladimir Kotal	debug("Setting signal handler")
1564598cc7dfSVladimir Kotal	signal.signal( signal.SIGINT, discontinue_processing )
1565598cc7dfSVladimir Kotal
1566598cc7dfSVladimir Kotal	# Create and unleash the threads
1567598cc7dfSVladimir Kotal	# Only at most max_threads must be running at any moment
1568598cc7dfSVladimir Kotal	mythreads = []
1569598cc7dfSVladimir Kotal	debug("Spawning " + str(max_threads) + " threads");
1570598cc7dfSVladimir Kotal	for i in range(max_threads) :
1571598cc7dfSVladimir Kotal		thread = workerThread()
1572598cc7dfSVladimir Kotal		mythreads.append(thread)
1573598cc7dfSVladimir Kotal		mythreads[i].start()
1574598cc7dfSVladimir Kotal
1575598cc7dfSVladimir Kotal	# Wait for the threads to finish and do cleanup if interrupted
1576598cc7dfSVladimir Kotal	debug("Waiting for the threads to finish")
1577598cc7dfSVladimir Kotal	while True:
1578d35eb64cSAndy Fiddaman		if not True in [thread.is_alive() for thread in mythreads]:
1579598cc7dfSVladimir Kotal		    break
1580598cc7dfSVladimir Kotal		else:
1581598cc7dfSVladimir Kotal		    # Some threads are still going
1582598cc7dfSVladimir Kotal		    time.sleep(1)
1583598cc7dfSVladimir Kotal
1584598cc7dfSVladimir Kotal	# Interrupted by SIGINT
1585598cc7dfSVladimir Kotal	if keep_processing == False :
1586598cc7dfSVladimir Kotal		cleanup(1)
1587598cc7dfSVladimir Kotal
1588598cc7dfSVladimir Kotal	# If the list of differences was sorted it is stored in an array
1589*a4f922faSAndy Fiddaman	if o_sorted :
1590598cc7dfSVladimir Kotal		differentFiles.sort()
1591598cc7dfSVladimir Kotal		for f in differentFiles :
1592598cc7dfSVladimir Kotal			info(fnFormat(f))
159396ccc8cbSesaxe
159496ccc8cbSesaxe	# We're done, cleanup.
159596ccc8cbSesaxe	cleanup(0)
159696ccc8cbSesaxe
159796ccc8cbSesaxeif __name__ == '__main__' :
159896ccc8cbSesaxe	try:
159996ccc8cbSesaxe		main()
160096ccc8cbSesaxe	except KeyboardInterrupt :
160196ccc8cbSesaxe		cleanup(1);
160296ccc8cbSesaxe
1603