Beispiel #1
0
        private RetCodes SetupOffline()
        {
            try
            {
                if (mOfflineService == null)
                {
                    AppLog.Line("Setting up 'Offline Sync Service'");

                    // http://go.microsoft.com/fwlink/p/?LinkID=74689
                    mOfflineService = mUpdateServiceManager.AddScanPackageService(mMyOfflineSvc, dlPath + @"\wsusscn2.cab");
                }

                mUpdateSearcher.ServerSelection = ServerSelection.ssOthers;
                mUpdateSearcher.ServiceID       = mOfflineService.ServiceID;
                //mUpdateSearcher.Online = false;
            }
            catch (Exception err)
            {
                AppLog.Line(err.Message);
                RetCodes ret = RetCodes.InternalError;
                if (err.GetType() == typeof(System.IO.FileNotFoundException))
                {
                    ret = RetCodes.FileNotFound;
                }
                if (err.GetType() == typeof(System.UnauthorizedAccessException))
                {
                    ret = RetCodes.AccessError;
                }
                return(ret);
            }
            return(RetCodes.Success);
        }
Beispiel #2
0
        protected void OnFinished(RetCodes ret, bool needReboot = false)
        {
            FinishedArgs args = new FinishedArgs(mCurOperation, ret, needReboot);

            mCurOperation = AgentOperation.None;

            Finished?.Invoke(this, args);
        }
Beispiel #3
0
        private RetCodes OnWuError(Exception err)
        {
            bool     access = err.GetType() == typeof(System.UnauthorizedAccessException);
            RetCodes ret    = access ? RetCodes.AccessError : RetCodes.InternalError;

            mCallback = null;
            AppLog.Line(err.Message);
            OnFinished(ret);
            return(ret);
        }
Beispiel #4
0
        protected void OnUpdatesDownloaded(IDownloadJob downloadJob, List <MsUpdate> Updates)
        {
            if (downloadJob != mDownloadJob)
            {
                return;
            }
            mDownloadJob = null;
            mCallback    = null;

            IDownloadResult DownloadResults = null;

            try
            {
                DownloadResults = mDownloader.EndDownload(downloadJob);
            }
            catch (Exception err)
            {
                AppLog.Line("Downloading updates failed");
                LogError(err);
                OnFinished(RetCodes.InternalError);
                return;
            }

            OnUpdatesChanged();

            if (mCurOperation == AgentOperation.PreparingUpdates)
            {
                RetCodes ret = InstallUpdates(Updates);
                if (ret <= 0)
                {
                    OnFinished(ret);
                }
            }
            else
            {
                AppLog.Line("Updates downloaded to %windir%\\SoftwareDistribution\\Download");

                RetCodes ret = RetCodes.Undefined;
                if (DownloadResults.ResultCode == OperationResultCode.orcSucceeded || DownloadResults.ResultCode == OperationResultCode.orcSucceededWithErrors)
                {
                    ret = RetCodes.Success;
                }
                else if (DownloadResults.ResultCode == OperationResultCode.orcAborted)
                {
                    ret = RetCodes.Abborted;
                }
                else if (DownloadResults.ResultCode == OperationResultCode.orcFailed)
                {
                    ret = RetCodes.InternalError;
                }
                OnFinished(ret);
            }
        }
Beispiel #5
0
        void InstallFinished(object sender, UpdateInstaller.FinishedEventArgs args) // "manuall" mode
        {
            if (args.Success)
            {
                AppLog.Line("Updates (Un)Installed succesfully");

                foreach (MsUpdate Update in args.Updates)
                {
                    if (mCurOperation == AgentOperation.InstallingUpdates)
                    {
                        if (RemoveFrom(mPendingUpdates, Update))
                        {
                            Update.Attributes |= (int)MsUpdate.UpdateAttr.Installed;
                            mInstalledUpdates.Add(Update);
                        }
                    }
                    else if (mCurOperation == AgentOperation.RemoveingUpdates)
                    {
                        if (RemoveFrom(mInstalledUpdates, Update))
                        {
                            Update.Attributes &= ~(int)MsUpdate.UpdateAttr.Installed;
                            mPendingUpdates.Add(Update);
                        }
                    }
                }
            }
            else
            {
                AppLog.Line("Updates failed to (Un)Install");
            }

            if (args.Reboot)
            {
                AppLog.Line("Reboot is required for one of more updates");
            }

            OnUpdatesChanged();

            RetCodes ret = args.Success ? RetCodes.Success : RetCodes.InstallFailed;

            if (mCurOperation == AgentOperation.CancelingOperation)
            {
                ret = RetCodes.Abborted;
            }
            OnFinished(ret, args.Reboot);
        }
Beispiel #6
0
        public RetCodes SearchForUpdates(bool Download, bool IncludePotentiallySupersededUpdates = false)
        {
            if (mCallback != null)
            {
                return(RetCodes.Busy);
            }

            mUpdateSearcher.IncludePotentiallySupersededUpdates = IncludePotentiallySupersededUpdates;

            if (Download)
            {
                mCurOperation = AgentOperation.PreparingCheck;
                OnProgress(-1, 0, 0, 0);

                AppLog.Line("downloading wsusscn2.cab");

                List <UpdateDownloader.Task> downloads = new List <UpdateDownloader.Task>();
                UpdateDownloader.Task        download  = new UpdateDownloader.Task();
                download.Url      = Program.IniReadValue("Options", "OfflineCab", "http://go.microsoft.com/fwlink/p/?LinkID=74689");
                download.Path     = dlPath;
                download.FileName = "wsusscn2.cab";
                downloads.Add(download);
                if (!mUpdateDownloader.Download(downloads))
                {
                    OnFinished(RetCodes.DownloadFailed);
                    return(RetCodes.DownloadFailed);
                }
                return(RetCodes.InProgress);
            }

            RetCodes ret = SetupOffline();

            if (ret < 0)
            {
                return(ret);
            }

            return(SearchForUpdates());
        }
Beispiel #7
0
        protected void OnUpdatesFound(ISearchJob searchJob)
        {
            if (searchJob != mSearchJob)
            {
                return;
            }
            mSearchJob = null;
            mCallback  = null;

            ISearchResult SearchResults = null;

            try
            {
                SearchResults = mUpdateSearcher.EndSearch(searchJob);
            }
            catch (Exception err)
            {
                AppLog.Line("Search for updates failed");
                LogError(err);
                OnFinished(RetCodes.InternalError);
                return;
            }

            mPendingUpdates.Clear();
            mInstalledUpdates.Clear();
            mHiddenUpdates.Clear();
            mIsValid = true;

            foreach (IUpdate update in SearchResults.Updates)
            {
                if (update.IsHidden)
                {
                    mHiddenUpdates.Add(new MsUpdate(update, MsUpdate.UpdateState.Hidden));
                }
                else if (update.IsInstalled)
                {
                    mInstalledUpdates.Add(new MsUpdate(update, MsUpdate.UpdateState.Installed));
                }
                else
                {
                    mPendingUpdates.Add(new MsUpdate(update, MsUpdate.UpdateState.Pending));
                }
                Console.WriteLine(update.Title);
            }

            AppLog.Line("Found {0} pending updates.", mPendingUpdates.Count);

            OnUpdatesChanged(true);

            RetCodes ret = RetCodes.Undefined;

            if (SearchResults.ResultCode == OperationResultCode.orcSucceeded || SearchResults.ResultCode == OperationResultCode.orcSucceededWithErrors)
            {
                ret = RetCodes.Success;
            }
            else if (SearchResults.ResultCode == OperationResultCode.orcAborted)
            {
                ret = RetCodes.Abborted;
            }
            else if (SearchResults.ResultCode == OperationResultCode.orcFailed)
            {
                ret = RetCodes.InternalError;
            }
            OnFinished(ret);
        }
