OSDN Git Service

na-get-lib,ウイルススキャン結果を参照するように処理を変更。(IOfficeAntiVirus::Scan()の戻り値を見るようにした)
[applistation/AppliStation.git] / na-get-lib / NaGet.SubCommands / NaGetInstall.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using NaGet.Packages.Install;\r
4 using NaGet.Packages;\r
5 using NaGet.Net;\r
6 \r
7 namespace NaGet.SubCommands\r
8 {\r
9         public class NaGetInstall : NaGetTaskSet\r
10         {\r
11                 private bool done = false;\r
12                 \r
13                 private int currentTaskSetIndex = -1;\r
14                 \r
15                 private PackageListsManager pkgListMan;\r
16                 \r
17                 /// <summary>\r
18                 /// ダウンロードに使うダウンローダオブジェクト\r
19                 /// </summary>\r
20                 public Downloader Downloader {\r
21                         get {\r
22                                 if (downloader == null) {\r
23                                         downloader = new Downloader();\r
24                                 }\r
25                                 return downloader;\r
26                         }\r
27                 }\r
28                 \r
29                 private Downloader downloader;\r
30                 \r
31                 private bool packageInstallerDownloaded = false;\r
32                 \r
33                 public override bool Cancelable {\r
34                         get { return ! done; }\r
35                 }\r
36                 \r
37                 /// <summary>\r
38                 /// インストールするパッケージ\r
39                 /// </summary>\r
40                 public Installation[] Installations;\r
41                 \r
42                 /// <summary>\r
43                 /// コンストラクタ\r
44                 /// </summary>\r
45                 /// <param name="pkgs">インストールするパッケージ</param>\r
46                 public NaGetInstall(PackageListsManager pkgListMan, Package[] pkgs)\r
47                         : this(pkgListMan, Installation.ConvertInstallations(pkgs))\r
48                 {\r
49                 }\r
50                 \r
51                 /// <summary>\r
52                 /// コンストラクタ\r
53                 /// </summary>\r
54                 /// <param name="installations">インストール処理の配列</param>\r
55                 public NaGetInstall(PackageListsManager pkgMan, Installation[] installations)\r
56                 {\r
57                         pkgListMan = pkgMan;\r
58                         \r
59                         Installations = installations;\r
60                         initializeMainTaskSetNames();\r
61                 }\r
62                 \r
63                 private void initializeMainTaskSetNames()\r
64                 {\r
65                         List<string> taskSetNames = new List<string>();\r
66                         \r
67                         for (int i =0; i < Installations.Length; i++) {\r
68                                 taskSetNames.Add(string.Format("取得: {0}", Installations[i].ToString()));\r
69                                 taskSetNames.Add(string.Format("ウイルススキャン: {0}", Installations[i].ToString()));\r
70                         }\r
71                         taskSetNames.Add("インストーラーの検証");\r
72                         for (int i =0; i < Installations.Length; i++) {\r
73                                 taskSetNames.Add(string.Format("インストール: {0}", Installations[i].ToString()));\r
74                         }\r
75                         taskSetNames.Add(string.Format("リスト更新: {0}", NaGet.Env.ArchiveInstalledPackageListFile));\r
76                         taskSetNames.Add(string.Format("リスト更新: {0}", NaGet.Env.SystemInstalledPackageListFile));\r
77                         \r
78                         TaskSetNames = taskSetNames.ToArray();\r
79                 }\r
80                                 \r
81                 public override void Run()\r
82                 {\r
83                         currentTaskSetIndex = 0;\r
84                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED, "インストール処理開始");\r
85                         \r
86                         {\r
87                                 // ハッシュ非適合なインストーラの表\r
88                                 List<Installation> invalidInstallers = null;\r
89                                 \r
90                                 while (invalidInstallers == null || invalidInstallers.Count > 0) {\r
91                                         currentTaskSetIndex = 0;\r
92                                         packageInstallerDownloaded = false;\r
93                                         \r
94                                         runDownloadAndVirusCheckInstallers();\r
95                                         if (done) return; // もしrunDownloadInstallers()内でエラー終了していたなら終了\r
96                                         \r
97                                         packageInstallerDownloaded = true;\r
98                                         \r
99                                         \r
100                                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
101                                         \r
102                                         // ハッシュの壊れているインストーラーを取得\r
103                                         invalidInstallers = runCheckHashForInstaller();\r
104                                         \r
105                                         // ハッシュが壊れているときの対策\r
106                                         if (invalidInstallers.Count > 0) {\r
107                                                 System.Text.StringBuilder invalidInstallerNames = new System.Text.StringBuilder();\r
108                                                 foreach (Installation invalidInst in invalidInstallers) {\r
109                                                         invalidInstallerNames.AppendFormat(" - {0}\n", invalidInst.ToString());\r
110                                                 }\r
111                                                 \r
112                                                 string msg = string.Format("以下の{0}個のパッケージでファイルが壊れている可能性があります。\n{1}\n強制的にインストールを続行しますか?",\r
113                                                                            invalidInstallers.Count, invalidInstallerNames.ToString());\r
114                                                 NaGetTaskQueryResult result = NaGetTaskQueryResult.CANCEL;\r
115                                                 \r
116                                                 if (!cancelCalled) {\r
117                                                         result = RaiseTaskSetQueryEvent(msg, NaGetTaskQueryResult.CONTINUE\r
118                                                                                         | NaGetTaskQueryResult.RETRY\r
119                                                                                         | NaGetTaskQueryResult.CANCEL);\r
120                                                 }\r
121                                                 \r
122                                                 switch (result) {\r
123                                                         case NaGetTaskQueryResult.CONTINUE:\r
124                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "ハッシュの非整合を無視してインストールを継続");\r
125                                                                 invalidInstallers.Clear(); // ハッシュ非適合パッケージを強制的に抹消\r
126                                                                 break;\r
127                                                         case NaGetTaskQueryResult.RETRY:\r
128                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, "ダウンロード処理を再試行");\r
129                                                                 \r
130                                                                 foreach (Installation invalidInst in invalidInstallers) {\r
131                                                                         invalidInst.RemoveDownloadedFile();\r
132                                                                 }\r
133                                                                 \r
134                                                                 break;\r
135                                                         //case NaGetTaskQueryResult.CANCEL:\r
136                                                         default:\r
137                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.CANCELED, "パッケージのインストール処理がキャンセルされました");\r
138                                                                 done = true;\r
139                                                                 return;\r
140                                                 }\r
141                                         }\r
142                                         currentTaskSetIndex ++;\r
143                                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex-1]);\r
144                                 }\r
145                         }\r
146                         \r
147                         foreach (Installation inst in Installations) {\r
148                                 string installTaskMsg = TaskSetNames[currentTaskSetIndex];\r
149                                 if (inst.Silent && (!inst.SupportsSilentOnly)) {\r
150                                         installTaskMsg += " (サイレントインストール)";\r
151                                 }\r
152                                 \r
153                                 RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, installTaskMsg);\r
154                                 \r
155                                 try {\r
156                                         \r
157                                         inst.ErrorDataReceived += this.ReceivedErrorData;\r
158                                         inst.OutputDataReceived += this.ReceivedOutputData;\r
159                                         int exitCode = inst.Install();\r
160                                         if (exitCode != 0) {\r
161                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "インストールが正常に終えていない可能性があります。プロセスの終了コード:"+exitCode);\r
162                                         }\r
163                                         \r
164                                         pkgListMan.WriteInstallationLog(inst);\r
165                                 } catch (Exception e) {\r
166                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, e.Message);\r
167                                         done = true;\r
168                                         return;\r
169                                 }\r
170                                 currentTaskSetIndex ++;\r
171                                 \r
172                                 RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, installTaskMsg);\r
173                                 \r
174                                 if (cancelCalled) {\r
175                                         RaiseTaskSetEvent(NaGetTaskSetEventType.CANCELED, "パッケージのインストール処理がキャンセルされました");\r
176                                         done = true;\r
177                                         return;\r
178                                 }\r
179                         }\r
180                         pkgListMan.SaveSystemInstalledLogList(); // ログのコミット\r
181                         \r
182                         runLocalUpdate();\r
183                         \r
184                         done = true;\r
185                         \r
186                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED, "終了", 100);\r
187                 }\r
188                 \r
189                 \r
190                 /// <summary>\r
191                 /// 処理内容のダウンロード・ウイルススキャン部分のサブルーチン\r
192                 /// </summary>\r
193                 private void runDownloadAndVirusCheckInstallers()\r
194                 {\r
195                         using (DownloadScanner scanner = new DownloadScanner()) {\r
196                                 scanner.Init();\r
197                                 foreach (Installation inst in Installations) {\r
198                                         if (! inst.IsInstallablePackage()) {\r
199                                                 string msg = string.Format("{0}はインストールすることができません", inst.ToString());\r
200                                                 \r
201                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, msg);\r
202                                                 done = true;\r
203                                                 return;\r
204                                         }\r
205                                         \r
206                                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
207                                         \r
208                                         if (! inst.Downloaded) {\r
209                                                 try {\r
210                                                         inst.Download(Downloader);\r
211                                                 } catch (NaGetTaskCanceledException) {\r
212                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.CANCELED, "インストーラーのダウンロード処理がキャンセルされました");\r
213                                                         done = true;\r
214                                                         return;\r
215                                                 } catch (System.Net.WebException e) {\r
216                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, e.Message);\r
217                                                         if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) {\r
218                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, "ネットワークに接続されていません。");\r
219                                                         } else {\r
220                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, "ネットワークに接続できませんでした。ネットワークが切断されているか、ファイアウォールによって遮断された可能性があります。");\r
221                                                         }\r
222                                                         done = true;\r
223                                                         return;\r
224                                                 } catch (Exception e) {\r
225                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, e.Message);\r
226                                                         done = true;\r
227                                                         return;\r
228                                                 }\r
229                                         }\r
230                                         currentTaskSetIndex ++;\r
231                                         \r
232                                         if (inst.Downloaded) { // 正常終了\r
233                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex-1]);\r
234                                         } else { // インストールが完了せずに終わった=失敗=エラー\r
235                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, string.Format("{0}のインストーラーファイルを正常にダウンロードできませんでした", inst.ToString()));\r
236                                                 done = true;\r
237                                                 return;\r
238                                         }\r
239                                         \r
240                                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
241                                         if (! NaGet.Env.EnableScanInstallerFile) {\r
242                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, "ウイルススキャンを行わない設定のため、ダウンロードしたファイルはウイルススキャンされませんでした");\r
243                                         } else if (!scanner.HasScanner) {\r
244                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, "ダウンロードしたファイルはウイルススキャンされませんでした(ウイルススキャンソフトが検出できませんでした)");\r
245                                         } else {\r
246                                                 try {\r
247                                                         DownloadScannerResult result = inst.ScanInstallerFile(scanner);\r
248                                                         \r
249                                                         switch (result) {\r
250                                                                 case DownloadScannerResult.ScannerNotFound:\r
251                                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, "ダウンロードしたファイルはウイルススキャンされませんでした(ウイルススキャンソフトが検出できませんでした)");\r
252                                                                         break;\r
253                                                                 case DownloadScannerResult.InfectedAndCleaned:\r
254                                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR,\r
255                                                                                   "インストーラーファイルからウイルス感染が検出されたため、削除されました。");\r
256                                                                         done = true;\r
257                                                                         return;\r
258                                                                 case DownloadScannerResult.InfectedButNotCleaned:\r
259                                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR,\r
260                                                                                   "インストーラーファイルからウイルス感染が検出されました。");\r
261                                                                         done = true;\r
262                                                                         break;\r
263                                                                 case DownloadScannerResult.ErrorNotFound:\r
264                                                                         throw new System.IO.FileNotFoundException(string.Empty);\r
265                                                                         //break;\r
266                                                         }\r
267                                                         \r
268                                                 } catch (System.Runtime.InteropServices.COMException ex) {\r
269                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING,\r
270                                                                           string.Format("{0} (E{1})", ex.Message, ex.ErrorCode));\r
271                                                 } catch (System.IO.FileNotFoundException ex) {\r
272                                                         if (ex.InnerException is System.Runtime.InteropServices.COMException) {\r
273                                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING,\r
274                                                                                   string.Format("{0} (E{1})", ex.InnerException.Message, ((System.Runtime.InteropServices.COMException) ex.InnerException).ErrorCode));\r
275                                                         }\r
276                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, "インストーラーファイルがウイルススキャナーによって削除されました。");\r
277                                                         done = true;\r
278                                                         return;\r
279                                                 }\r
280                                         }\r
281                                         currentTaskSetIndex ++;\r
282                                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex-1]);\r
283                                         \r
284                                         if (cancelCalled) {\r
285                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.CANCELED, "パッケージのインストール処理がキャンセルされました");\r
286                                                 done = true;\r
287                                                 return;\r
288                                         }\r
289                                 }\r
290                         }\r
291                 }\r
292                 \r
293                 /// <summary>\r
294                 /// ダウンロードしたパッケージが整合したか否かハッシュでチェック\r
295                 /// </summary>\r
296                 /// <returns>整合しなかったインストーラーのリスト</returns>\r
297                 private List<Installation> runCheckHashForInstaller()\r
298                 {\r
299                         List<Installation> invalidInstallers = new List<Installation>();\r
300                         \r
301                         int i = 0;\r
302                         foreach (Installation inst in Installations) {\r
303                                 float percent = (CurrentTaskSetIndex+((float)i / Installations.Length))*100f/TaskSetNames.Length;\r
304                                 \r
305                                 if (inst.GetRegisteredHashCount() > 0) {\r
306                                         RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, "検証: "+inst.ToString(), percent);\r
307                                         \r
308                                         if (inst.IsInstallablePackage() && inst.VerifyHashValues() == false) {\r
309                                                 invalidInstallers.Add(inst);\r
310                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "検証: "+inst.ToString() + " 非整合", percent);\r
311                                         } else {\r
312                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, "検証: "+inst.ToString() + " OK", percent);\r
313                                         }\r
314                                 }\r
315                                 i++;\r
316                         }\r
317                         \r
318                         return invalidInstallers;\r
319                 }\r
320                 \r
321                 private void runLocalUpdate()\r
322                 {\r
323                         // インストールトリストの更新\r
324                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
325                         pkgListMan.DetectInstalledPkgs();\r
326                         pkgListMan.SaveInstalledPackageList();\r
327                         currentTaskSetIndex++;\r
328                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex-1]);\r
329                 \r
330                         // システムにインストールされているリストの更新\r
331                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
332                         pkgListMan.DetectSystemInstalledPkgs();\r
333                         pkgListMan.SaveSystemInstalledPackageList();\r
334                         currentTaskSetIndex++;\r
335                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex-1]);\r
336                 }\r
337                 \r
338                 public override bool Done {\r
339                         get { return done; }\r
340                 }\r
341                 \r
342                 public override int CurrentTaskSetIndex {\r
343                         get { return currentTaskSetIndex; }\r
344                 }\r
345                 \r
346                 private bool cancelCalled = false;\r
347                 \r
348                 public override bool Cancel()\r
349                 {\r
350                         cancelCalled = true;\r
351                         if (! packageInstallerDownloaded) {\r
352                                 return Downloader.Cancel();\r
353                         } else return true;\r
354                 }\r
355         }\r
356 }\r