/// <summary> /// Attempts to delete a file /// </summary> /// <param name="fileSystem">PhysicalFileSystem</param> /// <param name="path">Path of file to delete</param> /// <param name="exception">Exception thrown, if any, while attempting to delete file (or reset file attributes)</param> /// <returns>True if the delete succeed, and false otherwise</returns> /// <remarks>The files attributes will be set to Normal before deleting the file</remarks> public static bool TryDeleteFile(this PhysicalFileSystem fileSystem, string path, out Exception exception) { exception = null; try { if (fileSystem.FileExists(path)) { fileSystem.SetAttributes(path, FileAttributes.Normal); fileSystem.DeleteFile(path); } return(true); } catch (FileNotFoundException) { // SetAttributes could not find the file return(true); } catch (IOException e) { exception = e; return(false); } catch (UnauthorizedAccessException e) { exception = e; return(false); } }
public static bool IsServiceRunningAndInstalled( ITracer tracer, PhysicalFileSystem fileSystem, out bool isServiceInstalled, out bool isDriverFileInstalled, out bool isNativeLibInstalled) { bool isRunning = false; isServiceInstalled = false; isDriverFileInstalled = fileSystem.FileExists(Path.Combine(Environment.SystemDirectory, "drivers", DriverFileName)); isNativeLibInstalled = IsNativeLibInstalled(tracer, fileSystem); try { ServiceController controller = new ServiceController(DriverName); isRunning = controller.Status.Equals(ServiceControllerStatus.Running); isServiceInstalled = true; } catch (InvalidOperationException e) { if (tracer != null) { EventMetadata metadata = CreateEventMetadata(); metadata.Add("Exception", e.Message); metadata.Add(TracingConstants.MessageKey.InfoMessage, $"{nameof(IsServiceRunningAndInstalled)}: InvalidOperationException: {ServiceName} service was not found"); tracer.RelatedEvent(EventLevel.Informational, $"{nameof(IsServiceRunningAndInstalled)}_ServiceNotFound", metadata); } return(false); } return(isRunning); }
/// <summary> /// Retry delete until it succeeds (or maximum number of retries have failed) /// </summary> /// <param name="fileSystem">PhysicalFileSystem</param> /// <param name="tracer">ITracer for logging and telemetry, can be null</param> /// <param name="path">Path of file to delete</param> /// <param name="retryDelayMs"> /// Amount of time to wait between each delete attempt. If 0, there will be no delays between attempts /// </param> /// <param name="maxRetries">Maximum number of retries (if 0, a single attempt will be made)</param> /// <param name="retryLoggingThreshold"> /// Number of retries to attempt before logging a failure. First and last failure is always logged if tracer is not null. /// </param> /// <returns>True if the delete succeed, and false otherwise</returns> /// <remarks>The files attributes will be set to Normal before deleting the file</remarks> public static bool TryWaitForDelete( this PhysicalFileSystem fileSystem, ITracer tracer, string path, int retryDelayMs, int maxRetries, int retryLoggingThreshold) { int failureCount = 0; while (fileSystem.FileExists(path)) { Exception exception = null; if (!TryDeleteFile(fileSystem, path, out exception)) { if (failureCount == maxRetries) { if (tracer != null) { EventMetadata metadata = new EventMetadata(); if (exception != null) { metadata.Add("Exception", exception.ToString()); } metadata.Add("path", path); metadata.Add("failureCount", failureCount + 1); metadata.Add("maxRetries", maxRetries); tracer.RelatedWarning(metadata, $"{nameof(TryWaitForDelete)}: Failed to delete file."); } return(false); } else { if (tracer != null && failureCount % retryLoggingThreshold == 0) { EventMetadata metadata = new EventMetadata(); metadata.Add("Exception", exception.ToString()); metadata.Add("path", path); metadata.Add("failureCount", failureCount + 1); metadata.Add("maxRetries", maxRetries); tracer.RelatedWarning(metadata, $"{nameof(TryWaitForDelete)}: Failed to delete file, retrying ..."); } } ++failureCount; if (retryDelayMs > 0) { Thread.Sleep(retryDelayMs); } } } return(true); }
public static bool IsNativeLibInstalled(ITracer tracer, PhysicalFileSystem fileSystem) { string system32Path = Path.Combine(Environment.SystemDirectory, ProjFSNativeLibFileName); bool existsInSystem32 = fileSystem.FileExists(system32Path); string gvfsAppDirectory = ProcessHelper.GetCurrentProcessLocation(); string appFilePath; string installFilePath; GetNativeLibPaths(gvfsAppDirectory, out installFilePath, out appFilePath); bool existsInAppDirectory = fileSystem.FileExists(appFilePath); EventMetadata metadata = CreateEventMetadata(); metadata.Add(nameof(system32Path), system32Path); metadata.Add(nameof(existsInSystem32), existsInSystem32); metadata.Add(nameof(gvfsAppDirectory), gvfsAppDirectory); metadata.Add(nameof(appFilePath), appFilePath); metadata.Add(nameof(installFilePath), installFilePath); metadata.Add(nameof(existsInAppDirectory), existsInAppDirectory); tracer.RelatedEvent(EventLevel.Informational, nameof(IsNativeLibInstalled), metadata); return(existsInSystem32 || existsInAppDirectory); }
private static bool TryCopyNativeLibToAppDirectory(ITracer tracer, PhysicalFileSystem fileSystem, string gvfsAppDirectory) { string installFilePath; string appFilePath; GetNativeLibPaths(gvfsAppDirectory, out installFilePath, out appFilePath); EventMetadata pathMetadata = CreateEventMetadata(); pathMetadata.Add(nameof(gvfsAppDirectory), gvfsAppDirectory); pathMetadata.Add(nameof(installFilePath), installFilePath); pathMetadata.Add(nameof(appFilePath), appFilePath); if (fileSystem.FileExists(installFilePath)) { tracer.RelatedEvent(EventLevel.Informational, $"{nameof(TryCopyNativeLibToAppDirectory)}_CopyingNativeLib", pathMetadata); try { fileSystem.CopyFile(installFilePath, appFilePath, overwrite: true); try { Common.NativeMethods.FlushFileBuffers(appFilePath); } catch (Win32Exception e) { EventMetadata metadata = CreateEventMetadata(e); metadata.Add(nameof(appFilePath), appFilePath); metadata.Add(nameof(installFilePath), installFilePath); tracer.RelatedWarning(metadata, $"{nameof(TryCopyNativeLibToAppDirectory)}: Win32Exception while trying to flush file buffers", Keywords.Telemetry); } } catch (UnauthorizedAccessException e) { EventMetadata metadata = CreateEventMetadata(e); tracer.RelatedError(metadata, $"{nameof(TryCopyNativeLibToAppDirectory)}: UnauthorizedAccessException caught while trying to copy native lib"); return(false); } catch (DirectoryNotFoundException e) { EventMetadata metadata = CreateEventMetadata(e); tracer.RelatedError(metadata, $"{nameof(TryCopyNativeLibToAppDirectory)}: DirectoryNotFoundException caught while trying to copy native lib"); return(false); } catch (FileNotFoundException e) { EventMetadata metadata = CreateEventMetadata(e); tracer.RelatedError(metadata, $"{nameof(TryCopyNativeLibToAppDirectory)}: FileNotFoundException caught while trying to copy native lib"); return(false); } catch (IOException e) { EventMetadata metadata = CreateEventMetadata(e); tracer.RelatedWarning(metadata, $"{nameof(TryCopyNativeLibToAppDirectory)}: IOException caught while trying to copy native lib"); if (fileSystem.FileExists(appFilePath)) { tracer.RelatedWarning( CreateEventMetadata(), "Could not copy native lib to app directory, but file already exists, continuing with install", Keywords.Telemetry); } else { tracer.RelatedError($"{nameof(TryCopyNativeLibToAppDirectory)}: Failed to copy native lib to app directory"); return(false); } } } else { tracer.RelatedError(pathMetadata, $"{nameof(TryCopyNativeLibToAppDirectory)}: Native lib does not exist in install directory"); return(false); } return(true); }