OSDN Git Service

Merge branch 'mustard'
[applistation/AppliStation.git] / na-get-lib / NaGet.SubCommands.SubTask / VirusScanSubTask.cs
1 using System;
2 using System.IO;
3 using System.Runtime.InteropServices;
4 using NaGet.Tasks;
5 using NaGet.InteropServices;
6
7 namespace NaGet.SubCommands.SubTask
8 {
9         public class VirusScanSubTask : NaGetSubTask
10         {
11                 private string targetFilePath = null;
12                 
13                 private string targetUrl = null;
14                 
15                 private DownloadScannerService scanner = null;
16                 
17                 public VirusScanSubTask(DownloadScannerService scanner)
18                         : this(scanner, null, null)
19                 {
20                 }
21                 
22                 public VirusScanSubTask(DownloadScannerService scanner, string targetFilePath, string targetUrl)
23                 {
24                         this.scanner = scanner;
25                         this.targetFilePath = targetFilePath;
26                         this.targetUrl = targetUrl;
27                 }
28                 
29                 public string TargetFilePath {
30                         get { return targetFilePath; }
31                         set { targetFilePath = value; }
32                 }
33                 
34                 public string TargetUrl {
35                         get { return targetUrl; }
36                         set { targetUrl = value; }
37                 }
38                 
39                 private void runScanFile()
40                 {
41                         DownloadScannerResult result;
42                         try {
43                                 RaiseTaskSetEvent(TaskEventType.PING, string.Empty, -1);
44                                 result = scanner.Scan(targetFilePath, targetUrl);
45                         } catch (COMException ex) {
46                                 result = DownloadScannerResult.ScannerNotFound;
47                                 RaiseTaskSetEvent(TaskEventType.WARNING, string.Format("{0} (E{1})", ex.Message, ex.ErrorCode), 0);
48                         } catch (FileNotFoundException) {
49                                 result = DownloadScannerResult.ErrorNotFound;
50                         } catch (Exception) {
51                                 result = DownloadScannerResult.ScannerNotFound;
52                         }
53                         
54                         Exception e = null;
55                         switch (result) {
56                                 case DownloadScannerResult.OK:
57                                         RaiseTaskSetEvent(TaskEventType.PING, string.Empty, 100);
58                                         break;
59                                 case DownloadScannerResult.ScannerNotFound:
60                                         RaiseTaskSetEvent(TaskEventType.INFO, "ダウンロードしたファイルへのウイルススキャンはされなかったか、スキャン結果は無視されました", 0);
61                                         break;
62                                 case DownloadScannerResult.InfectedAndCleaned:
63                                         e = new ApplicationException("インストーラーファイルからウイルス感染が検出されたため、削除されました。");
64                                         break;
65                                 case DownloadScannerResult.InfectedButNotCleaned:
66                                         e = new ApplicationException("インストーラーファイルからウイルス感染が検出されました。");
67                                         break;
68                                 case DownloadScannerResult.ErrorNotFound:
69                                         e = new FileNotFoundException(string.Empty);
70                                         break;
71                         }
72                         
73                         if (e != null) {
74                                 throw e;
75                         }
76                 }
77                 
78                 public override void Run()
79                 {
80                         NotifyStarted();
81                         RaiseTaskSetEvent(TaskEventType.STARTED, string.Format("ウイルススキャン: {0}", targetFilePath), 0);
82                         
83                         try {
84                                 if (! NaGet.Env.EnableScanInstallerFile) {
85                                         RaiseTaskSetEvent(TaskEventType.INFO, "ウイルススキャンを行わない設定のため、ダウンロードしたファイルはウイルススキャンされませんでした", 0);
86                                 } else if (!scanner.HasScanner) {
87                                         RaiseTaskSetEvent(TaskEventType.INFO, "ダウンロードしたファイルはウイルススキャンされませんでした(ウイルススキャンソフトが検出できませんでした)", 0);
88                                 } else if (! File.Exists(targetFilePath)) {
89                                         RaiseTaskSetEvent(TaskEventType.ERROR, "ダウンロードしたファイルは存在しませんでした(ウイルススキャンソフトが自動的に即座に削除した可能性があります)", 0);
90                                 } else {
91                                         runScanFile();
92                                 }
93                         } finally {
94                                 RaiseTaskSetEvent(TaskEventType.COMPLETED, string.Format("ウイルススキャン: {0}", targetFilePath), 0);
95                                 NotifyCompleted();
96                         }
97                 }
98         }
99 }