From 115502326f548fec81d2869f2a9300b8d2875656 Mon Sep 17 00:00:00 2001 From: Salvador Cipolla Date: Fri, 6 Sep 2024 21:18:05 -0300 Subject: [PATCH] Disable max crc check attempts and remove cancel on 7z compression fail -Now compression fail is not considered a show stopper, it will try to compress the file again. -The number of crc checks and recompress are now infinite -Add a wait for file close before getting the hash --- Knossos.NET/Classes/KnUtils.cs | 47 +++++++ .../ViewModels/Templates/TaskItemViewModel.cs | 123 ++++++++++-------- 2 files changed, 118 insertions(+), 52 deletions(-) diff --git a/Knossos.NET/Classes/KnUtils.cs b/Knossos.NET/Classes/KnUtils.cs index bcfa2777..46152247 100644 --- a/Knossos.NET/Classes/KnUtils.cs +++ b/Knossos.NET/Classes/KnUtils.cs @@ -950,5 +950,52 @@ public static string EscapeUnderscores(string? text) return text.Replace("_", "__"); } + + /// + /// Checks if a file is currently in use + /// + /// + /// true or false + public static bool IsFileInUse(string filePath) + { + try + { + using (FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.None)) + { + stream.Close(); + } + } + catch (IOException) + { + return true; + } + catch (Exception ex) + { + Log.Add(Log.LogSeverity.Error, "KnUtils.IsFileInUse()", ex); + } + return false; + } + + /// + /// Deletes a file checking if it exists first and then waits for the file to be closed. + /// + public static void DeleteFileSafe(string filePath) + { + try + { + if (File.Exists(filePath)) + { + while (IsFileInUse(filePath)) + { + Log.Add(Log.LogSeverity.Information, "TaskItemViewModel.PrepareModPkg()", "Waiting for file to be closed to delete it: " + filePath); + } + File.Delete(filePath); + } + } + catch (Exception ex) + { + Log.Add(Log.LogSeverity.Error, "KnUtils.DeleteFileSafe()", ex); + } + } } } diff --git a/Knossos.NET/ViewModels/Templates/TaskItemViewModel.cs b/Knossos.NET/ViewModels/Templates/TaskItemViewModel.cs index 1c2a5cb4..9b57492b 100644 --- a/Knossos.NET/ViewModels/Templates/TaskItemViewModel.cs +++ b/Knossos.NET/ViewModels/Templates/TaskItemViewModel.cs @@ -1215,7 +1215,7 @@ private async Task PrepareModPkg(ModPackage pkg, string modFullPath, Cance IsTextTask = false; IsFileDownloadTask = false; Name = "Prepare Pkg: " + pkg.name; - var maxCrcAttempts = 5; //How many times try to compress a pkg with 7z in case of CRC error + //var maxCrcAttempts = 5; //How many times try to compress a pkg with 7z in case of CRC error (LIMIT DISABLED) if (cancelSource != null) cancellationTokenSource = cancelSource; @@ -1299,27 +1299,31 @@ private async Task PrepareModPkg(ModPackage pkg, string modFullPath, Cance if (!await compressor.CompressFile(vpPath, modFullPath + Path.DirectorySeparatorChar + "kn_upload" + Path.DirectorySeparatorChar + "vps", zipPath, true)) { Log.Add(Log.LogSeverity.Error, "TaskItemViewModel.PrepareModPkg()", "Error while compressing the package"); - throw new TaskCanceledException(); + //Disable failing and instead delete the file if it exists + //throw new TaskCanceledException(); + KnUtils.DeleteFileSafe(zipPath); } - //CRC CHECK - Info = "CRC Check"; - crcResult = await compressor.VerifyFile(zipPath); - if(!crcResult) + else { - if(crcAttempt >= maxCrcAttempts) - { - Log.Add(Log.LogSeverity.Error, "TaskItemViewModel.PrepareModPkg()", "CRC error on file: " + zipPath + ". Max attempts reached, cancelling upload..."); - throw new TaskCanceledException(); - } - Log.Add(Log.LogSeverity.Error, "TaskItemViewModel.PrepareModPkg()", "CRC error on file: " + zipPath + ". Retrying..."); - ProgressBarMax = 100; - ProgressCurrent = 0; - Info = "Retry: Compressing (7z)"; - if (File.Exists(zipPath)) + //CRC CHECK + Info = "CRC Check"; + crcResult = await compressor.VerifyFile(zipPath); + if (!crcResult) { - File.Delete(zipPath); + /* + if(crcAttempt >= maxCrcAttempts) + { + Log.Add(Log.LogSeverity.Error, "TaskItemViewModel.PrepareModPkg()", "CRC error on file: " + zipPath + ". Max attempts reached, cancelling upload..."); + throw new TaskCanceledException(); + } + */ + Log.Add(Log.LogSeverity.Error, "TaskItemViewModel.PrepareModPkg()", "CRC error on file: " + zipPath + ". Retrying..."); + ProgressBarMax = 100; + ProgressCurrent = 0; + Info = "Retry: Compressing (7z)"; + KnUtils.DeleteFileSafe(zipPath); + crcAttempt++; } - crcAttempt++; } } while (!crcResult); Log.Add(Log.LogSeverity.Information, "TaskItemViewModel.PrepareModPkg()", "CRC Verify OK on File: " + zipPath); @@ -1376,27 +1380,31 @@ private async Task PrepareModPkg(ModPackage pkg, string modFullPath, Cance if (!await compressor.CompressFolderTarGz(modFullPath + Path.DirectorySeparatorChar + pkg.folder, zipPath)) { Log.Add(Log.LogSeverity.Error, "TaskItemViewModel.PrepareModPkg()", "Error while compressing the package"); - throw new TaskCanceledException(); + //Disable failing and instead delete the file if it exists + //throw new TaskCanceledException(); + KnUtils.DeleteFileSafe(zipPath + ".tar.gz"); } - //CRC CHECK - Info = "CRC Check"; - crcResult = await compressor.VerifyFile(zipPath + ".tar.gz"); - if (!crcResult) + else { - if (crcAttempt >= maxCrcAttempts) - { - Log.Add(Log.LogSeverity.Error, "TaskItemViewModel.PrepareModPkg()", "CRC error on file: " + zipPath + ".tar.gz. Max attempts reached, cancelling upload..."); - throw new TaskCanceledException(); - } - Log.Add(Log.LogSeverity.Error, "TaskItemViewModel.PrepareModPkg()", "CRC error on file: " + zipPath + ".tar.gz. Retrying..."); - ProgressBarMax = 100; - ProgressCurrent = 0; - Info = "Retry: Compressing (.tar.gz)"; - if (File.Exists(zipPath)) + //CRC CHECK + Info = "CRC Check"; + crcResult = await compressor.VerifyFile(zipPath + ".tar.gz"); + if (!crcResult) { - File.Delete(zipPath); + /* + if (crcAttempt >= maxCrcAttempts) + { + Log.Add(Log.LogSeverity.Error, "TaskItemViewModel.PrepareModPkg()", "CRC error on file: " + zipPath + ".tar.gz. Max attempts reached, cancelling upload..."); + throw new TaskCanceledException(); + } + */ + Log.Add(Log.LogSeverity.Error, "TaskItemViewModel.PrepareModPkg()", "CRC error on file: " + zipPath + ".tar.gz. Retrying..."); + ProgressBarMax = 100; + ProgressCurrent = 0; + Info = "Retry: Compressing (.tar.gz)"; + KnUtils.DeleteFileSafe(zipPath + ".tar.gz"); + crcAttempt++; } - crcAttempt++; } } while (!crcResult); zipPath += ".tar.gz"; @@ -1411,27 +1419,31 @@ private async Task PrepareModPkg(ModPackage pkg, string modFullPath, Cance if (!await compressor.CompressFolder(modFullPath + Path.DirectorySeparatorChar + pkg.folder, zipPath)) { Log.Add(Log.LogSeverity.Error, "TaskItemViewModel.PrepareModPkg()", "Error while compressing the package"); - throw new TaskCanceledException(); + //Disable failing and instead delete the file if it exists + //throw new TaskCanceledException(); + KnUtils.DeleteFileSafe(zipPath); } - //CRC CHECK - Info = "CRC Check"; - crcResult = await compressor.VerifyFile(zipPath); - if (!crcResult) + else { - if (crcAttempt >= maxCrcAttempts) - { - Log.Add(Log.LogSeverity.Error, "TaskItemViewModel.PrepareModPkg()", "CRC error on file: " + zipPath + ". Max attempts reached, cancelling upload..."); - throw new TaskCanceledException(); - } - Log.Add(Log.LogSeverity.Error, "TaskItemViewModel.PrepareModPkg()", "CRC error on file: " + zipPath + ". Retrying..."); - ProgressBarMax = 100; - ProgressCurrent = 0; - Info = "Retry: Compressing (7z)"; - if (File.Exists(zipPath)) + //CRC CHECK + Info = "CRC Check"; + crcResult = await compressor.VerifyFile(zipPath); + if (!crcResult) { - File.Delete(zipPath); + /* + if (crcAttempt >= maxCrcAttempts) + { + Log.Add(Log.LogSeverity.Error, "TaskItemViewModel.PrepareModPkg()", "CRC error on file: " + zipPath + ". Max attempts reached, cancelling upload..."); + throw new TaskCanceledException(); + } + */ + Log.Add(Log.LogSeverity.Error, "TaskItemViewModel.PrepareModPkg()", "CRC error on file: " + zipPath + ". Retrying..."); + ProgressBarMax = 100; + ProgressCurrent = 0; + Info = "Retry: Compressing (7z)"; + KnUtils.DeleteFileSafe(zipPath); + crcAttempt++; } - crcAttempt++; } } while (!crcResult); } @@ -1439,6 +1451,13 @@ private async Task PrepareModPkg(ModPackage pkg, string modFullPath, Cance } } + //Wait for file to be closed + while(KnUtils.IsFileInUse(zipPath)) + { + Info = "Waiting for file to be closed"; + Log.Add(Log.LogSeverity.Information, "TaskItemViewModel.PrepareModPkg()", "Waiting for file to be closed: " + zipPath); + } + Info = "Getting Hash"; /* * TODO: it is unclear to me, at this moment, why this would be needed since 7z should extract with fullpath.