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: [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