/* * [Gather.java] * * Summary: Gather all the names of all the files part of the current distribution, including files distributed before. * * Copyright: (c) 2003-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 4.8 2007-08-18 */ package com.mindprod.replicatorsender; import com.mindprod.replicatorcommon.Config; import com.mindprod.replicatorcommon.ReplicatorCommon; import com.mindprod.sorted.Merge; import static com.mindprod.replicatorsender.ConfigForSender.*; import static java.lang.System.*; /** * Gather all the names of all the files part of the current distribution, including files distributed before. *

* that have either changed or are unchanged. When we are done, filesToDistribute will be sorted by BY_FILENAME. It will * include files already distributed, but not deletions, or old files replaced. * * @author Roedy Green, Canadian Mind Products * @version 4.8 2007-08-18 * @since 2003-03-23 */ final class Gather { /** * Add files in dirs we distribute. */ private static void addWantedDirs() { final Traverse wantedDirs = new Traverse( ReplicatorSender.getEstimate( 2 ), SENDER_BASE_DIR ); for ( String aDIRS_TO_DISTRIBUTE : DIRS_TO_DISTRIBUTE ) { wantedDirs.addFilesInDir( aDIRS_TO_DISTRIBUTE ); } // estimate of total files in wanted dirs for next time. ReplicatorSender.setEstimate( 2, wantedDirs.size() ); // trees wanted - trees withheld // + dirs wanted ReplicatorSender.filesToDistribute = Merge.union( ReplicatorSender.filesToDistribute, wantedDirs.getCollectedFiles(), MaxiFD.BY_DATE_AND_FILENAME ); } /** * Add individual files we will distribute. */ private static void addWantedIndividualFiles() { final Traverse wantedIndividualFiles = new Traverse( ReplicatorSender .getEstimate( 4 ), SENDER_BASE_DIR ); for ( String aFILES_TO_DISTRIBUTE : FILES_TO_DISTRIBUTE ) { wantedIndividualFiles .addFile( aFILES_TO_DISTRIBUTE ); } // estimate of total files in wanted individual list for next time. ReplicatorSender.setEstimate( 4, wantedIndividualFiles.size() ); // trees wanted - trees withheld // + dirs wanted - dirs withheld // + files wanted ReplicatorSender.filesToDistribute = Merge.union( ReplicatorSender.filesToDistribute, wantedIndividualFiles .getCollectedFiles(), MaxiFD.BY_DATE_AND_FILENAME ); } /** * Find all the files we potentially want to distribute. */ private static void addWantedTrees() { final Traverse wantedTrees = new Traverse( ReplicatorSender.getEstimate( 0 ), SENDER_BASE_DIR ); for ( String aTREES_TO_DISTRIBUTE : TREES_TO_DISTRIBUTE ) { wantedTrees.addFilesInTree( aTREES_TO_DISTRIBUTE ); } // estimate of total files in wanted tree for next time. ReplicatorSender.setEstimate( 0, wantedTrees.size() ); ReplicatorSender.filesToDistribute = wantedTrees.getCollectedFiles(); } /** * Prune out duplicates. */ private static void pruneDuplicates() { // If user updated files while the Replicator was running, // of if there were overlapping trees and directories, // there could be duplicates. ReplicatorSender.filesToDistribute = Merge.deDupKeepingLast( ReplicatorSender.filesToDistribute, MaxiFD.BY_FILENAME ); } // end pruneUnwantedExtensions /** * Prune out the large files. */ private static void pruneLargeFiles() { boolean hasNulls = false; final int size = ReplicatorSender.filesToDistribute.size(); // don't change to for:each, we need i. for ( int i = 0; i < size; i++ ) { MaxiFD fd = ReplicatorSender.filesToDistribute.get( i ); if ( fd.getFileLength() > ConfigForSender.LARGEST_FILE_TO_DISTRIBUTE ) { ReplicatorSender.filesToDistribute.set( i, null ); hasNulls = true; } } // end for if ( hasNulls ) { ReplicatorSender.filesToDistribute = Merge.pruneNulls( ReplicatorSender.filesToDistribute ); } } // end pruneUnwantedExtensions /** * remove files in dirs we withhold. */ private static void pruneUnwantedDirs() { final Traverse unwantedDirs = new Traverse( ReplicatorSender.getEstimate( 3 ), SENDER_BASE_DIR ); for ( String aDIRS_TO_WITHHOLD : DIRS_TO_WITHHOLD ) { unwantedDirs.addFilesInDir( aDIRS_TO_WITHHOLD ); } // estimate of total files in unwanted dirs for next time. ReplicatorSender.setEstimate( 3, unwantedDirs.size() ); // trees wanted - trees withheld // + dirs wanted - dirs withheld ReplicatorSender.filesToDistribute = Merge.allBut( ReplicatorSender.filesToDistribute, unwantedDirs .getCollectedFiles(), MaxiFD.BY_DATE_AND_FILENAME ); } /** * Prune out the unwanted extensions. */ @SuppressWarnings( { "ConstantConditions" } ) private static void pruneUnwantedExtensions() { boolean hasNulls = false; final int size = ReplicatorSender.filesToDistribute.size(); // don't change to for:each. We need i. for ( int i = 0; i < size; i++ ) { final MaxiFD fd = ReplicatorSender.filesToDistribute.get( i ); final String extension = fd.getExtension(); for ( String aEXTENSIONS_TO_WITHHOLD : EXTENSIONS_TO_WITHHOLD ) { if ( Config.CASE_SENSITIVE // could use == instead of equals since all is interned. // but for safety use equals which is faster when // interned. ? extension .equals( aEXTENSIONS_TO_WITHHOLD ) : extension .equalsIgnoreCase( aEXTENSIONS_TO_WITHHOLD ) ) { ReplicatorSender.filesToDistribute.set( i, null ); hasNulls = true; break; } } // end inner for } // end outer for if ( hasNulls ) { ReplicatorSender.filesToDistribute = Merge.pruneNulls( ReplicatorSender.filesToDistribute ); } } // end pruneUnwantedExtensions /** * Remove individual files we will withhold. */ private static void pruneUnwantedIndividualFiles() { final Traverse unwantedIndividualFiles = new Traverse( ReplicatorSender .getEstimate( 5 ), SENDER_BASE_DIR ); for ( String aFileToWithhold : FILES_TO_WITHHOLD ) { unwantedIndividualFiles.addFile( aFileToWithhold ); } // estimate of total files in unwanted individual list for next time. ReplicatorSender.setEstimate( 5, unwantedIndividualFiles.size() ); // trees wanted - trees withheld // + dirs wanted - dirs withheld // + files wanted - files withheld ReplicatorSender.filesToDistribute = Merge.allBut( ReplicatorSender.filesToDistribute, unwantedIndividualFiles.getCollectedFiles(), MaxiFD.BY_DATE_AND_FILENAME ); } /** * Remove files in trees we will withhold. */ private static void pruneUnwantedTrees() { final Traverse unwantedTrees = new Traverse( ReplicatorSender.getEstimate( 1 ), SENDER_BASE_DIR ); for ( String aTREES_TO_WITHHOLD : TREES_TO_WITHHOLD ) { unwantedTrees .addFilesInTree( aTREES_TO_WITHHOLD ); } // estimate of total files in unwanted tree for next time. ReplicatorSender.setEstimate( 1, unwantedTrees.size() ); // trees wanted - trees withheld ReplicatorSender.filesToDistribute = Merge.allBut( ReplicatorSender.filesToDistribute, unwantedTrees .getCollectedFiles(), MaxiFD.BY_DATE_AND_FILENAME ); } /** * Do complete initial processing to the point of assigning files to zips. */ public static void process() { try { out.println( "gathering files to distribute..." ); addWantedTrees(); pruneUnwantedTrees(); addWantedDirs(); pruneUnwantedDirs(); addWantedIndividualFiles(); pruneUnwantedIndividualFiles(); pruneLargeFiles(); pruneUnwantedExtensions(); pruneDuplicates(); } catch ( Exception e ) { ReplicatorCommon.fatal( "Problem gathering files." + "\n" + e.getMessage() ); } } }