From: Niki Roo Date: Sun, 14 Apr 2019 09:09:25 +0000 (+0200) Subject: traces, select TMP X-Git-Tag: fanfix-3.0.1~29^2~157 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=82fcfcde9722e9e3b3eb713cf1f2bcb35fefaa7e;p=fanfix.git traces, select TMP --- diff --git a/changelog.md b/changelog.md index bd68657..bf410cb 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,11 @@ # nikiroo-utils +## Version WIP + +- new: can now select the TempFiles root +- new: can now select the Image temporary root +- fix: Cache now offer some tracing when cleaning the cache + ## Version 4.7.0 - tracer: traces on stderr by default diff --git a/src/be/nikiroo/utils/Cache.java b/src/be/nikiroo/utils/Cache.java index 4750ef8..18b811e 100644 --- a/src/be/nikiroo/utils/Cache.java +++ b/src/be/nikiroo/utils/Cache.java @@ -173,12 +173,36 @@ public class Cache { * @return the number of cleaned items */ private int clean(boolean onlyOld, File cacheDir) { + long ms = System.currentTimeMillis(); + + tracer.trace("Cleaning cache from old files..."); + + int num = doClean(onlyOld, cacheDir); + + tracer.trace("Cache cleaned in " + (System.currentTimeMillis() - ms) + + " ms"); + + return num; + } + + /** + * Actual work done for {@link Cache#clean(boolean, File)}. + * + * @param onlyOld + * only clean the files that are considered too old for stable + * resources + * @param cacheDir + * the cache directory to clean + * + * @return the number of cleaned items + */ + private int doClean(boolean onlyOld, File cacheDir) { int num = 0; File[] files = cacheDir.listFiles(); if (files != null) { for (File file : files) { if (file.isDirectory()) { - num += clean(onlyOld, file); + num += doClean(onlyOld, file); } else { if (!onlyOld || isOld(file, true)) { if (file.delete()) { diff --git a/src/be/nikiroo/utils/Image.java b/src/be/nikiroo/utils/Image.java index 4eacdac..6edc105 100644 --- a/src/be/nikiroo/utils/Image.java +++ b/src/be/nikiroo/utils/Image.java @@ -13,6 +13,7 @@ import java.io.InputStream; * @author niki */ public class Image implements Closeable { + static private File tempRoot; static private TempFiles tmpRepository; static private long count = 0; static private Object lock = new Object(); @@ -143,7 +144,7 @@ public class Image implements Closeable { private File getTemporaryFile() throws IOException { synchronized (lock) { if (tmpRepository == null) { - tmpRepository = new TempFiles("images"); + tmpRepository = new TempFiles(tempRoot, "images"); count = 0; } @@ -152,4 +153,23 @@ public class Image implements Closeable { return tmpRepository.createTempFile("image"); } } + + /** + * Change the temporary root directory used by the program. + *

+ * Caution: the directory will be owned by the system, all its files + * now belong to us (and will most probably be deleted). + *

+ * Note: it may take some time until the new temporary root is used, we + * first need to make sure the previous one is not used anymore (i.e., we + * must reach a point where no unclosed {@link Image} remains in memory) to + * switch the temporary root. + * + * @param root + * the new temporary root, which will be owned by the + * system + */ + public static void setTemporaryFilesRoot(File root) { + tempRoot = root; + } } diff --git a/src/be/nikiroo/utils/TempFiles.java b/src/be/nikiroo/utils/TempFiles.java index 186bfb8..b4ac6d2 100644 --- a/src/be/nikiroo/utils/TempFiles.java +++ b/src/be/nikiroo/utils/TempFiles.java @@ -11,6 +11,10 @@ import java.io.IOException; * @author niki */ public class TempFiles implements Closeable { + /** + * Root directory of this instance, owned by it, where all temporary files + * must reside. + */ protected File root; /** @@ -18,7 +22,7 @@ public class TempFiles implements Closeable { * dedicated sub-directory in a shared temporary root. *

* The whole repository will be deleted on close (if you fail to call it, - * the program will try to all it on JVM termination). + * the program will try to call it on JVM termination). * * @param name * the instance name (will be part of the final directory @@ -28,7 +32,37 @@ public class TempFiles implements Closeable { * in case of I/O error */ public TempFiles(String name) throws IOException { - root = File.createTempFile(".temp", ""); + this(null, name); + } + + /** + * Create a new {@link TempFiles} -- each instance is separate and have a + * dedicated sub-directory in a given temporary root. + *

+ * The whole repository will be deleted on close (if you fail to call it, + * the program will try to call it on JVM termination). + *

+ * Be careful, this instance will own the given root directory, and + * will most probably delete all its files. + * + * @param base + * the root base directory to use for all the temporary files of + * this instance (if NULL, will be the default temporary + * directory of the OS) + * @param name + * the instance name (will be part of the final directory + * name) + * + * @throws IOException + * in case of I/O error + */ + public TempFiles(File base, String name) throws IOException { + if (base == null) { + base = File.createTempFile(".temp", ""); + } + + root = base; + IOUtils.deltree(root, true); root = new File(root.getParentFile(), ".temp");