private static int getBuildNumber(File installDirectory) { installDirectory = installDirectory.getAbsoluteFile(); File buildTxt = new File(installDirectory, BUILD_NUMBER_FILE); if ((!buildTxt.exists()) || (buildTxt.isDirectory())) { buildTxt = new File(new File(installDirectory, BIN_FOLDER), BUILD_NUMBER_FILE); } if (buildTxt.exists() && !buildTxt.isDirectory()) { int buildNumber = -1; String buildNumberText = getContent(buildTxt); if (buildNumberText != null) { try { if (buildNumberText.length() > 1) { buildNumberText = buildNumberText.trim(); buildNumber = Integer.parseInt(buildNumberText); } } catch (Exception e) { // OK } } return buildNumber; } return -1; }
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())); } } }
@SuppressWarnings({"HardCodedStringLiteral"}) public static boolean isJarOrZip(File file) { if (file.isDirectory()) { return false; } final String name = file.getName(); return StringUtil.endsWithIgnoreCase(name, ".jar") || StringUtil.endsWithIgnoreCase(name, ".zip"); }
@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 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; }
/** * Returns empty string for empty path. First checks whether provided path is a path of a file * with sought-for name. Unless found, checks if provided file was a directory. In this case * checks existence of child files with given names in order "as provided". Finally checks * filename among brother-files of provided. Returns null if nothing found. * * @return path of the first of found files or empty string or null. */ @Nullable public static String findFileInProvidedPath(String providedPath, String... fileNames) { if (StringUtil.isEmpty(providedPath)) { return ""; } File providedFile = new File(providedPath); if (providedFile.exists()) { String name = providedFile.getName(); for (String fileName : fileNames) { if (name.equals(fileName)) { return toSystemDependentName(providedFile.getPath()); } } } if (providedFile.isDirectory()) { // user chose folder with file for (String fileName : fileNames) { File file = new File(providedFile, fileName); if (fileName.equals(file.getName()) && file.exists()) { return toSystemDependentName(file.getPath()); } } } providedFile = providedFile.getParentFile(); // users chose wrong file in same directory if (providedFile != null && providedFile.exists()) { for (String fileName : fileNames) { File file = new File(providedFile, fileName); if (fileName.equals(file.getName()) && file.exists()) { return toSystemDependentName(file.getPath()); } } } return 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); } }