This commit is contained in:
2019-10-16 21:37:17 +00:00
parent 80e3b031ad
commit 15f6c2cd2e
6 changed files with 204 additions and 0 deletions

11
.classpath Executable file
View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>

18
.project Executable file
View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>TreeSize</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.scala-ide.sdt.core.scalabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.scala-ide.sdt.core.scalanature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,55 @@
package dev.rsems.treesize.j;
import java.io.File;
import java.io.FileFilter;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Directory {
Directory parent;
File fsdir;
Long localSize;
Long totalSize;
List<Directory> children;
public Directory(Directory parent, File fsdir) {
this.parent = parent;
this.fsdir = fsdir;
File[] files = fsdir.listFiles(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isFile() && !Files.isSymbolicLink(f.toPath());
}
});
localSize = 0L;
if (files != null) {
for (int i = 0; i < files.length; i++) {
localSize += files[i].length();
}
}
File[] fsdirs = fsdir.listFiles(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory() && !Files.isSymbolicLink(f.toPath());
}
});
children = new ArrayList<>();
totalSize = localSize;
if (fsdirs != null) {
for (int i = 0; i < fsdirs.length; i++) {
Directory subdirectory = new Directory(this, fsdirs[i]);
children.add(subdirectory);
totalSize += subdirectory.totalSize;
}
Collections.sort(children, new Comparator<Directory>() {
@Override
public int compare(Directory d1, Directory d2) {
return -d1.totalSize.compareTo(d2.totalSize);
}
});
}
}
}

View File

@@ -0,0 +1,49 @@
package dev.rsems.treesize.j;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class Test {
static void recurse(Directory d, int level) {
List<Directory> subs = d.children;
for (int i = 0; i < subs.size(); i++) {
Directory child = subs.get(i);
Long ts = child.totalSize;
if (true) /* (ts >= 100000000L) */ {
StringBuilder sb = new StringBuilder(String.format("%,16d ", ts));
String s = "";
Directory temp = child;
int loop = 0;
while (temp.parent != null) {
/* ─ │ ┌ ┐ └ ┘ ├ ┤ ┬ ┴ ┼ */
if (temp.parent.children.indexOf(temp) == temp.parent.children.size() - 1) {
if (loop == 0) {
s = " └── " + s;
} else {
s = " " + s;
}
} else if (loop == 0) {
s = " ├── " + s;
} else {
s = "" + s;
}
temp = temp.parent;
loop++;
}
sb.append(s).append(child.fsdir.getName());
System.out.println(sb.toString());
}
if (level <= 4) recurse(child, level + 1);
}
}
public static void main(String[] args) throws IOException {
Directory d = new Directory(null, new File("/home/rob"));
String s = String.format("%,16d", d.totalSize) + " " + d.fsdir.getCanonicalPath();
System.out.println(s);
recurse(d, 1);
}
}

View File

@@ -0,0 +1,23 @@
package dev.rsems.treesize.s
import java.io.File
import java.nio.file.Files
import scala.collection.mutable.ListBuffer
class Directory(val fsdir: File, val parent: Directory = null, val sort: Boolean = false) {
private var localSize = 0L
var children = ListBuffer[Directory]()
private val files = fsdir.listFiles
if (files != null)
localSize = files.filter(f => f.isFile() && !Files.isSymbolicLink(f.toPath)).map(f => f.length).sum
var totalSize = localSize
private val dirs = fsdir.listFiles
if (dirs != null) {
totalSize += dirs.filter(f => f.isDirectory() && !Files.isSymbolicLink(f.toPath)).map(dir => {
val subdir = new Directory(dir, this, sort)
children += subdir
subdir.totalSize
}).sum
if (sort) children = children.sortBy(-_.totalSize)
}
}

View File

@@ -0,0 +1,48 @@
package dev.rsems.treesize.s
import java.io.File
object Test extends App {
// ─ │ ┌ ┐ └ ┘ ├ ┤ ┬ ┴ ┼
val LineUpAndRight = " \u2514\u2500\u2500 "; // " └── "
val LineBlank = " ";
val LineVerticalAndRight = " \u251c\u2500\u2500 "; // " ├── "
val LineVertical = " \u2502 "; // " │ "
def recurse(d: Directory, level: Int) {
for (child <- d.children) {
val ts = child.totalSize
val sb = new StringBuilder("%,16d ".format(ts))
var s = ""
var temp = child
var loop = 0
while (temp.parent != null) {
if (temp.parent.children.indexOf(temp) == temp.parent.children.size - 1) {
if (loop == 0) {
s = "└─ " + s
} else {
s = " " + s
}
} else if (loop == 0) {
s = "├─ " + s
} else {
s = "│ " + s
}
temp = temp.parent
loop += 1
}
sb.append(s).append(child.fsdir.getName)
println(sb.toString)
// if (level <= 4)
recurse(child, level + 1)
}
}
val dir = new Directory(new File("/home/rob"))
val s = "%,16d".format(dir.totalSize) + " " + dir.fsdir.getCanonicalPath
println(s)
recurse(dir, 1)
}