/// <summary> /// Determine the type of a nuget source. This works for both offline and online sources. /// </summary> public static FeedType GetFeedType(PackageSource packageSource) { // Default to unknown file system var type = FeedType.FileSystemUnknown; if (packageSource.IsHttp) { if (packageSource.Source.EndsWith(".json", StringComparison.OrdinalIgnoreCase)) { type = FeedType.HttpV3; } else { type = FeedType.HttpV2; } } else if (packageSource.IsLocal) { var path = UriUtility.GetLocalPath(packageSource.Source); if (!Directory.Exists(path)) { // If the directory doesn't exist check again later type = FeedType.FileSystemUnknown; } else { // Try to determine the actual folder feed type by looking for nupkgs type = LocalFolderUtility.GetLocalFeedType(path, NullLogger.Instance); } } return(type); }
public void UriUtility_GetLocalPath(string input, string expected) { // Arrange & Act var local = UriUtility.GetLocalPath(input); // Assert // Trim for xplat Assert.Equal(expected, local.TrimStart('\\').TrimStart('/')); }
private static bool IsLocalOrUNC(string currentSource) { Uri currentURI = UriUtility.TryCreateSourceUri(currentSource, UriKind.Absolute); if (currentURI != null) { if (currentURI.IsFile || currentURI.IsUnc) { if (Directory.Exists(UriUtility.GetLocalPath(currentSource))) { return(true); } } } return(false); }
/// <summary> /// If a relative local URI is passed, it converts it into an absolute URI. /// If the local URI does not exist or it is neither http nor local type, then the source is rejected. /// If the URI is not relative then no action is taken. /// </summary> /// <param name="source">The source string specified by -Source switch.</param> /// <returns>The source validation result.</returns> private SourceValidationResult CheckSourceValidity(string inputSource) { // Convert file:// to a local path if needed, this noops for other types var source = UriUtility.GetLocalPath(inputSource); // Convert a relative local URI into an absolute URI var packageSource = new PackageSource(source); Uri sourceUri; if (Uri.TryCreate(source, UriKind.Relative, out sourceUri)) { string outputPath; bool? exists; string errorMessage; if (PSPathUtility.TryTranslatePSPath(SessionState, source, out outputPath, out exists, out errorMessage) && exists == true) { source = outputPath; packageSource = new PackageSource(source); } else if (exists == false) { return(SourceValidationResult.UnknownSource(source)); } } else if (!packageSource.IsHttp) { // Throw and unknown source type error if the specified source is neither local nor http return(SourceValidationResult.UnknownSourceType(source)); } // Check if the source is a valid HTTP URI. if (packageSource.IsHttp && packageSource.TrySourceAsUri == null) { return(SourceValidationResult.UnknownSource(source)); } var sourceRepository = CreateRepositoryFromSource(source); return(SourceValidationResult.Valid(source, sourceRepository)); }
/// <summary> /// Verify that a path could be a valid directory. Throw a FatalProtocolException otherwise. /// </summary> public static DirectoryInfo GetAndVerifyRootDirectory(string root) { // Check for package files one level deep. DirectoryInfo rootDirectoryInfo = null; try { // Convert file:// to a local path if needed var localPath = UriUtility.GetLocalPath(root); // Verify that the directory is a valid path. rootDirectoryInfo = new DirectoryInfo(localPath); // The root must also be parsable as a URI (relative or absolute). This rejects // sources that have the weird "C:Source" format. For more information about this // format, see: // https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#paths var uriResult = new Uri(root, UriKind.RelativeOrAbsolute); // Allow only local paths if (uriResult?.IsAbsoluteUri == true && !uriResult.IsFile) { throw new NotSupportedException(uriResult.AbsoluteUri); } } catch (Exception ex) when(ex is ArgumentException || ex is IOException || ex is SecurityException || ex is UriFormatException || ex is NotSupportedException) { var message = string.Format(CultureInfo.CurrentCulture, Strings.Log_FailedToRetrievePackage, root); throw new FatalProtocolException(message, ex); } return(rootDirectoryInfo); }