mbien commented on code in PR #6148:
URL: https://github.com/apache/netbeans/pull/6148#discussion_r1252123244
##########
platform/janitor/src/org/netbeans/modules/janitor/Janitor.java:
##########
@@ -96,39 +95,60 @@ public class Janitor {
private static final String LOGFILE_NAME = "var/log/messages.log"; //NOI18N
private static final String ALL_CHECKSUM_NAME =
"lastModified/all-checksum.txt"; //NOI18N
+ private static final String LAST_VERSION_NAME = ".lastUsedVersion";
//NOI18N
+
+ private static final String NB_VERSION;
+
@StaticResource
private static final String CLEAN_ICON =
"org/netbeans/modules/janitor/resources/clean.gif"; //NOI18N
static final RequestProcessor JANITOR_RP = new RequestProcessor("janitor",
1); //NOI18N
static final Map<ActionListener, Notification> CLEANUP_TASKS = new
WeakHashMap<>();
+ static {
+ String version = System.getProperty("netbeans.buildnumber"); //NOI18N
+ if (version != null) {
+ // remove git hash from the build number
+ int dash = version.lastIndexOf('-');
+ if (dash + 41 == version.length()) { // 40 chars for git SHA sum,
1 for the dash
+ version = version.substring(0, dash);
+ }
+ }
+ NB_VERSION = version;
+ }
+
static void scanForJunk() {
// Remove previously opened notifications
CLEANUP_TASKS.values().forEach((nf) -> nf.clear());
CLEANUP_TASKS.clear();
Icon clean = ImageUtilities.loadImageIcon(CLEAN_ICON, false);
- List<Pair<String, Integer>> otherVersions = getOtherVersions();
-
- for (Pair<String, Integer> ver : otherVersions) {
- String name = ver.first();
- Integer age = ver.second();
- long toFree = size(getUserDir(name)) + size(getCacheDir(name));
- toFree = toFree / (1_000_000) + 1;
- if (getUserDir(name) != null) {
- ActionListener cleanupListener = cleanupAction(name,
Bundle.TXT_CONFIRM_CLEANUP(name));
- Notification nf = NotificationDisplayer.getDefault().notify(
- Bundle.TIT_ABANDONED_USERDIR(name, age, toFree),
- clean,
- Bundle.DESC_ABANDONED_USERDIR(name, age, toFree),
- cleanupListener);
-
- CLEANUP_TASKS.put(cleanupListener, nf);
+ List<CleanupPair> candidates = getCandidates();
+
+ Instant now = Instant.now();
+ int maxUnused = getUnusedDays();
+
+ for (CleanupPair candidate : candidates) {
+ int age = candidate.age(now);
+ int toFree = candidate.size();
+ String name = candidate.getName();
+ if (candidate.userdir != null) {
+ if (age > maxUnused) {
+ ActionListener cleanupListener = cleanupAction(candidate,
Bundle.TXT_CONFIRM_CLEANUP(name));
+ Notification nf =
NotificationDisplayer.getDefault().notify(
+ Bundle.TIT_ABANDONED_USERDIR(name, age, toFree),
+ clean,
+ Bundle.DESC_ABANDONED_USERDIR(name, age, toFree),
+ cleanupListener);
+
+ CLEANUP_TASKS.put(cleanupListener, nf);
+ }
} else {
if (isAutoRemoveAbanconedCache()) {
Review Comment:
typo in method name:
`isAutoRemoveAbanconedCache`
->
`isAutoRemoveAbandonedCache`
##########
platform/janitor/src/org/netbeans/modules/janitor/Janitor.java:
##########
@@ -338,4 +290,163 @@ static void setAutoRemoveAbanconedCache(boolean b) {
getPreferences().putBoolean(PROP_AUTO_REMOVE_ABANDONED_CACHE, b);
}
+ private static class CleanupDir {
+
+ enum Kind {USERDIR, CACHEDIR};
+
+ private final File dir;
+ private final Kind kind;
+
+ private CleanupDir(File dir, Kind kind) {
+ this.dir = dir;
+ this.kind = kind;
+ }
+
+ static CleanupDir get(CleanupDir.Kind kind, String version) {
+ File dir = kind == CleanupDir.Kind.USERDIR ?
Places.getUserDirectory() : Places.getCacheDirectory();
+ File f = new File(dir.getParentFile(), version);
+ File test = kind == CleanupDir.Kind.USERDIR ? new File(f,
LOGFILE_NAME) : new File(f, ALL_CHECKSUM_NAME);
+ return !f.equals(dir) && f.isDirectory() && test.isFile() ? new
CleanupDir(f, kind) : null;
+ }
+
+ public long size() {
+
+ if (dir == null) {
+ return 0;
+ }
+ final Path path = dir.toPath();
+ final AtomicLong size = new AtomicLong(0);
+
+ try {
+ Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
+ @Override
+ public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) {
+ size.addAndGet(attrs.size());
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult visitFileFailed(Path file,
IOException exc) {
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult postVisitDirectory(Path dir,
IOException exc) {
+ return FileVisitResult.CONTINUE;
+ }
+ });
+ } catch (IOException e) {
+ LOG.log(Level.FINE, "Something went wrong calculating the size
of " + dir.getAbsolutePath(), e); //NOI18N
+ }
+
+ return size.get();
+ }
+
+ public String getName() {
+ String name = dir.getName();
+ File f = new File(dir, LAST_VERSION_NAME);
+ if (f.isFile()) {
+ if (f.length() < 100) {
+ try (BufferedReader r = new BufferedReader(new
FileReader(f))) {
+ name = r.readLine();
+ } catch (IOException ex) {
+ // Could not read the file, stick to the dirname
+ }
+ } else{
+ LOG.log(Level.WARNING, "Skipped version file " +
f.getAbsolutePath() + " as it is suspiciously large."); //NOI18N
+ }
+ } else {
+ LOG.log(Level.INFO, f.getAbsolutePath() + " is missing
fallback to dirname: " + name); //NOI18N
+ switch (name) { // Map a few elder Snap release revision to
IDE version number, these could be removed in NetBeans 21/22
+ case "80":
+ return "18";
+ case "76":
+ return "17";
+ case "74":
+ return "16";
+ case "69":
+ return "15";
+ }
+ }
+ return name;
+ }
+
+ public int age(Instant now) {
+ int ret = -1;
+ File f;
Review Comment:
could be `Path`/`Paths.get`, since you call `toPath()` later anyway.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists