Example #1
0
 private void TryBackupRegistryForBiVersion(BiVersionInfo versionInfo)
 {
     if (versionInfo != null && Program.settings.includeRegistryWithUpdateBackup)
     {
         RegistryBackup.BackupNow(BiUpdateHelperSettings.GetBeforeUpdatesRegistryBackupLocation() + Path.DirectorySeparatorChar + "BI_REG_" + versionInfo.cpu_32_64 + "-" + versionInfo.version + ".reg");
     }
 }
Example #2
0
        private void UpdateWatch()
        {
            try
            {
                try
                {
                    Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.AboveNormal;
                }
                catch (ThreadAbortException) { throw; }
                catch { }
                cpuCounterTotal       = new PerformanceCounter("Processor", "% Processor Time", "_Total");
                interruptCounterTotal = new PerformanceCounter("Processor", "% Interrupt Time", "_Total");
                DateTime lastDailyRegistryBackup = DateTime.MinValue;
                while (true)
                {
                    Thread.Sleep(1500);
                    Verbose("Starting Iteration");
                    try
                    {
                        PerformanceDataCollector.HandlePossiblePerfDataReport();
                        DateTime now = DateTime.Now;
                        if (lastDailyRegistryBackup.Year != now.Year || lastDailyRegistryBackup.Month != now.Month || lastDailyRegistryBackup.Day != now.Day)
                        {
                            lastDailyRegistryBackup = now;
                            if (Program.settings.dailyRegistryBackups)
                            {
                                RegistryBackup.BackupNow(BiUpdateHelperSettings.GetDailyRegistryBackupLocation() + Path.DirectorySeparatorChar + "BI_REG_" + DateTime.Now.ToString("yyyy-MM-dd") + ".reg");
                            }
                        }
                        // Build a list of unique directories that have an active blueiris.exe.
                        // There is not likely to be more than one directory, though a single directory
                        // can easily have two blueiris.exe (service and GUI).
                        List <BiUpdateMapping> biUpdateMap = GetUpdateInfo();

                        if (biUpdateMap.Count == 0)
                        {
                            Verbose("No Blue Iris processes detected");
                        }
                        else
                        {
                            foreach (BiUpdateMapping mapping in biUpdateMap)
                            {
                                if (mapping.updateProcs.Length > 0)
                                {
                                    // Blue Iris is currently being updated.  Kill the blueiris.exe processes if configured to do so.
                                    Logger.Info("Blue Iris update detected in path: " + mapping.dirPath);
                                    if (Program.settings.includeRegistryWithUpdateBackup)
                                    {
                                        BiVersionInfo versionInfo = GetBiVersionInfo(mapping);
                                        TryBackupRegistryForBiVersion(versionInfo);
                                    }
                                    if (Program.settings.killBlueIrisProcessesDuringUpdate2)
                                    {
                                        Logger.Info("Killing " + mapping.GetNumProcsLabel());
                                        mapping.KillBiProcs();
                                    }
                                    Verbose("Waiting for update to complete");
                                    mapping.WaitUntilUpdateProcsStop(TimeSpan.FromMinutes(5));
                                }
                                else
                                {
                                    // Blue Iris is not being updated in this directory.

                                    if (Program.settings.killBlueIrisProcessesDuringUpdate2 && (blueIrisServiceStopping || systemInFrozenState))
                                    {
                                        mapping.KillBiProcs();

                                        if (blueIrisServiceStopping)
                                        {
                                            Logger.Info("Blue Iris service found in stopping state. Killed " + mapping.GetNumProcsLabel());
                                        }
                                        else if (systemInFrozenState)
                                        {
                                            Logger.Info("System freeze with high interrupt % detected. Killed " + mapping.GetNumProcsLabel());
                                        }
                                        continue;
                                    }

                                    // Back up the update file if configured to do so.
                                    if (!Program.settings.backupUpdateFiles)
                                    {
                                        continue;
                                    }

                                    //Check for the existence of an update.exe file
                                    FileInfo fiUpdate = new FileInfo(mapping.dirPath + "update.exe");
                                    if (!fiUpdate.Exists)
                                    {
                                        Verbose("No update file to back up in path: " + mapping.dirPath);
                                        continue;
                                    }

                                    Verbose("Backing up update file: " + fiUpdate.FullName);

                                    // Get Blue Iris process(es) (necessary to learn if it is 64 or 32 bit)
                                    BiVersionInfo versionInfo = GetBiVersionInfo(mapping);
                                    if (versionInfo == null)
                                    {
                                        continue;                                         // BI is probably not running
                                    }
                                    FileInfo targetUpdateFile = new FileInfo(mapping.dirPath + "update" + versionInfo.cpu_32_64 + "_" + versionInfo.version + ".exe");
                                    if (targetUpdateFile.Exists)
                                    {
                                        // A backed-up update file for the active Blue Iris version already exists, so we should do nothing now
                                        Verbose("Target update file \"" + targetUpdateFile.FullName + "\" already exists.  Probably, the new update file hasn't been installed yet.");
                                        TryBackupRegistryForBiVersion(versionInfo);
                                    }
                                    else
                                    {
                                        // Find out if the file can be opened exclusively (meaning it is finished downloading, etc)
                                        bool fileIsUnlocked = false;
                                        try
                                        {
                                            using (FileStream fs = new FileStream(fiUpdate.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
                                            {
                                                fileIsUnlocked = true;
                                            }
                                        }
                                        catch (ThreadAbortException) { throw; }
                                        catch (Exception) { }

                                        if (fileIsUnlocked)
                                        {
                                            // This is a pretty good sign that the update is not curently being installed, so we should be safe to rename the update file.
                                            Logger.Info("Renaming update file to: " + targetUpdateFile.FullName);
                                            fiUpdate.MoveTo(targetUpdateFile.FullName);
                                        }
                                        else
                                        {
                                            Verbose("Update file could not be exclusively locked. Backup will not occur this iteration.");
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (ThreadAbortException) { throw; }
                    catch (Exception ex)
                    {
                        Logger.Debug(ex);
                    }
                }
            }
            catch (ThreadAbortException) { }
            catch (Exception ex)
            {
                Logger.Debug(ex);
            }
        }