Example #1
0
        /// <inheritdoc />
        protected override UPath ConvertPathFromInternalImpl(string innerPath)
        {
            if (IsOnWindows)
            {
                // We currently don't support special Windows files (\\.\ \??\  DosDevices...etc.)
                if (innerPath.StartsWith(@"\\") || innerPath.StartsWith(@"\?"))
                {
                    throw new NotSupportedException($"Path starting with `\\\\` or `\\?` are not supported -> `{innerPath}` ");
                }

                var absolutePath = Path.GetFullPath(innerPath);
                var driveIndex   = absolutePath.IndexOf(":\\", StringComparison.Ordinal);
                if (driveIndex != 1)
                {
                    throw new ArgumentException($"Expecting a drive for the path `{absolutePath}`");
                }

                var builder = UPath.GetSharedStringBuilder();
                builder.Append(DrivePrefixOnWindows).Append(char.ToLowerInvariant(absolutePath[0])).Append('/');
                if (absolutePath.Length > 2)
                {
                    builder.Append(absolutePath.Substring(2));
                }

                var result = builder.ToString();
                builder.Length = 0;
                return(new UPath(result));
            }
            return(innerPath);
        }
Example #2
0
        // ----------------------------------------------
        // Path API
        // ----------------------------------------------

        /// <inheritdoc />
        protected override string ConvertPathToInternalImpl(UPath path)
        {
            var absolutePath = path.FullName;

            if (IsOnWindows)
            {
                if (!absolutePath.StartsWith(DrivePrefixOnWindows) ||
                    absolutePath.Length == DrivePrefixOnWindows.Length ||
                    !IsDriveLetter(absolutePath[DrivePrefixOnWindows.Length]))
                {
                    throw new ArgumentException($"A path on Windows must start by `{DrivePrefixOnWindows}` followed by the drive letter");
                }

                var driveLetter = char.ToUpper(absolutePath[DrivePrefixOnWindows.Length]);
                if (absolutePath.Length != DrivePrefixOnWindows.Length + 1 &&
                    absolutePath[DrivePrefixOnWindows.Length + 1] !=
                    UPath.DirectorySeparator)
                {
                    throw new ArgumentException($"The driver letter `/{DrivePrefixOnWindows}{absolutePath[DrivePrefixOnWindows.Length]}` must be followed by a `/` or nothing in the path -> `{absolutePath}`");
                }

                var builder = UPath.GetSharedStringBuilder();
                builder.Append(driveLetter).Append(":\\");
                if (absolutePath.Length > DrivePrefixOnWindows.Length + 1)
                {
                    builder.Append(absolutePath.Replace(UPath.DirectorySeparator, '\\').Substring(DrivePrefixOnWindows.Length + 2));
                }

                var result = builder.ToString();
                builder.Length = 0;
                return(result);
            }
            return(absolutePath);
        }