/* * [ScanClient.java] * * Summary: Gather files from client to upload. * * Copyright: (c) 2011-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: * 1.0 2011-12-07 initial release */ package com.mindprod.bulkftp; import com.mindprod.common18.EIO; import com.mindprod.filter.AllButSVNDirectoriesFilter; import com.mindprod.filter.AvoidJunkFilter; import java.io.File; import static java.lang.System.*; /** * Gather files from client to upload. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2011-12-07 initial release * @since 2011-12-07 */ class ScanClient { // todo: add filter to exclude dir tree and dir with filter // similar logic for server // todo: check logic about how webroot behaves. do check to adjust to how it does work. /** * FilenameFilter than accepts only directories. */ private static final AllButSVNDirectoriesFilter justDirs = new AllButSVNDirectoriesFilter(); /** * FilenameFilter that accepts only files, rejects * unwanted extensions and unwanted files. Accepts only files. */ private static final AvoidJunkFilter justFiles = new AvoidJunkFilter(); static { justFiles.setExtensions( Config.EXTENSIONS_TO_WITHHOLD ); justFiles.setFilenames( Config.FILES_TO_WITHHOLD ); } /** * Add all the files in this directory tree to the list. * * @param dirString Directory tree to search for files to add. Name relative to base. */ private static void addDir( String dirString ) { final File dir = dirString.equals( "*" ) ? Config.CLIENT_ROOT_DIR : new File( Config.CLIENT_ROOT_DIR, dirString ); if ( !dir.isDirectory() ) { err.println( EIO.getCanOrAbsPath( dir ) + " is not an existing directory. Ignored." ); return; } addFilesInTree( dir, false ); } // end addFilesInTree /** * Find all files in the given dir and add them to the list. * * @param dir Directory whose files you want added. Must be a directory. * @param recurse True if you want files in subdirs of that directory added too. */ private static void addFilesInTree( File dir, boolean recurse ) { // processZipsPass1 all files in this dir first String[] allFilesInDir = dir.list( justFiles ); for ( String s : allFilesInDir ) { final File f = new File( dir, s ); final String filenameWithSlashes = getFilenameWithSlashes( f ); FD match = FD.get( filenameWithSlashes ); if ( match != null ) { match.setLastModifiedOnClient( f.lastModified() ); match.setSizeOnClient( f.length() ); match.setFlag( true ); } else { final FD fd = new FD( filenameWithSlashes, f.lastModified(), -1, f.length(), -1 ); fd.setFlag( true ); FD.put( filenameWithSlashes, fd ); } } // recursively processZipsPass1 all subdirs in this directory if ( recurse ) { String[] allDirs = dir.list( justDirs ); for ( String allDir : allDirs ) { addFilesInTree( new File( dir, allDir ), recurse ); } } } // end addFilesInTree /** * Add files specified individually in the configuration. */ private static void addIndividualFiles() { for ( String s : Config.FILES_TO_DISTRIBUTE ) { final File f = new File( Config.CLIENT_ROOT_DIR, s ); final String filenameWithSlashes = getFilenameWithSlashes( f ); FD match = FD.get( filenameWithSlashes ); if ( match != null ) { match.setLastModifiedOnClient( f.lastModified() ); match.setSizeOnClient( f.length() ); match.setFlag( true ); } else { final FD fd = new FD( filenameWithSlashes, f.lastModified(), -1, f.length(), -1 ); fd.setFlag( true ); FD.put( filenameWithSlashes, fd ); } } } /** * Add all the files in this directory tree to the list. * * @param dirTreeString Directory tree to search for files to add. Name relative to base. */ private static void addTree( String dirTreeString ) { final File dirTree = dirTreeString.equals( "*" ) ? Config.CLIENT_ROOT_DIR : new File( Config.CLIENT_ROOT_DIR, dirTreeString ); if ( !dirTree.isDirectory() ) { err.println( EIO.getCanOrAbsPath( dirTree ) + " is not an existing directory. Ignored." ); return; } addFilesInTree( dirTree, true ); } // end addFilesInTree /** * Find all the dirs we potentially want to distribute. */ private static void addWantedDirs() { for ( String aDIRS_TO_DISTRIBUTE : Config.DIRS_TO_DISTRIBUTE ) { addDir( aDIRS_TO_DISTRIBUTE ); } } /** * Find all the trues we potentially want to distribute. */ private static void addWantedTrees() { for ( String aTREES_TO_DISTRIBUTE : Config.TREES_TO_DISTRIBUTE ) { addTree( aTREES_TO_DISTRIBUTE ); } } /** * get webroot relative filename with slashes from client file * * @param f file * * @return filename with client root dir removed, and \ converted to / . */ private static String getFilenameWithSlashes( File f ) { return EIO.getCanOrAbsPath( f ).substring( Config.CLIENT_PREFIX_LENGTH ).replace( File.separatorChar, '/' ); } /** * collect files from client that we might need to upload */ static void scanClientFiles() { FD.clearAllFlags(); addWantedDirs(); addWantedTrees(); addIndividualFiles(); // anything without a flag no longer exists on client. This may trigger delete from server. for ( FD fd : FD.values() ) { if ( !fd.isFlag() ) { fd.setSizeOnClient( -1 ); } } } }