Esempio n. 1
0
        public static bool TryCopyNativeLibIfDriverVersionsMatch(ITracer tracer, PhysicalFileSystem fileSystem, out string copyNativeDllError)
        {
            string system32NativeLibraryPath = Path.Combine(Environment.SystemDirectory, ProjFSNativeLibFileName);

            if (fileSystem.FileExists(system32NativeLibraryPath))
            {
                copyNativeDllError = $"{ProjFSNativeLibFileName} already exists at {system32NativeLibraryPath}";
                return(false);
            }

            string gvfsProcessLocation = ProcessHelper.GetCurrentProcessLocation();
            string nonInboxNativeLibInstallPath;
            string packagedNativeLibPath;

            GetNativeLibPaths(gvfsProcessLocation, out packagedNativeLibPath, out nonInboxNativeLibInstallPath);
            if (fileSystem.FileExists(nonInboxNativeLibInstallPath))
            {
                copyNativeDllError = $"{ProjFSNativeLibFileName} already exists at {nonInboxNativeLibInstallPath}";
                return(false);
            }

            if (!fileSystem.FileExists(packagedNativeLibPath))
            {
                copyNativeDllError = $"{packagedNativeLibPath} not found, no {ProjFSNativeLibFileName} available to copy";
                return(false);
            }

            string packagedPrjfltDriverPath = Path.Combine(gvfsProcessLocation, "Filter", DriverFileName);

            if (!fileSystem.FileExists(packagedPrjfltDriverPath))
            {
                copyNativeDllError = $"{packagedPrjfltDriverPath} not found, unable to validate that packaged driver matches installed driver";
                return(false);
            }

            string system32PrjfltDriverPath = Path.Combine(Environment.ExpandEnvironmentVariables(System32DriversRoot), DriverFileName);

            if (!fileSystem.FileExists(system32PrjfltDriverPath))
            {
                copyNativeDllError = $"{system32PrjfltDriverPath} not found, unable to validate that packaged driver matches installed driver";
                return(false);
            }

            FileVersionInfo packagedDriverVersion;
            FileVersionInfo system32DriverVersion;

            try
            {
                packagedDriverVersion = fileSystem.GetVersionInfo(packagedPrjfltDriverPath);
                system32DriverVersion = fileSystem.GetVersionInfo(system32PrjfltDriverPath);
                if (!fileSystem.FileVersionsMatch(packagedDriverVersion, system32DriverVersion))
                {
                    copyNativeDllError = $"Packaged sys FileVersion '{packagedDriverVersion.FileVersion}' does not match System32 sys FileVersion '{system32DriverVersion.FileVersion}'";
                    return(false);
                }

                if (!fileSystem.ProductVersionsMatch(packagedDriverVersion, system32DriverVersion))
                {
                    copyNativeDllError = $"Packaged sys ProductVersion '{packagedDriverVersion.ProductVersion}' does not match System32 sys ProductVersion '{system32DriverVersion.ProductVersion}'";
                    return(false);
                }
            }
            catch (FileNotFoundException e)
            {
                EventMetadata metadata = CreateEventMetadata(e);
                tracer.RelatedWarning(
                    metadata,
                    $"{nameof(TryCopyNativeLibIfDriverVersionsMatch)}: Exception caught while comparing sys versions");
                copyNativeDllError = $"Exception caught while comparing sys versions: {e.Message}";
                return(false);
            }

            EventMetadata driverVersionMetadata = CreateEventMetadata();

            driverVersionMetadata.Add($"{nameof(packagedDriverVersion)}.FileVersion", packagedDriverVersion.FileVersion.ToString());
            driverVersionMetadata.Add($"{nameof(system32DriverVersion)}.FileVersion", system32DriverVersion.FileVersion.ToString());
            driverVersionMetadata.Add($"{nameof(packagedDriverVersion)}.ProductVersion", packagedDriverVersion.ProductVersion.ToString());
            driverVersionMetadata.Add($"{nameof(system32DriverVersion)}.ProductVersion", system32DriverVersion.ProductVersion.ToString());
            tracer.RelatedInfo(driverVersionMetadata, $"{nameof(TryCopyNativeLibIfDriverVersionsMatch)}: Copying native library");

            if (!TryCopyNativeLibToNonInboxInstallLocation(tracer, fileSystem, gvfsProcessLocation))
            {
                copyNativeDllError = "Failed to copy native library";
                return(false);
            }

            copyNativeDllError = null;
            return(true);
        }