/// <summary> /// Indicates whether the given path is a UNC or drive pattern root directory. /// <para>Note: This function mimics the behavior of checking if Path.GetDirectoryName(path) == null.</para> /// </summary> /// <param name="path"></param> /// <returns></returns> private static bool IsRootDirectory(string path) { // Eliminate all non-rooted paths if (!Path.IsPathRooted(path)) { return(false); } int uncMatchLength = FileUtilitiesRegex.StartsWithUncPatternMatchLength(path); // Determine if the given path is a standard drive/unc pattern root if (FileUtilitiesRegex.IsDrivePattern(path) || FileUtilitiesRegex.IsDrivePatternWithSlash(path) || uncMatchLength == path.Length) { return(true); } // Eliminate all non-root unc paths. if (uncMatchLength != -1) { return(false); } // Eliminate any drive patterns that don't have a slash after the colon or where the 4th character is a non-slash // A non-slash at [3] is specifically checked here because Path.GetDirectoryName // considers "C:///" a valid root. if (FileUtilitiesRegex.StartsWithDrivePattern(path) && ((path.Length >= 3 && path[2] != '\\' && path[2] != '/') || (path.Length >= 4 && path[3] != '\\' && path[3] != '/'))) { return(false); } // There are some edge cases that can get to this point. // After eliminating valid / invalid roots, fall back on original behavior. return(Path.GetDirectoryName(path) == null); }
internal static string GetItemSpecModifier(string currentDirectory, string itemSpec, string definingProjectEscaped, string modifier, ref string fullPath) { ErrorUtilities.VerifyThrow(itemSpec != null, "Need item-spec to modify."); ErrorUtilities.VerifyThrow(modifier != null, "Need modifier to apply to item-spec."); string modifiedItemSpec = null; try { if (string.Equals(modifier, FileUtilities.ItemSpecModifiers.FullPath, StringComparison.OrdinalIgnoreCase)) { if (fullPath != null) { return(fullPath); } if (currentDirectory == null) { currentDirectory = String.Empty; } modifiedItemSpec = GetFullPath(itemSpec, currentDirectory); fullPath = modifiedItemSpec; ThrowForUrl(modifiedItemSpec, itemSpec, currentDirectory); } else if (string.Equals(modifier, FileUtilities.ItemSpecModifiers.RootDir, StringComparison.OrdinalIgnoreCase)) { GetItemSpecModifier(currentDirectory, itemSpec, definingProjectEscaped, ItemSpecModifiers.FullPath, ref fullPath); modifiedItemSpec = Path.GetPathRoot(fullPath); if (!EndsWithSlash(modifiedItemSpec)) { ErrorUtilities.VerifyThrow(FileUtilitiesRegex.StartsWithUncPattern(modifiedItemSpec), "Only UNC shares should be missing trailing slashes."); // restore/append trailing slash if Path.GetPathRoot() has either removed it, or failed to add it // (this happens with UNC shares) modifiedItemSpec += Path.DirectorySeparatorChar; } } else if (string.Equals(modifier, FileUtilities.ItemSpecModifiers.Filename, StringComparison.OrdinalIgnoreCase)) { // if the item-spec is a root directory, it can have no filename if (IsRootDirectory(itemSpec)) { // NOTE: this is to prevent Path.GetFileNameWithoutExtension() from treating server and share elements // in a UNC file-spec as filenames e.g. \\server, \\server\share modifiedItemSpec = String.Empty; } else { // Fix path to avoid problem with Path.GetFileNameWithoutExtension when backslashes in itemSpec on Unix modifiedItemSpec = Path.GetFileNameWithoutExtension(FixFilePath(itemSpec)); } } else if (string.Equals(modifier, FileUtilities.ItemSpecModifiers.Extension, StringComparison.OrdinalIgnoreCase)) { // if the item-spec is a root directory, it can have no extension if (IsRootDirectory(itemSpec)) { // NOTE: this is to prevent Path.GetExtension() from treating server and share elements in a UNC // file-spec as filenames e.g. \\server.ext, \\server\share.ext modifiedItemSpec = String.Empty; } else { modifiedItemSpec = Path.GetExtension(itemSpec); } } else if (string.Equals(modifier, FileUtilities.ItemSpecModifiers.RelativeDir, StringComparison.OrdinalIgnoreCase)) { modifiedItemSpec = GetDirectory(itemSpec); } else if (string.Equals(modifier, FileUtilities.ItemSpecModifiers.Directory, StringComparison.OrdinalIgnoreCase)) { GetItemSpecModifier(currentDirectory, itemSpec, definingProjectEscaped, ItemSpecModifiers.FullPath, ref fullPath); modifiedItemSpec = GetDirectory(fullPath); if (NativeMethodsShared.IsWindows) { int length = -1; if (FileUtilitiesRegex.StartsWithDrivePattern(modifiedItemSpec)) { length = 2; } else { length = FileUtilitiesRegex.StartsWithUncPatternMatchLength(modifiedItemSpec); } if (length != -1) { ErrorUtilities.VerifyThrow((modifiedItemSpec.Length > length) && IsSlash(modifiedItemSpec[length]), "Root directory must have a trailing slash."); modifiedItemSpec = modifiedItemSpec.Substring(length + 1); } } else { ErrorUtilities.VerifyThrow(!string.IsNullOrEmpty(modifiedItemSpec) && IsSlash(modifiedItemSpec[0]), "Expected a full non-windows path rooted at '/'."); // A full unix path is always rooted at // `/`, and a root-relative path is the // rest of the string. modifiedItemSpec = modifiedItemSpec.Substring(1); } } else if (string.Equals(modifier, FileUtilities.ItemSpecModifiers.RecursiveDir, StringComparison.OrdinalIgnoreCase)) { // only the BuildItem class can compute this modifier -- so leave empty modifiedItemSpec = String.Empty; } else if (string.Equals(modifier, FileUtilities.ItemSpecModifiers.Identity, StringComparison.OrdinalIgnoreCase)) { modifiedItemSpec = itemSpec; } else if (string.Equals(modifier, FileUtilities.ItemSpecModifiers.ModifiedTime, StringComparison.OrdinalIgnoreCase)) { // About to go out to the filesystem. This means data is leaving the engine, so need // to unescape first. string unescapedItemSpec = EscapingUtilities.UnescapeAll(itemSpec); FileInfo info = FileUtilities.GetFileInfoNoThrow(unescapedItemSpec); if (info != null) { modifiedItemSpec = info.LastWriteTime.ToString(FileTimeFormat, null); } else { // File does not exist, or path is a directory modifiedItemSpec = String.Empty; } } else if (string.Equals(modifier, FileUtilities.ItemSpecModifiers.CreatedTime, StringComparison.OrdinalIgnoreCase)) { // About to go out to the filesystem. This means data is leaving the engine, so need // to unescape first. string unescapedItemSpec = EscapingUtilities.UnescapeAll(itemSpec); if (FileSystems.Default.FileExists(unescapedItemSpec)) { modifiedItemSpec = File.GetCreationTime(unescapedItemSpec).ToString(FileTimeFormat, null); } else { // File does not exist, or path is a directory modifiedItemSpec = String.Empty; } } else if (string.Equals(modifier, FileUtilities.ItemSpecModifiers.AccessedTime, StringComparison.OrdinalIgnoreCase)) { // About to go out to the filesystem. This means data is leaving the engine, so need // to unescape first. string unescapedItemSpec = EscapingUtilities.UnescapeAll(itemSpec); if (FileSystems.Default.FileExists(unescapedItemSpec)) { modifiedItemSpec = File.GetLastAccessTime(unescapedItemSpec).ToString(FileTimeFormat, null); } else { // File does not exist, or path is a directory modifiedItemSpec = String.Empty; } } else if (IsDefiningProjectModifier(modifier)) { if (String.IsNullOrEmpty(definingProjectEscaped)) { // We have nothing to work with, but that's sometimes OK -- so just return String.Empty modifiedItemSpec = String.Empty; } else { if (string.Equals(modifier, FileUtilities.ItemSpecModifiers.DefiningProjectDirectory, StringComparison.OrdinalIgnoreCase)) { // ItemSpecModifiers.Directory does not contain the root directory modifiedItemSpec = Path.Combine ( GetItemSpecModifier(currentDirectory, definingProjectEscaped, null, ItemSpecModifiers.RootDir), GetItemSpecModifier(currentDirectory, definingProjectEscaped, null, ItemSpecModifiers.Directory) ); } else { string additionalModifier = null; if (string.Equals(modifier, FileUtilities.ItemSpecModifiers.DefiningProjectFullPath, StringComparison.OrdinalIgnoreCase)) { additionalModifier = ItemSpecModifiers.FullPath; } else if (string.Equals(modifier, FileUtilities.ItemSpecModifiers.DefiningProjectName, StringComparison.OrdinalIgnoreCase)) { additionalModifier = ItemSpecModifiers.Filename; } else if (string.Equals(modifier, FileUtilities.ItemSpecModifiers.DefiningProjectExtension, StringComparison.OrdinalIgnoreCase)) { additionalModifier = ItemSpecModifiers.Extension; } else { ErrorUtilities.ThrowInternalError("\"{0}\" is not a valid item-spec modifier.", modifier); } modifiedItemSpec = GetItemSpecModifier(currentDirectory, definingProjectEscaped, null, additionalModifier); } } } else { ErrorUtilities.ThrowInternalError("\"{0}\" is not a valid item-spec modifier.", modifier); } } catch (Exception e) when(ExceptionHandling.IsIoRelatedException(e)) { ErrorUtilities.VerifyThrowInvalidOperation(false, "Shared.InvalidFilespecForTransform", modifier, itemSpec, e.Message); } return(modifiedItemSpec); }