internal static string GetItemSpecModifier()

in src/Deprecated/Engine/Shared/FileUtilities.cs [313:517]


        internal static string GetItemSpecModifier(string currentDirectory, string itemSpec, string modifier, ref Hashtable cachedModifiers)
        {
            ErrorUtilities.VerifyThrow(itemSpec != null, "Need item-spec to modify.");
            ErrorUtilities.VerifyThrow(modifier != null, "Need modifier to apply to item-spec.");

            string modifiedItemSpec = null;

            // check if we have computed this modifier before
            if (cachedModifiers != null)
            {
                ErrorUtilities.VerifyThrow((string)cachedModifiers[String.Empty] == itemSpec,
                    "The cache of modifiers is only valid for one item-spec. If the item-spec changes, the cache must be nulled out, or a different cache passed in.");

                modifiedItemSpec = (string)cachedModifiers[modifier];
            }

            if (modifiedItemSpec == null)
            {
                // certain properties can't be cached -- this will be turned to true in those cases
                bool isVolatile = false;

                try
                {
                    if (String.Compare(modifier, ItemSpecModifiers.FullPath, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        if(currentDirectory == null)
                        {
                            currentDirectory = String.Empty;
                        }

                        modifiedItemSpec = GetFullPath(itemSpec, currentDirectory);
                    }
                    else if (String.Compare(modifier, ItemSpecModifiers.RootDir, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        if (currentDirectory == null)
                        {
                            currentDirectory = String.Empty;
                        }

                        string fullPath = Path.GetFullPath(Path.Combine(currentDirectory, itemSpec));
                        modifiedItemSpec = Path.GetPathRoot(fullPath);

                        if (!EndsWithSlash(modifiedItemSpec))
                        {
                            Debug.Assert(FileUtilitiesRegex.UNCPattern.IsMatch(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.Compare(modifier, ItemSpecModifiers.Filename, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        // if the item-spec is a root directory, it can have no filename
                        if (Path.GetDirectoryName(itemSpec) == null)
                        {
                            // 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
                        {
                            modifiedItemSpec = Path.GetFileNameWithoutExtension(itemSpec);
                        }
                    }
                    else if (String.Compare(modifier, ItemSpecModifiers.Extension, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        // if the item-spec is a root directory, it can have no extension
                        if (Path.GetDirectoryName(itemSpec) == null)
                        {
                            // 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.Compare(modifier, ItemSpecModifiers.RelativeDir, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        modifiedItemSpec = GetDirectory(itemSpec);
                    }
                    else if (String.Compare(modifier, ItemSpecModifiers.Directory, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        if (currentDirectory == null)
                        {
                            currentDirectory = String.Empty;
                        }

                        modifiedItemSpec = GetDirectory(GetFullPath(itemSpec, currentDirectory));
                        Match root = FileUtilitiesRegex.DrivePattern.Match(modifiedItemSpec);

                        if (!root.Success)
                        {
                            root = FileUtilitiesRegex.UNCPattern.Match(modifiedItemSpec);
                        }

                        if (root.Success)
                        {
                            ErrorUtilities.VerifyThrow((modifiedItemSpec.Length > root.Length) && IsSlash(modifiedItemSpec[root.Length]),
                                "Root directory must have a trailing slash.");

                            modifiedItemSpec = modifiedItemSpec.Substring(root.Length + 1);
                        }
                    }
                    else if (String.Compare(modifier, ItemSpecModifiers.RecursiveDir, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        // only the BuildItem class can compute this modifier -- so leave empty
                        modifiedItemSpec = String.Empty;
                    }
                    else if (String.Compare(modifier, ItemSpecModifiers.Identity, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        modifiedItemSpec = itemSpec;
                    }
                    else if (String.Compare(modifier, ItemSpecModifiers.ModifiedTime, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        isVolatile = true;

                        // 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.Compare(modifier, ItemSpecModifiers.CreatedTime, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        isVolatile = true;

                        // 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 (File.Exists(unescapedItemSpec))
                        {
                            modifiedItemSpec = File.GetCreationTime(unescapedItemSpec).ToString(FileTimeFormat, null);
                        }
                        else
                        {
                            // File does not exist, or path is a directory                        
                            modifiedItemSpec = String.Empty;
                        }
                    }
                    else if (String.Compare(modifier, ItemSpecModifiers.AccessedTime, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        isVolatile = true;

                        // 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 (File.Exists(unescapedItemSpec))
                        {
                            modifiedItemSpec = File.GetLastAccessTime(unescapedItemSpec).ToString(FileTimeFormat, null);
                        }
                        else
                        {
                            // File does not exist, or path is a directory                        
                            modifiedItemSpec = String.Empty;
                        }
                    }
                    else
                    {
                        ErrorUtilities.VerifyThrow(false, "\"{0}\" is not a valid item-spec modifier.", modifier);
                    }
                }
                catch (Exception e) // Catching Exception, but rethrowing unless it's a well-known exception.
                {
                    if (ExceptionHandling.NotExpectedException(e))
                        throw;
                    ErrorUtilities.VerifyThrowInvalidOperation(false, "Shared.InvalidFilespecForTransform", modifier, itemSpec, e.Message);
                }

                ErrorUtilities.VerifyThrow(modifiedItemSpec != null, "The item-spec modifier \"{0}\" was not evaluated.", modifier);

                // cache the modifier
                if (!isVolatile)
                {
                    if (cachedModifiers == null)
                    {
                        cachedModifiers = new Hashtable(StringComparer.OrdinalIgnoreCase);
     
                        // mark the cache to indicate the item-spec for which it was created
                        // NOTE: we've intentionally picked a key here that will never conflict with any modifier name -- if we
                        // use the item-spec as the key, it's possible for it to conflict with the name of a modifier
                        cachedModifiers[String.Empty] = itemSpec;
                    }

                    cachedModifiers[modifier] = modifiedItemSpec;
                }
            }

            return modifiedItemSpec;
        }