method (and prefs) for cleaning up temp dirs (resolves #529)

This commit is contained in:
Ben Fry
2022-08-06 11:35:54 -04:00
parent 673745a934
commit ae874f6ef7
5 changed files with 105 additions and 40 deletions
+48 -2
View File
@@ -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();
}
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
+31 -21
View File
@@ -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;
+1
View File
@@ -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;
+9 -1
View File
@@ -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
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+16 -16
View File
@@ -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