diff --git a/src/main/java/net/pms/util/TempFileMgr.java b/src/main/java/net/pms/util/TempFileMgr.java index 7a9c2885cb0..6aaa1bdeb82 100644 --- a/src/main/java/net/pms/util/TempFileMgr.java +++ b/src/main/java/net/pms/util/TempFileMgr.java @@ -39,8 +39,14 @@ public class TempFileMgr { private static final int DEFAULT_CLEAN_TIME = 14 * 24 * 3600 * 1000; private static final int INTERVAL = 24 * 3600 * 1000; private final HashMap files; + private final File registryFile; public TempFileMgr() { + this(cleanFile()); + } + + TempFileMgr(File registryFile) { + this.registryFile = registryFile; files = new HashMap<>(); parseCleanFile(); } @@ -57,7 +63,7 @@ public void add(File f, String str) { } } - public void add(File f, int cleanTime) { + public synchronized void add(File f, int cleanTime) { files.put(f, cleanTime); try { dumpFile(); @@ -71,7 +77,7 @@ public void add(File f, int cleanTime) { } } - private void scan() { + private synchronized void scan() { long now = System.currentTimeMillis(); for (Iterator> it = files.entrySet().iterator(); it.hasNext();) { Entry entry = it.next(); @@ -98,14 +104,21 @@ private void scan() { } public void schedule() { - TimerTask task = new TimerTask() { + Timer t = new Timer(); + t.scheduleAtFixedRate(createCleanupTask(), 0, INTERVAL); + } + + TimerTask createCleanupTask() { + return new TimerTask() { @Override public void run() { - scan(); + try { + scan(); + } catch (RuntimeException e) { + LOGGER.error("Temporary file cleanup failed; the next scheduled run will retry", e); + } } }; - Timer t = new Timer(); - t.scheduleAtFixedRate(task, 0, INTERVAL); } private static File cleanFile() { @@ -113,7 +126,7 @@ private static File cleanFile() { } private void parseCleanFile() { - File f = cleanFile(); + File f = registryFile; if (!f.exists()) { return; } @@ -125,12 +138,18 @@ private void parseCleanFile() { if (StringUtils.isEmpty(str) || str.startsWith("#")) { continue; } - String[] tmp = str.split(","); - if (tmp.length > 1) { - add(new File(tmp[0]), tmp[1]); - } else { - add(new File(tmp[0])); + int separator = str.lastIndexOf(','); + String filename = str; + int cleanTime = DEFAULT_CLEAN_TIME; + if (separator >= 0) { + filename = str.substring(0, separator); + try { + cleanTime = Integer.parseInt(str.substring(separator + 1)); + } catch (NumberFormatException e) { + LOGGER.warn("Invalid cleanup time for temporary file \"{}\"; using default", filename); + } } + files.put(new File(filename), cleanTime); } } } catch (IOException e) { @@ -143,8 +162,8 @@ private void parseCleanFile() { } } - private void dumpFile() throws IOException { - try (FileOutputStream out = new FileOutputStream(cleanFile())) { + private synchronized void dumpFile() throws IOException { + try (FileOutputStream out = new FileOutputStream(registryFile)) { Date now = new Date(); String n = "## " + now.toString() + "\n"; out.write("#########\n".getBytes(StandardCharsets.UTF_8)); @@ -157,3 +176,4 @@ private void dumpFile() throws IOException { } } } + diff --git a/src/test/java/net/pms/util/TempFileMgrTest.java b/src/test/java/net/pms/util/TempFileMgrTest.java new file mode 100644 index 00000000000..13ed8e51f0a --- /dev/null +++ b/src/test/java/net/pms/util/TempFileMgrTest.java @@ -0,0 +1,166 @@ +/* + * This file is part of Universal Media Server, based on PS3 Media Server. + * + * This program is a free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; version 2 of the License only. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package net.pms.util; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.TimerTask; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicBoolean; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +class TempFileMgrTest { + + @TempDir + Path directory; + + @Test + void loadsLargeRegistryWithoutChangingIt() throws Exception { + Path registry = directory.resolve("UMS.tmpmgr"); + StringBuilder original = new StringBuilder("# existing registry\n"); + for (int i = 0; i < 1000; i++) { + original.append(directory.resolve("temporary-file-" + i)) + .append(",1209600000\n"); + } + Files.writeString(registry, original, StandardCharsets.UTF_8); + TempFileMgr manager = new TempFileMgr(registry.toFile()); + assertEquals(original.toString(), Files.readString(registry)); + + manager.add(directory.resolve("extra").toFile()); + String persisted = Files.readString(registry); + assertEquals(1001, persisted.lines().filter(line -> !line.startsWith("#")).count()); + for (int i = 0; i < 1000; i++) { + assertTrue(persisted.contains(directory.resolve("temporary-file-" + i) + ",1209600000\n")); + } + } + + @Test + void loadsCommaNamesAndLegacyOrInvalidTimeouts() throws Exception { + Path registry = directory.resolve("UMS.tmpmgr"); + Path comma = directory.resolve("movie,part1"); + Path legacy = directory.resolve("legacy"); + Path invalid = directory.resolve("invalid"); + Files.writeString(registry, comma + ",12345\n" + legacy + "\n" + invalid + ",oops\n"); + TempFileMgr manager = new TempFileMgr(registry.toFile()); + manager.add(directory.resolve("extra").toFile()); + String persisted = Files.readString(registry); + assertTrue(persisted.contains(comma + ",12345\n")); + assertTrue(persisted.contains(legacy + ",1209600000\n")); + assertTrue(persisted.contains(invalid + ",1209600000\n")); + } + + @Test + void serializesAddingWithAnInProgressScan() throws Exception { + TempFileMgr manager = new TempFileMgr(directory.resolve("UMS.tmpmgr").toFile()); + CountDownLatch entered = new CountDownLatch(1); + CountDownLatch release = new CountDownLatch(1); + File blocking = new File(directory.resolve("blocking").toString()) { + @Override + public boolean exists() { + entered.countDown(); + try { + if (!release.await(5, TimeUnit.SECONDS)) { + throw new IllegalStateException("Test scan timed out"); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IllegalStateException(e); + } + return true; + } + + @Override + public long lastModified() { + return System.currentTimeMillis(); + } + }; + manager.add(blocking); + ExecutorService executor = Executors.newFixedThreadPool(2); + try { + Future scan = executor.submit(manager.createCleanupTask()); + assertTrue(entered.await(5, TimeUnit.SECONDS)); + CountDownLatch adding = new CountDownLatch(1); + Future add = executor.submit(() -> { + adding.countDown(); + manager.add(directory.resolve("added").toFile()); + }); + assertTrue(adding.await(5, TimeUnit.SECONDS)); + assertThrows(TimeoutException.class, () -> add.get(100, TimeUnit.MILLISECONDS)); + release.countDown(); + scan.get(5, TimeUnit.SECONDS); + add.get(5, TimeUnit.SECONDS); + String persisted = Files.readString(directory.resolve("UMS.tmpmgr")); + assertTrue(persisted.contains(blocking.getAbsolutePath())); + assertTrue(persisted.contains(directory.resolve("added").toString())); + } finally { + release.countDown(); + executor.shutdownNow(); + assertTrue(executor.awaitTermination(5, TimeUnit.SECONDS)); + } + } + + @Test + void cleanupTaskCanRunAgainAfterRuntimeFailure() throws Exception { + Path registry = directory.resolve("UMS.tmpmgr"); + TempFileMgr manager = new TempFileMgr(registry.toFile()); + AtomicBoolean fail = new AtomicBoolean(true); + File unstable = new File(directory.resolve("unstable").toString()) { + @Override + public boolean exists() { + if (fail.getAndSet(false)) { + throw new IllegalStateException("Simulated filesystem failure"); + } + return false; + } + }; + manager.add(unstable); + TimerTask task = manager.createCleanupTask(); + assertDoesNotThrow(task::run); + assertTrue(Files.readString(registry).contains(unstable.getAbsolutePath())); + assertDoesNotThrow(task::run); + assertFalse(Files.readString(registry).contains(unstable.getAbsolutePath())); + } + + @Test + void deletesExpiredFilesAndKeepsRecentFiles() throws Exception { + Path registry = directory.resolve("UMS.tmpmgr"); + Path expired = Files.createFile(directory.resolve("expired")); + Path recent = Files.createFile(directory.resolve("recent")); + assertTrue(expired.toFile().setLastModified(System.currentTimeMillis() - 10000)); + TempFileMgr manager = new TempFileMgr(registry.toFile()); + manager.add(expired.toFile(), 1000); + manager.add(recent.toFile()); + manager.createCleanupTask().run(); + assertFalse(Files.exists(expired)); + assertTrue(Files.exists(recent)); + assertFalse(Files.readString(registry).contains(expired.toString())); + assertTrue(Files.readString(registry).contains(recent.toString())); + } +}