private ResolvePathResult ScanPathForLibrary([NotNull] string path, [NotNull] string library) { var libraryLocation = Path.GetFullPath(Path.Combine(path, library)); if (File.Exists(libraryLocation)) { return(ResolvePathResult.FromSuccess(libraryLocation)); } // Check the local library directory libraryLocation = Path.GetFullPath(Path.Combine(path, "lib", library)); if (File.Exists(libraryLocation)) { return(ResolvePathResult.FromSuccess(libraryLocation)); } // Check platform-specific directory var bitness = Environment.Is64BitProcess ? "x64" : "x86"; libraryLocation = Path.GetFullPath(Path.Combine(path, "lib", bitness, library)); if (File.Exists(libraryLocation)) { return(ResolvePathResult.FromSuccess(libraryLocation)); } return(ResolvePathResult.FromError(new FileNotFoundException("No local copy of the given library could be found.", library))); }
/// <inheritdoc /> public ResolvePathResult Resolve(string library) { // First, check next to the entry executable if (!(_entryAssemblyDirectory is null)) { var result = ScanPathForLibrary(_entryAssemblyDirectory, library); if (result.IsSuccess) { return(result); } } if (!(_executingAssemblyDirectory is null)) { var result = ScanPathForLibrary(_executingAssemblyDirectory, library); if (result.IsSuccess) { return(result); } } // Then, check the current directory if (!(_currentDirectory is null)) { var result = ScanPathForLibrary(_currentDirectory, library); if (result.IsSuccess) { return(result); } } return(ResolvePathResult.FromError(new FileNotFoundException("No local copy of the given library could be found.", library))); }
/// <inheritdoc /> public ResolvePathResult Resolve(string library) { // First, check next to the executable var libraryLocation = Path.GetFullPath(Path.Combine(_executingDirectory, library)); if (File.Exists(libraryLocation)) { return(ResolvePathResult.FromSuccess(libraryLocation)); } // Check the local library directory libraryLocation = Path.GetFullPath(Path.Combine(_libraryDirectory, library)); if (File.Exists(libraryLocation)) { return(ResolvePathResult.FromSuccess(libraryLocation)); } // Check platform-specific directory libraryLocation = Path.GetFullPath(Path.Combine(_platformLibraryDirectory, library)); if (File.Exists(libraryLocation)) { return(ResolvePathResult.FromSuccess(libraryLocation)); } return(ResolvePathResult.FromError(new FileNotFoundException("No local copy of the given library could be found.", library))); }
/// <inheritdoc /> public ResolvePathResult Resolve(string library) { var executingDir = Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName; var libraryLocation = Path.GetFullPath(Path.Combine(executingDir, library)); if (File.Exists(libraryLocation)) { return(ResolvePathResult.FromSuccess(libraryLocation)); } var windowsDir = Environment.GetFolderPath(Environment.SpecialFolder.Windows); var sys32Dir = Path.Combine(windowsDir, "System32"); libraryLocation = Path.GetFullPath(Path.Combine(sys32Dir, library)); if (File.Exists(libraryLocation)) { return(ResolvePathResult.FromSuccess(libraryLocation)); } var sys16Dir = Path.Combine(windowsDir, "System"); libraryLocation = Path.GetFullPath(Path.Combine(sys16Dir, library)); if (File.Exists(libraryLocation)) { return(ResolvePathResult.FromSuccess(libraryLocation)); } libraryLocation = Path.GetFullPath(Path.Combine(windowsDir, library)); if (File.Exists(libraryLocation)) { return(ResolvePathResult.FromSuccess(libraryLocation)); } var currentDir = Directory.GetCurrentDirectory(); libraryLocation = Path.GetFullPath(Path.Combine(currentDir, library)); if (File.Exists(libraryLocation)) { return(ResolvePathResult.FromSuccess(libraryLocation)); } var pathDirs = Environment.GetEnvironmentVariable("PATH").Split(';').Where(p => !p.IsNullOrWhiteSpace()); foreach (var path in pathDirs) { libraryLocation = Path.GetFullPath(Path.Combine(path, library)); if (File.Exists(libraryLocation)) { return(ResolvePathResult.FromSuccess(libraryLocation)); } } return(ResolvePathResult.FromError(new FileNotFoundException("The specified library was not found in any of the loader search paths.", library))); }
/// <inheritdoc /> public ResolvePathResult Resolve(string library) { var libraryPaths = Environment.GetEnvironmentVariable("LD_LIBRARY_PATH")?.Split(':').Where(p => !p.IsNullOrWhiteSpace()); string libraryLocation; if (!(libraryPaths is null)) { foreach (var path in libraryPaths) { libraryLocation = Path.GetFullPath(Path.Combine(path, library)); if (File.Exists(libraryLocation)) { return(ResolvePathResult.FromSuccess(libraryLocation)); } } } if (File.Exists("/etc/ld.so.cache")) { var cachedLibraries = File.ReadAllText("/etc/ld.so.cache").Split('\0'); var cachedMatch = cachedLibraries.FirstOrDefault ( l => l.EndsWith(library) && Path.GetFileName(l) == Path.GetFileName(library) ); if (!(cachedMatch is null)) { return(ResolvePathResult.FromSuccess(cachedMatch)); } } libraryLocation = Path.GetFullPath(Path.Combine("/lib", library)); if (File.Exists(libraryLocation)) { return(ResolvePathResult.FromSuccess(libraryLocation)); } libraryLocation = Path.GetFullPath(Path.Combine("/usr/lib", library)); if (File.Exists(libraryLocation)) { return(ResolvePathResult.FromSuccess(libraryLocation)); } return(ResolvePathResult.FromError(new FileNotFoundException("The specified library was not found in any of the loader search paths.", library))); }
/// <summary> /// Resolves the absolute path to the given library. /// </summary> /// <param name="library">The name or path of the library to load.</param> /// <param name="localFirst">Whether or not the executable's local directory should be searched first.</param> /// <returns>The absolute path to the library.</returns> /// <exception cref="PlatformNotSupportedException"> /// Thrown if the current platform doesn't have a path /// resolver defined.</exception> /// <exception cref="FileNotFoundException">Thrown if no library file can be found.</exception> private ResolvePathResult ResolveAbsolutePath([NotNull] string library, bool localFirst) { var candidates = GenerateLibraryCandidates(library).ToList(); if (library.IsValidPath()) { foreach (var candidate in candidates) { if (File.Exists(candidate)) { return(ResolvePathResult.FromSuccess(Path.GetFullPath(candidate))); } } } if (localFirst) { foreach (var candidate in candidates) { var result = LocalPathResolver.Resolve(candidate); if (result.IsSuccess) { return(result); } } } foreach (var candidate in candidates) { var result = PathResolver.Resolve(candidate); if (result.IsSuccess) { return(result); } } if (!(Type.GetType("Mono.Runtime") is null) && library == "__Internal") { // Mono extension: Search the main program return(ResolvePathResult.FromSuccess(null)); } return(ResolvePathResult.FromError(new FileNotFoundException("The specified library was not found in any of the loader search paths.", library))); }
/// <inheritdoc /> public ResolvePathResult Resolve(string library) { foreach (var variable in EnvironmentVariables) { var libraryPaths = Environment.GetEnvironmentVariable(variable)?.Split(':').Where(p => !p.IsNullOrWhiteSpace()); if (libraryPaths is null) { continue; } foreach (var path in libraryPaths) { var libraryLocation = Path.GetFullPath(Path.Combine(path, library)); if (File.Exists(libraryLocation)) { return(ResolvePathResult.FromSuccess(libraryLocation)); } } } return(ResolvePathResult.FromError(new FileNotFoundException("The specified library was not found in any of the loader search paths.", library))); }
/// <summary> /// Resolves the absolute path to the given library. /// </summary> /// <param name="library">The name or path of the library to load.</param> /// <param name="localFirst">Whether or not the executable's local directory should be searched first.</param> /// <returns>The absolute path to the library.</returns> /// <exception cref="PlatformNotSupportedException"> /// Thrown if the current platform doesn't have a path /// resolver defined.</exception> /// <exception cref="FileNotFoundException">Thrown if no library file can be found.</exception> private ResolvePathResult ResolveAbsolutePath([NotNull] string library, bool localFirst) { var candidates = GenerateLibraryCandidates(library).ToList(); if (library.IsValidPath()) { foreach (var candidate in candidates) { if (File.Exists(candidate)) { return(ResolvePathResult.FromSuccess(Path.GetFullPath(candidate))); } } } // Check the native probing paths (.NET Core defines this, Mono doesn't. Users can set this at runtime, too) if (AppContext.GetData("NATIVE_DLL_SEARCH_DIRECTORIES") is string directories) { var paths = directories.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries); foreach (var path in paths) { foreach (var candidate in candidates) { var candidatePath = Path.Combine(path, candidate); if (File.Exists(candidatePath)) { return(ResolvePathResult.FromSuccess(Path.GetFullPath(candidatePath))); } } } } if (localFirst) { foreach (var candidate in candidates) { var result = LocalPathResolver.Resolve(candidate); if (result.IsSuccess) { return(result); } } } foreach (var candidate in candidates) { var result = PathResolver.Resolve(candidate); if (result.IsSuccess) { return(result); } } if (library == "__Internal") { // Mono extension: Search the main program. Allowed for all runtimes return(ResolvePathResult.FromSuccess(null)); } return(ResolvePathResult.FromError(new FileNotFoundException("The specified library was not found in any of the loader search paths.", library))); }