// Credit: http://stackoverflow.com/questions/1460273/how-to-check-if-a-file-exists-on-an-webserver-by-its-url // Credit: http://stackoverflow.com/questions/3614034/system-net-webexception-http-status-code public bool FileExistsAtUrl(string url, out long fileSize) { if (string.IsNullOrEmpty(url)) { fileSize = 0L; return(false); } if (IsGoogleDriveURL(url)) { url = PatchUtils.GetGoogleDriveDownloadLinkFromUrl(url); } WebRequest request = WebRequest.Create(url); request.Method = "HEAD"; request.Timeout = PatchParameters.FileAvailabilityCheckTimeout; try { using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { fileSize = response.ContentLength; return(response.StatusCode == HttpStatusCode.OK); } } catch (WebException e) { fileSize = 0L; if (e.Response != null && e.Response is HttpWebResponse) { using (HttpWebResponse exResponse = (HttpWebResponse)e.Response) { if (exResponse.StatusCode == HttpStatusCode.ServiceUnavailable) { // Drive returns 503 error while requesting HEAD for valid download links if (IsGoogleDriveURL(url)) { return(true); } } } } comms.LogToFile(e); return(false); } }
// For self-patching applications only - should be called after Run(true) returns PatchResult.Success // Starts specified self patcher executable with required parameters public bool ApplySelfPatch(string selfPatcherExecutable, string postSelfPatchExecutable = null) { IsRunning = true; Operation = PatchOperation.ApplyingSelfPatch; comms.InitializeFileLogger(); comms.ListenerCallStarted(); try { selfPatcherExecutable = selfPatcherExecutable.Trim(); if (postSelfPatchExecutable != null) { postSelfPatchExecutable = postSelfPatchExecutable.Trim(); } if (!File.Exists(selfPatcherExecutable)) { Result = PatchResult.Failed; FailReason = PatchFailReason.SelfPatcherNotFound; FailDetails = Localization.Get(StringId.E_SelfPatcherDoesNotExist); comms.Log(comms.FailDetails); return(false); } string instructionsPath = comms.CachePath + PatchParameters.SELF_PATCH_INSTRUCTIONS_FILENAME; string completedInstructionsPath = comms.CachePath + PatchParameters.SELF_PATCH_COMPLETED_INSTRUCTIONS_FILENAME; if (!File.Exists(instructionsPath)) { Result = PatchResult.Failed; FailReason = PatchFailReason.Unknown; FailDetails = ""; comms.LogToFile(Localization.Get(StringId.E_XDoesNotExist, instructionsPath)); return(false); } FileInfo selfPatcher = new FileInfo(selfPatcherExecutable); string args = "\"" + instructionsPath + "\" \"" + completedInstructionsPath + "\""; if (!string.IsNullOrEmpty(postSelfPatchExecutable) && File.Exists(postSelfPatchExecutable)) { args += " \"" + postSelfPatchExecutable + "\""; } ProcessStartInfo startInfo = new ProcessStartInfo(selfPatcher.FullName) { Arguments = args, WorkingDirectory = selfPatcher.DirectoryName }; Process.Start(startInfo); Result = PatchResult.Success; } catch (Exception e) { Result = PatchResult.Failed; FailReason = PatchFailReason.FatalException; FailDetails = e.ToString(); comms.LogToFile(e); return(false); } finally { comms.DisposeFileLogger(); IsRunning = false; comms.ListenerCallFinished(); } Process.GetCurrentProcess().Kill(); return(true); }