OSDN Git Service

na-get-lib,ダウンロード後にファイル名が引き継がれずインストールなどが動作していなかったのを修正
authorttp <ttp@users.sourceforge.jp>
Sun, 26 Jun 2011 01:48:17 +0000 (10:48 +0900)
committerttp <ttp@users.sourceforge.jp>
Sun, 26 Jun 2011 01:48:17 +0000 (10:48 +0900)
na-get-lib/NaGet.Packages.Install/Installation.cs
na-get-lib/NaGet.SubCommands.SubTask/DownloadSubTask.cs
na-get-lib/NaGet.SubCommands/NaGetDownloadToCache2.cs
na-get-lib/NaGet.SubCommands/NaGetInstall2.cs

index 0746cf4..2809ad0 100644 (file)
@@ -79,6 +79,7 @@ namespace NaGet.Packages.Install
                /// </summary>\r
                public string InstallerFile {\r
                        get { return installerFile; }\r
                /// </summary>\r
                public string InstallerFile {\r
                        get { return installerFile; }\r
+                       set { installerFile = value; }\r
                }\r
                \r
                /// <summary>\r
                }\r
                \r
                /// <summary>\r
index e944dfc..7699906 100644 (file)
@@ -49,11 +49,21 @@ namespace NaGet.SubCommands.SubTask
                public System.Net.Cache.RequestCacheLevel CacheLevel = System.Net.Cache.RequestCacheLevel.NoCacheNoStore;
                
                /// <summary>
                public System.Net.Cache.RequestCacheLevel CacheLevel = System.Net.Cache.RequestCacheLevel.NoCacheNoStore;
                
                /// <summary>
+               /// ダウンロード時に downloadedFileName に改名するか否か。
+               /// </summary>
+               protected bool enableChangeFileName = false;
+               
+               /// <summary>
                /// キャンセルが呼ばれたか否か。
                /// </summary>
                private bool cancelCalled = false;
                
                /// <summary>
                /// キャンセルが呼ばれたか否か。
                /// </summary>
                private bool cancelCalled = false;
                
                /// <summary>
+               /// ダウンロード中のファイル名につける接尾辞
+               /// </summary>
+               private static readonly string PartialFilePostfix = ".part";
+               
+               /// <summary>
                /// コンストラクタ
                /// </summary>
                /// <param name="url">ダウンロード先URL</param>
                /// コンストラクタ
                /// </summary>
                /// <param name="url">ダウンロード先URL</param>
@@ -98,6 +108,21 @@ namespace NaGet.SubCommands.SubTask
                }
                
                /// <summary>
                }
                
                /// <summary>
+               /// 保存先ファイル名を本来のファイル名に変えるか。
+               /// </summary>
+               public bool EnableChangeFileName {
+                       get { return enableChangeFileName; }
+                       set { enableChangeFileName = value; }
+               }
+               
+               /// <summary>
+               /// 保存ファイル。
+               /// </summary>
+               public string Filepath {
+                       get { return filepath; }
+               }
+               
+               /// <summary>
                /// キャンセル可能
                /// </summary>
                public override bool Cancelable {
                /// キャンセル可能
                /// </summary>
                public override bool Cancelable {
@@ -216,21 +241,33 @@ namespace NaGet.SubCommands.SubTask
                        } catch (Exception) {
                        }
                        
                        } catch (Exception) {
                        }
                        
-                       if (File.Exists(filepath)) { // ファイルが存在するとき削除
+                       // パス名を変えるときは、HTTPヘッダから取得したファイル名に変更する。
+                       if (enableChangeFileName && (!string.IsNullOrEmpty(downloadedFileName))) {
+                               filepath = Path.Combine(Path.GetDirectoryName(filepath), downloadedFileName);
+                       }
+                       
+                       // ファイルが存在するとき削除
+                       if (File.Exists(filepath)) {
                                File.Delete(filepath);
                        }
                                File.Delete(filepath);
                        }
