2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.openapi.vfs;
18 import com.intellij.openapi.application.ApplicationManager;
19 import com.intellij.openapi.diagnostic.Logger;
20 import com.intellij.openapi.fileTypes.FileTypeManager;
21 import com.intellij.openapi.fileTypes.FileTypes;
22 import com.intellij.openapi.util.*;
23 import com.intellij.openapi.util.io.FileUtil;
24 import com.intellij.openapi.util.text.StringUtil;
25 import com.intellij.openapi.vfs.encoding.EncodingManager;
26 import com.intellij.util.ArrayUtil;
27 import com.intellij.util.Function;
28 import com.intellij.util.PathUtil;
29 import com.intellij.util.Processor;
30 import com.intellij.util.containers.Convertor;
31 import com.intellij.util.io.URLUtil;
32 import com.intellij.util.io.fs.FileSystem;
33 import com.intellij.util.io.fs.IFile;
34 import gnu.trove.THashSet;
35 import org.jetbrains.annotations.NonNls;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
40 import java.net.MalformedURLException;
42 import java.nio.charset.Charset;
45 public class VfsUtil {
46 private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.vfs.VfsUtil");
48 public static String loadText(@NotNull VirtualFile file) throws IOException{
49 InputStreamReader reader = new InputStreamReader(file.getInputStream(), file.getCharset());
51 return new String(FileUtil.loadText(reader, (int)file.getLength()));
58 public static void saveText(@NotNull VirtualFile file, @NotNull String text) throws IOException {
59 Charset charset = file.getCharset();
60 file.setBinaryContent(text.getBytes(charset.name()));
64 * Checks whether the <code>ancestor {@link VirtualFile}</code> is parent of <code>file
65 * {@link VirtualFile}</code>.
67 * @param ancestor the file
68 * @param file the file
69 * @param strict if <code>false</code> then this method returns <code>true</code> if <code>ancestor</code>
70 * and <code>file</code> are equal
71 * @return <code>true</code> if <code>ancestor</code> is parent of <code>file</code>; <code>false</code> otherwise
73 public static boolean isAncestor(@NotNull VirtualFile ancestor, @NotNull VirtualFile file, boolean strict) {
74 if (!file.getFileSystem().equals(ancestor.getFileSystem())) return false;
75 VirtualFile parent = strict ? file.getParent() : file;
77 if (parent == null) return false;
78 if (parent.equals(ancestor)) return true;
79 parent = parent.getParent();
84 * Gets the relative path of <code>file</code> to its <code>ancestor</code>. Uses <code>separator</code> for
87 * @param file the file
88 * @param ancestor parent file
89 * @param separator character to use as files separator
90 * @return the relative path
92 public static String getRelativePath(@NotNull VirtualFile file, @NotNull VirtualFile ancestor, char separator) {
93 if (!file.getFileSystem().equals(ancestor.getFileSystem())) return null;
96 VirtualFile parent = file;
98 if (parent == null) return null;
99 if (parent.equals(ancestor)) break;
103 length += parent.getName().length();
104 parent = parent.getParent();
107 char[] chars = new char[length];
108 int index = chars.length;
111 if (parent.equals(ancestor)) break;
112 if (index < length) {
113 chars[--index] = separator;
115 String name = parent.getName();
116 for (int i = name.length() - 1; i >= 0; i--) {
117 chars[--index] = name.charAt(i);
119 parent = parent.getParent();
121 return new String(chars);
125 * Copies all files matching the <code>filter</code> from <code>fromDir</code> to <code>toDir</code>.
127 * @param requestor any object to control who called this method. Note that
128 * it is considered to be an external change if <code>requestor</code> is <code>null</code>.
129 * See {@link VirtualFileEvent#getRequestor}
130 * @param fromDir the directory to copy from
131 * @param toDir the directory to copy to
132 * @param filter {@link VirtualFileFilter}
133 * @throws IOException if files failed to be copied
135 public static void copyDirectory(Object requestor, @NotNull VirtualFile fromDir, @NotNull VirtualFile toDir, @Nullable VirtualFileFilter filter)
137 VirtualFile[] children = fromDir.getChildren();
138 for (VirtualFile child : children) {
139 if (filter == null || filter.accept(child)) {
140 if (!child.isDirectory()) {
141 copyFile(requestor, child, toDir);
144 VirtualFile newChild = toDir.createChildDirectory(requestor, child.getName());
145 copyDirectory(requestor, child, newChild, filter);
152 * Makes a copy of the <code>file</code> in the <code>toDir</code> folder and returns it.
154 * @param requestor any object to control who called this method. Note that
155 * it is considered to be an external change if <code>requestor</code> is <code>null</code>.
156 * See {@link VirtualFileEvent#getRequestor}
157 * @param file file to make a copy of
158 * @param toDir directory to make a copy in
159 * @return a copy of the file
160 * @throws IOException if file failed to be copied
162 public static VirtualFile copyFile(Object requestor, @NotNull VirtualFile file, @NotNull VirtualFile toDir) throws IOException {
163 return copyFile(requestor, file, toDir, file.getName());
167 * Makes a copy of the <code>file</code> in the <code>toDir</code> folder with the <code>newName</code> and returns it.
169 * @param requestor any object to control who called this method. Note that
170 * it is considered to be an external change if <code>requestor</code> is <code>null</code>.
171 * See {@link VirtualFileEvent#getRequestor}
172 * @param file file to make a copy of
173 * @param toDir directory to make a copy in
174 * @param newName new name of the file
175 * @return a copy of the file
176 * @throws IOException if file failed to be copied
178 public static VirtualFile copyFile(Object requestor, @NotNull VirtualFile file, @NotNull VirtualFile toDir, @NotNull @NonNls String newName)
180 final VirtualFile newChild = toDir.createChildData(requestor, newName);
181 // [jeka] TODO: to be duscussed if the copy should have the same timestamp as the original
182 //OutputStream out = newChild.getOutputStream(requestor, -1, file.getActualTimeStamp());
183 newChild.setBinaryContent(file.contentsToByteArray());
188 * Copies content of resource to the given file
190 * @param file to copy to
191 * @param resourceUrl url of the resource to be copied
192 * @throws java.io.IOException if resource not found or copying failed
194 public static void copyFromResource(@NotNull VirtualFile file, @NonNls @NotNull String resourceUrl) throws IOException {
195 InputStream out = VfsUtil.class.getResourceAsStream(resourceUrl);
197 throw new FileNotFoundException(resourceUrl);
200 byte[] bytes = FileUtil.adaptiveLoadBytes(out);
201 file.setBinaryContent(bytes);
208 * Gets the array of common ancestors for passed files.
210 * @param files array of files
211 * @return array of common ancestors for passed files
214 public static VirtualFile[] getCommonAncestors(@NotNull VirtualFile[] files) {
215 // Separate files by first component in the path.
216 HashMap<VirtualFile,Set<VirtualFile>> map = new HashMap<VirtualFile, Set<VirtualFile>>();
217 for (VirtualFile aFile : files) {
218 VirtualFile directory = aFile.isDirectory() ? aFile : aFile.getParent();
219 if (directory == null) return VirtualFile.EMPTY_ARRAY;
220 VirtualFile[] path = getPathComponents(directory);
221 Set<VirtualFile> filesSet;
222 final VirtualFile firstPart = path[0];
223 if (map.containsKey(firstPart)) {
224 filesSet = map.get(firstPart);
227 filesSet = new THashSet<VirtualFile>();
228 map.put(firstPart, filesSet);
230 filesSet.add(directory);
232 // Find common ancestor for each set of files.
233 ArrayList<VirtualFile> ancestorsList = new ArrayList<VirtualFile>();
234 for (Set<VirtualFile> filesSet : map.values()) {
235 VirtualFile ancestor = null;
236 for (VirtualFile file : filesSet) {
237 if (ancestor == null) {
241 ancestor = getCommonAncestor(ancestor, file);
242 //assertTrue(ancestor != null);
244 ancestorsList.add(ancestor);
247 return VfsUtil.toVirtualFileArray(ancestorsList);
251 * Gets the common ancestor for passed files, or null if the files do not have common ancestors.
253 * @param file1 fist file
254 * @param file2 second file
255 * @return common ancestor for the passed files. Returns <code>null</code> if
256 * the files do not have common ancestor
258 public static VirtualFile getCommonAncestor(@NotNull VirtualFile file1, @NotNull VirtualFile file2) {
259 if (!file1.getFileSystem().equals(file2.getFileSystem())) {
263 VirtualFile[] path1 = getPathComponents(file1);
264 VirtualFile[] path2 = getPathComponents(file2);
266 VirtualFile[] minLengthPath;
267 VirtualFile[] maxLengthPath;
268 if (path1.length < path2.length) {
269 minLengthPath = path1;
270 maxLengthPath = path2;
273 minLengthPath = path2;
274 maxLengthPath = path1;
277 int lastEqualIdx = -1;
278 for (int i = 0; i < minLengthPath.length; i++) {
279 if (minLengthPath[i].equals(maxLengthPath[i])) {
286 return lastEqualIdx == -1 ? null : minLengthPath[lastEqualIdx];
290 * Gets an array of files representing paths from root to the passed file.
292 * @param file the file
293 * @return virtual files which represents paths from root to the passed file
296 private static VirtualFile[] getPathComponents(@NotNull VirtualFile file) {
297 ArrayList<VirtualFile> componentsList = new ArrayList<VirtualFile>();
298 while (file != null) {
299 componentsList.add(file);
300 file = file.getParent();
302 int size = componentsList.size();
303 VirtualFile[] components = new VirtualFile[size];
304 for (int i = 0; i < size; i++) {
305 components[i] = componentsList.get(size - i - 1);
311 public static VirtualFile findRelativeFile(@NotNull VirtualFile base, String ... path) {
312 VirtualFile file = base;
314 for (String pathElement : path) {
315 file = file.findChild(pathElement);
316 if (file == null) return null;
322 @SuppressWarnings({"HardCodedStringLiteral"})
324 public static VirtualFile findRelativeFile(@NotNull String uri, VirtualFile base) {
326 if (!base.isValid()){
327 LOG.error("Invalid file name: " + base.getName() + ", url: " + uri);
331 uri = uri.replace('\\', '/');
333 if (uri.startsWith("file:///")) {
334 uri = uri.substring("file:///".length());
335 if (!SystemInfo.isWindows) uri = "/" + uri;
337 else if (uri.startsWith("file:/")) {
338 uri = uri.substring("file:/".length());
339 if (!SystemInfo.isWindows) uri = "/" + uri;
341 else if (uri.startsWith("file:")) {
342 uri = uri.substring("file:".length());
345 VirtualFile file = null;
347 if (uri.startsWith("jar:file:/")) {
348 uri = uri.substring("jar:file:/".length());
349 if (!SystemInfo.isWindows) uri = "/" + uri;
350 file = VirtualFileManager.getInstance().findFileByUrl(JarFileSystem.PROTOCOL_PREFIX + uri);
353 if (!SystemInfo.isWindows && StringUtil.startsWithChar(uri, '/')) {
354 file = LocalFileSystem.getInstance().findFileByPath(uri);
356 else if (SystemInfo.isWindows && uri.length() >= 2 && Character.isLetter(uri.charAt(0)) && uri.charAt(1) == ':') {
357 file = LocalFileSystem.getInstance().findFileByPath(uri);
361 if (file == null && uri.contains(JarFileSystem.JAR_SEPARATOR)) {
362 file = JarFileSystem.getInstance().findFileByPath(uri);
363 if (file == null && base == null) {
364 file = VirtualFileManager.getInstance().findFileByUrl(uri);
369 if (base == null) return LocalFileSystem.getInstance().findFileByPath(uri);
370 if (!base.isDirectory()) base = base.getParent();
371 if (base == null) return LocalFileSystem.getInstance().findFileByPath(uri);
372 file = VirtualFileManager.getInstance().findFileByUrl(base.getUrl() + "/" + uri);
373 if (file == null) return null;
379 @NonNls private static final String FILE = "file";
380 @NonNls private static final String JAR = "jar";
381 @NonNls private static final String MAILTO = "mailto";
382 private static final String PROTOCOL_DELIMITER = ":";
385 * Searches for the file specified by given java,net.URL.
386 * Note that this method currently tested only for "file" and "jar" protocols under Unix and Windows
388 * @param url the URL to find file by
389 * @return <code>{@link VirtualFile}</code> if the file was found, <code>null</code> otherwise
391 public static VirtualFile findFileByURL(@NotNull URL url) {
392 VirtualFileManager virtualFileManager = VirtualFileManager.getInstance();
393 return findFileByURL(url, virtualFileManager);
396 public static VirtualFile findFileByURL(@NotNull URL url, @NotNull VirtualFileManager virtualFileManager) {
397 String vfUrl = convertFromUrl(url);
398 return virtualFileManager.findFileByUrl(vfUrl);
402 * Converts VsfUrl info java.net.URL. Does not support "jar:" protocol.
404 * @param vfsUrl VFS url (as constructed by VfsFile.getUrl())
405 * @return converted URL or null if error has occured
409 public static URL convertToURL(@NotNull String vfsUrl) {
410 if (vfsUrl.startsWith(JAR)) {
411 LOG.error("jar: protocol not supported.");
415 // [stathik] for supporting mail URLs in Plugin Manager
416 if (vfsUrl.startsWith(MAILTO)) {
418 return new URL (vfsUrl);
420 catch (MalformedURLException e) {
425 String[] split = vfsUrl.split("://");
427 if (split.length != 2) {
428 LOG.debug("Malformed VFS URL: " + vfsUrl);
432 String protocol = split[0];
433 String path = split[1];
436 if (protocol.equals(FILE)) {
437 return new URL(protocol, "", path);
440 return new URL(vfsUrl);
443 catch (MalformedURLException e) {
444 LOG.debug("MalformedURLException occured:" + e.getMessage());
450 public static String convertFromUrl(@NotNull URL url) {
451 String protocol = url.getProtocol();
452 String path = url.getPath();
453 if (protocol.equals(JAR)) {
454 if (StringUtil.startsWithConcatenationOf(path, FILE, PROTOCOL_DELIMITER)) {
456 URL subURL = new URL(path);
457 path = subURL.getPath();
459 catch (MalformedURLException e) {
460 throw new RuntimeException(VfsBundle.message("url.parse.unhandled.exception"), e);
464 throw new RuntimeException(new IOException(VfsBundle.message("url.parse.error", url.toExternalForm())));
467 if (SystemInfo.isWindows || SystemInfo.isOS2) {
468 while (path.length() > 0 && path.charAt(0) == '/') {
469 path = path.substring(1, path.length());
473 path = URLUtil.unescapePercentSequences(path);
474 return protocol + "://" + path;
477 public static String urlToPath(@NonNls String url) {
478 if (url == null) return "";
479 return VirtualFileManager.extractPath(url);
483 public static String pathToUrl(@NotNull String path) {
484 return VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, path);
488 public static File virtualToIoFile(@NotNull VirtualFile file) {
489 return new File(PathUtil.toPresentableUrl(file.getUrl()));
493 public static IFile virtualToIFile(@NotNull VirtualFile file) {
494 return FileSystem.FILE_SYSTEM.createFile(PathUtil.toPresentableUrl(file.getUrl()));
497 public static VirtualFile copyFileRelative(Object requestor, @NotNull VirtualFile file, @NotNull VirtualFile toDir, @NotNull String relativePath) throws IOException {
498 StringTokenizer tokenizer = new StringTokenizer(relativePath,"/");
499 VirtualFile curDir = toDir;
502 String token = tokenizer.nextToken();
503 if (tokenizer.hasMoreTokens()) {
504 VirtualFile childDir = curDir.findChild(token);
505 if (childDir == null) {
506 childDir = curDir.createChildDirectory(requestor, token);
511 return copyFile(requestor, file, curDir, token);
517 public static String fixIDEAUrl(@NotNull String ideaUrl ) {
518 int idx = ideaUrl.indexOf("://");
520 String s = ideaUrl.substring(0, idx);
522 if (s.equals(JarFileSystem.PROTOCOL)) {
523 //noinspection HardCodedStringLiteral
526 ideaUrl = s+":/"+ideaUrl.substring(idx+3);
532 public static String fixURLforIDEA(@NotNull String url ) {
533 int idx = url.indexOf(":/");
534 if( idx >= 0 && idx+2 < url.length() && url.charAt(idx+2) != '/' ) {
535 String prefix = url.substring(0, idx);
536 String suffix = url.substring(idx+2);
538 if (SystemInfo.isWindows) {
539 url = prefix+"://"+suffix;
541 url = prefix+":///"+suffix;
547 public static boolean isAncestor(@NotNull File ancestor, @NotNull File file, boolean strict) {
548 File parent = strict ? file.getParentFile() : file;
549 while (parent != null) {
550 if (parent.equals(ancestor)) return true;
551 parent = parent.getParentFile();
558 * Returns the relative path from one virtual file to another.
560 * @param src the file from which the relative path is built.
561 * @param dst the file to which the path is built.
562 * @param separatorChar the separator for the path components.
563 * @return the relative path, or null if the files have no common ancestor.
568 public static String getPath(@NotNull VirtualFile src, @NotNull VirtualFile dst, char separatorChar) {
569 final VirtualFile commonAncestor = getCommonAncestor(src, dst);
570 if (commonAncestor != null) {
571 StringBuilder buffer = new StringBuilder();
572 if (src != commonAncestor) {
573 while (src.getParent() != commonAncestor) {
574 buffer.append("..").append(separatorChar);
575 src = src.getParent();
579 buffer.append(getRelativePath(dst, commonAncestor, separatorChar));
580 return buffer.toString();
586 public static boolean isValidName(@NotNull String name) {
587 return name.indexOf('\\') < 0 && name.indexOf('/') < 0;
590 public static String getUrlForLibraryRoot(@NotNull File libraryRoot) {
591 String path = FileUtil.toSystemIndependentName(libraryRoot.getAbsolutePath());
592 if (FileTypeManager.getInstance().getFileTypeByFileName(libraryRoot.getName()) == FileTypes.ARCHIVE) {
593 return VirtualFileManager.constructUrl(JarFileSystem.getInstance().getProtocol(), path + JarFileSystem.JAR_SEPARATOR);
596 return VirtualFileManager.constructUrl(LocalFileSystem.getInstance().getProtocol(), path);
600 public static VirtualFile createChildSequent(Object requestor, @NotNull VirtualFile dir, @NotNull String prefix, @NotNull String extension) throws IOException {
601 String fileName = prefix + "." + extension;
603 while (dir.findChild(fileName) != null) {
604 fileName = prefix + i + "." + extension;
607 return dir.createChildData(requestor, fileName);
611 public static String[] filterNames(@NotNull String[] names) {
612 int filteredCount = 0;
613 for (String string : names) {
614 if (isBadName(string)) filteredCount++;
616 if (filteredCount == 0) return names;
618 String[] result = ArrayUtil.newStringArray(names.length - filteredCount);
620 for (String string : names) {
621 if (isBadName(string)) continue;
622 result[count++] = string;
628 public static boolean isBadName(String name) {
629 return name == null || name.length() == 0 || "/".equals(name) || "\\".equals(name);
632 public static VirtualFile createDirectories(@NotNull final String dir) throws IOException {
633 final Ref<IOException> err = new Ref<IOException>();
634 VirtualFile result = ApplicationManager.getApplication().runWriteAction(new Computable<VirtualFile>() {
635 public VirtualFile compute() {
637 return createDirectoryIfMissing(dir);
639 catch (IOException e) {
645 if (!err.isNull()) throw err.get();
649 public static VirtualFile createDirectoryIfMissing(VirtualFile parent, String relativePath) throws IOException {
650 for (String each : StringUtil.split(relativePath, "/")) {
651 VirtualFile child = parent.findChild(each);
653 child = parent.createChildDirectory(LocalFileSystem.getInstance(), each);
661 public static VirtualFile createDirectoryIfMissing(@NotNull String dir) throws IOException {
662 return doCreateDirectoriesIfMissing(FileUtil.toSystemIndependentName(dir));
665 private static VirtualFile doCreateDirectoriesIfMissing(String dir) throws IOException {
666 final VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByPath(dir);
668 int pos = dir.lastIndexOf('/');
669 if (pos < 0) return null;
670 VirtualFile parent = createDirectoryIfMissing(dir.substring(0, pos));
671 if (parent == null) return null;
672 final String dirName = dir.substring(pos + 1);
673 return parent.createChildDirectory(LocalFileSystem.getInstance(), dirName);
678 public static <E extends Throwable> VirtualFile doActionAndRestoreEncoding(@NotNull VirtualFile fileBefore, @NotNull ThrowableComputable<VirtualFile, E> action) throws E {
679 Charset charsetBefore = EncodingManager.getInstance().getEncoding(fileBefore, true);
680 VirtualFile fileAfter = null;
682 fileAfter = action.compute();
686 if (fileAfter != null) {
687 Charset actual = EncodingManager.getInstance().getEncoding(fileAfter, true);
688 if (!Comparing.equal(actual, charsetBefore)) {
689 EncodingManager.getInstance().setEncoding(fileAfter, charsetBefore);
695 public static void processFileRecursivelyWithoutIgnored(@NotNull final VirtualFile root, @NotNull final Processor<VirtualFile> processor) {
696 final FileTypeManager ftm = FileTypeManager.getInstance();
697 processFilesRecursively(root, processor, new Convertor<VirtualFile, Boolean>() {
698 public Boolean convert(final VirtualFile vf) {
699 return ! ftm.isFileIgnored(vf.getName());
704 public static void processFilesRecursively(@NotNull VirtualFile root, @NotNull Processor<VirtualFile> processor,
705 @NotNull Convertor<VirtualFile, Boolean> directoryFilter) {
706 if (!processor.process(root)) return;
708 if (root.isDirectory() && directoryFilter.convert(root)) {
709 final LinkedList<VirtualFile[]> queue = new LinkedList<VirtualFile[]>();
711 queue.add(root.getChildren());
714 final VirtualFile[] files = queue.removeFirst();
716 for (VirtualFile file : files) {
717 if (!processor.process(file)) return;
718 if (file.isDirectory() && directoryFilter.convert(file)) {
719 queue.add(file.getChildren());
722 } while (!queue.isEmpty());
726 public static boolean processFilesRecursively(@NotNull VirtualFile root, @NotNull Processor<VirtualFile> processor) {
727 if (!processor.process(root)) return false;
729 if (root.isDirectory()) {
730 final LinkedList<VirtualFile[]> queue = new LinkedList<VirtualFile[]>();
732 queue.add(root.getChildren());
735 final VirtualFile[] files = queue.removeFirst();
737 for (VirtualFile file : files) {
738 if (!processor.process(file)) return false;
739 if (file.isDirectory()) {
740 queue.add(file.getChildren());
743 } while (!queue.isEmpty());
750 public static <T> T processInputStream(@NotNull final VirtualFile file, @NotNull Function<InputStream, T> function) {
751 InputStream stream = null;
753 stream = file.getInputStream();
754 return function.fun(stream);
756 catch (IOException e) {
760 if (stream != null) {
764 catch (IOException e) {
772 public static VirtualFile[] toVirtualFileArray(@NotNull Collection<? extends VirtualFile> files) {
773 int size = files.size();
774 if (size == 0) return VirtualFile.EMPTY_ARRAY;
775 return files.toArray(new VirtualFile[size]);