Path: blob/jdk8u272-b10-aarch32-20201026/jdk/make/non-build-utils/src/build/tools/dirdiff/DirDiff.java
48795 views
/*1* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package build.tools.dirdiff;2627import java.io.File;28import java.util.TreeSet;2930public class DirDiff implements Runnable {31private final static String FILE_SEPARATOR = System.getProperty("file.separator");32private static final boolean traversSccsDirs;33private static final boolean recurseExtraDirs;34private static final boolean verboseMode;35private static final boolean checkSizes;36private static long SizeTolerance = 0;37private File goldenDir = null;38private File testDir = null;3940// static initializer:41static {42String traversePropertyValue = System.getProperty("sccs");43traversSccsDirs = (traversePropertyValue != null &&44traversePropertyValue.toLowerCase().equals("true"))? true : false;45if (traversSccsDirs) {46System.err.println("traversing SCCS directories...");47}4849String verbosePropertyValue = System.getProperty("verbose");50verboseMode = (verbosePropertyValue != null &&51verbosePropertyValue.toLowerCase().equals("true"))? true : false;52if (verboseMode) {53System.err.println("verbose mode truned on...");54}5556String noRecurseExtraDirsPropertyValue = System.getProperty("recurse");57recurseExtraDirs = (noRecurseExtraDirsPropertyValue != null &&58noRecurseExtraDirsPropertyValue.toLowerCase().equals("true"))? true : false;59if (recurseExtraDirs) {60System.err.println("recursing extra directories...");61}6263String sizeToleranceValue = System.getProperty("sizeTolerance");64checkSizes = (sizeToleranceValue != null);65if (checkSizes) {66try {67SizeTolerance = Long.parseLong(sizeToleranceValue);68}69catch (NumberFormatException e) {70System.err.println("Invlalid sizeTolerance value: " + sizeToleranceValue);71System.err.println("Expecting a long value. Exiting.");72System.exit(1);73}74System.err.println("checking matching files for size differences of at least " + SizeTolerance);75}76}7778public DirDiff(File inGoldenDir, File inTestDir) {79goldenDir = inGoldenDir;80testDir = inTestDir;81}8283private void whatToDoWithMatchingFiles(File goldenChild, File testChild) {84if (verboseMode) {85System.out.println("Files Match:\t" + goldenChild.getAbsolutePath() +86" and " + testChild.getAbsolutePath());87}88if (checkSizes) {89// compare file sizes...90long goldenLength = 0;91long testLength = 0;92try {93goldenLength = goldenChild.length();94testLength = testChild.length();95}96catch (Exception e) {97System.err.println("Error: exception thrown and caught:");98e.printStackTrace();99}100if (java.lang.Math.abs(goldenLength - testLength) > SizeTolerance) {101if (goldenLength > testLength) {102System.out.println("File short [" + (testLength - goldenLength) + "]:\t" + testChild.getAbsolutePath());103} else {104System.out.println("File long [" + (testLength - goldenLength) + "]:\t" + testChild.getAbsolutePath());105}106}107}108}109110111private void whatToDoWithMatchingDirs(File goldenChild, File testChild) {112if (verboseMode) {113System.out.println("Dirs Match:\t" + goldenChild.getAbsolutePath() +114" and " + testChild.getAbsolutePath());115}116}117118private void whatToDoWithMissingFiles(File missingFile) {119long length = 0;120try {121length = missingFile.length();122}123catch (Exception e) {124System.err.println("Error: exception thrown and caught:");125e.printStackTrace();126}127128System.out.println("Missing File [" + length + "]:\t" + missingFile.getAbsolutePath());129}130131private void whatToDoWithExtraFiles(File extraFile) {132long length = 0;133try {134length = extraFile.length();135}136catch (Exception e) {137System.err.println("Error: exception thrown and caught:");138e.printStackTrace();139}140141System.out.println("Extra File [" + length + "]:\t" + extraFile.getAbsolutePath());142}143144private void whatToDoWithMissingDirs(File missingDir) {145System.out.println("Missing Dir:\t" + missingDir.getAbsolutePath());146}147148private void whatToDoWithExtraDirs(File extraDir) {149System.out.println("Extra Dir:\t" + extraDir.getAbsolutePath());150}151152private void whatToDoWithNonMatchingChildren(File goldenChild, File testChild) {153System.out.println("Type Mismatch:\t" + goldenChild.getAbsolutePath() + " is a " +154(goldenChild.isDirectory()? "directory" : "file") +155" and " + testChild.getAbsolutePath() + " is a " +156(testChild.isDirectory()? "directory" : "file"));157}158159public void run() {160File[] currentTestDirs = null;161if (testDir != null) {162currentTestDirs = testDir.listFiles();163}164165File[] currentGoldenDirs = null;166TreeSet<String> goldDirSet = new TreeSet<>();167if (goldenDir != null) {168currentGoldenDirs = goldenDir.listFiles();169for (int i=0; i<currentGoldenDirs.length; i++) {170goldDirSet.add(currentGoldenDirs[i].getName());171}172}173174// now go through the list of members175if (currentGoldenDirs != null) {176for (int i=0; i<currentGoldenDirs.length; i++) {177File newGoldenDir = currentGoldenDirs[i];178179// do not traverse SCCS directories...180if (!(newGoldenDir.getAbsolutePath().endsWith("SCCS")) || traversSccsDirs ) {181// start a compare of this child and the like-named test child...182File newTestDir = new File(testDir.getAbsolutePath() + FILE_SEPARATOR +183newGoldenDir.getName());184185if (newTestDir.exists()) {186if (newGoldenDir.isDirectory()) {187if (newTestDir.isDirectory()) {188whatToDoWithMatchingDirs(newGoldenDir, newTestDir);189Thread t = new Thread( new DirDiff(newGoldenDir, newTestDir));190t.start();191} else {192whatToDoWithNonMatchingChildren(newGoldenDir, newTestDir);193}194} else { // of... newGoldenDir.isDirectory()...195if (newTestDir.isDirectory()) {196whatToDoWithNonMatchingChildren(newGoldenDir, newTestDir);197}198whatToDoWithMatchingFiles(newGoldenDir, newTestDir);199}200} else { // of... newTestDir.exists()...201if (newGoldenDir.isDirectory()) {202whatToDoWithMissingDirs(newTestDir);203Thread t = new Thread( new DirDiff(newGoldenDir, newTestDir));204t.start();205} else {206whatToDoWithMissingFiles(newTestDir);207}208}209}210}211}212213// look for extra test objs...214if (currentTestDirs != null) {215for (int i=0; i<currentTestDirs.length; i++) {216// do not traverse SCCS directories...217if (!(currentTestDirs[i].getAbsolutePath().endsWith("SCCS")) || traversSccsDirs ) {218if (!goldDirSet.contains(currentTestDirs[i].getName())) {219if (currentTestDirs[i].isDirectory()) {220whatToDoWithExtraDirs(currentTestDirs[i]);221if (recurseExtraDirs) {222Thread t = new Thread( new DirDiff( null, currentTestDirs[i]));223t.start();224}225} else {226whatToDoWithExtraFiles(currentTestDirs[i]);227}228}229}230}231}232}233234public static void main(String[] args) {235if (args.length != 2) {236System.err.println("You must provide two directory names on the command line.");237System.err.println("Usage:\tDirDiff dir1 dir2");238System.err.println("\tJava Runtime Properties (set to \"true\"):");239System.err.println("\t\tsccs\trecurse SCCS directories");240System.err.println("\t\tverbose\tprint verbose diagnostics");241System.err.println("\t\trecurse\trecursing extra directories showing extra files");242System.err.println("\t\tsizeTolerance\tset tolerance for size differences - default is infinite");243244System.exit(0);245} else {246File golden = new File(args[0]);247File test = new File(args[1]);248249if (!golden.exists()) {250System.err.println("Error: path " + golden.getAbsolutePath() +251" does not exist. Skipping.");252System.exit(0);253}254if (!golden.isDirectory()) {255System.err.println("Error: path " + golden.getAbsolutePath() +256" must be a directory. Skipping.");257System.exit(0);258}259if (!test.exists()) {260System.err.println("Error: path " + test.getAbsolutePath() +261" does not exist. Skipping.");262System.exit(0);263}264if (!test.isDirectory()) {265System.err.println("Error: path " + test.getAbsolutePath() +266" must be a directory. Skipping.");267System.exit(0);268}269270Thread t = new Thread( new DirDiff(golden, test));271t.start();272}273}274}275276277