OSDN Git Service

na-get-lib,タスクセットの改修(NaGetTaskSet2)および、そのUnitTestを追加
[applistation/AppliStation.git] / na-get-lib / NaGet.Net / DownloadScanner.cs
index 5dc9b7c..9b65a84 100644 (file)
@@ -13,6 +13,15 @@ using NaGet.InteropServices;
 \r
 namespace NaGet.Net\r
 {\r
 \r
 namespace NaGet.Net\r
 {\r
+       public enum DownloadScannerResult : uint {\r
+               OK = 0, // S_OK\r
+               InfectedAndCleaned = 1, // S_FALSE\r
+               InfectedButNotCleaned = 0x80004005,     // E_FAIL\r
+               ErrorNotFound = 2,      // ERROR_NOT_FOUND\r
+               \r
+               ScannerNotFound = 0xFFFFFFFF,\r
+       }\r
+       \r
        /// <summary>\r
        /// ダウンロードしたファイルをスキャンする\r
        /// </summary>\r
        /// <summary>\r
        /// ダウンロードしたファイルをスキャンする\r
        /// </summary>\r
@@ -48,14 +57,15 @@ namespace NaGet.Net
                [Guid("56FFCC30-D398-11D0-B2AE-00A0C908FA49")]\r
                [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]\r
                private interface IOfficeAntiVirus {\r
                [Guid("56FFCC30-D398-11D0-B2AE-00A0C908FA49")]\r
                [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]\r
                private interface IOfficeAntiVirus {\r
-                       void Scan(ref MSOAVINFO pmsoavinfo);\r
+                       [PreserveSig()]\r
+                       uint Scan(ref MSOAVINFO pmsoavinfo);\r
                }\r
                #endregion\r
                \r
                /// <summary>\r
                /// ウイルススキャナーに渡すプログラム・ホスト名。\r
                /// </summary>\r
                }\r
                #endregion\r
                \r
                /// <summary>\r
                /// ウイルススキャナーに渡すプログラム・ホスト名。\r
                /// </summary>\r
-               public string HostName {\r
+               public static string HostName {\r
                        get {\r
                                // 実行アセンブリ名を返す\r
                                return Assembly.GetExecutingAssembly().GetName().FullName;\r
                        get {\r
                                // 実行アセンブリ名を返す\r
                                return Assembly.GetExecutingAssembly().GetName().FullName;\r
@@ -69,7 +79,10 @@ namespace NaGet.Net
                /// </summary>\r
                public DownloadScanner()\r
                {\r
                /// </summary>\r
                public DownloadScanner()\r
                {\r
-                       ComDirectAccess.CoInitialize(IntPtr.Zero);\r
+                       int result = ComDirectAccess.CoInitialize(IntPtr.Zero);\r
+                       if (result == 0) {\r
+                               throw new System.ComponentModel.Win32Exception();\r
+                       }\r
                }\r
                \r
                /// <summary>\r
                }\r
                \r
                /// <summary>\r
@@ -79,6 +92,7 @@ namespace NaGet.Net
                {\r
                        Release();\r
                        ComDirectAccess.CoUninitialize();\r
                {\r
                        Release();\r
                        ComDirectAccess.CoUninitialize();\r
+                       GC.SuppressFinalize(this);\r
                }\r
                \r
                /// <summary>\r
                }\r
                \r
                /// <summary>\r
@@ -131,14 +145,10 @@ namespace NaGet.Net
                /// <param name="path">ファイルのパス</param>\r
                /// <param name="origin">ファイルをダウンロードしたURL。nullであってはならない</param>\r
                /// <exception cref="COMException">COMのエラー発生時。たとえば、AVGではウイルスと検出されたのにユーザが「無視」を指定したときにも投げられる。</exception>\r
                /// <param name="path">ファイルのパス</param>\r
                /// <param name="origin">ファイルをダウンロードしたURL。nullであってはならない</param>\r
                /// <exception cref="COMException">COMのエラー発生時。たとえば、AVGではウイルスと検出されたのにユーザが「無視」を指定したときにも投げられる。</exception>\r
-               /// <returns>ウイルススキャンがされたか否か</returns>\r
+               /// <returns>ウイルススキャン結果。</returns>\r
                /// <remarks>Init()呼出し後に使える</remarks>\r
                /// <remarks>Init()呼出し後に使える</remarks>\r
-               public bool Scan(string path, string origin)\r
+               public DownloadScannerResult Scan(string path, string origin)\r
                {\r
                {\r
-                       if (scanners.Count <= 0) {\r
-                               return false;\r
-                       }\r
-                       \r
                        MSOAVINFO info = new MSOAVINFO();\r
                        info.cbsize = Marshal.SizeOf(info);\r
                        info.uFlags = MSOAVINFOFLAG.fPath | MSOAVINFOFLAG.fHttpDownload;\r
                        MSOAVINFO info = new MSOAVINFO();\r
                        info.cbsize = Marshal.SizeOf(info);\r
                        info.uFlags = MSOAVINFOFLAG.fPath | MSOAVINFOFLAG.fHttpDownload;\r
@@ -147,11 +157,23 @@ namespace NaGet.Net
                        info.pwzHostName = HostName;\r
                        info.pwzOrigURL = origin;\r
                        \r
                        info.pwzHostName = HostName;\r
                        info.pwzOrigURL = origin;\r
                        \r
+                       DownloadScannerResult result = DownloadScannerResult.ScannerNotFound;\r
                        foreach (IOfficeAntiVirus i in scanners) {\r
                        foreach (IOfficeAntiVirus i in scanners) {\r
-                               if (! System.IO.File.Exists(path)) break;\r
-                               i.Scan(ref info);\r
+                               if (System.IO.File.Exists(path)) {\r
+                                       result = (DownloadScannerResult) i.Scan(ref info);\r
+                                       if (result == DownloadScannerResult.OK && System.IO.File.Exists(path) == false) {\r
+                                               result = DownloadScannerResult.InfectedAndCleaned;\r
+                                       }\r
+                               } else {\r
+                                       result = DownloadScannerResult.ErrorNotFound;\r
+                               }\r
+                               \r
+                               if (result != DownloadScannerResult.OK) {\r
+                                       break;\r
+                               }\r
                        }\r
                        }\r
-                       return true;\r
+                       \r
+                       return result;\r
                }\r
        }\r
 }\r
                }\r
        }\r
 }\r