Skip to content

Commit 51fece5

Browse files
author
Joe Berria
committed
Merge pull request #4 from vmiklos/master
Fix counting size of directories counting symlinks
2 parents edf793a + 976917e commit 51fece5

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

src/com/nexes/manager/FileManager.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -626,10 +626,6 @@ private void zip_folder(File file, ZipOutputStream zout) throws IOException {
626626
}
627627

628628
/*
629-
* This function will be rewritten as there is a problem getting
630-
* the directory size in certain folders from root. ex /sys, /proc.
631-
* The app will continue until a stack overflow. get size is fine uder the
632-
* sdcard folder.
633629
*
634630
* @param path
635631
*/
@@ -641,15 +637,31 @@ private void get_dir_size(File path) {
641637
len = list.length;
642638

643639
for (int i = 0; i < len; i++) {
644-
if(list[i].isFile() && list[i].canRead()) {
645-
mDirSize += list[i].length();
646-
647-
} else if(list[i].isDirectory() && list[i].canRead()) {
648-
get_dir_size(list[i]);
640+
try {
641+
if(list[i].isFile() && list[i].canRead()) {
642+
mDirSize += list[i].length();
643+
644+
} else if(list[i].isDirectory() && list[i].canRead() && !isSymlink(list[i])) {
645+
get_dir_size(list[i]);
646+
}
647+
} catch(IOException e) {
648+
Log.e("IOException", e.getMessage());
649649
}
650650
}
651651
}
652652
}
653+
654+
// Inspired by org.apache.commons.io.FileUtils.isSymlink()
655+
private static boolean isSymlink(File file) throws IOException {
656+
File fileInCanonicalDir = null;
657+
if (file.getParent() == null) {
658+
fileInCanonicalDir = file;
659+
} else {
660+
File canonicalDir = file.getParentFile().getCanonicalFile();
661+
fileInCanonicalDir = new File(canonicalDir, file.getName());
662+
}
663+
return !fileInCanonicalDir.getCanonicalFile().equals(fileInCanonicalDir.getAbsoluteFile());
664+
}
653665

654666
/*
655667
* (non-JavaDoc)

0 commit comments

Comments
 (0)