public static boolean isFolderRelevant( File f, PmsConfiguration configuration, Set<String> ignoreFiles) { if (f.isDirectory() && configuration.isHideEmptyFolders()) { File[] children = f.listFiles(); /** * listFiles() returns null if "this abstract pathname does not denote a directory, or if an * I/O error occurs". in this case (since we've already confirmed that it's a directory), this * seems to mean the directory is non-readable * http://www.ps3mediaserver.org/forum/viewtopic.php?f=6&t=15135 * http://stackoverflow.com/questions/3228147/retrieving-the-underlying-error-when-file-listfiles-return-null */ if (children == null) { LOGGER.warn("Can't list files in non-readable directory: {}", f.getAbsolutePath()); } else { for (File child : children) { if (ignoreFiles.contains(child.getAbsolutePath())) { continue; } if (child.isFile()) { if (FormatFactory.getAssociatedFormat(child.getName()) != null || isFileRelevant(child, configuration)) { return true; } } else { if (isFolderRelevant(child, configuration, ignoreFiles)) { return true; } } } } } return false; }
// XXX dir.canWrite() has issues on Windows, so verify it directly: // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6203387 public static boolean isDirectoryWritable(File dir) { boolean isWritable = false; if (dir != null) { // new File("").isDirectory() is false, even though getAbsolutePath() returns the right path. // this resolves it dir = dir.getAbsoluteFile(); if (dir.isDirectory()) { File file = new File( dir, String.format( "pms_directory_write_test_%d_%d.tmp", System.currentTimeMillis(), Thread.currentThread().getId())); try { if (file.createNewFile()) { if (isFileWritable(file)) { isWritable = true; } if (!file.delete()) { LOGGER.warn("Can't delete temporary test file: {}", file.getAbsolutePath()); } } } catch (IOException | SecurityException ioe) { } } } return isWritable; }
// this is called from a static initialiser, where errors aren't clearly reported, // so do everything possible to return a valid reponse, even if the parameters // aren't sane public static FileLocation getFileLocation( String customPath, String defaultDirectory, String defaultBasename) { File customFile = null; File directory = null; File file = null; if (isBlank(defaultBasename)) { // shouldn't get here defaultBasename = DEFAULT_BASENAME; } if (defaultDirectory == null) { defaultDirectory = ""; // current directory } if (customPath != null) { customFile = new File(customPath).getAbsoluteFile(); } if (customFile != null) { if (customFile.exists()) { if (customFile.isDirectory()) { directory = customFile; file = new File(customFile, defaultBasename).getAbsoluteFile(); } else { directory = customFile.getParentFile(); file = customFile; } } else { File parentDirectoryFile = customFile.getParentFile(); if (parentDirectoryFile != null && parentDirectoryFile.exists()) { // parent directory exists: the file can be created directory = parentDirectoryFile; file = customFile; } } } if (directory == null || file == null) { directory = new File(defaultDirectory).getAbsoluteFile(); file = new File(directory, defaultBasename).getAbsoluteFile(); } return new FileLocation(directory, file); }
// XXX dir.canRead() has issues on Windows, so verify it directly: // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6203387 public static boolean isDirectoryReadable(File dir) { boolean isReadable = false; if (dir != null) { // new File("").isDirectory() is false, even though getAbsolutePath() returns the right path. // this resolves it dir = dir.getAbsoluteFile(); if (dir.isDirectory()) { try { File[] files = dir.listFiles(); // null if an I/O error occurs isReadable = files != null; } catch (SecurityException se) { } } } return isReadable; }