From ae874f6ef7583461c7613384bb829275e5779f25 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Sat, 6 Aug 2022 11:35:54 -0400 Subject: [PATCH] method (and prefs) for cleaning up temp dirs (resolves #529) --- app/src/processing/app/Base.java | 50 +++++++++++++++++++++++++-- app/src/processing/app/Console.java | 52 +++++++++++++++++------------ app/src/processing/app/Sketch.java | 1 + build/shared/lib/defaults.txt | 10 +++++- todo.txt | 32 +++++++++--------- 5 files changed, 105 insertions(+), 40 deletions(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index c1594ed55..b9966a464 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -258,8 +258,10 @@ public class Base { // Create a location for untitled sketches try { - untitledFolder = Util.createTempFolder("untitled", "sketches", null); - untitledFolder.deleteOnExit(); + //untitledFolder = Util.createTempFolder("untitled", "sketches", null); + //untitledFolder.deleteOnExit(); + untitledFolder = Util.getProcessingTemp(); + } catch (IOException e) { Messages.showError("Trouble without a name", "Could not create a place to store untitled sketches.\n" + @@ -281,6 +283,7 @@ public class Base { handleWelcomeScreen(base); handleCrustyDisplay(); + handleTempCleaning(); } catch (Throwable t) { // Catch-all to pick up badness during startup. @@ -406,6 +409,49 @@ public class Base { } + static private void handleTempCleaning() { + new Thread(() -> { + Console.cleanTempFiles(); + cleanTempFolders(); + }).start(); + } + + + /** + * Clean folders and files from the Processing subdirectory + * of the user's temp folder (java.io.tmpdir). + */ + static public void cleanTempFolders() { + try { + final File tempDir = Util.getProcessingTemp(); + final int days = Preferences.getInteger("temp.days"); + + if (days > 0) { + final long now = new Date().getTime(); + final long diff = days * 24 * 60 * 60 * 1000L; + File[] expiredFiles = + tempDir.listFiles(file -> (now - file.lastModified()) > diff); + if (expiredFiles != null) { + // Remove the files approved for deletion + for (File file : expiredFiles) { + //file.delete(); // not as safe + try { + Platform.deleteFile(file); // move to trash + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + + + + // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . diff --git a/app/src/processing/app/Console.java b/app/src/processing/app/Console.java index b63add2fe..badc4bb6d 100644 --- a/app/src/processing/app/Console.java +++ b/app/src/processing/app/Console.java @@ -88,27 +88,7 @@ public class Console { final String stamp = formatter.format(new Date()); File consoleDir = Base.getSettingsFile("console"); - if (consoleDir.exists()) { - // clear old debug files - File[] stdFiles = consoleDir.listFiles(new FileFilter() { - final String todayPrefix = stamp.substring(0, 4); - - public boolean accept(File file) { - if (!file.isDirectory()) { - String name = file.getName(); - if (name.endsWith(".err") || name.endsWith(".out")) { - // don't delete any of today's debug messages - return !name.startsWith(todayPrefix); - } - } - return false; - } - }); - // Remove any files that aren't from today - for (File file : stdFiles) { - file.delete(); - } - } else { + if (!consoleDir.exists()) { consoleDir.mkdirs(); consoleDir.setWritable(true, false); } @@ -141,6 +121,36 @@ public class Console { } + static public void cleanTempFiles() { + final File consoleDir = Base.getSettingsFile("console"); + final int days = Preferences.getInteger("console.temp.days"); + + if (days > 0) { + final long now = new Date().getTime(); + final long diff = days * 24 * 60 * 60 * 1000L; + File[] expiredFiles = consoleDir.listFiles(file -> { + if (!file.isDirectory()) { + String name = file.getName(); + // Not really + if (name.endsWith(".err") || name.endsWith(".out")) { + return now - file.lastModified() > diff; + } + } + return false; + }); + // Remove the files approved for deletion + for (File file : expiredFiles) { + //file.delete(); // not as safe + try { + Platform.deleteFile(file); // move to trash + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + static public void setEditor(OutputStream out, OutputStream err) { editorOut = out; editorErr = err; diff --git a/app/src/processing/app/Sketch.java b/app/src/processing/app/Sketch.java index 75638077f..a8f0fad73 100644 --- a/app/src/processing/app/Sketch.java +++ b/app/src/processing/app/Sketch.java @@ -39,6 +39,7 @@ import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.io.*; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; diff --git a/build/shared/lib/defaults.txt b/build/shared/lib/defaults.txt index ed5b2eda6..c943119c8 100644 --- a/build/shared/lib/defaults.txt +++ b/build/shared/lib/defaults.txt @@ -189,6 +189,10 @@ console.lines = 4 # each time 'run' is hit console.auto_clear = true +# number of days of history to keep around before cleaning +# setting to 0 will never clean files +console.temp.days = 7 + # set the maximum number of lines remembered by the console # the default is 500, lengthen at your own peril console.scrollback.lines = 500 @@ -214,7 +218,7 @@ run.options.bits.macos = 32 # -1 means the default display, 0 means all displays run.display = -1 -# set internally +# set internally because it comes from the system #run.window.bgcolor= # set to false to open a new untitled window when closing the last window @@ -238,6 +242,10 @@ sketch.name.replace_underscore = true # what to use for generating sketch names (change in the prefs window) #sketch.name.approach = +# number of days of build history and other temp files to keep around +# these are kept around for debugging purposes, and in case code is lost +temp.days = 7 + # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! diff --git a/todo.txt b/todo.txt index 55bd4a60a..9624cade2 100755 --- a/todo.txt +++ b/todo.txt @@ -24,6 +24,22 @@ X update the platforms page X https://github.com/processing/processing4/wiki/Supported-Platforms o rename to 'platforms'? +temp +X inside Sketch, makeTempFolder() would be the place to modify the location +X perhaps make a 'temp' inside the sketchbook folder? +X on startup, check to see if there are a lot of files, remove them? +X or maybe auto-delete once older than 24 hours? +X also don't search it when walking the sketchbook +X untitled folders are stored in temp folder +o add a note about temp dir to the bug on windows temp dirs +o move away from using a temp dir at all for sketches +o -Djava.io.tmpdir=Z:\temp +o change up how temp directories are handled +o it's sometimes copying the files to a different drive on windows +X clean up /tmp folders used during build +X https://github.com/processing/processing/issues/1896 (moved) +X https://github.com/processing/processing4/issues/529 + cleaning X the gradient.top and gradient.bottom lines are used by makeGradient(), X so search for makeGradient() calls to see whether they're still in use. @@ -335,24 +351,8 @@ _ errors inside setup() aren't coming through at all? _ seen in Eclipse; have to turn on the debugger... same as #4703? -temp -_ inside Sketch, makeTempFolder() would be the place to modify the location -_ perhaps make a 'temp' inside the sketchbook folder? -_ on startup, check to see if there are a lot of files, remove them? -_ or maybe auto-delete once older than 24 hours? -_ also don't search it when walking the sketchbook -_ untitled folders are stored in temp folder -_ add a note about temp dir to the bug on windows temp dirs -_ move away from using a temp dir at all for sketches -_ -Djava.io.tmpdir=Z:\temp -_ clean up /tmp folders used during build -_ https://github.com/processing/processing/issues/1896 -_ clean Windows temp folders -_ https://github.com/processing/processing/issues/1896 _ could not write to temporary directory (virus checker problems) _ https://github.com/processing/processing/issues/4757 -_ change up how temp directories are handled -_ it's sometimes copying the files to a different drive on windows modes