From 56d8c73d2380061cc6119130fc958a92934af3df Mon Sep 17 00:00:00 2001 From: ttp Date: Wed, 3 Dec 2008 12:11:50 +0000 Subject: [PATCH] =?utf8?q?na-get-lib,r997=E3=82=B3=E3=83=9F=E3=83=83?= =?utf8?q?=E3=83=88=E3=81=AE=E3=82=B3=E3=83=BC=E3=83=89=E3=81=AE=E3=82=B3?= =?utf8?q?=E3=83=A1=E3=83=B3=E3=83=88=E3=81=AA=E3=81=A9=E3=81=AE=E8=BF=BD?= =?utf8?q?=E5=8A=A0=E3=80=82=E3=82=BD=E3=83=BC=E3=82=B9=E6=95=B4=E7=90=86?= =?utf8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit git-svn-id: http://localhost/svn/AppliStation/trunk@998 34ed2c89-c49f-4a4b-abdb-c318350108cf --- .../NaGet.InteropServices/ComDirectAccess.cs | 17 ++++ na-get-lib/NaGet.Net/DownloadScanner.cs | 60 +++++++++-- .../NaGet.Net/GuidEnumeratorForCategories.cs | 2 +- na-get-lib/NaGet.SubCommands/NaGetInstall.cs | 112 ++++++++++----------- 4 files changed, 125 insertions(+), 66 deletions(-) diff --git a/na-get-lib/NaGet.InteropServices/ComDirectAccess.cs b/na-get-lib/NaGet.InteropServices/ComDirectAccess.cs index f9ac556..1e6d0b5 100644 --- a/na-get-lib/NaGet.InteropServices/ComDirectAccess.cs +++ b/na-get-lib/NaGet.InteropServices/ComDirectAccess.cs @@ -49,15 +49,32 @@ namespace NaGet.InteropServices CLSCTX_ALL = CLSCTX_SERVER|CLSCTX_INPROC_HANDLER, } + /// + /// 初期化禁止 + /// private ComDirectAccess() { } + /// + /// COMオブジェクトを生成する。CoCreateInstanceのラッパメソッド + /// + /// オブジェクトのCLSID + /// アグリゲートオブジェクト + /// コンテキスト + /// オブジェクトのIID + /// 生成されたCOMオブジェクト public static object CreateInstance(Guid rclsid, object pUnkOuter, CLSCTX dwClsContext, Guid riid) { return CoCreateInstance(rclsid, pUnkOuter, dwClsContext, riid); } + /// + /// COMオブジェクトを生成する。COMオブジェクトの型はテンプレートTから自動取得・キャストされる。 + /// + /// オブジェクトのCLSID + /// コンテキスト + /// 生成されたCOMオブジェクト public static T CreateInstance(Guid clsid, CLSCTX dwClsContext) { Guid riid = new Guid(((GuidAttribute) Attribute.GetCustomAttribute(typeof(T), typeof(GuidAttribute))).Value); diff --git a/na-get-lib/NaGet.Net/DownloadScanner.cs b/na-get-lib/NaGet.Net/DownloadScanner.cs index af3e39e..12912d5 100644 --- a/na-get-lib/NaGet.Net/DownloadScanner.cs +++ b/na-get-lib/NaGet.Net/DownloadScanner.cs @@ -1,4 +1,10 @@ -using System; +/* + * This code is based on /mozilla/source/toolkit/components/downloads/src/nsDownloadScanner.cpp + * and sample code at https://bugzilla.mozilla.org/show_bug.cgi?id=103487, + * created by Rob Arnold. + */ + +using System; using System.Reflection; using System.Collections.Generic; using System.Runtime.InteropServices; @@ -7,6 +13,9 @@ using NaGet.InteropServices; namespace NaGet.Net { + /// + /// ダウンロードしたファイルをスキャンする + /// public class DownloadScanner : IDisposable { #region COMInterop @@ -54,28 +63,35 @@ namespace NaGet.Net } private List scanners; - + + /// + /// コンストラクタ。内部でCOM呼び出し初期化(CoInitialize)されます。 + /// public DownloadScanner() { ComDirectAccess.CoInitialize(IntPtr.Zero); } + /// + /// 内部でCOM開放(CoUninitialize)します。必ず呼び出す必要があります。 + /// public void Dispose() { - if ((scanners != null) && (scanners.Count > 0)) { - foreach (IOfficeAntiVirus i in scanners) { - Marshal.ReleaseComObject(i); - } - scanners.Clear(); - } - + Release(); ComDirectAccess.CoUninitialize(); } + /// + /// ウイルススキャンがあるかないか + /// + /// Init()呼出し後に使える public bool HasScanner { get { return scanners.Count > 0; } } + /// + /// 初期化処理としてウイルススキャンを探す。 + /// public void Init() { scanners = new List(); @@ -91,6 +107,32 @@ namespace NaGet.Net } } + /// + /// ウイルススキャンのオブジェクトを開放しInitの前の状態に戻す。 + /// + public void Release() + { + if ((scanners != null) && (scanners.Count > 0)) { + foreach (IOfficeAntiVirus i in scanners) { + Marshal.ReleaseComObject(i); + } + scanners.Clear(); + } + } + + /// + /// ファイルをスキャンする。ウイルススキャンが複数個見つかっている + /// ならばそれらすべてでスキャンする。 + /// ウイルススキャンの実装によるが、ウイルス発見時にはダイアログが開く。 + /// ウイルスの処理はユーザに委ねられるので、それの制御は一切できない。 + /// + /// ウイルスが見つかったか否かは取得できない。 + /// 本メソッド呼び出し後にウイルスが退避されているかもしれないが、ファイルの存在確認でしかそれをチェックできない + /// ファイルのパス + /// ファイルをダウンロードしたURL。nullであってはならない + /// COMのエラー発生時。たとえば、AVGではウイルスと検出されたのにユーザが「無視」を指定したときにも投げられる。 + /// ウイルススキャンがされたか否か + /// Init()呼出し後に使える public bool Scan(string path, string origin) { if (scanners.Count <= 0) { diff --git a/na-get-lib/NaGet.Net/GuidEnumeratorForCategories.cs b/na-get-lib/NaGet.Net/GuidEnumeratorForCategories.cs index 41bd433..e778d54 100644 --- a/na-get-lib/NaGet.Net/GuidEnumeratorForCategories.cs +++ b/na-get-lib/NaGet.Net/GuidEnumeratorForCategories.cs @@ -5,7 +5,7 @@ using Microsoft.Win32; namespace NaGet.Net { /// - /// ICatManagerの暫定的かつピュアC#実装 + /// ICatManagerの暫定的かつピュアC#実装。レジストリを舐めてICatManagerと同等の機能を実現する。 /// class GuidEnumeratorForCategories : IEnumerable, IDisposable { diff --git a/na-get-lib/NaGet.SubCommands/NaGetInstall.cs b/na-get-lib/NaGet.SubCommands/NaGetInstall.cs index bfd003f..b2cd573 100644 --- a/na-get-lib/NaGet.SubCommands/NaGetInstall.cs +++ b/na-get-lib/NaGet.SubCommands/NaGetInstall.cs @@ -178,74 +178,74 @@ namespace NaGet.SubCommands /// private void runDownloadAndVirusCheckInstallers() { - using (DownloadScanner scanner = new DownloadScanner()) { - scanner.Init(); - foreach (Installation inst in Installations) { - if (! inst.IsInstallablePackage()) { - string msg = string.Format("{0}はインストールすることができません", inst.ToString()); - - RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, msg); - done = true; - return; - } - - RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]); - - if (! inst.Downloaded) { - try { - inst.Download(Downloader); - } catch (NaGetTaskCanceledException) { - RaiseTaskSetEvent(NaGetTaskSetEventType.CANCELED, "インストーラのダウンロード処理がキャンセルされました"); + using (DownloadScanner scanner = new DownloadScanner()) { + scanner.Init(); + foreach (Installation inst in Installations) { + if (! inst.IsInstallablePackage()) { + string msg = string.Format("{0}はインストールすることができません", inst.ToString()); + + RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, msg); done = true; return; - } catch (System.Net.WebException e) { - RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, e.Message); - if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) { - RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, "ネットワークに接続されていません。"); - } else { - RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, "ネットワークに接続できませんでした。ネットワークが切断されているか、ファイアウォールによって遮断された可能性があります。"); + } + + RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]); + + if (! inst.Downloaded) { + try { + inst.Download(Downloader); + } catch (NaGetTaskCanceledException) { + RaiseTaskSetEvent(NaGetTaskSetEventType.CANCELED, "インストーラのダウンロード処理がキャンセルされました"); + done = true; + return; + } catch (System.Net.WebException e) { + RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, e.Message); + if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) { + RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, "ネットワークに接続されていません。"); + } else { + RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, "ネットワークに接続できませんでした。ネットワークが切断されているか、ファイアウォールによって遮断された可能性があります。"); + } + done = true; + return; + } catch (Exception e) { + RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, e.Message); + done = true; + return; } - done = true; - return; - } catch (Exception e) { - RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, e.Message); + } + currentTaskSetIndex ++; + + if (inst.Downloaded) { // 正常終了 + RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex]); + } else { // インストールが完了せずに終わった=失敗=エラー + RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, string.Format("{0}のインストーラを正常にダウンロードできませんでした", inst.ToString())); done = true; return; } - } - currentTaskSetIndex ++; - - if (inst.Downloaded) { // 正常終了 - RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex]); - } else { // インストールが完了せずに終わった=失敗=エラー - RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, string.Format("{0}のインストーラを正常にダウンロードできませんでした", inst.ToString())); - done = true; - return; - } - - RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]); - if (scanner.HasScanner) { - try { - inst.ScanInstallerFile(scanner); - } catch (System.Runtime.InteropServices.COMException ex) { - RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, - string.Format("{0} (E{1})", ex.Message, ex.ErrorCode)); - } catch (System.IO.FileNotFoundException ex) { - if (ex.InnerException is System.Runtime.InteropServices.COMException) { + + RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]); + if (scanner.HasScanner) { + try { + inst.ScanInstallerFile(scanner); + } catch (System.Runtime.InteropServices.COMException ex) { RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, - string.Format("{0} (E{1})", ex.InnerException.Message, ((System.Runtime.InteropServices.COMException) ex.InnerException).ErrorCode)); + string.Format("{0} (E{1})", ex.Message, ex.ErrorCode)); + } catch (System.IO.FileNotFoundException ex) { + if (ex.InnerException is System.Runtime.InteropServices.COMException) { + RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, + string.Format("{0} (E{1})", ex.InnerException.Message, ((System.Runtime.InteropServices.COMException) ex.InnerException).ErrorCode)); + } + RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, "インストーラがウイルススキャナによって削除されました。"); + done = true; + return; } - RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, "インストーラがウイルススキャナによって削除されました。"); - done = true; - return; + } else { + RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, string.Format("ダウンロードしたファイルはウイルススキャンされませんでした(ウイルススキャンソフトが検出できませんでした)")); } - } else { - RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, string.Format("ダウンロードしたファイルはウイルススキャンされませんでした(ウイルススキャンソフトが検出できませんでした)")); + RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex]); } - RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex]); } } - } /// /// ダウンロードしたパッケージが整合したか否かハッシュでチェック -- 2.11.0