@NotNull public static String expandUserHome(@NotNull String path) { if (path.startsWith("~/") || path.startsWith("~\\")) { path = SystemProperties.getUserHome() + path.substring(1); } return path; }
@Contract("null -> null") public static String getLocationRelativeToUserHome(@Nullable final String path) { if (path == null) return null; if (SystemInfo.isUnix) { final File projectDir = new File(path); final File userHomeDir = new File(SystemProperties.getUserHome()); if (isAncestor(userHomeDir, projectDir, true)) { return "~/" + getRelativePath(userHomeDir, projectDir); } } return path; }
// null means we were unable to get roots, so do not check access @Nullable @TestOnly private static Set<String> allowedRoots() { if (insideGettingRoots) return null; Project[] openProjects = ProjectManager.getInstance().getOpenProjects(); if (openProjects.length == 0) return null; final Set<String> allowed = new THashSet<String>(); allowed.add(FileUtil.toSystemIndependentName(PathManager.getHomePath())); try { URL outUrl = Application.class.getResource("/"); String output = new File(outUrl.toURI()).getParentFile().getParentFile().getPath(); allowed.add(FileUtil.toSystemIndependentName(output)); } catch (URISyntaxException ignored) { } allowed.add(FileUtil.toSystemIndependentName(SystemProperties.getJavaHome())); allowed.add( FileUtil.toSystemIndependentName(new File(FileUtil.getTempDirectory()).getParent())); allowed.add(FileUtil.toSystemIndependentName(System.getProperty("java.io.tmpdir"))); allowed.add(FileUtil.toSystemIndependentName(SystemProperties.getUserHome())); for (Project project : openProjects) { if (!project.isInitialized()) { return null; // all is allowed } for (VirtualFile root : ProjectRootManager.getInstance(project).getContentRoots()) { allowed.add(root.getPath()); } for (VirtualFile root : getAllRoots(project)) { allowed.add(StringUtil.trimEnd(root.getPath(), JarFileSystem.JAR_SEPARATOR)); } String location = project.getBasePath(); assert location != null : project; allowed.add(FileUtil.toSystemIndependentName(location)); } allowed.addAll(ourAdditionalRoots); return allowed; }
@Nullable public static VirtualFile getUserHomeDir() { final String path = SystemProperties.getUserHome(); return LocalFileSystem.getInstance().findFileByPath(FileUtil.toSystemIndependentName(path)); }