Avoid endless loops when loading paths from text files

This commit is contained in:
Reinhard Pointner 2016-03-24 08:58:20 +00:00
parent 0304a12b76
commit 7843cab138
1 changed files with 33 additions and 33 deletions

View File

@ -1,16 +1,17 @@
package net.filebot.ui.rename; package net.filebot.ui.rename;
import static java.nio.charset.StandardCharsets.*;
import static java.util.stream.Collectors.*;
import static net.filebot.Logging.*; import static net.filebot.Logging.*;
import static net.filebot.MediaTypes.*; import static net.filebot.MediaTypes.*;
import static net.filebot.util.FileUtilities.*; import static net.filebot.util.FileUtilities.*;
import java.awt.datatransfer.Transferable; import java.awt.datatransfer.Transferable;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.nio.file.Files;
import java.util.Collection;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Scanner;
import java.util.Set; import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
@ -49,49 +50,48 @@ class FilesListTransferablePolicy extends BackgroundFileTransferablePolicy<File>
@Override @Override
protected void load(List<File> files, TransferAction action) { protected void load(List<File> files, TransferAction action) {
load(files, action != TransferAction.LINK); Set<File> fileset = new LinkedHashSet<File>();
// load files recursively by default
load(files, action != TransferAction.LINK, fileset);
publish(FastFile.create(fileset));
} }
protected void load(List<File> files, boolean recursive) { private void load(List<File> files, boolean recursive, Collection<File> sink) {
Set<File> entries = new LinkedHashSet<File>(); for (File f : files) {
LinkedList<File> queue = new LinkedList<File>(files); // ignore hidden files
if (f.isHidden()) {
while (queue.size() > 0) {
File f = queue.removeFirst();
if (f.isHidden())
continue; continue;
}
// load file paths from text files
if (recursive && LIST_FILES.accept(f)) { if (recursive && LIST_FILES.accept(f)) {
// don't use new Scanner(File) because of BUG 6368019 (http://bugs.sun.com/view_bug.do?bug_id=6368019) try {
try (Scanner scanner = new Scanner(createTextReader(f))) { List<File> list = Files.lines(f.toPath(), UTF_8).map(File::new).filter(it -> {
List<File> paths = new ArrayList<File>(); return it.isAbsolute() && it.exists();
while (scanner.hasNextLine()) { }).collect(toList());
String line = scanner.nextLine().trim();
if (line.length() > 0) {
File path = new File(line);
if (path.isAbsolute() && path.exists()) {
paths.add(path);
}
}
}
if (paths.isEmpty()) { if (list.isEmpty()) {
entries.add(f); // treat as simple text file sink.add(f); // treat as simple text file
} else { } else {
queue.addAll(0, paths); // add paths from text file load(list, false, sink); // add paths from text file
} }
} catch (Exception e) { } catch (Exception e) {
debug.log(Level.WARNING, e.getMessage(), e); debug.log(Level.WARNING, e.getMessage(), e);
} }
} else if (!recursive || f.isFile() || MediaDetection.isDiskFolder(f)) { }
entries.add(f);
} else if (f.isDirectory()) { // load normal files
queue.addAll(0, sortByUniquePath(getChildren(f))); // FORCE NATURAL FILE ORDER else if (!recursive || f.isFile() || MediaDetection.isDiskFolder(f)) {
sink.add(f);
}
// load folders recursively
else if (f.isDirectory()) {
load(sortByUniquePath(getChildren(f)), true, sink); // FORCE NATURAL FILE ORDER
} }
} }
publish(FastFile.create(entries));
} }
@Override @Override