From bb32741af9d831ed4942b502c7444b02d42dea7a Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Mon, 13 Jan 2014 04:11:05 +0000 Subject: [PATCH] * try to make sure we always start a new cache for each update (so we don't have to worry about outdated yet cached data) --- .../chocolatey/tools/chocolateyUninstall.ps1 | 2 ++ installer/nsis/filebot.nsi | 6 +++- source/net/sourceforge/filebot/Main.java | 31 +++++++++++++++++-- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/installer/chocolatey/tools/chocolateyUninstall.ps1 b/installer/chocolatey/tools/chocolateyUninstall.ps1 index 1b99b8b4..d0be22ec 100644 --- a/installer/chocolatey/tools/chocolateyUninstall.ps1 +++ b/installer/chocolatey/tools/chocolateyUninstall.ps1 @@ -1,3 +1,5 @@ +Invoke-Expression 'filebot -clear-cache' + $app = Get-WmiObject -Query "SELECT * FROM Win32_Product WHERE Name = 'FileBot'" echo $app diff --git a/installer/nsis/filebot.nsi b/installer/nsis/filebot.nsi index 8e83eddc..f01a9d2b 100644 --- a/installer/nsis/filebot.nsi +++ b/installer/nsis/filebot.nsi @@ -312,6 +312,10 @@ Section "$(Section_Name_MainProduct)" SECTIONID_MAINPRODUCT Pop $MSI_STATUS # grab return value ${if} $MSI_STATUS == "0" + DetailPrint "Clear cache and temporary files" + nsExec::Exec `"C:\Program Files\FileBot\filebot.exe" -clear-cache` + nsExec::Exec `"C:\Program Files\FileBot\filebot.exe" -script "g:net.sourceforge.filebot.Main.warmupCachedResources()"` + # [OpenCandy] ; This section is hidden. It will always execute during installation ; but it won't appear on your component selection screen. @@ -320,7 +324,7 @@ Section "$(Section_Name_MainProduct)" SECTIONID_MAINPRODUCT # [/OpenCandy] ${else} DetailPrint "msiexec error $MSI_STATUS" - DetailPrint "Install failed." + DetailPrint "Install failed. Please download the .msi package manually." Abort ${endif} SectionEnd diff --git a/source/net/sourceforge/filebot/Main.java b/source/net/sourceforge/filebot/Main.java index f73b1aac..329f3061 100644 --- a/source/net/sourceforge/filebot/Main.java +++ b/source/net/sourceforge/filebot/Main.java @@ -23,6 +23,7 @@ import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; +import java.nio.charset.Charset; import java.security.CodeSource; import java.security.Permission; import java.security.PermissionCollection; @@ -34,6 +35,7 @@ import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.Properties; +import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; @@ -458,13 +460,38 @@ public class Main { throw new IOException("Failed to create cache dir: " + cache); } - File lockFile = new File(cache, ".lock"); + final File lockFile = new File(cache, ".lock"); final RandomAccessFile handle = new RandomAccessFile(lockFile, "rw"); - final FileLock lock = handle.getChannel().tryLock(); + final FileChannel channel = handle.getChannel(); + final FileLock lock = channel.tryLock(); if (lock != null) { // setup cache dir for ehcache System.setProperty("ehcache.disk.store.dir", cache.getAbsolutePath()); + int applicationRevision = getApplicationRevisionNumber(); + int cacheRevision = 0; + try { + cacheRevision = new Scanner(channel, "UTF-8").nextInt(); + } catch (Exception e) { + // ignore + } + + if (cacheRevision != applicationRevision && applicationRevision > 0) { + System.out.format("Application (r%d) does not match cache (r%d): reset cache%n", applicationRevision, cacheRevision); + + // delete all files related to previous cache instances + for (File it : cache.listFiles()) { + if (!it.equals(lockFile)) { + delete(cache); + } + } + + // set new cache revision + channel.position(0); + channel.write(Charset.forName("UTF-8").encode(String.valueOf(applicationRevision))); + channel.truncate(channel.position()); + } + // make sure to orderly shutdown cache Runtime.getRuntime().addShutdownHook(new Thread() {