Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Knossos.NET/Classes/KnUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -950,5 +950,52 @@ public static string EscapeUnderscores(string? text)

return text.Replace("_", "__");
}

/// <summary>
/// Checks if a file is currently in use
/// </summary>
/// <param name="file"></param>
/// <returns>true or false</returns>
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;
}

/// <summary>
/// Deletes a file checking if it exists first and then waits for the file to be closed.
/// </summary>
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);
}
}
}
}
123 changes: 71 additions & 52 deletions Knossos.NET/ViewModels/Templates/TaskItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ private async Task<bool> 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;
Expand Down Expand Up @@ -1299,27 +1299,31 @@ private async Task<bool> 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);
Expand Down Expand Up @@ -1376,27 +1380,31 @@ private async Task<bool> 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";
Expand All @@ -1411,34 +1419,45 @@ private async Task<bool> 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);
}
Log.Add(Log.LogSeverity.Information, "TaskItemViewModel.PrepareModPkg()", "CRC Verify OK on File: " + zipPath);
}
}

//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.
Expand Down