xref: /illumos-gate/usr/src/tools/scripts/wsdiff.py (revision b0d58672)
13f770aabSAndy Fiddaman#!@TOOLS_PYTHON@ -Es
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.
23a32b2b2aSAndy Fiddaman# Copyright 2021 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#
492f7dba3eSAndy Fiddaman# By invoking nightly(1) with the -w option to NIGHTLY_FLAGS, nightly(1) will
502f7dba3eSAndy 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#
582f7dba3eSAndy Fiddaman# Use the -i option in conjunction with -v and -V to dive deeper and have
592f7dba3eSAndy Fiddaman# wsdiff(1) report with more verbosity.
6096ccc8cbSesaxe#
61b7c90935SAndy 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#
67b7c90935SAndy Fiddaman#       -d      Print debug messages about the progress
68b7c90935SAndy Fiddaman#       -i      Tell wsdiff which objects to compare via an input file list
69b7c90935SAndy Fiddaman#       -r      Log results and observed differences
70b7c90935SAndy Fiddaman#       -s      Produce sorted list of differences
71b7c90935SAndy Fiddaman#       -t      Use onbld tools in $SRC/tools
72b7c90935SAndy Fiddaman#       -u      Produce unified diff output
73b7c90935SAndy Fiddaman#       -U      Produce unified diff output with <lines> lines of context
74b7c90935SAndy Fiddaman#       -v      Do not truncate observed diffs in results
75b7c90935SAndy Fiddaman#       -V      Log *all* ELF sect diffs vs. logging the first diff found
7696ccc8cbSesaxe
77ca13eaa5SAndy Fiddamanfrom __future__ import print_function
78c8cc6853SAndy 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 *
822f7dba3eSAndy Fiddaman
832f7dba3eSAndy FiddamanPY3 = sys.version_info[0] == 3
842f7dba3eSAndy 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
88b7c90935SAndy 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#
113ca13eaa5SAndy Fiddamanwsdiff_exceptions = [
114ca13eaa5SAndy Fiddaman]
11596ccc8cbSesaxe
116a32b2b2aSAndy Fiddaman# Path to genunix, used for finding the proto root, and for CTF diff
117a32b2b2aSAndy Fiddamangenunix = "kernel/amd64/genunix"
118b7c90935SAndy Fiddaman
119fe949611SAlexander Pyhalovif PY3:
120fe949611SAlexander Pyhalov	def getoutput(cmd):
121fe949611SAlexander Pyhalov		import shlex, tempfile
122fe949611SAlexander Pyhalov		f, fpath = tempfile.mkstemp()
123fe949611SAlexander Pyhalov		status = os.system("{ " + cmd + "; } >" +
124fe949611SAlexander Pyhalov			shlex.quote(fpath) + " 2>&1")
125fe949611SAlexander Pyhalov		returncode = os.WEXITSTATUS(status)
126b7c90935SAndy Fiddaman		with os.fdopen(f, mode="r", errors="ignore") as tfile:
127fe949611SAlexander Pyhalov			output = tfile.read()
128fe949611SAlexander Pyhalov		os.unlink(fpath)
129fe949611SAlexander Pyhalov		if output[-1:] == '\n':
130fe949611SAlexander Pyhalov			output = output[:-1]
131fe949611SAlexander Pyhalov		return returncode, output
132fe949611SAlexander Pyhalovelse:
133b7c90935SAndy Fiddaman	import commands
134fe949611SAlexander Pyhalov	getoutput = commands.getstatusoutput
135c8cc6853SAndy 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()
147ca13eaa5SAndy Fiddaman		print(msg)
148598cc7dfSVladimir Kotal		sys.stdout.flush()
149598cc7dfSVladimir Kotal		output_lock.release()
150598cc7dfSVladimir Kotal		if logging :
151598cc7dfSVladimir Kotal			log_lock.acquire()
152ca13eaa5SAndy 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()
160ca13eaa5SAndy Fiddaman	print(msg)
161598cc7dfSVladimir Kotal	sys.stdout.flush()
162598cc7dfSVladimir Kotal	output_lock.release()
16396ccc8cbSesaxe	if logging :
164598cc7dfSVladimir Kotal		log_lock.acquire()
165ca13eaa5SAndy 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) :
171ca13eaa5SAndy Fiddaman
172598cc7dfSVladimir Kotal	output_lock.acquire()
173ca13eaa5SAndy Fiddaman	print("ERROR: " + msg, file=sys.stderr)
17496ccc8cbSesaxe	sys.stderr.flush()
175598cc7dfSVladimir Kotal	output_lock.release()
17696ccc8cbSesaxe	if logging :
177598cc7dfSVladimir Kotal		log_lock.acquire()
178ca13eaa5SAndy 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()
187ca13eaa5SAndy Fiddaman		print(msg, file=log)
18896ccc8cbSesaxe		log.flush()
189598cc7dfSVladimir Kotal		log_lock.release()
190ca13eaa5SAndy Fiddaman
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()
201b7c90935SAndy Fiddaman	if o_sorted :
202598cc7dfSVladimir Kotal		differentFiles.append(f)
203598cc7dfSVladimir Kotal	else:
204ca13eaa5SAndy Fiddaman		print(f)
205598cc7dfSVladimir Kotal		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()
217ca13eaa5SAndy Fiddaman		print(f, file=log)
218ca13eaa5SAndy Fiddaman		print("NOTE: " + dtype + " difference detected.", file=log)
21996ccc8cbSesaxe
22096ccc8cbSesaxe		difflen = len(diffs)
221e7aca734SRichard Lowe		if difflen > 0 :
222ca13eaa5SAndy Fiddaman			print('', file=log)
22396ccc8cbSesaxe
22496ccc8cbSesaxe			if not vdiffs and difflen > diffs_sz_thresh :
225ca13eaa5SAndy Fiddaman				print(diffs[:diffs_sz_thresh], file=log)
226ca13eaa5SAndy Fiddaman				print("... truncated due to length: " +
227ca13eaa5SAndy Fiddaman				      "use -v to override ...", file=log)
22896ccc8cbSesaxe			else :
229ca13eaa5SAndy Fiddaman				print(diffs, file=log)
230ca13eaa5SAndy Fiddaman			print('\n', file=log)
23196ccc8cbSesaxe		log.flush()
232598cc7dfSVladimir Kotal		log_lock.release()
23396ccc8cbSesaxe
23496ccc8cbSesaxe
23596ccc8cbSesaxe#####
23696ccc8cbSesaxe# diff generating routines
23796ccc8cbSesaxe#
23896ccc8cbSesaxe
23996ccc8cbSesaxe#
2402f7dba3eSAndy 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"
252ca13eaa5SAndy Fiddaman
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)
257ca13eaa5SAndy Fiddaman
25896ccc8cbSesaxe		tmpf1 = tmp_od1
25996ccc8cbSesaxe		tmpf2 = tmp_od2
26096ccc8cbSesaxe
261b7c90935SAndy Fiddaman	dcmd = "{} {} {} {}".format(diff_cmd, diff_args, tmpf1, tmpf2)
262598cc7dfSVladimir Kotal	try:
263b7c90935SAndy Fiddaman		rc, data = getoutput(dcmd)
264b7c90935SAndy Fiddaman		if rc == 0:
265b7c90935SAndy Fiddaman			# No differences found
266b7c90935SAndy Fiddaman			data = ''
267b7c90935SAndy Fiddaman		# If producing unified output, strip the first two lines
268b7c90935SAndy Fiddaman		# which just show the temporary file names.
269b7c90935SAndy Fiddaman		if diff_args:
270b7c90935SAndy Fiddaman			data = data.split("\n", 2)[-1]
271b7c90935SAndy 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)
276598cc7dfSVladimir Kotal				os.unlink(tmp_od2)
277ca13eaa5SAndy Fiddaman			except OSError as e:
278a32b2b2aSAndy Fiddaman				error("diffFileData: unlink failed {}"
279a32b2b2aSAndy Fiddaman				    .format(e))
280598cc7dfSVladimir Kotal	except:
281b7c90935SAndy Fiddaman		error("failed to get output of command: " + dcmd)
282598cc7dfSVladimir Kotal
283598cc7dfSVladimir Kotal		# Send exception for the failed command up
284598cc7dfSVladimir Kotal		raise
285598cc7dfSVladimir Kotal		return
28696ccc8cbSesaxe
28796ccc8cbSesaxe	return data
28896ccc8cbSesaxe
28996ccc8cbSesaxe#####
29096ccc8cbSesaxe# Misc utility functions
29196ccc8cbSesaxe#
29296ccc8cbSesaxe
29396ccc8cbSesaxe# Prune off the leading prefix from string s
29496ccc8cbSesaxedef str_prefix_trunc(s, prefix) :
29596ccc8cbSesaxe	snipLen = len(prefix)
29696ccc8cbSesaxe	return s[snipLen:]
29796ccc8cbSesaxe
29896ccc8cbSesaxe#
29996ccc8cbSesaxe# Prune off leading proto path goo (if there is one) to yield
30096ccc8cbSesaxe# the deliverable's eventual path relative to root
30196ccc8cbSesaxe# e.g. proto.base/root_sparc/usr/src/cmd/prstat => usr/src/cmd/prstat
302e7aca734SRichard Lowe#
30396ccc8cbSesaxedef fnFormat(fn) :
304a32b2b2aSAndy Fiddaman	global baseWsRoot, ptchWsRoot
305a32b2b2aSAndy Fiddaman
3067713a058SAndy Fiddaman	if (baseWsRoot and
3077713a058SAndy Fiddaman	    len(os.path.commonprefix([fn, baseWsRoot])) == len(baseWsRoot)):
308a32b2b2aSAndy Fiddaman		return fn[len(baseWsRoot) + 1:]
309e7aca734SRichard Lowe
3107713a058SAndy Fiddaman	if (ptchWsRoot and
3117713a058SAndy Fiddaman	    len(os.path.commonprefix([fn, ptchWsRoot])) == len(ptchWsRoot)):
312a32b2b2aSAndy Fiddaman		return fn[len(ptchWsRoot) + 1:]
313a32b2b2aSAndy Fiddaman
314a32b2b2aSAndy Fiddaman	# Fall back to looking for the expected root_<arch>[-nd] string
315a32b2b2aSAndy Fiddaman
316a32b2b2aSAndy Fiddaman	pos = fn.find("root_" + arch)
31796ccc8cbSesaxe	if pos == -1 :
31896ccc8cbSesaxe		return fn
31996ccc8cbSesaxe
32096ccc8cbSesaxe	pos = fn.find("/", pos)
32196ccc8cbSesaxe	if pos == -1 :
32296ccc8cbSesaxe		return fn
32396ccc8cbSesaxe
32496ccc8cbSesaxe	return fn[pos + 1:]
32596ccc8cbSesaxe
326b7c90935SAndy Fiddaman#
327b7c90935SAndy Fiddaman# Find the path to a proto root given the name of a file or directory under it
328b7c90935SAndy Fiddaman# e.g. proto.base/root_i386-nd/usr/bin => proto.base/root_i386-nd
329a32b2b2aSAndy Fiddaman#      root/usr/bin => root
330b7c90935SAndy Fiddaman#
331b7c90935SAndy Fiddamandef protoroot(fn):
3326be183eeSAndy Fiddaman	root = fn.rstrip('/')
333a32b2b2aSAndy Fiddaman	while len(root) > 1:
334a32b2b2aSAndy Fiddaman		if os.path.isfile(os.path.join(root, genunix)):
335a32b2b2aSAndy Fiddaman			return root
336a32b2b2aSAndy Fiddaman		root = os.path.dirname(root)
337a32b2b2aSAndy Fiddaman
338a32b2b2aSAndy Fiddaman	# genunix was not found, try and determine the root by checking for
339a32b2b2aSAndy Fiddaman	# the expected root_<arch>[-nd] string
340b7c90935SAndy Fiddaman
341a32b2b2aSAndy Fiddaman	pos = fn.find("root_" + arch)
342b7c90935SAndy Fiddaman	if pos == -1:
343b7c90935SAndy Fiddaman		return None
344b7c90935SAndy Fiddaman
345b7c90935SAndy Fiddaman	pos = fn.find("/", pos)
346b7c90935SAndy Fiddaman	if pos == -1:
347b7c90935SAndy Fiddaman		return fn
348b7c90935SAndy Fiddaman
349b7c90935SAndy Fiddaman	return fn[:pos]
350b7c90935SAndy Fiddaman
35196ccc8cbSesaxe#####
35296ccc8cbSesaxe# Usage / argument processing
35396ccc8cbSesaxe#
35496ccc8cbSesaxe
35596ccc8cbSesaxe#
35696ccc8cbSesaxe# Display usage message
35796ccc8cbSesaxe#
35896ccc8cbSesaxedef usage() :
35996ccc8cbSesaxe	sys.stdout.flush()
360b7c90935SAndy Fiddaman	print("""Usage: wsdiff [-dstuvV] [-U lines] [-r results ] [-i filelist ] old new
361598cc7dfSVladimir Kotal        -d      Print debug messages about the progress
362b7c90935SAndy Fiddaman        -i      Tell wsdiff which objects to compare via an input file list
36396ccc8cbSesaxe        -r      Log results and observed differences
364598cc7dfSVladimir Kotal        -s      Produce sorted list of differences
365b7c90935SAndy Fiddaman        -t      Use onbld tools in $SRC/tools
366b7c90935SAndy Fiddaman        -u      Produce unified diff output
367b7c90935SAndy Fiddaman        -U      Produce unified diff output with <lines> lines of context
368b7c90935SAndy Fiddaman        -v      Do not truncate observed diffs in results
369b7c90935SAndy Fiddaman        -V      Log *all* ELF sect diffs vs. logging the first diff found""",
370ca13eaa5SAndy Fiddaman	    file=sys.stderr)
37196ccc8cbSesaxe	sys.exit(1)
37296ccc8cbSesaxe
37396ccc8cbSesaxe#
37496ccc8cbSesaxe# Process command line options
37596ccc8cbSesaxe#
37696ccc8cbSesaxedef args() :
37796ccc8cbSesaxe
378598cc7dfSVladimir Kotal	global debugon
37996ccc8cbSesaxe	global logging
38096ccc8cbSesaxe	global vdiffs
38196ccc8cbSesaxe	global reportAllSects
382b7c90935SAndy Fiddaman	global o_sorted
383b7c90935SAndy Fiddaman	global diff_args
38496ccc8cbSesaxe
385b7c90935SAndy Fiddaman	validOpts = 'di:r:uU:vVst?'
38696ccc8cbSesaxe
38796ccc8cbSesaxe	baseRoot = ""
38896ccc8cbSesaxe	ptchRoot = ""
38996ccc8cbSesaxe	fileNamesFile = ""
39096ccc8cbSesaxe	results = ""
39196ccc8cbSesaxe	localTools = False
39296ccc8cbSesaxe
39396ccc8cbSesaxe	# getopt.getopt() returns:
39496ccc8cbSesaxe	#	an option/value tuple
39596ccc8cbSesaxe	#	a list of remaining non-option arguments
39696ccc8cbSesaxe	#
39796ccc8cbSesaxe	# A correct wsdiff invocation will have exactly two non option
39896ccc8cbSesaxe	# arguments, the paths to the base (old), ptch (new) proto areas
39996ccc8cbSesaxe	try:
40096ccc8cbSesaxe		optlist, args = getopt.getopt(sys.argv[1:], validOpts)
401ca13eaa5SAndy Fiddaman	except getopt.error as val:
40296ccc8cbSesaxe		usage()
40396ccc8cbSesaxe
40496ccc8cbSesaxe	if len(args) != 2 :
40596ccc8cbSesaxe		usage();
40696ccc8cbSesaxe
40796ccc8cbSesaxe	for opt,val in optlist :
408598cc7dfSVladimir Kotal		if opt == '-d' :
409598cc7dfSVladimir Kotal			debugon = True
410598cc7dfSVladimir Kotal		elif opt == '-i' :
41196ccc8cbSesaxe			fileNamesFile = val
41296ccc8cbSesaxe		elif opt == '-r' :
41396ccc8cbSesaxe			results = val
41496ccc8cbSesaxe			logging = True
415598cc7dfSVladimir Kotal		elif opt == '-s' :
416b7c90935SAndy Fiddaman			o_sorted = True
417b7c90935SAndy Fiddaman		elif opt == '-u' :
418b7c90935SAndy Fiddaman			diff_args = '-u'
419b7c90935SAndy Fiddaman		elif opt == '-U' :
420b7c90935SAndy Fiddaman			diff_args = '-U' + str(val)
42196ccc8cbSesaxe		elif opt == '-v' :
42296ccc8cbSesaxe			vdiffs = True
42396ccc8cbSesaxe		elif opt == '-V' :
42496ccc8cbSesaxe			reportAllSects = True
42596ccc8cbSesaxe		elif opt == '-t':
42696ccc8cbSesaxe			localTools = True
42796ccc8cbSesaxe		else:
42896ccc8cbSesaxe			usage()
42996ccc8cbSesaxe
43096ccc8cbSesaxe	baseRoot = args[0]
43196ccc8cbSesaxe	ptchRoot = args[1]
43296ccc8cbSesaxe
43396ccc8cbSesaxe	if len(baseRoot) == 0 or len(ptchRoot) == 0 :
43496ccc8cbSesaxe		usage()
43596ccc8cbSesaxe
43696ccc8cbSesaxe	if logging and len(results) == 0 :
43796ccc8cbSesaxe		usage()
43896ccc8cbSesaxe
43996ccc8cbSesaxe	if vdiffs and not logging :
44096ccc8cbSesaxe		error("The -v option requires a results file (-r)")
44196ccc8cbSesaxe		sys.exit(1)
44296ccc8cbSesaxe
44396ccc8cbSesaxe	if reportAllSects and not logging :
44496ccc8cbSesaxe		error("The -V option requires a results file (-r)")
44596ccc8cbSesaxe		sys.exit(1)
44696ccc8cbSesaxe
44796ccc8cbSesaxe	# alphabetical order
44896ccc8cbSesaxe	return	baseRoot, fileNamesFile, localTools, ptchRoot, results
44996ccc8cbSesaxe
45096ccc8cbSesaxe#####
45196ccc8cbSesaxe# File identification
45296ccc8cbSesaxe#
45396ccc8cbSesaxe
45496ccc8cbSesaxe#
45596ccc8cbSesaxe# Identify the file type.
45696ccc8cbSesaxe# If it's not ELF, use the file extension to identify
45796ccc8cbSesaxe# certain file types that require special handling to
45896ccc8cbSesaxe# compare. Otherwise just return a basic "ASCII" type.
45996ccc8cbSesaxe#
46096ccc8cbSesaxedef getTheFileType(f) :
46196ccc8cbSesaxe
46296ccc8cbSesaxe	extensions = { 'a'	:	'ELF Object Archive',
46396ccc8cbSesaxe		       'jar'	:	'Java Archive',
46496ccc8cbSesaxe		       'html'	:	'HTML',
46596ccc8cbSesaxe		       'ln'	:	'Lint Library',
46696ccc8cbSesaxe		       'db'	:	'Sqlite Database' }
467e7aca734SRichard Lowe
468619b4598Srotondo	try:
469619b4598Srotondo		if os.stat(f)[ST_SIZE] == 0 :
470619b4598Srotondo			return 'ASCII'
471619b4598Srotondo	except:
472619b4598Srotondo		error("failed to stat " + f)
473619b4598Srotondo		return 'Error'
47496ccc8cbSesaxe
47596ccc8cbSesaxe	if isELF(f) == 1 :
47696ccc8cbSesaxe		return 'ELF'
47796ccc8cbSesaxe
47896ccc8cbSesaxe	fnamelist = f.split('.')
47996ccc8cbSesaxe	if len(fnamelist) > 1 :	# Test the file extension
48096ccc8cbSesaxe		extension = fnamelist[-1]
48196ccc8cbSesaxe		if extension in extensions.keys():
48296ccc8cbSesaxe			return extensions[extension]
48396ccc8cbSesaxe
48496ccc8cbSesaxe	return 'ASCII'
48596ccc8cbSesaxe
48696ccc8cbSesaxe#
48796ccc8cbSesaxe# Return non-zero if "f" is an ELF file
48896ccc8cbSesaxe#
489c8cc6853SAndy Fiddamanelfmagic = b'\177ELF'
49096ccc8cbSesaxedef isELF(f) :
49196ccc8cbSesaxe	try:
4922f7dba3eSAndy Fiddaman		with open(f, mode='rb') as fd:
493c8cc6853SAndy Fiddaman			magic = fd.read(len(elfmagic))
49496ccc8cbSesaxe
495c8cc6853SAndy Fiddaman		if magic == elfmagic :
496c8cc6853SAndy Fiddaman			return 1
497c8cc6853SAndy Fiddaman	except:
498c8cc6853SAndy Fiddaman		pass
49996ccc8cbSesaxe	return 0
50096ccc8cbSesaxe
50196ccc8cbSesaxe#
50296ccc8cbSesaxe# Return non-zero is "f" is binary.
50396ccc8cbSesaxe# Consider the file to be binary if it contains any null characters
504e7aca734SRichard Lowe#
50596ccc8cbSesaxedef isBinary(f) :
50696ccc8cbSesaxe	try:
5072f7dba3eSAndy Fiddaman		with open(f, mode='rb') as fd:
508c8cc6853SAndy Fiddaman			s = fd.read()
50996ccc8cbSesaxe
510c8cc6853SAndy Fiddaman		if s.find(b'\0') == -1 :
511c8cc6853SAndy Fiddaman			return 0
512c8cc6853SAndy Fiddaman	except:
513c8cc6853SAndy Fiddaman		pass
514c8cc6853SAndy Fiddaman	return 1
51596ccc8cbSesaxe
51696ccc8cbSesaxe#####
51796ccc8cbSesaxe# Directory traversal and file finding
518e7aca734SRichard Lowe#
51996ccc8cbSesaxe
52096ccc8cbSesaxe#
52196ccc8cbSesaxe# Return a sorted list of files found under the specified directory
52296ccc8cbSesaxe#
52396ccc8cbSesaxedef findFiles(d) :
52496ccc8cbSesaxe	for path, subdirs, files in os.walk(d) :
52596ccc8cbSesaxe		files.sort()
52696ccc8cbSesaxe		for name in files :
52796ccc8cbSesaxe			yield os.path.join(path, name)
52896ccc8cbSesaxe
52996ccc8cbSesaxe#
53096ccc8cbSesaxe# Examine all files in base, ptch
53196ccc8cbSesaxe#
53296ccc8cbSesaxe# Return a list of files appearing in both proto areas,
53396ccc8cbSesaxe# a list of new files (files found only in ptch) and
53496ccc8cbSesaxe# a list of deleted files (files found only in base)
53596ccc8cbSesaxe#
53696ccc8cbSesaxedef protoCatalog(base, ptch) :
537598cc7dfSVladimir Kotal
53896ccc8cbSesaxe	compFiles = []		# List of files in both proto areas
53996ccc8cbSesaxe	ptchList = []		# List of file in patch proto area
54096ccc8cbSesaxe
54196ccc8cbSesaxe	newFiles = []		# New files detected
54296ccc8cbSesaxe	deletedFiles = []	# Deleted files
543e7aca734SRichard Lowe
544598cc7dfSVladimir Kotal	debug("Getting the list of files in the base area");
54596ccc8cbSesaxe	baseFilesList = list(findFiles(base))
54696ccc8cbSesaxe	baseStringLength = len(base)
547598cc7dfSVladimir Kotal	debug("Found " + str(len(baseFilesList)) + " files")
548ca13eaa5SAndy Fiddaman
549598cc7dfSVladimir Kotal	debug("Getting the list of files in the patch area");
55096ccc8cbSesaxe	ptchFilesList = list(findFiles(ptch))
55196ccc8cbSesaxe	ptchStringLength = len(ptch)
552598cc7dfSVladimir Kotal	debug("Found " + str(len(ptchFilesList)) + " files")
55396ccc8cbSesaxe
55496ccc8cbSesaxe	# Inventory files in the base proto area
555598cc7dfSVladimir Kotal	debug("Determining the list of regular files in the base area");
55696ccc8cbSesaxe	for fn in baseFilesList :
55796ccc8cbSesaxe		if os.path.islink(fn) :
55896ccc8cbSesaxe			continue
55996ccc8cbSesaxe
56096ccc8cbSesaxe		fileName = fn[baseStringLength:]
56196ccc8cbSesaxe		compFiles.append(fileName)
562598cc7dfSVladimir Kotal	debug("Found " + str(len(compFiles)) + " files")
56396ccc8cbSesaxe
56496ccc8cbSesaxe	# Inventory files in the patch proto area
565598cc7dfSVladimir Kotal	debug("Determining the list of regular files in the patch area");
56696ccc8cbSesaxe	for fn in ptchFilesList :
56796ccc8cbSesaxe		if os.path.islink(fn) :
56896ccc8cbSesaxe			continue
56996ccc8cbSesaxe
57096ccc8cbSesaxe		fileName = fn[ptchStringLength:]
57196ccc8cbSesaxe		ptchList.append(fileName)
572598cc7dfSVladimir Kotal	debug("Found " + str(len(ptchList)) + " files")
57396ccc8cbSesaxe
57496ccc8cbSesaxe	# Deleted files appear in the base area, but not the patch area
575598cc7dfSVladimir Kotal	debug("Searching for deleted files by comparing the lists")
57696ccc8cbSesaxe	for fileName in compFiles :
57796ccc8cbSesaxe		if not fileName in ptchList :
57896ccc8cbSesaxe			deletedFiles.append(fileName)
579598cc7dfSVladimir Kotal	debug("Found " + str(len(deletedFiles)) + " deleted files")
58096ccc8cbSesaxe
58196ccc8cbSesaxe	# Eliminate "deleted" files from the list of objects appearing
58296ccc8cbSesaxe	# in both the base and patch proto areas
583598cc7dfSVladimir Kotal	debug("Eliminating deleted files from the list of objects")
58496ccc8cbSesaxe	for fileName in deletedFiles :
58596ccc8cbSesaxe		try:
586ca13eaa5SAndy Fiddaman			compFiles.remove(fileName)
58796ccc8cbSesaxe		except:
58896ccc8cbSesaxe			error("filelist.remove() failed")
589ca13eaa5SAndy Fiddaman	debug("List for comparison reduced to " + str(len(compFiles))
590598cc7dfSVladimir Kotal	    + " files")
59196ccc8cbSesaxe
59296ccc8cbSesaxe	# New files appear in the patch area, but not the base
593598cc7dfSVladimir Kotal	debug("Getting the list of newly added files")
59496ccc8cbSesaxe	for fileName in ptchList :
59596ccc8cbSesaxe		if not fileName in compFiles :
59696ccc8cbSesaxe			newFiles.append(fileName)
597598cc7dfSVladimir Kotal	debug("Found " + str(len(newFiles)) + " new files")
59896ccc8cbSesaxe
59996ccc8cbSesaxe	return compFiles, newFiles, deletedFiles
60096ccc8cbSesaxe
60196ccc8cbSesaxe#
60296ccc8cbSesaxe# Examine the files listed in the input file list
60396ccc8cbSesaxe#
60496ccc8cbSesaxe# Return a list of files appearing in both proto areas,
60596ccc8cbSesaxe# a list of new files (files found only in ptch) and
60696ccc8cbSesaxe# a list of deleted files (files found only in base)
60796ccc8cbSesaxe#
60896ccc8cbSesaxedef flistCatalog(base, ptch, flist) :
60996ccc8cbSesaxe	compFiles = []		# List of files in both proto areas
61096ccc8cbSesaxe	newFiles = []		# New files detected
61196ccc8cbSesaxe	deletedFiles = []	# Deleted files
612e7aca734SRichard Lowe
61396ccc8cbSesaxe	try:
61496ccc8cbSesaxe		fd = open(flist, "r")
61596ccc8cbSesaxe	except:
61696ccc8cbSesaxe		error("could not open: " + flist)
61796ccc8cbSesaxe		cleanup(1)
61896ccc8cbSesaxe
61996ccc8cbSesaxe	files = []
62096ccc8cbSesaxe	files = fd.readlines()
621598cc7dfSVladimir Kotal	fd.close()
622e7aca734SRichard Lowe
62396ccc8cbSesaxe	for f in files :
62496ccc8cbSesaxe		ptch_present = True
62596ccc8cbSesaxe		base_present = True
626e7aca734SRichard Lowe
62796ccc8cbSesaxe		if f == '\n' :
62896ccc8cbSesaxe			continue
62996ccc8cbSesaxe
63096ccc8cbSesaxe		# the fileNames have a trailing '\n'
63196ccc8cbSesaxe		f = f.rstrip()
63296ccc8cbSesaxe
63396ccc8cbSesaxe		# The objects in the file list have paths relative
63496ccc8cbSesaxe		# to $ROOT or to the base/ptch directory specified on
63596ccc8cbSesaxe		# the command line.
63696ccc8cbSesaxe		# If it's relative to $ROOT, we'll need to add back the
63796ccc8cbSesaxe		# root_`uname -p` goo we stripped off in fnFormat()
63896ccc8cbSesaxe		if os.path.exists(base + f) :
63996ccc8cbSesaxe			fn = f;
64096ccc8cbSesaxe		elif os.path.exists(base + "root_" + arch + "/" + f) :
64196ccc8cbSesaxe			fn = "root_" + arch + "/" + f
64296ccc8cbSesaxe		else :
64396ccc8cbSesaxe			base_present = False
64496ccc8cbSesaxe
64596ccc8cbSesaxe		if base_present :
64696ccc8cbSesaxe			if not os.path.exists(ptch + fn) :
64796ccc8cbSesaxe				ptch_present = False
64896ccc8cbSesaxe		else :
64996ccc8cbSesaxe			if os.path.exists(ptch + f) :
65096ccc8cbSesaxe				fn = f
65196ccc8cbSesaxe			elif os.path.exists(ptch + "root_" + arch + "/" + f) :
65296ccc8cbSesaxe				fn = "root_" + arch + "/" + f
65396ccc8cbSesaxe			else :
65496ccc8cbSesaxe				ptch_present = False
65596ccc8cbSesaxe
65696ccc8cbSesaxe		if os.path.islink(base + fn) :	# ignore links
65796ccc8cbSesaxe			base_present = False
65896ccc8cbSesaxe		if os.path.islink(ptch + fn) :
65996ccc8cbSesaxe			ptch_present = False
66096ccc8cbSesaxe
66196ccc8cbSesaxe		if base_present and ptch_present :
66296ccc8cbSesaxe			compFiles.append(fn)
66396ccc8cbSesaxe		elif base_present :
66496ccc8cbSesaxe			deletedFiles.append(fn)
66596ccc8cbSesaxe		elif ptch_present :
66696ccc8cbSesaxe			newFiles.append(fn)
66796ccc8cbSesaxe		else :
668ca13eaa5SAndy Fiddaman			if (os.path.islink(base + fn) and
669ca13eaa5SAndy Fiddaman			    os.path.islink(ptch + fn)) :
67096ccc8cbSesaxe				continue
671ca13eaa5SAndy Fiddaman			error(f + " in file list, but not in either tree. " +
672598cc7dfSVladimir Kotal			    "Skipping...")
673e7aca734SRichard Lowe
67496ccc8cbSesaxe	return compFiles, newFiles, deletedFiles
67596ccc8cbSesaxe
67696ccc8cbSesaxe
67796ccc8cbSesaxe#
67896ccc8cbSesaxe# Build a fully qualified path to an external tool/utility.
67996ccc8cbSesaxe# Consider the default system locations. For onbld tools, if
68096ccc8cbSesaxe# the -t option was specified, we'll try to use built tools in $SRC tools,
68196ccc8cbSesaxe# and otherwise, we'll fall back on /opt/onbld/
68296ccc8cbSesaxe#
68396ccc8cbSesaxedef find_tool(tool) :
68496ccc8cbSesaxe
68596ccc8cbSesaxe	# First, check what was passed
68696ccc8cbSesaxe	if os.path.exists(tool) :
68796ccc8cbSesaxe		return tool
68896ccc8cbSesaxe
68996ccc8cbSesaxe	# Next try in wsdiff path
69096ccc8cbSesaxe	for pdir in wsdiff_path :
69196ccc8cbSesaxe		location = pdir + "/" + tool
69296ccc8cbSesaxe		if os.path.exists(location) :
69396ccc8cbSesaxe			return location + " "
69496ccc8cbSesaxe
69596ccc8cbSesaxe		location = pdir + "/" + arch + "/" + tool
69696ccc8cbSesaxe		if os.path.exists(location) :
69796ccc8cbSesaxe			return location + " "
698e7aca734SRichard Lowe
69996ccc8cbSesaxe	error("Could not find path to: " + tool);
70096ccc8cbSesaxe	sys.exit(1);
701e7aca734SRichard Lowe
70296ccc8cbSesaxe
70396ccc8cbSesaxe#####
70496ccc8cbSesaxe# ELF file comparison helper routines
70596ccc8cbSesaxe#
70696ccc8cbSesaxe
70796ccc8cbSesaxe#
70896ccc8cbSesaxe# Return a dictionary of ELF section types keyed by section name
709e7aca734SRichard Lowe#
71096ccc8cbSesaxedef get_elfheader(f) :
71196ccc8cbSesaxe
71296ccc8cbSesaxe	header = {}
71396ccc8cbSesaxe
714c8cc6853SAndy Fiddaman	rc, hstring = getoutput(elfdump_cmd + " -c " + f)
71596ccc8cbSesaxe
71696ccc8cbSesaxe	if len(hstring) == 0 :
71796ccc8cbSesaxe		error("Failed to dump ELF header for " + f)
718598cc7dfSVladimir Kotal		raise
71996ccc8cbSesaxe		return
72096ccc8cbSesaxe
72196ccc8cbSesaxe	# elfdump(1) dumps the section headers with the section name
72296ccc8cbSesaxe	# following "sh_name:", and the section type following "sh_type:"
72396ccc8cbSesaxe	sections = hstring.split("Section Header")
72496ccc8cbSesaxe	for sect in sections :
72596ccc8cbSesaxe		datap = sect.find("sh_name:");
72696ccc8cbSesaxe		if datap == -1 :
72796ccc8cbSesaxe			continue
72896ccc8cbSesaxe		section = sect[datap:].split()[1]
72996ccc8cbSesaxe		datap = sect.find("sh_type:");
73096ccc8cbSesaxe		if datap == -1 :
731ca13eaa5SAndy Fiddaman			error("Could not get type for sect: " + section +
73296ccc8cbSesaxe			      " in " + f)
73396ccc8cbSesaxe		sh_type = sect[datap:].split()[2]
73496ccc8cbSesaxe		header[section] = sh_type
73596ccc8cbSesaxe
73696ccc8cbSesaxe	return header
73796ccc8cbSesaxe
73896ccc8cbSesaxe#
73996ccc8cbSesaxe# Extract data in the specified ELF section from the given file
74096ccc8cbSesaxe#
74196ccc8cbSesaxedef extract_elf_section(f, section) :
74296ccc8cbSesaxe
743c8cc6853SAndy Fiddaman	rc, data = getoutput(dump_cmd + " -sn " + section + " " + f)
74496ccc8cbSesaxe
74596ccc8cbSesaxe	if len(data) == 0 :
746ca13eaa5SAndy Fiddaman		error(dump_cmd + "yielded no data on section " + section +
747598cc7dfSVladimir Kotal		    " of " + f)
748598cc7dfSVladimir Kotal		raise
74996ccc8cbSesaxe		return
75096ccc8cbSesaxe
75196ccc8cbSesaxe	# dump(1) displays the file name to start...
75296ccc8cbSesaxe	# get past it to the data itself
75396ccc8cbSesaxe	dbegin = data.find(":") + 1
75496ccc8cbSesaxe	data = data[dbegin:];
75596ccc8cbSesaxe
75696ccc8cbSesaxe	return (data)
75796ccc8cbSesaxe
75896ccc8cbSesaxe#
75996ccc8cbSesaxe# Return a (hopefully meaningful) human readable set of diffs
76096ccc8cbSesaxe# for the specified ELF section between f1 and f2
76196ccc8cbSesaxe#
76296ccc8cbSesaxe# Depending on the section, various means for dumping and diffing
76396ccc8cbSesaxe# the data may be employed.
76496ccc8cbSesaxe#
765b7c90935SAndy Fiddaman
76696ccc8cbSesaxetext_sections = [ '.text', '.init', '.fini' ]
767b7c90935SAndy Fiddaman
768b7c90935SAndy Fiddaman# Helper to generate the requireed commands for diffing two .SUNW_ctf
769b7c90935SAndy Fiddaman# sections.
770b7c90935SAndy Fiddamandef diff_ctf(f1, f2):
771b7c90935SAndy Fiddaman
772b7c90935SAndy Fiddaman	# Find genunix so that it can be used for parent CTF data when
773b7c90935SAndy Fiddaman	# appropriate.
774b7c90935SAndy Fiddaman	if diff_ctf.genunix1 is None:
775a32b2b2aSAndy Fiddaman		global genunix, baseWsRoot, ptchWsRoot
776a32b2b2aSAndy Fiddaman
777a32b2b2aSAndy Fiddaman		if (baseWsRoot and ptchWsRoot and
778a32b2b2aSAndy Fiddaman		    os.path.isfile(os.path.join(baseWsRoot, genunix)) and
779a32b2b2aSAndy Fiddaman		    os.path.isfile(os.path.join(ptchWsRoot, genunix))):
780a32b2b2aSAndy Fiddaman			diff_ctf.genunix1 = os.path.join(baseWsRoot, genunix)
781a32b2b2aSAndy Fiddaman			diff_ctf.genunix2 = os.path.join(ptchWsRoot, genunix)
782b7c90935SAndy Fiddaman			debug("CTF: Found {}".format(diff_ctf.genunix1))
783b7c90935SAndy Fiddaman			debug("CTF: Found {}".format(diff_ctf.genunix2))
784b7c90935SAndy Fiddaman		else:
785b7c90935SAndy Fiddaman			# Could not find genunix, do the best we can.
786b7c90935SAndy Fiddaman			error("diff_ctf: Could not find genunix. " +
787b7c90935SAndy Fiddaman			    "CTF diffs will be less useful.")
788b7c90935SAndy Fiddaman			diff_ctf.genunix1 = diff_ctf.genunix2 = False
789b7c90935SAndy Fiddaman
790b7c90935SAndy Fiddaman	# Determine if this is a merged file from genunix by looking
791b7c90935SAndy Fiddaman	# at the parent
792b7c90935SAndy Fiddaman	rc, data = getoutput("{} -h {}".format(ctfdump_cmd, f1))
793b7c90935SAndy Fiddaman	if rc != 0:
794b7c90935SAndy Fiddaman		error("Could not read CTF header: {}".format(data))
795b7c90935SAndy Fiddaman		return (None, None)
796b7c90935SAndy Fiddaman
797b7c90935SAndy Fiddaman	parent = None
798b7c90935SAndy Fiddaman	for line in data.split('\n'):
799b7c90935SAndy Fiddaman		if line.strip().startswith('cth_parname'):
800b7c90935SAndy Fiddaman			try:
801b7c90935SAndy Fiddaman				parent = line.split('=')[1].strip()
802b7c90935SAndy Fiddaman				break
803b7c90935SAndy Fiddaman			except:
804b7c90935SAndy Fiddaman				pass
805b7c90935SAndy Fiddaman
806b7c90935SAndy Fiddaman	cmd1 = cmd2 = "{} -c ".format(ctfdump_cmd)
807b7c90935SAndy Fiddaman	if parent == "genunix":
808b7c90935SAndy Fiddaman		if diff_ctf.genunix1 and diff_ctf.genunix2:
809b7c90935SAndy Fiddaman			cmd1 += "-p {} ".format(diff_ctf.genunix1)
810b7c90935SAndy Fiddaman			cmd2 += "-p {} ".format(diff_ctf.genunix2)
811b7c90935SAndy Fiddaman	elif parent is None or (len(parent) > 0 and parent != "(anon)"):
812b7c90935SAndy Fiddaman		error("Unknown CTF Parent: {}".format(parent))
813b7c90935SAndy Fiddaman		return (None, None)
814b7c90935SAndy Fiddaman
815b7c90935SAndy Fiddaman	cmd1 += f1
816b7c90935SAndy Fiddaman	cmd2 += f2
817b7c90935SAndy Fiddaman
818b7c90935SAndy Fiddaman	return (cmd1, cmd2)
819b7c90935SAndy Fiddaman
820b7c90935SAndy Fiddamandiff_ctf.genunix1 = None
821b7c90935SAndy Fiddamandiff_ctf.genunix2 = None
822b7c90935SAndy Fiddaman
82396ccc8cbSesaxedef diff_elf_section(f1, f2, section, sh_type) :
82496ccc8cbSesaxe
825a2e1144aSAndy Fiddaman	t = threading.current_thread()
826a2e1144aSAndy Fiddaman	tmpFile1 = tmpDir1 + os.path.basename(f1) + t.name
827a2e1144aSAndy Fiddaman	tmpFile2 = tmpDir2 + os.path.basename(f2) + t.name
828598cc7dfSVladimir Kotal
829*b0d58672SRichard Lowe	if ((sh_type == "SHT_RELA") or
830*b0d58672SRichard Lowe	    (sh_type == "SHT_REL")): # relocation section
83196ccc8cbSesaxe		cmd1 = elfdump_cmd + " -r " + f1 + " > " + tmpFile1
83296ccc8cbSesaxe		cmd2 = elfdump_cmd + " -r " + f2 + " > " + tmpFile2
83396ccc8cbSesaxe	elif (section == ".group") :
83496ccc8cbSesaxe		cmd1 = elfdump_cmd + " -g " + f1 + " > " + tmpFile1
83596ccc8cbSesaxe		cmd2 = elfdump_cmd + " -g " + f2 + " > " + tmpFile2
83696ccc8cbSesaxe	elif (section == ".hash") :
83796ccc8cbSesaxe		cmd1 = elfdump_cmd + " -h " + f1 + " > " + tmpFile1
83896ccc8cbSesaxe		cmd2 = elfdump_cmd + " -h " + f2 + " > " + tmpFile2
83996ccc8cbSesaxe	elif (section == ".dynamic") :
84096ccc8cbSesaxe		cmd1 = elfdump_cmd + " -d " + f1 + " > " + tmpFile1
84196ccc8cbSesaxe		cmd2 = elfdump_cmd + " -d " + f2 + " > " + tmpFile2
84296ccc8cbSesaxe	elif (section == ".got") :
84396ccc8cbSesaxe		cmd1 = elfdump_cmd + " -G " + f1 + " > " + tmpFile1
84496ccc8cbSesaxe		cmd2 = elfdump_cmd + " -G " + f2 + " > " + tmpFile2
84596ccc8cbSesaxe	elif (section == ".SUNW_cap") :
84696ccc8cbSesaxe		cmd1 = elfdump_cmd + " -H " + f1 + " > " + tmpFile1
84796ccc8cbSesaxe		cmd2 = elfdump_cmd + " -H " + f2 + " > " + tmpFile2
84896ccc8cbSesaxe	elif (section == ".interp") :
84996ccc8cbSesaxe		cmd1 = elfdump_cmd + " -i " + f1 + " > " + tmpFile1
85096ccc8cbSesaxe		cmd2 = elfdump_cmd + " -i " + f2 + " > " + tmpFile2
85196ccc8cbSesaxe	elif (section == ".symtab" or section == ".dynsym") :
852ca13eaa5SAndy Fiddaman		cmd1 = (elfdump_cmd + " -s -N " + section + " " + f1 +
853ca13eaa5SAndy Fiddaman		    " > " + tmpFile1)
854ca13eaa5SAndy Fiddaman		cmd2 = (elfdump_cmd + " -s -N " + section + " " + f2 +
855ca13eaa5SAndy Fiddaman		    " > " + tmpFile2)
85696ccc8cbSesaxe	elif (section in text_sections) :
85796ccc8cbSesaxe		# dis sometimes complains when it hits something it doesn't
85896ccc8cbSesaxe		# know how to disassemble. Just ignore it, as the output
85996ccc8cbSesaxe		# being generated here is human readable, and we've already
86096ccc8cbSesaxe		# correctly flagged the difference.
861ca13eaa5SAndy Fiddaman		cmd1 = (dis_cmd + " -t " + section + " " + f1 +
862ca13eaa5SAndy Fiddaman		       " 2>/dev/null | grep -v disassembly > " + tmpFile1)
863ca13eaa5SAndy Fiddaman		cmd2 = (dis_cmd + " -t " + section + " " + f2 +
864ca13eaa5SAndy Fiddaman		       " 2>/dev/null | grep -v disassembly > " + tmpFile2)
865b7c90935SAndy Fiddaman	elif (section == ".SUNW_ctf"):
866b7c90935SAndy Fiddaman		(cmd1, cmd2) = diff_ctf(f1, f2)
867b7c90935SAndy Fiddaman		if not cmd1:
868b7c90935SAndy Fiddaman			return ""
869b7c90935SAndy Fiddaman		cmd1 += " > {}".format(tmpFile1)
870b7c90935SAndy Fiddaman		cmd2 += " > {}".format(tmpFile2)
871b7c90935SAndy Fiddaman
87296ccc8cbSesaxe	else :
873ca13eaa5SAndy Fiddaman		cmd1 = (elfdump_cmd + " -w " + tmpFile1 + " -N " +
874ca13eaa5SAndy Fiddaman		       section + " " + f1)
875ca13eaa5SAndy Fiddaman		cmd2 = (elfdump_cmd + " -w " + tmpFile2 + " -N " +
876ca13eaa5SAndy Fiddaman		       section + " " + f2)
87796ccc8cbSesaxe
87896ccc8cbSesaxe	os.system(cmd1)
87996ccc8cbSesaxe	os.system(cmd2)
88096ccc8cbSesaxe
88196ccc8cbSesaxe	data = diffFileData(tmpFile1, tmpFile2)
882e7aca734SRichard Lowe
883598cc7dfSVladimir Kotal	# remove temp files as we no longer need them
884598cc7dfSVladimir Kotal	try:
885598cc7dfSVladimir Kotal		os.unlink(tmpFile1)
886598cc7dfSVladimir Kotal		os.unlink(tmpFile2)
887ca13eaa5SAndy Fiddaman	except OSError as e:
888a32b2b2aSAndy Fiddaman		error("diff_elf_section: unlink failed {}".format(e))
889598cc7dfSVladimir Kotal
89096ccc8cbSesaxe	return (data)
89196ccc8cbSesaxe
89296ccc8cbSesaxe#
89396ccc8cbSesaxe# compare the relevant sections of two ELF binaries
89496ccc8cbSesaxe# and report any differences
89596ccc8cbSesaxe#
89696ccc8cbSesaxe# Returns: 1 if any differenes found
89796ccc8cbSesaxe#          0 if no differences found
89896ccc8cbSesaxe#	  -1 on error
89996ccc8cbSesaxe#
90096ccc8cbSesaxe
90196ccc8cbSesaxe# Sections deliberately not considered when comparing two ELF
90296ccc8cbSesaxe# binaries. Differences observed in these sections are not considered
90396ccc8cbSesaxe# significant where patch deliverable identification is concerned.
90496ccc8cbSesaxesections_to_skip = [ ".SUNW_signature",
90596ccc8cbSesaxe		     ".comment",
90696ccc8cbSesaxe		     ".debug",
90796ccc8cbSesaxe		     ".plt",
90896ccc8cbSesaxe		     ".rela.bss",
90996ccc8cbSesaxe		     ".rela.plt",
91096ccc8cbSesaxe		     ".line",
91196ccc8cbSesaxe		     ".note",
912f6a1d796Sesaxe		     ".compcom",
913b7c90935SAndy Fiddaman		     ".SUNW_dof",
91496ccc8cbSesaxe		     ]
91596ccc8cbSesaxe
916b7c90935SAndy Fiddamansections_preferred = [ ".SUNW_ctf",
917b7c90935SAndy Fiddaman		       ".rodata.str1.8",
91896ccc8cbSesaxe		       ".rodata.str1.1",
91996ccc8cbSesaxe		       ".rodata",
92096ccc8cbSesaxe		       ".data1",
92196ccc8cbSesaxe		       ".data",
92296ccc8cbSesaxe		       ".text",
92396ccc8cbSesaxe		       ]
92496ccc8cbSesaxe
925b7c90935SAndy Fiddaman# Some sections must always be extracted and diffed to check that there are
926b7c90935SAndy Fiddaman# real differences.
927b7c90935SAndy Fiddamansections_to_always_diff = [ ".SUNW_ctf" ]
928b7c90935SAndy Fiddaman
92996ccc8cbSesaxedef compareElfs(base, ptch, quiet) :
93096ccc8cbSesaxe
93196ccc8cbSesaxe	global logging
93296ccc8cbSesaxe
933598cc7dfSVladimir Kotal	try:
934598cc7dfSVladimir Kotal		base_header = get_elfheader(base)
935598cc7dfSVladimir Kotal	except:
936598cc7dfSVladimir Kotal		return
937c8cc6853SAndy Fiddaman	sections = list(base_header.keys())
938b7c90935SAndy Fiddaman	sections.sort()
93996ccc8cbSesaxe
940598cc7dfSVladimir Kotal	try:
941598cc7dfSVladimir Kotal		ptch_header = get_elfheader(ptch)
942598cc7dfSVladimir Kotal	except:
943598cc7dfSVladimir Kotal		return
944c8cc6853SAndy Fiddaman	e2_only_sections = list(ptch_header.keys())
945b7c90935SAndy Fiddaman	e2_only_sections.sort()
94696ccc8cbSesaxe
94796ccc8cbSesaxe	e1_only_sections = []
94896ccc8cbSesaxe
94996ccc8cbSesaxe	fileName = fnFormat(base)
95096ccc8cbSesaxe
95196ccc8cbSesaxe	# Derive the list of ELF sections found only in
95296ccc8cbSesaxe	# either e1 or e2.
95396ccc8cbSesaxe	for sect in sections :
95496ccc8cbSesaxe		if not sect in e2_only_sections :
95596ccc8cbSesaxe			e1_only_sections.append(sect)
95696ccc8cbSesaxe		else :
95796ccc8cbSesaxe			e2_only_sections.remove(sect)
95896ccc8cbSesaxe
95996ccc8cbSesaxe	if len(e1_only_sections) > 0 :
96096ccc8cbSesaxe		if quiet :
96196ccc8cbSesaxe			return 1
96296ccc8cbSesaxe
963598cc7dfSVladimir Kotal		data = ""
964598cc7dfSVladimir Kotal		if logging :
965598cc7dfSVladimir Kotal			slist = ""
966598cc7dfSVladimir Kotal			for sect in e1_only_sections :
967598cc7dfSVladimir Kotal				slist = slist + sect + "\t"
968ca13eaa5SAndy Fiddaman			data = ("ELF sections found in " +
969ca13eaa5SAndy Fiddaman				base + " but not in " + ptch +
970ca13eaa5SAndy Fiddaman				"\n\n" + slist)
971e7aca734SRichard Lowe
972598cc7dfSVladimir Kotal		difference(fileName, "ELF", data)
973598cc7dfSVladimir Kotal		return 1
974ca13eaa5SAndy Fiddaman
97596ccc8cbSesaxe	if len(e2_only_sections) > 0 :
97696ccc8cbSesaxe		if quiet :
97796ccc8cbSesaxe			return 1
978ca13eaa5SAndy Fiddaman
979598cc7dfSVladimir Kotal		data = ""
980598cc7dfSVladimir Kotal		if logging :
981598cc7dfSVladimir Kotal			slist = ""
982598cc7dfSVladimir Kotal			for sect in e2_only_sections :
983598cc7dfSVladimir Kotal				slist = slist + sect + "\t"
984ca13eaa5SAndy Fiddaman			data = ("ELF sections found in " +
985ca13eaa5SAndy Fiddaman				ptch + " but not in " + base +
986ca13eaa5SAndy Fiddaman				"\n\n" + slist)
987598cc7dfSVladimir Kotal
988598cc7dfSVladimir Kotal		difference(fileName, "ELF", data)
98996ccc8cbSesaxe		return 1
99096ccc8cbSesaxe
99196ccc8cbSesaxe	# Look for preferred sections, and put those at the
99296ccc8cbSesaxe	# top of the list of sections to compare
99396ccc8cbSesaxe	for psect in sections_preferred :
99496ccc8cbSesaxe		if psect in sections :
99596ccc8cbSesaxe			sections.remove(psect)
99696ccc8cbSesaxe			sections.insert(0, psect)
99796ccc8cbSesaxe
99896ccc8cbSesaxe	# Compare ELF sections
99996ccc8cbSesaxe	first_section = True
100096ccc8cbSesaxe	for sect in sections :
100196ccc8cbSesaxe
100296ccc8cbSesaxe		if sect in sections_to_skip :
100396ccc8cbSesaxe			continue
100496ccc8cbSesaxe
1005598cc7dfSVladimir Kotal		try:
1006598cc7dfSVladimir Kotal			s1 = extract_elf_section(base, sect);
1007598cc7dfSVladimir Kotal		except:
1008598cc7dfSVladimir Kotal			return
1009598cc7dfSVladimir Kotal
1010598cc7dfSVladimir Kotal		try:
1011598cc7dfSVladimir Kotal			s2 = extract_elf_section(ptch, sect);
1012598cc7dfSVladimir Kotal		except:
1013598cc7dfSVladimir Kotal			return
101496ccc8cbSesaxe
101596ccc8cbSesaxe		if len(s1) != len (s2) or s1 != s2:
1016b7c90935SAndy Fiddaman			if not quiet or sect in sections_to_always_diff:
101796ccc8cbSesaxe				sh_type = base_header[sect]
1018ca13eaa5SAndy Fiddaman				data = diff_elf_section(base, ptch,
1019598cc7dfSVladimir Kotal							sect, sh_type)
102096ccc8cbSesaxe
1021b7c90935SAndy Fiddaman				if len(data) == 0:
1022b7c90935SAndy Fiddaman					continue # No differences
1023b7c90935SAndy Fiddaman
1024b7c90935SAndy Fiddaman			if not quiet:
102596ccc8cbSesaxe				# If all ELF sections are being reported, then
102696ccc8cbSesaxe				# invoke difference() to flag the file name to
1027b7c90935SAndy Fiddaman				# stdout only once. Any other section
1028b7c90935SAndy Fiddaman				# differences should be logged to the results
1029b7c90935SAndy Fiddaman				# file directly
103096ccc8cbSesaxe				if not first_section :
1031ca13eaa5SAndy Fiddaman					log_difference(fileName,
1032598cc7dfSVladimir Kotal					    "ELF " + sect, data)
103396ccc8cbSesaxe				else :
1034ca13eaa5SAndy Fiddaman					difference(fileName, "ELF " + sect,
1035598cc7dfSVladimir Kotal					    data)
1036e7aca734SRichard Lowe
103796ccc8cbSesaxe			if not reportAllSects :
103896ccc8cbSesaxe				return 1
103996ccc8cbSesaxe			first_section = False
1040598cc7dfSVladimir Kotal
104196ccc8cbSesaxe	return 0
1042e7aca734SRichard Lowe
1043598cc7dfSVladimir Kotal#####
1044598cc7dfSVladimir Kotal# recursively remove 2 directories
1045598cc7dfSVladimir Kotal#
1046598cc7dfSVladimir Kotal# Used for removal of temporary directory strucures (ignores any errors).
1047598cc7dfSVladimir Kotal#
1048598cc7dfSVladimir Kotaldef clearTmpDirs(dir1, dir2) :
1049598cc7dfSVladimir Kotal
1050598cc7dfSVladimir Kotal	if os.path.isdir(dir1) > 0 :
1051598cc7dfSVladimir Kotal		shutil.rmtree(dir1, True)
1052598cc7dfSVladimir Kotal
1053598cc7dfSVladimir Kotal	if os.path.isdir(dir2) > 0 :
1054598cc7dfSVladimir Kotal		shutil.rmtree(dir2, True)
1055598cc7dfSVladimir Kotal
1056598cc7dfSVladimir Kotal
105796ccc8cbSesaxe#####
105896ccc8cbSesaxe# Archive object comparison
105996ccc8cbSesaxe#
106096ccc8cbSesaxe# Returns 1 if difference detected
106196ccc8cbSesaxe#         0 if no difference detected
106296ccc8cbSesaxe#        -1 on error
106396ccc8cbSesaxe#
106496ccc8cbSesaxedef compareArchives(base, ptch, fileType) :
106596ccc8cbSesaxe
106696ccc8cbSesaxe	fileName = fnFormat(base)
1067a2e1144aSAndy Fiddaman	t = threading.current_thread()
1068a2e1144aSAndy Fiddaman	ArchTmpDir1 = tmpDir1 + os.path.basename(base) + t.name
1069a2e1144aSAndy Fiddaman	ArchTmpDir2 = tmpDir2 + os.path.basename(base) + t.name
107096ccc8cbSesaxe
107196ccc8cbSesaxe	#
107296ccc8cbSesaxe	# Be optimistic and first try a straight file compare
107396ccc8cbSesaxe	# as it will allow us to finish up quickly.
1074598cc7dfSVladimir Kotal	#
107596ccc8cbSesaxe	if compareBasic(base, ptch, True, fileType) == 0 :
107696ccc8cbSesaxe		return 0
107796ccc8cbSesaxe
1078598cc7dfSVladimir Kotal	try:
1079598cc7dfSVladimir Kotal		os.makedirs(ArchTmpDir1)
1080598cc7dfSVladimir Kotal		os.makedirs(ArchTmpDir2)
1081ca13eaa5SAndy Fiddaman	except OSError as e:
1082a32b2b2aSAndy Fiddaman		error("compareArchives: makedir failed {}".format(e))
1083598cc7dfSVladimir Kotal		return -1
1084598cc7dfSVladimir Kotal
108596ccc8cbSesaxe	# copy over the objects to the temp areas, and
108696ccc8cbSesaxe	# unpack them
1087598cc7dfSVladimir Kotal	baseCmd = "cp -fp " + base + " " + ArchTmpDir1
1088c8cc6853SAndy Fiddaman	rc, output = getoutput(baseCmd)
1089c8cc6853SAndy Fiddaman	if rc != 0:
109096ccc8cbSesaxe		error(baseCmd + " failed: " + output)
1091598cc7dfSVladimir Kotal		clearTmpDirs(ArchTmpDir1, ArchTmpDir2)
109296ccc8cbSesaxe		return -1
109396ccc8cbSesaxe
1094598cc7dfSVladimir Kotal	ptchCmd = "cp -fp " + ptch + " " + ArchTmpDir2
1095c8cc6853SAndy Fiddaman	rc, output = getoutput(ptchCmd)
1096c8cc6853SAndy Fiddaman	if rc != 0:
109796ccc8cbSesaxe		error(ptchCmd + " failed: " + output)
1098598cc7dfSVladimir Kotal		clearTmpDirs(ArchTmpDir1, ArchTmpDir2)
109996ccc8cbSesaxe		return -1
110096ccc8cbSesaxe
1101c8cc6853SAndy Fiddaman	bname = fileName.split('/')[-1]
110296ccc8cbSesaxe	if fileType == "Java Archive" :
1103ca13eaa5SAndy Fiddaman		baseCmd = ("cd " + ArchTmpDir1 + "; " + "jar xf " + bname +
1104ca13eaa5SAndy Fiddaman			  "; rm -f " + bname + " META-INF/MANIFEST.MF")
1105ca13eaa5SAndy Fiddaman		ptchCmd = ("cd " + ArchTmpDir2 + "; " + "jar xf " + bname +
1106ca13eaa5SAndy Fiddaman			  "; rm -f " + bname + " META-INF/MANIFEST.MF")
110796ccc8cbSesaxe	elif fileType == "ELF Object Archive" :
1108ca13eaa5SAndy Fiddaman		baseCmd = ("cd " + ArchTmpDir1 + "; " + "/usr/ccs/bin/ar x " +
1109ca13eaa5SAndy Fiddaman			  bname + "; rm -f " + bname)
1110ca13eaa5SAndy Fiddaman		ptchCmd = ("cd " + ArchTmpDir2 + "; " + "/usr/ccs/bin/ar x " +
1111ca13eaa5SAndy Fiddaman			  bname + "; rm -f " + bname)
111296ccc8cbSesaxe	else :
111396ccc8cbSesaxe		error("unexpected file type: " + fileType)
1114598cc7dfSVladimir Kotal		clearTmpDirs(ArchTmpDir1, ArchTmpDir2)
111596ccc8cbSesaxe		return -1
111696ccc8cbSesaxe
111796ccc8cbSesaxe	os.system(baseCmd)
111896ccc8cbSesaxe	os.system(ptchCmd)
111996ccc8cbSesaxe
1120598cc7dfSVladimir Kotal	baseFlist = list(findFiles(ArchTmpDir1))
1121598cc7dfSVladimir Kotal	ptchFlist = list(findFiles(ArchTmpDir2))
112296ccc8cbSesaxe
112396ccc8cbSesaxe	# Trim leading path off base/ptch file lists
112496ccc8cbSesaxe	flist = []
112596ccc8cbSesaxe	for fn in baseFlist :
1126598cc7dfSVladimir Kotal		flist.append(str_prefix_trunc(fn, ArchTmpDir1))
112796ccc8cbSesaxe	baseFlist = flist
112896ccc8cbSesaxe
112996ccc8cbSesaxe	flist = []
113096ccc8cbSesaxe	for fn in ptchFlist :
1131598cc7dfSVladimir Kotal		flist.append(str_prefix_trunc(fn, ArchTmpDir2))
113296ccc8cbSesaxe	ptchFlist = flist
113396ccc8cbSesaxe
113496ccc8cbSesaxe	for fn in ptchFlist :
113596ccc8cbSesaxe		if not fn in baseFlist :
1136ca13eaa5SAndy Fiddaman			difference(fileName, fileType,
113796ccc8cbSesaxe				   fn + " added to " + fileName)
1138598cc7dfSVladimir Kotal			clearTmpDirs(ArchTmpDir1, ArchTmpDir2)
113996ccc8cbSesaxe			return 1
114096ccc8cbSesaxe
114196ccc8cbSesaxe	for fn in baseFlist :
114296ccc8cbSesaxe		if not fn in ptchFlist :
1143ca13eaa5SAndy Fiddaman			difference(fileName, fileType,
114496ccc8cbSesaxe				   fn + " removed from " + fileName)
1145598cc7dfSVladimir Kotal			clearTmpDirs(ArchTmpDir1, ArchTmpDir2)
114696ccc8cbSesaxe			return 1
114796ccc8cbSesaxe
1148ca13eaa5SAndy Fiddaman		differs = compareOneFile((ArchTmpDir1 + fn),
1149598cc7dfSVladimir Kotal		    (ArchTmpDir2 + fn), True)
115096ccc8cbSesaxe		if differs :
1151ca13eaa5SAndy Fiddaman			difference(fileName, fileType,
115296ccc8cbSesaxe				   fn + " in " + fileName + " differs")
1153598cc7dfSVladimir Kotal			clearTmpDirs(ArchTmpDir1, ArchTmpDir2)
115496ccc8cbSesaxe			return 1
1155598cc7dfSVladimir Kotal
1156598cc7dfSVladimir Kotal	clearTmpDirs(ArchTmpDir1, ArchTmpDir2)
115796ccc8cbSesaxe	return 0
115896ccc8cbSesaxe
115996ccc8cbSesaxe#####
116096ccc8cbSesaxe# (Basic) file comparison
116196ccc8cbSesaxe#
116296ccc8cbSesaxe# Returns 1 if difference detected
116396ccc8cbSesaxe#         0 if no difference detected
116496ccc8cbSesaxe#        -1 on error
116596ccc8cbSesaxe#
116696ccc8cbSesaxedef compareBasic(base, ptch, quiet, fileType) :
116796ccc8cbSesaxe
116896ccc8cbSesaxe	fileName = fnFormat(base);
116996ccc8cbSesaxe
117096ccc8cbSesaxe	if quiet and os.stat(base)[ST_SIZE] != os.stat(ptch)[ST_SIZE] :
117196ccc8cbSesaxe		return 1
117296ccc8cbSesaxe
117396ccc8cbSesaxe	try:
11742f7dba3eSAndy Fiddaman		with open(base, 'rb') as fh:
11752f7dba3eSAndy Fiddaman			baseData = fh.read()
117696ccc8cbSesaxe	except:
117796ccc8cbSesaxe		error("could not open " + base)
117896ccc8cbSesaxe		return -1
11792f7dba3eSAndy Fiddaman
118096ccc8cbSesaxe	try:
11812f7dba3eSAndy Fiddaman		with open(ptch, 'rb') as fh:
11822f7dba3eSAndy Fiddaman			ptchData = fh.read()
118396ccc8cbSesaxe	except:
118496ccc8cbSesaxe		error("could not open " + ptch)
118596ccc8cbSesaxe		return -1
118696ccc8cbSesaxe
118796ccc8cbSesaxe	if quiet :
118896ccc8cbSesaxe		if baseData != ptchData :
118996ccc8cbSesaxe			return 1
119096ccc8cbSesaxe	else :
119196ccc8cbSesaxe		if len(baseData) != len(ptchData) or baseData != ptchData :
11922f7dba3eSAndy Fiddaman			diffs = diffFileData(base, ptch)
119396ccc8cbSesaxe			difference(fileName, fileType, diffs)
119496ccc8cbSesaxe			return 1
119596ccc8cbSesaxe	return 0
119696ccc8cbSesaxe
119796ccc8cbSesaxe
119896ccc8cbSesaxe#####
119996ccc8cbSesaxe# Compare two objects by producing a data dump from
120096ccc8cbSesaxe# each object, and then comparing the dump data
120196ccc8cbSesaxe#
120296ccc8cbSesaxe# Returns: 1 if a difference is detected
120396ccc8cbSesaxe#          0 if no difference detected
120496ccc8cbSesaxe#         -1 upon error
120596ccc8cbSesaxe#
120696ccc8cbSesaxedef compareByDumping(base, ptch, quiet, fileType) :
120796ccc8cbSesaxe
120896ccc8cbSesaxe	fileName = fnFormat(base);
1209a2e1144aSAndy Fiddaman	t = threading.current_thread()
1210a2e1144aSAndy Fiddaman	tmpFile1 = tmpDir1 + os.path.basename(base) + t.name
1211a2e1144aSAndy Fiddaman	tmpFile2 = tmpDir2 + os.path.basename(ptch) + t.name
121296ccc8cbSesaxe
121396ccc8cbSesaxe	if fileType == "Lint Library" :
1214ca13eaa5SAndy Fiddaman		baseCmd = (lintdump_cmd + " -ir " + base +
1215ca13eaa5SAndy Fiddaman			  " | egrep -v '(LINTOBJ|LINTMOD):'" +
1216ca13eaa5SAndy Fiddaman			  " | grep -v PASS[1-3]:" +
1217ca13eaa5SAndy Fiddaman			  " > " + tmpFile1)
1218ca13eaa5SAndy Fiddaman		ptchCmd = (lintdump_cmd + " -ir " + ptch +
1219ca13eaa5SAndy Fiddaman			  " | egrep -v '(LINTOBJ|LINTMOD):'" +
1220ca13eaa5SAndy Fiddaman			  " | grep -v PASS[1-3]:" +
1221ca13eaa5SAndy Fiddaman			  " > " + tmpFile2)
122296ccc8cbSesaxe	elif fileType == "Sqlite Database" :
1223ca13eaa5SAndy Fiddaman		baseCmd = ("echo .dump | " + sqlite_cmd + base + " > " +
1224ca13eaa5SAndy Fiddaman			  tmpFile1)
1225ca13eaa5SAndy Fiddaman		ptchCmd = ("echo .dump | " + sqlite_cmd + ptch + " > " +
1226ca13eaa5SAndy Fiddaman			  tmpFile2)
1227ca13eaa5SAndy Fiddaman
122896ccc8cbSesaxe	os.system(baseCmd)
122996ccc8cbSesaxe	os.system(ptchCmd)
123096ccc8cbSesaxe
123196ccc8cbSesaxe	try:
12322f7dba3eSAndy Fiddaman		with open(tmpFile1, 'rb') as fh:
12332f7dba3eSAndy Fiddaman			baseData = fh.read()
123496ccc8cbSesaxe	except:
123596ccc8cbSesaxe		error("could not open: " + tmpFile1)
1236598cc7dfSVladimir Kotal		return
12372f7dba3eSAndy Fiddaman
123896ccc8cbSesaxe	try:
12392f7dba3eSAndy Fiddaman		with open(tmpFile2, 'rb') as fh:
12402f7dba3eSAndy Fiddaman			ptchData = fh.read()
124196ccc8cbSesaxe	except:
124296ccc8cbSesaxe		error("could not open: " + tmpFile2)
1243598cc7dfSVladimir Kotal		return
124496ccc8cbSesaxe
12452f7dba3eSAndy Fiddaman	ret = 0
124696ccc8cbSesaxe
124796ccc8cbSesaxe	if len(baseData) != len(ptchData) or baseData != ptchData :
124896ccc8cbSesaxe		if not quiet :
124996ccc8cbSesaxe			data = diffFileData(tmpFile1, tmpFile2);
12502f7dba3eSAndy Fiddaman		ret = 1
1251598cc7dfSVladimir Kotal
1252598cc7dfSVladimir Kotal	# Remove the temporary files now.
1253598cc7dfSVladimir Kotal	try:
1254598cc7dfSVladimir Kotal		os.unlink(tmpFile1)
1255598cc7dfSVladimir Kotal		os.unlink(tmpFile2)
1256ca13eaa5SAndy Fiddaman	except OSError as e:
1257a32b2b2aSAndy Fiddaman		error("compareByDumping: unlink failed {}".format(e))
1258598cc7dfSVladimir Kotal
12592f7dba3eSAndy Fiddaman	return ret
126096ccc8cbSesaxe
1261619b4598Srotondo#####
1262619b4598Srotondo#
1263598cc7dfSVladimir Kotal# SIGINT signal handler. Changes thread control variable to tell the threads
1264598cc7dfSVladimir Kotal# to finish their current job and exit.
1265619b4598Srotondo#
1266598cc7dfSVladimir Kotaldef discontinue_processing(signl, frme):
1267598cc7dfSVladimir Kotal	global keep_processing
1268619b4598Srotondo
1269ca13eaa5SAndy Fiddaman	print("Caught Ctrl-C, stopping the threads", file=sys.stderr)
1270598cc7dfSVladimir Kotal	keep_processing = False
1271619b4598Srotondo
1272598cc7dfSVladimir Kotal	return 0
1273619b4598Srotondo
1274598cc7dfSVladimir Kotal#####
1275598cc7dfSVladimir Kotal#
1276598cc7dfSVladimir Kotal# worker thread for changedFiles processing
1277598cc7dfSVladimir Kotal#
1278598cc7dfSVladimir Kotalclass workerThread(threading.Thread) :
1279ca13eaa5SAndy Fiddaman	def run(self):
1280ca13eaa5SAndy Fiddaman		global wset_lock
1281ca13eaa5SAndy Fiddaman		global changedFiles
1282ca13eaa5SAndy Fiddaman		global baseRoot
1283ca13eaa5SAndy Fiddaman		global ptchRoot
1284ca13eaa5SAndy Fiddaman		global keep_processing
1285ca13eaa5SAndy Fiddaman
1286ca13eaa5SAndy Fiddaman		while (keep_processing) :
1287ca13eaa5SAndy Fiddaman			# grab the lock to changedFiles and remove one member
1288ca13eaa5SAndy Fiddaman			# and process it
1289ca13eaa5SAndy Fiddaman			wset_lock.acquire()
1290ca13eaa5SAndy Fiddaman			try :
1291ca13eaa5SAndy Fiddaman				fn = changedFiles.pop()
1292ca13eaa5SAndy Fiddaman			except IndexError :
1293ca13eaa5SAndy Fiddaman				# there is nothing more to do
1294ca13eaa5SAndy Fiddaman				wset_lock.release()
1295ca13eaa5SAndy Fiddaman				return
1296598cc7dfSVladimir Kotal			wset_lock.release()
1297598cc7dfSVladimir Kotal
1298ca13eaa5SAndy Fiddaman			base = baseRoot + fn
1299ca13eaa5SAndy Fiddaman			ptch = ptchRoot + fn
1300598cc7dfSVladimir Kotal
1301ca13eaa5SAndy Fiddaman			compareOneFile(base, ptch, False)
1302619b4598Srotondo
1303619b4598Srotondo
130496ccc8cbSesaxe#####
130596ccc8cbSesaxe# Compare two objects. Detect type changes.
130696ccc8cbSesaxe# Vector off to the appropriate type specific
130796ccc8cbSesaxe# compare routine based on the type.
1308e7aca734SRichard Lowe#
130996ccc8cbSesaxedef compareOneFile(base, ptch, quiet) :
131096ccc8cbSesaxe
131196ccc8cbSesaxe	# Verify the file types.
131296ccc8cbSesaxe	# If they are different, indicate this and move on
131396ccc8cbSesaxe	btype = getTheFileType(base)
131496ccc8cbSesaxe	ptype = getTheFileType(ptch)
131596ccc8cbSesaxe
1316619b4598Srotondo	if btype == 'Error' or ptype == 'Error' :
1317619b4598Srotondo		return -1
1318619b4598Srotondo
131996ccc8cbSesaxe	fileName = fnFormat(base)
132096ccc8cbSesaxe
132196ccc8cbSesaxe	if (btype != ptype) :
1322619b4598Srotondo		if not quiet :
1323b7c90935SAndy Fiddaman			difference(fileName, "file type",
1324b7c90935SAndy Fiddaman			    btype + " to " + ptype)
132596ccc8cbSesaxe		return 1
132696ccc8cbSesaxe	else :
132796ccc8cbSesaxe		fileType = btype
132896ccc8cbSesaxe
132996ccc8cbSesaxe	if (fileType == 'ELF') :
133096ccc8cbSesaxe		return compareElfs(base, ptch, quiet)
133196ccc8cbSesaxe
133296ccc8cbSesaxe	elif (fileType == 'Java Archive' or fileType == 'ELF Object Archive') :
133396ccc8cbSesaxe		return compareArchives(base, ptch, fileType)
133496ccc8cbSesaxe
133596ccc8cbSesaxe	elif (fileType == 'HTML') :
133696ccc8cbSesaxe		return compareBasic(base, ptch, quiet, fileType)
133796ccc8cbSesaxe
133896ccc8cbSesaxe	elif ( fileType == 'Lint Library' ) :
133996ccc8cbSesaxe		return compareByDumping(base, ptch, quiet, fileType)
134096ccc8cbSesaxe
134196ccc8cbSesaxe	elif ( fileType == 'Sqlite Database' ) :
134296ccc8cbSesaxe		return compareByDumping(base, ptch, quiet, fileType)
1343619b4598Srotondo
134496ccc8cbSesaxe	else :
134596ccc8cbSesaxe		# it has to be some variety of text file
134696ccc8cbSesaxe		return compareBasic(base, ptch, quiet, fileType)
134796ccc8cbSesaxe
134896ccc8cbSesaxe# Cleanup and self-terminate
134996ccc8cbSesaxedef cleanup(ret) :
135096ccc8cbSesaxe
1351598cc7dfSVladimir Kotal	debug("Performing cleanup (" + str(ret) + ")")
1352598cc7dfSVladimir Kotal	if os.path.isdir(tmpDir1) > 0 :
1353598cc7dfSVladimir Kotal		shutil.rmtree(tmpDir1)
1354ca13eaa5SAndy Fiddaman
1355598cc7dfSVladimir Kotal	if os.path.isdir(tmpDir2) > 0 :
1356598cc7dfSVladimir Kotal		shutil.rmtree(tmpDir2)
1357ca13eaa5SAndy Fiddaman
135896ccc8cbSesaxe	if logging :
135996ccc8cbSesaxe		log.close()
1360e7aca734SRichard Lowe
136196ccc8cbSesaxe	sys.exit(ret)
136296ccc8cbSesaxe
136396ccc8cbSesaxedef main() :
136496ccc8cbSesaxe
136596ccc8cbSesaxe	# Log file handle
136696ccc8cbSesaxe	global log
136796ccc8cbSesaxe
136896ccc8cbSesaxe	# Globals relating to command line options
136996ccc8cbSesaxe	global logging, vdiffs, reportAllSects
137096ccc8cbSesaxe
137196ccc8cbSesaxe	# Named temporary files / directories
1372598cc7dfSVladimir Kotal	global tmpDir1, tmpDir2
137396ccc8cbSesaxe
137496ccc8cbSesaxe	# Command paths
1375b7c90935SAndy Fiddaman	global lintdump_cmd, elfdump_cmd, dump_cmd, dis_cmd, od_cmd, \
1376b7c90935SAndy Fiddaman	    diff_cmd, sqlite_cmd, ctfdump_cmd
137796ccc8cbSesaxe
137896ccc8cbSesaxe	# Default search path
137996ccc8cbSesaxe	global wsdiff_path
138096ccc8cbSesaxe
138196ccc8cbSesaxe	# Essentially "uname -p"
138296ccc8cbSesaxe	global arch
138396ccc8cbSesaxe
1384598cc7dfSVladimir Kotal	# changed files for worker thread processing
1385598cc7dfSVladimir Kotal	global changedFiles
1386a32b2b2aSAndy Fiddaman
1387a32b2b2aSAndy Fiddaman	global baseRoot, ptchRoot, baseWsRoot, ptchWsRoot
1388598cc7dfSVladimir Kotal
1389598cc7dfSVladimir Kotal	# Sort the list of files from a temporary file
1390b7c90935SAndy Fiddaman	global o_sorted
1391598cc7dfSVladimir Kotal	global differentFiles
1392598cc7dfSVladimir Kotal
1393598cc7dfSVladimir Kotal	# Debugging indicator
1394598cc7dfSVladimir Kotal	global debugon
1395598cc7dfSVladimir Kotal
139696ccc8cbSesaxe	# Some globals need to be initialized
1397b7c90935SAndy Fiddaman	debugon = logging = vdiffs = reportAllSects = o_sorted = False
139896ccc8cbSesaxe
139996ccc8cbSesaxe	# Process command line arguments
140096ccc8cbSesaxe	# Return values are returned from args() in alpha order
140196ccc8cbSesaxe	# (Yes, python functions can return multiple values (ewww))
140296ccc8cbSesaxe	# Note that args() also set the globals:
140396ccc8cbSesaxe	#	logging to True if verbose logging (to a file) was enabled
140496ccc8cbSesaxe	#	vdiffs to True if logged differences aren't to be truncated
1405b7c90935SAndy Fiddaman	#	reportAllSects to True if all ELF section differences are to
1406b7c90935SAndy Fiddaman	#	be reported
140796ccc8cbSesaxe	#
140896ccc8cbSesaxe	baseRoot, fileNamesFile, localTools, ptchRoot, results = args()
140996ccc8cbSesaxe
141096ccc8cbSesaxe	#
141196ccc8cbSesaxe	# Set up the results/log file
141296ccc8cbSesaxe	#
141396ccc8cbSesaxe	if logging :
141496ccc8cbSesaxe		try:
141596ccc8cbSesaxe			log = open(results, "w")
141696ccc8cbSesaxe		except:
141796ccc8cbSesaxe			logging = False
1418a32b2b2aSAndy Fiddaman			error("failed to open log file: {}".format(log))
141996ccc8cbSesaxe			sys.exit(1)
142096ccc8cbSesaxe
142196ccc8cbSesaxe		v_info("# This file was produced by wsdiff")
1422a32b2b2aSAndy Fiddaman		dateTimeStr= time.strftime('# %Y-%m-%d at %H:%M:%S',
1423a32b2b2aSAndy Fiddaman		    time.localtime())
142496ccc8cbSesaxe		v_info(dateTimeStr)
142596ccc8cbSesaxe
1426598cc7dfSVladimir Kotal	# Changed files (used only for the sorted case)
1427b7c90935SAndy Fiddaman	if o_sorted :
1428598cc7dfSVladimir Kotal		differentFiles = []
1429598cc7dfSVladimir Kotal
1430ca13eaa5SAndy Fiddaman	#
143196ccc8cbSesaxe	# Build paths to the tools required tools
143296ccc8cbSesaxe	#
143396ccc8cbSesaxe	# Try to look for tools in $SRC/tools if the "-t" option
143496ccc8cbSesaxe	# was specified
143596ccc8cbSesaxe	#
1436c8cc6853SAndy Fiddaman	rc, arch = getoutput("uname -p")
1437c8cc6853SAndy Fiddaman	arch = arch.rstrip()
143896ccc8cbSesaxe	if localTools :
143996ccc8cbSesaxe		try:
144096ccc8cbSesaxe			src = os.environ['SRC']
144196ccc8cbSesaxe		except:
1442b7c90935SAndy Fiddaman			error("-t specified, but $SRC not set. " +
1443b7c90935SAndy Fiddaman			    "Cannot find $SRC/tools")
144496ccc8cbSesaxe			src = ""
144596ccc8cbSesaxe		if len(src) > 0 :
1446b7c90935SAndy Fiddaman			wsdiff_path.insert(0,
1447b7c90935SAndy Fiddaman			    src + "/tools/proto/opt/onbld/bin")
144896ccc8cbSesaxe
144996ccc8cbSesaxe	lintdump_cmd = find_tool("lintdump")
145096ccc8cbSesaxe	elfdump_cmd = find_tool("elfdump")
145196ccc8cbSesaxe	dump_cmd = find_tool("dump")
145296ccc8cbSesaxe	od_cmd = find_tool("od")
145396ccc8cbSesaxe	dis_cmd = find_tool("dis")
145496ccc8cbSesaxe	diff_cmd = find_tool("diff")
145596ccc8cbSesaxe	sqlite_cmd = find_tool("sqlite")
1456b7c90935SAndy Fiddaman	ctfdump_cmd = find_tool("ctfdump")
145796ccc8cbSesaxe
1458598cc7dfSVladimir Kotal	#
1459598cc7dfSVladimir Kotal	# Set resource limit for number of open files as high as possible.
1460598cc7dfSVladimir Kotal	# This might get handy with big number of threads.
1461598cc7dfSVladimir Kotal	#
1462598cc7dfSVladimir Kotal	(nofile_soft, nofile_hard) = resource.getrlimit(resource.RLIMIT_NOFILE)
1463598cc7dfSVladimir Kotal	try:
1464598cc7dfSVladimir Kotal		resource.setrlimit(resource.RLIMIT_NOFILE,
1465598cc7dfSVladimir Kotal		    (nofile_hard, nofile_hard))
1466598cc7dfSVladimir Kotal	except:
1467598cc7dfSVladimir Kotal		error("cannot set resource limits for number of open files")
1468598cc7dfSVladimir Kotal		sys.exit(1)
1469598cc7dfSVladimir Kotal
147096ccc8cbSesaxe	#
147196ccc8cbSesaxe	# validate the base and patch paths
147296ccc8cbSesaxe	#
1473a32b2b2aSAndy Fiddaman	baseRoot = os.path.abspath(baseRoot) + os.sep
1474a32b2b2aSAndy Fiddaman	ptchRoot = os.path.abspath(ptchRoot) + os.sep
147596ccc8cbSesaxe
147696ccc8cbSesaxe	if not os.path.exists(baseRoot) :
1477a32b2b2aSAndy Fiddaman		error("old proto area: {} does not exist".format(baseRoot))
147896ccc8cbSesaxe		sys.exit(1)
147996ccc8cbSesaxe
148096ccc8cbSesaxe	if not os.path.exists(ptchRoot) :
1481a32b2b2aSAndy Fiddaman		error("new proto area: {} does not exist".format(ptchRoot))
148296ccc8cbSesaxe		sys.exit(1)
148396ccc8cbSesaxe
1484a32b2b2aSAndy Fiddaman	#
1485a32b2b2aSAndy Fiddaman	# attempt to find the workspace root directory for the proto area
1486a32b2b2aSAndy Fiddaman	#
1487a32b2b2aSAndy Fiddaman	baseWsRoot = protoroot(baseRoot)
1488a32b2b2aSAndy Fiddaman	ptchWsRoot = protoroot(ptchRoot)
1489a32b2b2aSAndy Fiddaman	debug("base workspace root: {}".format(baseWsRoot))
1490a32b2b2aSAndy Fiddaman	debug("ptch workspace root: {}".format(ptchWsRoot))
1491a32b2b2aSAndy Fiddaman
149296ccc8cbSesaxe	#
149396ccc8cbSesaxe	# log some information identifying the run
149496ccc8cbSesaxe	#
1495a32b2b2aSAndy Fiddaman	v_info("Old proto area: {}".format(baseRoot))
1496a32b2b2aSAndy Fiddaman	v_info("New proto area: {}".format(ptchRoot))
1497a32b2b2aSAndy Fiddaman	v_info("Results file: {}".format(results))
1498a32b2b2aSAndy Fiddaman	v_info("")
149996ccc8cbSesaxe
1500e7aca734SRichard Lowe	#
150196ccc8cbSesaxe	# Set up the temporary directories / files
150296ccc8cbSesaxe	# Could use python's tmpdir routines, but these should
150396ccc8cbSesaxe	# be easier to identify / keep around for debugging
150496ccc8cbSesaxe	pid = os.getpid()
150596ccc8cbSesaxe	tmpDir1 = "/tmp/wsdiff_tmp1_" + str(pid) + "/"
150696ccc8cbSesaxe	tmpDir2 = "/tmp/wsdiff_tmp2_" + str(pid) + "/"
1507598cc7dfSVladimir Kotal	try:
150896ccc8cbSesaxe		os.makedirs(tmpDir1)
150996ccc8cbSesaxe		os.makedirs(tmpDir2)
1510ca13eaa5SAndy Fiddaman	except OSError as e:
1511a32b2b2aSAndy Fiddaman		error("main: makedir failed {}".format(e))
151296ccc8cbSesaxe
151396ccc8cbSesaxe	# Derive a catalog of new, deleted, and to-be-compared objects
151496ccc8cbSesaxe	# either from the specified base and patch proto areas, or from
151596ccc8cbSesaxe	# from an input file list
151696ccc8cbSesaxe	newOrDeleted = False
151796ccc8cbSesaxe
151896ccc8cbSesaxe	if fileNamesFile != "" :
151996ccc8cbSesaxe		changedFiles, newFiles, deletedFiles = \
152096ccc8cbSesaxe			      flistCatalog(baseRoot, ptchRoot, fileNamesFile)
152196ccc8cbSesaxe	else :
1522598cc7dfSVladimir Kotal		changedFiles, newFiles, deletedFiles = \
1523598cc7dfSVladimir Kotal				protoCatalog(baseRoot, ptchRoot)
152496ccc8cbSesaxe
152596ccc8cbSesaxe	if len(newFiles) > 0 :
152696ccc8cbSesaxe		newOrDeleted = True
152796ccc8cbSesaxe		info("\nNew objects found: ")
152896ccc8cbSesaxe
1529b7c90935SAndy Fiddaman		if o_sorted :
1530598cc7dfSVladimir Kotal			newFiles.sort()
153196ccc8cbSesaxe		for fn in newFiles :
153296ccc8cbSesaxe			info(fnFormat(fn))
153396ccc8cbSesaxe
153496ccc8cbSesaxe	if len(deletedFiles) > 0 :
153596ccc8cbSesaxe		newOrDeleted = True
153696ccc8cbSesaxe		info("\nObjects removed: ")
153796ccc8cbSesaxe
1538b7c90935SAndy Fiddaman		if o_sorted :
1539598cc7dfSVladimir Kotal			deletedFiles.sort()
154096ccc8cbSesaxe		for fn in deletedFiles :
154196ccc8cbSesaxe			info(fnFormat(fn))
154296ccc8cbSesaxe
154396ccc8cbSesaxe	if newOrDeleted :
1544598cc7dfSVladimir Kotal		info("\nChanged objects: ")
1545b7c90935SAndy Fiddaman	if o_sorted :
1546598cc7dfSVladimir Kotal		debug("The list will appear after the processing is done")
154796ccc8cbSesaxe
154896ccc8cbSesaxe	# Here's where all the heavy lifting happens
154996ccc8cbSesaxe	# Perform a comparison on each object appearing in
155096ccc8cbSesaxe	# both proto areas. compareOneFile will examine the
155196ccc8cbSesaxe	# file types of each object, and will vector off to
155296ccc8cbSesaxe	# the appropriate comparison routine, where the compare
155396ccc8cbSesaxe	# will happen, and any differences will be reported / logged
1554e7aca734SRichard Lowe
1555ca13eaa5SAndy Fiddaman	# determine maximum number of worker threads by using
1556598cc7dfSVladimir Kotal	# DMAKE_MAX_JOBS environment variable set by nightly(1)
1557598cc7dfSVladimir Kotal	# or get number of CPUs in the system
1558598cc7dfSVladimir Kotal	try:
1559598cc7dfSVladimir Kotal		max_threads = int(os.environ['DMAKE_MAX_JOBS'])
1560598cc7dfSVladimir Kotal	except:
1561598cc7dfSVladimir Kotal		max_threads = os.sysconf("SC_NPROCESSORS_ONLN")
1562598cc7dfSVladimir Kotal		# If we cannot get number of online CPUs in the system
1563598cc7dfSVladimir Kotal		# run unparallelized otherwise bump the number up 20%
1564598cc7dfSVladimir Kotal		# to achieve best results.
1565598cc7dfSVladimir Kotal		if max_threads == -1 :
1566598cc7dfSVladimir Kotal			max_threads = 1
1567598cc7dfSVladimir Kotal		else :
15682f7dba3eSAndy Fiddaman			max_threads += int(max_threads/5)
1569598cc7dfSVladimir Kotal
1570598cc7dfSVladimir Kotal	# Set signal handler to attempt graceful exit
1571598cc7dfSVladimir Kotal	debug("Setting signal handler")
1572598cc7dfSVladimir Kotal	signal.signal( signal.SIGINT, discontinue_processing )
1573598cc7dfSVladimir Kotal
1574598cc7dfSVladimir Kotal	# Create and unleash the threads
1575598cc7dfSVladimir Kotal	# Only at most max_threads must be running at any moment
1576598cc7dfSVladimir Kotal	mythreads = []
1577598cc7dfSVladimir Kotal	debug("Spawning " + str(max_threads) + " threads");
1578598cc7dfSVladimir Kotal	for i in range(max_threads) :
1579598cc7dfSVladimir Kotal		thread = workerThread()
1580598cc7dfSVladimir Kotal		mythreads.append(thread)
1581598cc7dfSVladimir Kotal		mythreads[i].start()
1582598cc7dfSVladimir Kotal
1583598cc7dfSVladimir Kotal	# Wait for the threads to finish and do cleanup if interrupted
1584598cc7dfSVladimir Kotal	debug("Waiting for the threads to finish")
1585598cc7dfSVladimir Kotal	while True:
15868a647f8fSAndy Fiddaman		if not True in [thread.is_alive() for thread in mythreads]:
1587598cc7dfSVladimir Kotal		    break
1588598cc7dfSVladimir Kotal		else:
1589598cc7dfSVladimir Kotal		    # Some threads are still going
1590598cc7dfSVladimir Kotal		    time.sleep(1)
1591598cc7dfSVladimir Kotal
1592598cc7dfSVladimir Kotal	# Interrupted by SIGINT
1593598cc7dfSVladimir Kotal	if keep_processing == False :
1594598cc7dfSVladimir Kotal		cleanup(1)
1595598cc7dfSVladimir Kotal
1596598cc7dfSVladimir Kotal	# If the list of differences was sorted it is stored in an array
1597b7c90935SAndy Fiddaman	if o_sorted :
1598598cc7dfSVladimir Kotal		differentFiles.sort()
1599598cc7dfSVladimir Kotal		for f in differentFiles :
1600598cc7dfSVladimir Kotal			info(fnFormat(f))
160196ccc8cbSesaxe
160296ccc8cbSesaxe	# We're done, cleanup.
160396ccc8cbSesaxe	cleanup(0)
160496ccc8cbSesaxe
160596ccc8cbSesaxeif __name__ == '__main__' :
160696ccc8cbSesaxe	try:
160796ccc8cbSesaxe		main()
160896ccc8cbSesaxe	except KeyboardInterrupt :
160996ccc8cbSesaxe		cleanup(1);
1610