1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-08-13 17:03:45 -04:00

* due to permission issues File.listFiles() can return null

This commit is contained in:
Reinhard Pointner 2014-06-23 15:09:43 +00:00
parent 81f7c32b8c
commit f85c561b78
8 changed files with 114 additions and 164 deletions

View File

@ -48,8 +48,6 @@ import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.xml.parsers.DocumentBuilderFactory;
import net.miginfocom.swing.MigLayout;
import net.sf.ehcache.CacheManager;
import net.filebot.cli.ArgumentBean;
import net.filebot.cli.ArgumentProcessor;
import net.filebot.cli.CmdlineOperations;
@ -63,6 +61,8 @@ import net.filebot.util.ByteBufferInputStream;
import net.filebot.util.PreferencesMap.PreferencesEntry;
import net.filebot.util.TeePrintStream;
import net.filebot.web.CachedResource;
import net.miginfocom.swing.MigLayout;
import net.sf.ehcache.CacheManager;
import org.w3c.dom.NodeList;
@ -464,7 +464,7 @@ public class Main {
isNewCache = true;
// delete all files related to previous cache instances
for (File it : cache.listFiles()) {
for (File it : getChildren(cache)) {
if (!it.equals(lockFile)) {
delete(cache);
}

View File

@ -165,7 +165,7 @@ public class ArgumentBean {
if (recursive) {
files.addAll(listFiles(file));
} else {
files.addAll(asList(file.listFiles()));
files.addAll(getChildren(file));
}
} else {
files.add(file);

View File

@ -57,11 +57,7 @@ public class ScriptShellMethods {
}
public static List<File> listFiles(File self, Closure<?> closure) {
File[] files = self.listFiles();
if (files == null)
return emptyList();
return (List<File>) DefaultGroovyMethods.findAll(asList(files), closure);
return (List<File>) DefaultGroovyMethods.findAll(FileUtilities.getChildren(self), closure);
}
public static boolean isVideo(File self) {
@ -101,11 +97,7 @@ public class ScriptShellMethods {
}
public static boolean hasFile(File self, Closure<?> closure) {
File[] files = self.listFiles();
if (files == null)
return false;
return DefaultGroovyMethods.find(asList(files), closure) != null;
return DefaultGroovyMethods.find(FileUtilities.getChildren(self), closure) != null;
}
public static List<File> getFiles(File self) {
@ -163,7 +155,7 @@ public class ScriptShellMethods {
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
File folder = dir.toFile();
if (FileUtilities.filter(asList(folder.listFiles()), VIDEO_FILES).size() > 0 || MediaDetection.isDiskFolder(folder)) {
if (FileUtilities.filter(FileUtilities.getChildren(folder), VIDEO_FILES).size() > 0 || MediaDetection.isDiskFolder(folder)) {
mediaFolders.add(folder);
return FileVisitResult.SKIP_SUBTREE;
}

View File

@ -1,8 +1,7 @@
package net.filebot.ui.analyze;
import static net.filebot.ui.NotificationLogging.*;
import static net.filebot.util.FileUtilities.*;
import java.io.File;
import java.util.List;
@ -14,25 +13,20 @@ import net.filebot.ui.analyze.FileTree.FolderNode;
import net.filebot.ui.transfer.BackgroundFileTransferablePolicy;
import net.filebot.util.ExceptionUtilities;
import net.filebot.util.FastFile;
import net.filebot.util.FileUtilities;
class FileTreeTransferablePolicy extends BackgroundFileTransferablePolicy<AbstractTreeNode> {
private final FileTree tree;
public FileTreeTransferablePolicy(FileTree tree) {
this.tree = tree;
}
@Override
protected boolean accept(List<File> files) {
return true;
}
@Override
protected void clear() {
super.clear();
@ -40,7 +34,6 @@ class FileTreeTransferablePolicy extends BackgroundFileTransferablePolicy<Abstra
tree.clear();
}
@Override
protected void process(List<AbstractTreeNode> chunks) {
FolderNode root = tree.getRoot();
@ -52,13 +45,11 @@ class FileTreeTransferablePolicy extends BackgroundFileTransferablePolicy<Abstra
tree.getModel().reload();
}
@Override
protected void process(Exception e) {
UILogger.log(Level.WARNING, ExceptionUtilities.getRootCauseMessage(e), e);
}
@Override
protected void load(List<File> files) {
try {
@ -74,14 +65,13 @@ class FileTreeTransferablePolicy extends BackgroundFileTransferablePolicy<Abstra
}
}
private AbstractTreeNode getTreeNode(File file) throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
File[] files = file.listFiles();
if (files != null && file.isDirectory()) {
FolderNode node = new FolderNode(FileUtilities.getFolderName(file), files.length);
if (file.isDirectory()) {
List<File> files = getChildren(file);
FolderNode node = new FolderNode(getFolderName(file), files.size());
// add folders first
for (File f : files) {
@ -102,7 +92,6 @@ class FileTreeTransferablePolicy extends BackgroundFileTransferablePolicy<Abstra
return new FileNode(file);
}
@Override
public String getFileFilterDescription() {
return "files and folders";

View File

@ -1,6 +1,5 @@
package net.filebot.ui.rename;
import static java.util.Arrays.*;
import static net.filebot.MediaTypes.*;
import static net.filebot.ui.transfer.FileTransferable.*;
import static net.filebot.util.FileUtilities.*;
@ -11,7 +10,6 @@ import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -91,10 +89,7 @@ class FilesListTransferablePolicy extends FileTransferablePolicy {
} else if (!recursive || f.isFile() || MediaDetection.isDiskFolder(f)) {
entries.add(f);
} else if (f.isDirectory()) {
File[] children = f.listFiles();
if (children != null) {
queue.addAll(0, new TreeSet<File>(asList(children))); // FORCE NATURAL FILE ORDER
}
queue.addAll(0, sortByUniquePath(getChildren(f))); // FORCE NATURAL FILE ORDER
}
}

View File

@ -1,7 +1,5 @@
package net.filebot.ui.sfv;
import static java.util.Collections.*;
import static net.filebot.hash.VerificationUtilities.*;
import static net.filebot.ui.NotificationLogging.*;
@ -22,25 +20,21 @@ import net.filebot.hash.VerificationFileReader;
import net.filebot.ui.transfer.BackgroundFileTransferablePolicy;
import net.filebot.util.ExceptionUtilities;
class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<ChecksumCell> {
private final ChecksumTableModel model;
private final ChecksumComputationService computationService;
public ChecksumTableTransferablePolicy(ChecksumTableModel model, ChecksumComputationService checksumComputationService) {
this.model = model;
this.computationService = checksumComputationService;
}
@Override
protected boolean accept(List<File> files) {
return true;
}
@Override
protected void clear() {
super.clear();
@ -49,7 +43,6 @@ class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<C
model.clear();
}
@Override
protected void prepare(List<File> files) {
if (files.size() == 1 && getHashType(files.get(0)) != null) {
@ -57,23 +50,19 @@ class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<C
}
}
@Override
protected void process(List<ChecksumCell> chunks) {
model.addAll(chunks);
}
@Override
protected void process(Exception e) {
UILogger.log(Level.WARNING, ExceptionUtilities.getRootCauseMessage(e), e);
}
private final ThreadLocal<ExecutorService> executor = new ThreadLocal<ExecutorService>();
private final ThreadLocal<VerificationTracker> verificationTracker = new ThreadLocal<VerificationTracker>();
@Override
protected void load(List<File> files) throws IOException {
// initialize drop parameters
@ -87,7 +76,7 @@ class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<C
}
// handle single folder drop
else if (files.size() == 1 && files.get(0).isDirectory()) {
for (File file : files.get(0).listFiles()) {
for (File file : getChildren(files.get(0))) {
load(file, null, files.get(0));
}
}
@ -109,7 +98,6 @@ class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<C
}
}
protected void loadVerificationFile(File file, HashType type) throws IOException, InterruptedException {
VerificationFileReader parser = new VerificationFileReader(createTextReader(file), type.getFormat());
@ -137,7 +125,6 @@ class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<C
}
}
protected void load(File absoluteFile, File relativeFile, File root) throws IOException, InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
@ -151,7 +138,7 @@ class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<C
if (absoluteFile.isDirectory()) {
// load all files in the file tree
for (File child : absoluteFile.listFiles()) {
for (File child : getChildren(absoluteFile)) {
load(child, relativeFile, root);
}
} else {
@ -170,7 +157,6 @@ class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<C
}
}
protected ChecksumCell createComputationCell(String name, File root, HashType hash) {
ChecksumCell cell = new ChecksumCell(name, root, new ChecksumComputationTask(new File(root, name), hash));
@ -180,13 +166,11 @@ class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<C
return cell;
}
@Override
public String getFileFilterDescription() {
return "files, folders and sfv files";
}
private static class VerificationTracker {
private final Map<File, Integer> seen = new HashMap<File, Integer>();
@ -195,12 +179,10 @@ class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<C
private final int maxDepth;
public VerificationTracker(int maxDepth) {
this.maxDepth = maxDepth;
}
public Map<File, String> getHashByVerificationFile(File file) throws IOException {
// cache all verification files
File folder = file.getParentFile();
@ -250,12 +232,10 @@ class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<C
return result;
}
public HashType getVerificationFileType(File verificationFile) {
return types.get(verificationFile);
}
/**
* Completely read a verification file and resolve all relative file paths against a given base folder
*/

View File

@ -380,6 +380,17 @@ public final class FileUtilities {
return f;
}
public static List<File> getChildren(File file) {
File[] files = file.listFiles();
// children array may be null if folder permissions do not allow listing of files
if (files == null) {
return asList(new File[0]);
}
return asList(files);
}
public static List<File> listFiles(File... folders) {
return listFiles(asList(folders));
}
@ -417,11 +428,7 @@ public final class FileUtilities {
if (depth > maxDepth)
return;
File[] children = folder.listFiles();
if (children == null)
return;
for (File it : children) {
for (File it : getChildren(folder)) {
if (!addHidden && it.isHidden()) // ignore hidden files
continue;

View File

@ -1,6 +1,6 @@
package net.filebot.util;
import static net.filebot.util.FileUtilities.*;
import java.io.File;
import java.io.IOException;
@ -10,19 +10,15 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
public final class TemporaryFolder {
private static final Map<String, TemporaryFolder> folders = new HashMap<String, TemporaryFolder>();
/**
* Get a {@link TemporaryFolder} instance for a given name. The actual directory will be
* created lazily (e.g. when a file is created). The name of the directory will start with
* the given name (lower-case) and contain a unique id, so multiple application instances
* may run at the same time without the risk of interference.
* Get a {@link TemporaryFolder} instance for a given name. The actual directory will be created lazily (e.g. when a file is created). The name of the directory will start with the given name (lower-case) and contain a unique id, so multiple application instances may run at the same time without the risk of interference.
*
* @param name case-insensitive name of a temporary folder (e.g. application name)
* @param name
* case-insensitive name of a temporary folder (e.g. application name)
* @return temporary folder for this name
*/
public static TemporaryFolder getFolder(String name) {
@ -44,7 +40,6 @@ public final class TemporaryFolder {
}
}
/**
* Delete all temporary folders on shutdown
*/
@ -64,18 +59,18 @@ public final class TemporaryFolder {
private final File root;
private TemporaryFolder(File root) {
this.root = root;
}
/**
* Create an empty file in this temporary folder.
*
* @param name name of the file
* @param name
* name of the file
* @return newly created file
* @throws IOException if an I/O error occurred
* @throws IOException
* if an I/O error occurred
*/
public File createFile(String name) throws IOException {
@ -86,32 +81,28 @@ public final class TemporaryFolder {
return file;
}
/**
* Creates an empty file in this temporary folder, using the given prefix and suffix to
* generate its name.
* Creates an empty file in this temporary folder, using the given prefix and suffix to generate its name.
*
* @param prefix The prefix string to be used in generating the file's name; must be at
* least three characters long
* @param suffix The suffix string to be used in generating the file's name; may be null,
* in which case the suffix ".tmp" will be used
* @param prefix
* The prefix string to be used in generating the file's name; must be at least three characters long
* @param suffix
* The suffix string to be used in generating the file's name; may be null, in which case the suffix ".tmp" will be used
* @return An abstract pathname denoting a newly-created empty file
* @throws IOException If a file could not be created
* @throws IOException
* If a file could not be created
* @see File#createTempFile(String, String)
*/
public File createFile(String prefix, String suffix) throws IOException {
return File.createTempFile(prefix, suffix, getFolder());
}
public boolean deleteFile(String name) {
return new File(getFolder(), name).delete();
}
/**
* Retrieve the {@link File} object for this {@link TemporaryFolder}. The actual directory
* for the {@link TemporaryFolder} instance will be created by this method.
* Retrieve the {@link File} object for this {@link TemporaryFolder}. The actual directory for the {@link TemporaryFolder} instance will be created by this method.
*
* @return the {@link File} object for this {@link TemporaryFolder}
*/
@ -123,12 +114,10 @@ public final class TemporaryFolder {
return root;
}
public TemporaryFolder subFolder(String name) {
return new TemporaryFolder(new File(getFolder(), name));
}
public List<File> list(boolean recursive) {
List<File> list = new ArrayList<File>();
@ -137,10 +126,9 @@ public final class TemporaryFolder {
return list;
}
private void list(File file, List<File> list, boolean recursive) {
if (file.isDirectory()) {
for (File entry : file.listFiles()) {
for (File entry : getChildren(file)) {
if (entry.isDirectory()) {
if (recursive) {
list(entry, list, recursive);
@ -152,20 +140,19 @@ public final class TemporaryFolder {
}
}
public void delete() {
delete(root);
}
/**
* Delete files/folders recursively
*
* @param file file/folder that will be deleted
* @param file
* file/folder that will be deleted
*/
private void delete(File file) {
if (file.isDirectory()) {
for (File entry : file.listFiles()) {
for (File entry : getChildren(file)) {
delete(entry);
}
}