OSDN Git Service

na-get-lib,"セキュリティプロトコル設定の確認"を追加。SSLv3を無効化し、使用可能ならばTLSv1.1とTLSv1.2を有効にする
[applistation/AppliStation.git] / na-get-lib / NaGet.SubCommands / NaGetDownloadToCache2.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Collections.ObjectModel;
4 using NaGet.Packages;
5 using NaGet.Packages.Install;
6 using NaGet.SubCommands;
7 using NaGet.SubCommands.SubTask;
8 using NaGet.Tasks;
9 using NaGet.InteropServices;
10
11 namespace NaGet.SubCommands
12 {
13         /// <summary>
14         /// キャッシュへのインストーラファイルのダウンロード処理
15         /// </summary>
16         public class NaGetDownloadToCache2 : NaGetTaskSet2
17         {
18                 private IList<Installation> installations;
19                 
20                 private DownloadScannerService scanner;
21                 
22                 /// <summary>
23                 /// コンストラクタ
24                 /// </summary>
25                 /// <param name="pkgs">インストールするパッケージ</param>
26                 public NaGetDownloadToCache2(PackageListsManager pkgListMan, Package[] pkgs)
27                         : this(pkgListMan, Installation.ConvertInstallations(pkgs))
28                 {
29                 }
30                 
31                 /// <summary>
32                 /// コンストラクタ
33                 /// </summary>
34                 /// <param name="installations">インストール処理の配列</param>
35                 public NaGetDownloadToCache2(PackageListsManager pkgMan, IList<Installation> insts)
36                 {
37                         installations = new ReadOnlyCollection<Installation>(insts);
38                         
39                         scanner = new DownloadScannerService();
40                         scanner.Init();
41                         
42                         // taskセットの初期化
43                         initSubTask();
44                         registSubTask("セキュリティプロトコル設定の確認",
45                                       new SecurityProtocolConfigSubTask());
46                         foreach (Installation inst in installations) {
47                                 DownloadSubTask dlSTask = new DownloadSubTask(inst.InstallerURL, inst.InstallerFile);
48                                 VirusScanSubTask scanSTask = new VirusScanSubTask(scanner, inst.InstallerFile, inst.InstallerURL);
49                                 
50                                 dlSTask.EnableChangeFileName = true;
51                                 dlSTask.TaskEventRaised += delegate(object sender, TaskEventArgs e) {
52                                         if (e.Type == TaskEventType.COMPLETED) {
53                                                 scanSTask.TargetFilePath = inst.InstallerFile = dlSTask.Filepath;
54                                         }
55                                 };
56                                 
57                                 registSubTask(string.Format("ダウンロード: {0}", inst),
58                                               dlSTask);
59                                 registSubTask(string.Format("ウイルススキャン: {0}", inst),
60                                               scanSTask);
61                         }
62                         registSubTask("インストーラーの検証",
63                                       new VerifyInstallerFileSubTask(insts));
64                         registSubTask("インストール済みのソフトリスト更新",
65                                       new LocalUpdateSubTask(pkgMan));
66                 }
67                 
68                 public override void Run()
69                 {
70                         NotifyStarted();
71                         RaiseTaskSetEvent(TaskEventType.STARTED, string.Empty);
72                         
73                         try {
74                                 while (hasMoreSubTask) {
75                                         bool canGoToNextSubTask = true;
76                                         
77                                         RaiseTaskSetEvent(TaskEventType.STARTED_SUBTASK, currentSubTaskName);
78                                         currentSubTask.Run();
79                                         RaiseTaskSetEvent(TaskEventType.COMPLETED_SUBTASK, currentSubTaskName);
80                                         
81                                         if (runCheckVerify() == false) {
82                                                 canGoToNextSubTask = false;
83                                                 NotifyGoToSubTask(0); // 最初からやり直し。
84                                         }
85                                         if (cancelCalled) {
86                                                 throw new TaskCanceledException("cancel is called");
87                                         }
88                                         
89                                         if (canGoToNextSubTask) {
90                                                 NotifyGoToNextSubTask();
91                                         }
92                                 }
93                         } catch (TaskCanceledException) {
94                                 cancelCalled = true;
95                         } catch (Exception e) {
96                                 RaiseTaskSetEvent(TaskEventType.ERROR, e.Message);
97                         }
98                         
99                         if (cancelCalled) {
100                                 NotifyCancelled();
101                                 RaiseTaskSetEvent(TaskEventType.CANCELED, "キャンセルされました");
102                         } else {
103                                 NotifyCompleted();
104                                 RaiseTaskSetEvent(TaskEventType.COMPLETED, string.Empty);
105                         }
106                 }
107                 
108                 private bool runCheckVerify()
109                 {
110                         bool ret = true;
111                         
112                         if (currentSubTask is VerifyInstallerFileSubTask) {
113                                 VerifyInstallerFileSubTask verifySTask = currentSubTask as VerifyInstallerFileSubTask;
114                                 if (verifySTask.InvalidInstallers != null && verifySTask.InvalidInstallers.Count > 0) {
115                                         System.Text.StringBuilder invalidInstallerNames = new System.Text.StringBuilder();
116                                         foreach (Installation invalidInst in verifySTask.InvalidInstallers) {
117                                                 invalidInstallerNames.AppendFormat(" - {0}\n", invalidInst.ToString());
118                                         }
119                                         
120                                         string msg = string.Format("以下の{0}個のパッケージでファイルが壊れている可能性があります。\n{1}\nダウンロードし直しますか?",
121                                                                    verifySTask.InvalidInstallers.Count, invalidInstallerNames.ToString());
122                                         NaGetTaskQueryResult result = NaGetTaskQueryResult.CANCEL;
123                                         
124                                         if (!cancelCalled) {
125                                                 result = RaiseTaskSetQueryEvent(msg, NaGetTaskQueryResult.CONTINUE | NaGetTaskQueryResult.CANCEL);
126                                         }
127                                         
128                                         switch (result) {
129                                                 case NaGetTaskQueryResult.CONTINUE:
130                                                         RaiseTaskSetEvent(TaskEventType.INFO, "ダウンロード処理を再試行");
131                                                         
132                                                         foreach (Installation invalidInst in verifySTask.InvalidInstallers) {
133                                                                 invalidInst.RemoveDownloadedFile();
134                                                         }
135                                                         ret = false;
136                                                         
137                                                         break;
138                                                 case NaGetTaskQueryResult.CANCEL:
139                                                 case NaGetTaskQueryResult.CANCELED_AUTOMATICALLY:
140                                                 default:
141                                                         ret = false;
142                                                         throw new TaskCanceledException("処理の継続のキャンセルが選択されました");
143                                         }
144                                 }
145                         }
146                         
147                         return ret;
148                 }
149                 
150                 public override bool Cancelable {
151                         get {
152                                 return !cancelCalled && Running && isDuringDownloading;
153                         }
154                 }
155                 
156                 private bool isDuringDownloading {
157                         get {
158                                 return Running && (currentSubTask is DownloadSubTask);
159                         }
160                 }
161         }
162 }