private static void AssertEqualTranslatedPath(DirectoryTranslator translator, string[] expected, string[] path)
        {
            string expectedAbsolute = A(expected);
            string pathAbsolute     = A(path);

            XAssert.AreEqual(expectedAbsolute.ToUpperInvariant(), translator.Translate(pathAbsolute).ToUpperInvariant());
        }
        private static void AssertEqualTranslatedPath(DirectoryTranslator translator, PathTable pathTable, string[] expected, string[] path)
        {
            string expectedAbsolute = A(expected);
            string pathAbsolute     = A(path);

            XAssert.AreEqual(AbsolutePath.Create(pathTable, expectedAbsolute), translator.Translate(AbsolutePath.Create(pathTable, pathAbsolute), pathTable));
        }
Example #3
0
        public void TestMalformedPaths()
        {
            var context   = BuildXLContext.CreateInstanceForTesting();
            var pathTable = context.PathTable;

            var translations = new[]
            {
                CreateInputTranslation(pathTable, new string[] { "K", "dbs", "sh", "dtb", "b" }, new string[] { "d", "dbs", "sh", "dtb", "0629_120346" }),
            };

            var translator = new DirectoryTranslator();

            translator.AddTranslations(translations, pathTable);
            translator.Seal();

            // None of these paths should be mutated by DirectoryTranslator
            foreach (string pathToTest in new string[] {
                @"\??\",
                @"\\?\",
                @":",
                @"d",
                @"k",
                @"",
            })
            {
                // Test edge cases for various paths. Note these explicitly don't go through path generation utilities because they can
                // massage away malformed paths that we explicitly want to test.
                AssertAreEqual(pathToTest, translator.Translate(pathToTest));
            }
        }
Example #4
0
        private bool TryResolveSymlinkedPath(string path, out ExpandedAbsolutePath expandedFinalPath)
        {
            if (!FileUtilities.TryGetFinalPathNameByPath(path, out string finalPathAsString, out _, volumeGuidPath: false))
            {
                // If the final path cannot be resolved (most common cause is file not found), we stay with the original path
                expandedFinalPath = default;
                return(false);
            }

            if (m_directoryTranslator != null)
            {
                finalPathAsString = m_directoryTranslator.Translate(finalPathAsString);
            }

            // We want to compare the final path with the parsed path, so let's go through the path table
            // to fully canonicalize it
            var success = AbsolutePath.TryCreate(m_context.PathTable, finalPathAsString, out var finalPath);

            if (!success)
            {
                Contract.Assume(false, $"The result of GetFinalPathNameByPath should always be a path we can parse. Original path is '{path}', final path is '{finalPathAsString}'.");
            }

            expandedFinalPath = ExpandedAbsolutePath.CreateUnsafe(finalPath, finalPathAsString);
            return(true);
        }
Example #5
0
 /// <summary>
 /// Translate the path based on the added translations
 /// </summary>
 public string Translate(string path)
 {
     return(m_translator.Translate(path));
 }
Example #6
0
        /// <summary>
        /// Tries to parse input line.
        /// </summary>
        /// <remarks>
        /// Input line must be of the form:
        ///   full path
        /// or
        ///   full path|comma separated <see cref="PathChanges"/>
        /// The former assumes that changes are <see cref="PathChanges.DataOrMetadataChanged"/>.
        /// </remarks>
        private static bool TryParseInput(
            LoggingContext loggingContext,
            string input,
            string filePath,
            int lineNo,
            out ChangedPathInfo changedPathInfo,
            string sourceRoot = null,
            DirectoryTranslator directoryTranslator = null)
        {
            Contract.Requires(loggingContext != null);
            Contract.Requires(!string.IsNullOrEmpty(input));

            changedPathInfo = default;
            string[] splitInput = input.Split(s_inputSeparator);

            if (splitInput.Length > 2)
            {
                Logger.Log.InvalidFormatOfInputChange(loggingContext, input, filePath, lineNo);
                return(false);
            }

            string changedPath = splitInput[0].Trim();
            string changesStr  = null;

            if (splitInput.Length == 2)
            {
                changesStr = splitInput[1].Trim();
            }

            // Assume data or metadata change if unspecified.
            PathChanges changes = PathChanges.DataOrMetadataChanged;

            try
            {
                if (!Path.IsPathRooted(changedPath))
                {
                    if (string.IsNullOrEmpty(sourceRoot))
                    {
                        Logger.Log.InvalidChangedPathOfInputChange(loggingContext, changedPath, filePath, lineNo);
                        return(false);
                    }

                    changedPath = Path.GetFullPath(Path.Combine(sourceRoot, changedPath));
                }

                if (directoryTranslator != null)
                {
                    changedPath = directoryTranslator.Translate(changedPath);
                }

                if (!string.IsNullOrEmpty(changesStr))
                {
                    if (!Enum.TryParse(changesStr, true, out changes))
                    {
                        string validKinds = string.Join(", ", ((PathChanges[])Enum.GetValues(typeof(PathChanges))).Select(c => c.ToString()));
                        Logger.Log.InvalidChangeKindsOfInputChange(loggingContext, changesStr, filePath, lineNo, validKinds);
                        return(false);
                    }
                }
            }
            catch (ArgumentException argumentException)
            {
                Logger.Log.InvalidInputChange(loggingContext, input, filePath, lineNo, argumentException.ToString());
                return(false);
            }

            changedPathInfo = new ChangedPathInfo(changedPath, changes);
            return(true);
        }