/* * [RefreshAux.java] * * Summary: Download any stale auxiliary files. * * Copyright: (c) 2009-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 2007-07-18 07-18 */ package com.mindprod.replicator; import com.mindprod.replicatorcommon.Config; import com.mindprod.replicatorcommon.MiniFD; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.zip.GZIPInputStream; import static java.lang.System.*; /** * Download any stale auxiliary files. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2007-07-18 07-18 * @since 2009 */ final class RefreshAux { /** * check which auxiliary files are stale, and download them. * * @throws java.net.MalformedURLException if url is bad on one of files. */ static void refreshAuxFiles() throws MalformedURLException { if ( !ConfigForReceiver.KEEP_ZIPS ) { // if we don't keep zips to pass on, there is no need for aux files. return; } // Always freshly download freshness.ser final String freshnessFilename = ConfigForReceiver .RECEIVER_ZIP_STAGING_DIR + File .separatorChar + Config .FRESHNESS_SER; final String freshnessURL = ConfigForReceiver.RECEIVER_ZIP_URL + "/" + Config.FRESHNESS_SER; Replicator.doing( "downloading list of most recent aux files: " + freshnessURL ); if ( !FetchZips.ft .download( new URL( freshnessURL ), new File( freshnessFilename ), false ) ) { Replicator.fatal( "Unable to download " + freshnessURL + " to " + freshnessFilename ); } MiniFD[] auxFiles = null; // get array of files and freshness dates try { final FileInputStream fis = new FileInputStream( freshnessFilename ); // stream is compressed final GZIPInputStream gzis = new GZIPInputStream( fis, 4 * 1024 ); final ObjectInputStream ois = new ObjectInputStream( gzis ); final long fileSerialVersionUID = ( Long ) ois.readObject(); if ( fileSerialVersionUID != MiniFD.serialVersionUID ) { throw new IOException( "Incompatible version of " + Config .FRESHNESS_SER + " got: " + fileSerialVersionUID + " expected: " + MiniFD .serialVersionUID ); } auxFiles = ( MiniFD[] ) ois.readObject(); ois.close(); } catch ( ClassNotFoundException e ) { Replicator.fatal( "Corrupt: " + freshnessFilename + "\n" + e.getMessage() ); } catch ( IOException e ) { Replicator.fatal( "Unable to read " + freshnessFilename + " file." + "\n" + e.getMessage() ); } if ( auxFiles == null ) { err.println( "unable to read " + Config.FRESHNESS_SER ); Replicator.die(); } for ( MiniFD fd : auxFiles ) { String auxFilename = ConfigForReceiver .RECEIVER_ZIP_STAGING_DIR + File .separatorChar + fd.getRelativeFilename( '/' ); String auxURL = ConfigForReceiver .RECEIVER_ZIP_URL + "/" + fd.getRelativeFilename( '/' ); final File auxFile = new File( auxFilename ); if ( auxFile.exists() && auxFile.lastModified() >= fd.getClumping() ) { // can bypass download continue; } Replicator.doing( "downloading: " + auxURL ); if ( !FetchZips.ft .download( new URL( auxURL ), new File( auxFilename ), false ) ) { Replicator.fatal( "Unable to download auxiliary file " + auxURL + " to " + auxFilename ); } } // end for } }