public static void copyDir( @NotNull File fromDir, @NotNull File toDir, @Nullable final FileFilter filter) throws IOException { ensureExists(toDir); if (isAncestor(fromDir, toDir, true)) { LOG.error(fromDir.getAbsolutePath() + " is ancestor of " + toDir + ". Can't copy to itself."); return; } File[] files = fromDir.listFiles(); if (files == null) throw new IOException( CommonBundle.message("exception.directory.is.invalid", fromDir.getPath())); if (!fromDir.canRead()) throw new IOException( CommonBundle.message("exception.directory.is.not.readable", fromDir.getPath())); for (File file : files) { if (filter != null && !filter.accept(file)) { continue; } if (file.isDirectory()) { copyDir(file, new File(toDir, file.getName()), filter); } else { copy(file, new File(toDir, file.getName())); } } }
/** * Copies content of {@code fromDir} to {@code toDir}. It's equivalent to "cp -r fromDir/* toDir" * unix command. * * @param fromDir source directory * @param toDir destination directory * @throws IOException in case of any IO troubles */ public static void copyDirContent(@NotNull File fromDir, @NotNull File toDir) throws IOException { File[] children = ObjectUtils.notNull(fromDir.listFiles(), ArrayUtil.EMPTY_FILE_ARRAY); for (File child : children) { File target = new File(toDir, child.getName()); if (child.isFile()) { copy(child, target); } else { copyDir(child, target, true); } } }
@NotNull public static List<File> findFilesByMask(@NotNull Pattern pattern, @NotNull File dir) { final ArrayList<File> found = new ArrayList<File>(); final File[] files = dir.listFiles(); if (files != null) { for (File file : files) { if (file.isDirectory()) { found.addAll(findFilesByMask(pattern, file)); } else if (pattern.matcher(file.getName()).matches()) { found.add(file); } } } return found; }
public static boolean delete(@NotNull File file) { FileAttributes attributes = FileSystemUtil.getAttributes(file); if (attributes == null) return true; if (attributes.isDirectory() && !attributes.isSymLink()) { File[] files = file.listFiles(); if (files != null) { for (File child : files) { if (!delete(child)) return false; } } } return deleteFile(file); }
public static boolean visitFiles(@NotNull File root, @NotNull Processor<File> processor) { if (!processor.process(root)) { return false; } File[] children = root.listFiles(); if (children != null) { for (File child : children) { if (!visitFiles(child, processor)) { return false; } } } return true; }
public static boolean moveDirWithContent(@NotNull File fromDir, @NotNull File toDir) { if (!toDir.exists()) return fromDir.renameTo(toDir); File[] files = fromDir.listFiles(); if (files == null) return false; boolean success = true; for (File fromFile : files) { File toFile = new File(toDir, fromFile.getName()); success = success && fromFile.renameTo(toFile); } fromDir.delete(); return success; }
public static boolean processFilesRecursively( @NotNull File root, @NotNull Processor<File> processor, @Nullable final Processor<File> directoryFilter) { final LinkedList<File> queue = new LinkedList<File>(); queue.add(root); while (!queue.isEmpty()) { final File file = queue.removeFirst(); if (!processor.process(file)) return false; if (file.isDirectory() && (directoryFilter == null || directoryFilter.process(file))) { final File[] children = file.listFiles(); if (children != null) { ContainerUtil.addAll(queue, children); } } } return true; }
private static void collectMatchedFiles( @NotNull File absoluteRoot, @NotNull File root, @NotNull Pattern pattern, @NotNull List<File> files) { final File[] dirs = root.listFiles(); if (dirs == null) return; for (File dir : dirs) { if (dir.isFile()) { final String relativePath = getRelativePath(absoluteRoot, dir); if (relativePath != null) { final String path = toSystemIndependentName(relativePath); if (pattern.matcher(path).matches()) { files.add(dir); } } } else { collectMatchedFiles(absoluteRoot, dir, pattern, files); } } }
@Nullable private static File findOldConfigDir( @NotNull File configDir, @Nullable String customPathSelector) { final File selectorDir = CONFIG_RELATED_PATH.isEmpty() ? configDir : configDir.getParentFile(); final File parent = selectorDir.getParentFile(); if (parent == null || !parent.exists()) { return null; } File maxFile = null; long lastModified = 0; final String selector = PathManager.getPathsSelector() != null ? PathManager.getPathsSelector() : selectorDir.getName(); final String prefix = getPrefixFromSelector(selector); final String customPrefix = customPathSelector != null ? getPrefixFromSelector(customPathSelector) : null; for (File file : parent.listFiles( new FilenameFilter() { @Override public boolean accept(@NotNull File file, @NotNull String name) { return StringUtil.startsWithIgnoreCase(name, prefix) || customPrefix != null && StringUtil.startsWithIgnoreCase(name, customPrefix); } })) { File options = new File(file, CONFIG_RELATED_PATH + OPTIONS_XML); if (!options.exists()) { continue; } long modified = options.lastModified(); if (modified > lastModified) { lastModified = modified; maxFile = file; } } return maxFile != null ? new File(maxFile, CONFIG_RELATED_PATH) : null; }
private static void copy( @NotNull File src, @NotNull File dest, ConfigImportSettings settings, File oldInstallationHome) throws IOException { src = src.getCanonicalFile(); dest = dest.getCanonicalFile(); if (!src.isDirectory()) { throw new IOException( ApplicationBundle.message( "config.import.invalid.directory.error", src.getAbsolutePath())); } if (!dest.isDirectory()) { throw new IOException( ApplicationBundle.message( "config.import.invalid.directory.error", dest.getAbsolutePath())); } if (FileUtil.filesEqual(src, dest)) { return; } FileUtil.ensureExists(dest); File[] childFiles = src.listFiles( new FilenameFilter() { @Override public boolean accept(@NotNull File dir, @NotNull String name) { // Don't copy plugins just imported. They're most probably incompatible with newer // idea version. return !StringUtil.startsWithChar(name, '.') && !name.equals(PLUGINS_PATH); } }); if (childFiles == null || childFiles.length == 0) { return; } for (File from : childFiles) { File to = new File(dest, from.getName()); if (from.isDirectory()) { FileUtil.copyDir(from, to, false); } else { FileUtil.copy(from, to); } } File plugins = new File(src, PLUGINS_PATH); if (!loadOldPlugins(plugins, dest) && SystemInfo.isMac) { File oldPluginsDir = getOldPath( oldInstallationHome, settings, PathManager.PROPERTY_PLUGINS_PATH, new Function<String, String>() { @Override public String fun(String pathSelector) { return PathManager.getDefaultPluginPathFor(pathSelector); } }); if (oldPluginsDir == null) { // e.g. installation home referred to config home. Try with default selector, same as config // name oldPluginsDir = new File(PathManager.getDefaultPluginPathFor(src.getName())); } loadOldPlugins(oldPluginsDir, dest); } }