Beispiel #8
0
        void DownloadsFinished(object sender, UpdateDownloader.FinishedEventArgs args) // "manuall" mode
        {
            if (mCurOperation == AgentOperation.PreparingCheck)
            {
                AppLog.Line("wsusscn2.cab downloaded");

                RetCodes ret = ClearOffline();
                if (ret == RetCodes.Success)
                {
                    ret = SetupOffline();
                }
                if (ret == RetCodes.Success)
                {
                    ret = SearchForUpdates();
                }
                if (ret <= 0)
                {
                    OnFinished(ret);
                }
            }
            else
            {
                MultiValueDictionary <string, string> AllFiles = new MultiValueDictionary <string, string>();
                foreach (UpdateDownloader.Task task in args.Downloads)
                {
                    if (task.Failed && task.FileName != null)
                    {
                        continue;
                    }
                    AllFiles.Add(task.KB, task.Path + @"\" + task.FileName);
                }

                // TODO

                /*string INIPath = dlPath + @"\updates.ini";
                 * foreach (string KB in AllFiles.Keys)
                 * {
                 *  string Files = "";
                 *  foreach (string FileName in AllFiles.GetValues(KB))
                 *  {
                 *      if (Files.Length > 0)
                 *          Files += "|";
                 *      Files += FileName;
                 *  }
                 *  Program.IniWriteValue(KB, "Files", Files, INIPath);
                 * }*/

                AppLog.Line("Downloaded {0} out of {1} to {2}", AllFiles.GetCount(), args.Downloads.Count, dlPath);

                if (mCurOperation == AgentOperation.PreparingUpdates)
                {
                    RetCodes ret = InstallUpdatesManually(args.Updates, AllFiles);
                    if (ret <= 0)
                    {
                        OnFinished(ret);
                    }
                }
                else
                {
                    RetCodes ret = AllFiles.GetCount() == args.Downloads.Count ? RetCodes.Success : RetCodes.DownloadFailed;
                    if (mCurOperation == AgentOperation.CancelingOperation)
                    {
                        ret = RetCodes.Abborted;
                    }
                    OnFinished(ret);
                }
            }
        }
Beispiel #9
0
 public FinishedArgs(AgentOperation op, RetCodes ret, bool needReboot = false)
 {
     Op           = op;
     Ret          = ret;
     RebootNeeded = needReboot;
 }
Beispiel #10
0
        protected void OnInstalationCompleted(IInstallationJob installationJob, List <MsUpdate> Updates)
        {
            if (installationJob != mInstalationJob)
            {
                return;
            }
            mInstalationJob = null;
            mCallback       = null;

            IInstallationResult InstallationResults = null;

            try
            {
                if (mCurOperation == AgentOperation.InstallingUpdates)
                {
                    InstallationResults = mInstaller.EndInstall(installationJob);
                }
                else if (mCurOperation == AgentOperation.RemoveingUpdates)
                {
                    InstallationResults = mInstaller.EndUninstall(installationJob);
                }
            }
            catch (Exception err)
            {
                AppLog.Line("(Un)Installing updates failed");
                LogError(err);
                OnFinished(RetCodes.InternalError);
                return;
            }

            if (InstallationResults.ResultCode == OperationResultCode.orcSucceeded)
            {
                AppLog.Line("Updates (Un)Installed succesfully");

                foreach (MsUpdate Update in Updates)
                {
                    if (mCurOperation == AgentOperation.InstallingUpdates)
                    {
                        if (RemoveFrom(mPendingUpdates, Update))
                        {
                            Update.Attributes |= (int)MsUpdate.UpdateAttr.Installed;
                            mInstalledUpdates.Add(Update);
                        }
                    }
                    else if (mCurOperation == AgentOperation.RemoveingUpdates)
                    {
                        if (RemoveFrom(mInstalledUpdates, Update))
                        {
                            Update.Attributes &= ~(int)MsUpdate.UpdateAttr.Installed;
                            mPendingUpdates.Add(Update);
                        }
                    }
                }

                if (InstallationResults.RebootRequired == true)
                {
                    AppLog.Line("Reboot is required for one of more updates");
                }
            }
            else
            {
                AppLog.Line("Updates failed to (Un)Install");
            }

            OnUpdatesChanged();

            RetCodes ret = RetCodes.Undefined;

            if (InstallationResults.ResultCode == OperationResultCode.orcSucceeded || InstallationResults.ResultCode == OperationResultCode.orcSucceededWithErrors)
            {
                ret = RetCodes.Success;
            }
            else if (InstallationResults.ResultCode == OperationResultCode.orcAborted)
            {
                ret = RetCodes.Abborted;
            }
            else if (InstallationResults.ResultCode == OperationResultCode.orcFailed)
            {
                ret = RetCodes.InternalError;
            }
            OnFinished(ret, InstallationResults.RebootRequired);
        }