From 31e63bd47acbabfcead2de8bbfd3c61cfae0498c Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Wed, 3 Jun 2009 07:23:33 +0000 Subject: [PATCH] Bug 278769. Adding includes with ".." in the path. --- .../ui/editor/AddIncludeOnSelectionAction.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/AddIncludeOnSelectionAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/AddIncludeOnSelectionAction.java index 3ae22f90a7e..4fd8ebbaec5 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/AddIncludeOnSelectionAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/AddIncludeOnSelectionAction.java @@ -14,6 +14,7 @@ package org.eclipse.cdt.internal.ui.editor; import java.io.File; +import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.net.URI; import java.util.ArrayList; @@ -563,13 +564,13 @@ public class AddIncludeOnSelectionAction extends TextEditorAction { } // The file has never been included before. - IPath targetLocation = PathUtil.getCanonicalPath(new Path(file.getLocation().getURI().getPath())); + URI targetUri = file.getLocation().getURI(); + IPath targetLocation = PathUtil.getCanonicalPath(new Path(targetUri.getPath())); IPath sourceLocation = PathUtil.getCanonicalPath(fTu.getResource().getLocation()); boolean isSystemIncludePath = false; IPath path = PathUtil.makeRelativePathToIncludes(targetLocation, fIncludePath); - if (path != null && - ResourceLookup.findFilesForLocationURI(URI.create(targetLocation.toString())).length == 0) { + if (path != null && ResourceLookup.findFilesForLocationURI(targetUri).length == 0) { // A header file in the include path but outside the workspace is included with angle brackets. isSystemIncludePath = true; } @@ -584,6 +585,11 @@ public class AddIncludeOnSelectionAction extends TextEditorAction { targetLocation.getDevice().equalsIgnoreCase(sourceDirectory.getDevice())) { path = path.setDevice(null); } + if (path.isAbsolute() && path.getDevice() == null && + ResourceLookup.findFilesForLocationURI(targetUri).length != 0) { + // The file is inside workspace. Include with a relative path. + path = PathUtil.makeRelativePath(path, sourceDirectory); + } } return new RequiredInclude(path.toString(), isSystemIncludePath); } @@ -604,10 +610,14 @@ public class AddIncludeOnSelectionAction extends TextEditorAction { if (include.isSystemInclude()) { return false; } - return target.equals(new File(new File(fTu.getLocationURI().getPath()).getParent(), includeName)); + String directory = new File(fTu.getLocationURI().getPath()).getParent(); + return target.equals(new File(directory, includeName).getCanonicalFile()); } catch (CoreException e) { CUIPlugin.log(e); return false; + } catch (IOException e) { + CUIPlugin.log(e); + return false; } }