Index: lams_build/lib/lams/lams.jar =================================================================== diff -u -r6563149ab60daa42a7f37c0486e1cd248cacb200 -r70aab47cedf78d1e44e6e755b36fb095911bed52 Binary files differ Index: lams_common/src/java/org/lamsfoundation/lams/util/FileUtil.java =================================================================== diff -u -rb4afa75048185273ee61a4def2164134f343fda0 -r70aab47cedf78d1e44e6e755b36fb095911bed52 --- lams_common/src/java/org/lamsfoundation/lams/util/FileUtil.java (.../FileUtil.java) (revision b4afa75048185273ee61a4def2164134f343fda0) +++ lams_common/src/java/org/lamsfoundation/lams/util/FileUtil.java (.../FileUtil.java) (revision 70aab47cedf78d1e44e6e755b36fb095911bed52) @@ -29,6 +29,7 @@ import java.io.IOException; import java.io.OutputStream; import java.nio.channels.FileChannel; +import java.util.Date; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; @@ -43,6 +44,7 @@ public static final String LAMS_WWW_SECURE_DIR = "secure"; public static final String LAMS_WWW_DIR = "lams-www.war"; + private static final long numMilliSecondsInADay = 24 * 60 * 60 * 1000; protected static final String prefix = "lamstmp_"; // protected rather than private to suit junit test public static final String TEMP_DIR = System.getProperty("java.io.tmpdir"); @@ -427,4 +429,82 @@ return executable; } + + /** Clean up any old directories in the java tmp directory, where the + * directory name starts with lamszip_ or lamstmp_ and is days old + * or older. This has the potential to be a heavy call - it has to + * do complete directory listing and then recursively delete the + * files and directories as needed. + * + * Note: this method has not been tested as it is rather hard to write + * a junit test for! + * + * @param directories + * @return number of directories deleted + */ + public static int cleanupOldFiles(File[] directories) { + int numDeleted = 0; + if ( directories != null ) { + for ( int i=0; i < directories.length; i++) { + if ( FileUtil.deleteDirectory(directories[i]) ) { + log.info("Directory "+directories[i].getPath()+" deleted."); + } else { + log.info("Directory "+directories[i].getPath()+" partially deleted - some directories/files could not be deleted."); + } + numDeleted++; + } + } + return numDeleted; + } + + /** + * List files in temp directory older than numDays. + * @param numDays Number of days old that the directory should be to be + * deleted. Must be greater than 0 + * @return array of files older than input date + * @throws FileUtilException if numDays <= 0 + */ + public static File[] getOldTempFiles(int numDays) throws FileUtilException { + // Contract checking + if ( numDays < 0 ) { + throw new FileUtilException("Invalid getOldTempFiles call - the parameter numDays is "+ + numDays+". Must not be less than 0."); + } + + // calculate comparison date + long newestDateToKeep = System.currentTimeMillis() - ( numDays * numMilliSecondsInADay); + Date date = new Date(newestDateToKeep); + log.info("Getting all temp zipfile expanded directories before "+date.toString()+" (server time) ("+newestDateToKeep+")"); + + File tempSysDir = new File(TEMP_DIR); + File candidates[] = tempSysDir.listFiles(new TempDirectoryFilter(newestDateToKeep, log)); + return candidates; + } + + /** + * Recursively calculates size in bytes of given file or directory. + * @param file + * @return Size in bytes. + */ + public static long calculateFileSize(File file) { + if (file != null) { + if (file.isFile()) { + return file.length(); + } else if (file.isDirectory()) { + File[] fileList = file.listFiles(); + long totalSize = 0; + if (fileList != null) { + for (int i=0; i days old - * or older. This has the potential to be a heavy call - it has to - * do complete directory listing and then recursively delete the - * files and directories as needed. - * - * Note: this method has not been tested as it is rather hard to write - * a junit test for! - * - * @param numdays Number of days old that the directory should be to be - * deleted. Must be greater than 0 - * @return number of directories deleted - * @throws ZipFileUtilException if numDays <= 0 - */ - public static int cleanupOldFiles(int numDays) throws ZipFileUtilException { - - // Contract checking - if ( numDays <= 0 ) { - throw new ZipFileUtilException("Invalid cleanupOldFiles call - the parameter numDays is "+ - numDays+". Must be greater than 0. No files have been deleted."); - } - - // calculate comparison date - long newestDateToKeep = System.currentTimeMillis() - ( numDays * numMilliSecondsInADay); - Date date = new Date(newestDateToKeep); - log.info("Deleting all temp zipfile expanded directories before "+date.toString()+" (server time) ("+newestDateToKeep+")"); - - String tempSysDirName = System.getProperty( "java.io.tmpdir" ); - File tempSysDir = new File(tempSysDirName); - File candidates[] = tempSysDir.listFiles(new OldZipDirectoryFilter(newestDateToKeep, log)); - int numDeleted = 0; - if ( candidates != null ) { - for ( int i=0; i < candidates.length; i++) { - if ( FileUtil.deleteDirectory(candidates[i]) ) { - log.info("Directory "+candidates[i].getPath()+" deleted."); - } else { - log.info("Directory "+candidates[i].getPath()+" partially deleted - some directories/files could not be deleted."); - } - numDeleted++; - } - } - return numDeleted; - } private static String appendToString(String origString, String newMessage) { if ( origString != null )