예제 #1
0
 protected AbsolutePathBase(string path)
     : base(path)
 {
     Type = UncPathHelper.IsAnAbsoluteUncPath(CurrentPath)
                             ? AbsolutePathType.UNC
                             : AbsolutePathType.DriveLetter;
 }
예제 #2
0
            private static bool IsAnAbsolutePath(string normalizedPath)
            {
                //Debug.Assert(normalizedPath != null);
                //Debug.Assert(normalizedPath.IsNormalized());

                return(IsAnAbsoluteDriveLetterPath(normalizedPath) || UncPathHelper.IsAnAbsoluteUncPath(normalizedPath));
            }
예제 #3
0
            internal static bool TryResolveInnerSpecialDirectory(string normalizedPath, out string resolvedPath, out string failureMessage)
            {
                //Argument.IsNotNullOrEmpty(nameof(normalizedPath), normalizedPath);

                //Debug.Assert(normalizedPath.IsNormalized());
                //Debug.Assert(ContainsInnerSpecialDirectory(normalizedPath));

                var    isPathRelative         = IsARelativePath(normalizedPath);
                var    originalPathNormalized = normalizedPath;
                var    isUNCPath        = UncPathHelper.StartLikeUncPath(normalizedPath);
                string serverShareStart = null;

                if (isUNCPath)
                {
                    // We can assert that coz already checked in IsValidAbsoluteDirectory()
                    //Debug.Assert(UNCPathHelper.IsAnAbsoluteUNCPath(normalizedPath));

                    normalizedPath = UncPathHelper.ConvertUncToDriveLetter(normalizedPath, out serverShareStart);
                }

                var pathDirectories = normalizedPath.Split(MiscHelpers.DirectorySeparatorChar);
                var rootDirectory   = pathDirectories[0];

                //Debug.Assert(pathDirs.Length > 0);

                if (isUNCPath)
                {
                    rootDirectory = serverShareStart;
                }

                var result = TryResolveInnerSpecialDirectory(pathDirectories, isPathRelative, out resolvedPath);

                if (!result)
                {
                    failureMessage =
                        $@"The pathString {{{originalPathNormalized}}} references the parent dir \..\ of the root dir {{{rootDirectory}}}, it cannot be resolved.";

                    return(false);
                }

                //Debug.Assert(resolvedPath != null);
                //Debug.Assert(resolvedPath.IsNormalized());

                if (isUNCPath)
                {
                    resolvedPath = UncPathHelper.ConvertDriveLetterToUnc(resolvedPath, serverShareStart);
                }

                failureMessage = null;

                return(true);
            }
예제 #4
0
            internal static bool TryGetAbsolutePathFrom(IAbsoluteDirectoryPath pathFrom, IPath pathTo, out string absolutePath, out string failureMessage)
            {
                //Argument.IsNotNull(nameof(pathFrom), pathFrom);
                //Argument.IsNotNull(nameof(pathTo), pathTo);
                //Debug.Assert(pathTo.IsRelativePath);

                if (pathFrom.Type == AbsolutePathType.DriveLetter)
                {
                    // Only work with Directory
                    if (pathTo.IsFilePath)
                    {
                        pathTo = pathTo.ParentDirectoryPath;
                    }

                    return(TryGetAbsolutePath(pathFrom.ToString(), pathTo.ToString(), out absolutePath, out failureMessage));
                }

                //Debug.Assert(pathFrom.Kind == AbsolutePathKind.UNC);

                string uncServerShareStart;
                string pathResultDriveLetter;

                var pathFromString     = pathFrom.ToString();
                var fakePathFromString = UncPathHelper.ConvertUncToDriveLetter(pathFromString, out uncServerShareStart);
                var fakePathFrom       = fakePathFromString.ToAbsoluteDirectoryPath();

                //Debug.Assert(fakePathFromString.IsValidAbsoluteDirectoryPath());

                if (!TryGetAbsolutePathFrom(fakePathFrom, pathTo, out pathResultDriveLetter, out failureMessage))
                {
                    failureMessage = failureMessage.Replace(UncPathHelper.FakeDriveLetterPrefix, uncServerShareStart);
                    absolutePath   = null;

                    return(false);
                }

                //Debug.Assert(pathResultDriveLetter != null);
                //Debug.Assert(pathResultDriveLetter.Length > 0);
                //Debug.Assert(pathResultDriveLetter.StartsWith(UNCPathHelper.FAKE_DRIVE_LETTER_PREFIX));

                absolutePath = UncPathHelper.ConvertDriveLetterToUnc(pathResultDriveLetter, uncServerShareStart);

                return(true);
            }