You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by "lkishalmi (via GitHub)" <gi...@apache.org> on 2023/07/04 01:40:28 UTC

[GitHub] [netbeans] lkishalmi opened a new pull request, #6148: Rework Janitor, try to use real version numbers, in the future

lkishalmi opened a new pull request, #6148:
URL: https://github.com/apache/netbeans/pull/6148

   
   Janitor will write a `.lastUsedVersion` file into user and cache directory in order to track the version of NetBeans they are used last. From there following versions shall know the associated version of that directory. 
   
   NetBeans used the name of the directory. In the Snap version that directory name is the Snap revision number which could result messages that `NetBeans 76 was last used ...`
   
   It's probably easier to review the new file instead of the diff.
   
   


-- 
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: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] lkishalmi commented on a diff in pull request #6148: Rework Janitor, try to use real version numbers, in the future

Posted by "lkishalmi (via GitHub)" <gi...@apache.org>.
lkishalmi commented on code in PR #6148:
URL: https://github.com/apache/netbeans/pull/6148#discussion_r1255089126


##########
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:
   Moved the main file operations to javax.nio.file API.



-- 
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: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] lkishalmi merged pull request #6148: Rework Janitor, try to use real version numbers, in the future

Posted by "lkishalmi (via GitHub)" <gi...@apache.org>.
lkishalmi merged PR #6148:
URL: https://github.com/apache/netbeans/pull/6148


-- 
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: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on a diff in pull request #6148: Rework Janitor, try to use real version numbers, in the future

Posted by "mbien (via GitHub)" <gi...@apache.org>.
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: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] lkishalmi commented on a diff in pull request #6148: Rework Janitor, try to use real version numbers, in the future

Posted by "lkishalmi (via GitHub)" <gi...@apache.org>.
lkishalmi commented on code in PR #6148:
URL: https://github.com/apache/netbeans/pull/6148#discussion_r1255083440


##########
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:
   Fixed.



-- 
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: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists