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())); } } }
public static void createEmptyJar(@NotNull String dir, @NotNull String name) throws IOException { File jar = new File(dir, name); FileUtil.ensureExists(jar.getParentFile()); IoTestUtil.createTestJar(jar); MessageDigest digest; try { digest = MessageDigest.getInstance("SHA1"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } digest.update(FileUtil.loadFileBytes(jar)); byte[] sha1 = digest.digest(); PrintWriter out = new PrintWriter(new File(dir, name + ".sha1"), "UTF-8"); try { for (byte b : sha1) out.printf("%02x", b); out.println(" " + name); } finally { out.close(); } }
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); } }