+                       // 部分ファイルが存在するときも削除 TODO レジューム処理
+                       if (File.Exists(filepath + PartialFilePostfix)) {
+                               File.Delete(filepath + PartialFilePostfix);
+                       }
                }
                
                private void runDownloadToFile()
                {
                        Stopwatch stopwatch = new Stopwatch();
                }
                
                private void runDownloadToFile()
                {
                        Stopwatch stopwatch = new Stopwatch();
+                       string partialFilepath = filepath + PartialFilePostfix;
                        
                        using (Stream stream = response.GetResponseStream() )
                        
                        using (Stream stream = response.GetResponseStream() )
-                       using (FileStream fs = new FileStream(filepath,
+                       using (FileStream fs = new FileStream(partialFilepath,
                                                    FileMode.Create,
                                                    FileAccess.Write) ) {
                                                    FileMode.Create,
                                                    FileAccess.Write) ) {
+                               
                                try {
                                try {
-                                       File.SetAttributes(filepath, FileAttributes.Hidden);
+                                       File.SetAttributes(partialFilepath, FileAttributes.Hidden);
                                        long contentLength = response.ContentLength;
        
                                        stopwatch.Start();
                                        long contentLength = response.ContentLength;
        
                                        stopwatch.Start();
@@ -257,8 +294,6 @@ namespace NaGet.SubCommands.SubTask
                                        } finally {
                                                timer.Dispose();
                                        }
                                        } finally {
                                                timer.Dispose();
                                        }
-                                       
-                                       File.SetAttributes(filepath, FileAttributes.Normal);
                                } catch (IOException ex) {
                                        if (cancelCalled) {
                                                throw new TaskCanceledException(string.Empty);
                                } catch (IOException ex) {
                                        if (cancelCalled) {
                                                throw new TaskCanceledException(string.Empty);
@@ -272,10 +307,15 @@ namespace NaGet.SubCommands.SubTask
                                        }
                                }
                        }
                                        }
                                }
                        }
+                       
+                       if (File.Exists(partialFilepath)) {
+                               File.Move(partialFilepath, filepath);
+                               File.SetAttributes(filepath, FileAttributes.Normal);
+                       }
                }
                
                private void runPostprocess()
                }
                
                private void runPostprocess()
-               {                       
+               {
                        // 更新日を補完
                        if (File.Exists(filepath)) {
                                HttpWebResponse httpResponse = response as HttpWebResponse;
                        // 更新日を補完
                        if (File.Exists(filepath)) {
                                HttpWebResponse httpResponse = response as HttpWebResponse;
index 33dec45..1f3ac22 100644 (file)
@@ -44,9 +44,10 @@ namespace NaGet.SubCommands
                                DownloadSubTask dlSTask = new DownloadSubTask(inst.InstallerURL, inst.InstallerFile);
                                VirusScanSubTask scanSTask = new VirusScanSubTask(scanner, inst.InstallerFile, inst.InstallerURL);
                                
                                DownloadSubTask dlSTask = new DownloadSubTask(inst.InstallerURL, inst.InstallerFile);
                                VirusScanSubTask scanSTask = new VirusScanSubTask(scanner, inst.InstallerFile, inst.InstallerURL);
                                
+                               dlSTask.EnableChangeFileName = true;
                                dlSTask.TaskEventRaised += delegate(object sender, TaskEventArgs e) {
                                        if (e.Type == TaskEventType.COMPLETED) {
                                dlSTask.TaskEventRaised += delegate(object sender, TaskEventArgs e) {
                                        if (e.Type == TaskEventType.COMPLETED) {
-                                               scanSTask.TargetFilePath = inst.InstallerFile;
+                                               scanSTask.TargetFilePath = inst.InstallerFile = dlSTask.Filepath;
                                        }
                                };
                                
                                        }
                                };
                                
index 3ee6de0..dffebac 100644 (file)
@@ -53,10 +53,11 @@ namespace NaGet.SubCommands
                                
                                if (! inst.Downloaded) {
                                        DownloadSubTask dlSTask = new DownloadSubTask(inst.InstallerURL, inst.InstallerFile);
                                
                                if (! inst.Downloaded) {
                                        DownloadSubTask dlSTask = new DownloadSubTask(inst.InstallerURL, inst.InstallerFile);
-                               
+                                       
+                                       dlSTask.EnableChangeFileName = true;
                                        dlSTask.TaskEventRaised += delegate(object sender, TaskEventArgs e) {
                                                if (e.Type == TaskEventType.COMPLETED) {
                                        dlSTask.TaskEventRaised += delegate(object sender, TaskEventArgs e) {
                                                if (e.Type == TaskEventType.COMPLETED) {
-                                                       scanSTask.TargetFilePath = inst.InstallerFile;
+                                                       scanSTask.TargetFilePath = inst.InstallerFile = dlSTask.Filepath;
                                                }
                                        };
                                        
                                                }
                                        };