private static ReturnCode FixFileDelete(RvFile fixFile, out string errorMessage) { ReturnCode retCode = FixFileUtils.DoubleCheckDelete(fixFile, out errorMessage); if (retCode != ReturnCode.Good) { return(retCode); } string filename = fixFile.FullName; if (File.Exists(filename)) { if (Settings.rvSettings.DetailedFixReporting) { Report.ReportProgress(new bgwShowFix(Path.GetDirectoryName(filename), "", Path.GetFileName(filename), fixFile.Size, "Delete", "", "", "")); } if (!File.SetAttributes(filename, FileAttributes.Normal)) { int error = Error.GetLastError(); Report.ReportProgress(new bgwShowError(filename, "Error Setting File Attributes to Normal. Before Delete. Code " + error)); } File.Delete(filename); } // here we just deleted a file so also delete it from the DB, // and recurse up deleting unnedded DIR's FixFileUtils.CheckDeleteFile(fixFile); return(ReturnCode.Good); }
private static ReturnCode FixFileMoveToSort(RvFile fixFile, out string errorMessage) { ReturnCode returnCode; returnCode = FixFileUtils.CreateToSortDirs(fixFile, out RvFile outDir, out string toSortFileName); if (returnCode != ReturnCode.Good) { errorMessage = ""; return(returnCode); } string fixFileFullName = fixFile.FullName; string toSortFullName = Path.Combine(outDir.FullName, toSortFileName); //create new tosort record // FileInfo toSortFile = new FileInfo(toSortFullName); RvFile toSortRom = new RvFile(FileType.File) { Name = toSortFileName, Size = fixFile.Size, CRC = fixFile.CRC, //TimeStamp = toSortFile.LastWriteTime, DatStatus = DatStatus.InToSort }; Report.ReportProgress(new bgwShowFix(Path.GetDirectoryName(fixFileFullName), "", Path.GetFileName(fixFileFullName), fixFile.Size, "-->", outDir.FullName, "", fixFileFullName)); returnCode = FixFileUtils.MoveFile(fixFile, toSortRom, toSortFullName, out bool fileMoved, out errorMessage); if (returnCode != ReturnCode.Good) { return(returnCode); } if (!fileMoved) { returnCode = FixFileUtils.CopyFile(fixFile, null, toSortFullName, toSortRom, false, out errorMessage); if (returnCode != ReturnCode.Good) { return(returnCode); } string fixFilePath = fixFile.FullName; if (!File.SetAttributes(fixFilePath, FileAttributes.Normal)) { int error = Error.GetLastError(); Report.ReportProgress(new bgwShowError(fixFilePath, "Error Setting File Attributes to Normal. Before Delete Moving ToSort. Code " + error)); } File.Delete(fixFilePath); // here we just deleted a file so also delete it from the DB, // and recurse up deleting unnedded DIR's FixFileUtils.CheckDeleteFile(fixFile); } outDir.ChildAdd(toSortRom); return(ReturnCode.Good); }
private static ReturnCode FixDir(RvFile dir, bool lastSelected, List <RvFile> fileProcessQueue, ref int totalFixed, ref int reportedFixed, Stopwatch cacheSaveTimer) { //Debug.WriteLine(dir.FullName); bool thisSelected = lastSelected; if (dir.Tree != null) { thisSelected = dir.Tree.Checked == RvTreeRow.TreeSelect.Selected; } List <RvFile> lstToProcess = new List <RvFile>(); for (int j = 0; j < dir.ChildCount; j++) { lstToProcess.Add(dir.Child(j)); } foreach (RvFile child in lstToProcess) { ReturnCode returnCode = FixBase(child, thisSelected, fileProcessQueue, ref totalFixed, ref reportedFixed, cacheSaveTimer); if (returnCode != ReturnCode.Good) { return(returnCode); } while (fileProcessQueue.Any()) { returnCode = FixBase(fileProcessQueue[0], true, fileProcessQueue, ref totalFixed, ref reportedFixed, cacheSaveTimer); if (returnCode != ReturnCode.Good) { return(returnCode); } fileProcessQueue.RemoveAt(0); if (Report.CancellationPending()) { break; } } if (totalFixed != reportedFixed) { Report.ReportProgress(new bgwProgress(totalFixed)); reportedFixed = totalFixed; } if (Report.CancellationPending()) { break; } } // here we check to see if the directory we just scanned should be deleted FixFileUtils.CheckDeleteFile(dir); return(ReturnCode.Good); }
private static ReturnCode FixFileMoveToCorrupt(RvFile fixFile, out string errorMessage) { string corruptDir = Path.Combine(DB.ToSort(), "Corrupt"); if (!Directory.Exists(corruptDir)) { Directory.CreateDirectory(corruptDir); } string fixFileFullName = fixFile.FullName; string toSortCorruptFullName = Path.Combine(corruptDir, fixFile.Name); string toSortCorruptFileName = fixFile.Name; int fileC = 0; while (File.Exists(toSortCorruptFullName)) { fileC++; toSortCorruptFileName = fixFile.Name + fileC; toSortCorruptFullName = Path.Combine(corruptDir, toSortCorruptFileName); } //create new tosort record // FileInfo toSortCorruptFile = new FileInfo(toSortCorruptFullName); RvFile toSortCorruptRom = new RvFile(FileType.File) { Name = toSortCorruptFileName, Size = fixFile.Size, CRC = fixFile.CRC, //TimeStamp = toSortFile.LastWriteTime, DatStatus = DatStatus.InToSort }; Report.ReportProgress(new bgwShowFix(Path.GetDirectoryName(fixFileFullName), "", Path.GetFileName(fixFileFullName), fixFile.Size, "-->", "Corrupt", "", fixFile.Name)); ReturnCode returnCode = FixFileUtils.MoveFile(fixFile, toSortCorruptRom, toSortCorruptFullName, out bool fileMoved, out errorMessage); if (returnCode != ReturnCode.Good) { return(returnCode); } if (!fileMoved) { returnCode = FixFileUtils.CopyFile(fixFile, null, toSortCorruptFullName, toSortCorruptRom, false, out errorMessage); if (returnCode != ReturnCode.Good) { return(returnCode); } string fixFilePath = fixFile.FullName; if (!File.SetAttributes(fixFilePath, FileAttributes.Normal)) { int error = Error.GetLastError(); Report.ReportProgress(new bgwShowError(fixFilePath, "Error Setting File Attributes to Normal. Before Delete Moving ToSort. Code " + error)); } File.Delete(fixFilePath); // here we just deleted a file so also delete it from the DB, // and recurse up deleting unnedded DIR's FixFileUtils.CheckDeleteFile(fixFile); } RvFile toSort = DB.RvFileToSort(); RvFile rvCorruptDir = new RvFile(FileType.Dir) { Name = "Corrupt", DatStatus = DatStatus.InToSort }; int found = toSort.ChildNameSearch(rvCorruptDir, out int indexCorrupt); if (found != 0) { rvCorruptDir.GotStatus = GotStatus.Got; indexCorrupt = toSort.ChildAdd(rvCorruptDir); } toSort.Child(indexCorrupt).ChildAdd(toSortCorruptRom); errorMessage = ""; return(ReturnCode.Good); }
public static ReturnCode MoveZipToCorrupt(RvFile fixZip, out string errorMessage) { errorMessage = ""; string fixZipFullName = fixZip.FullName; if (!File.Exists(fixZipFullName)) { errorMessage = "File for move to corrupt not found " + fixZip.FullName; return(ReturnCode.RescanNeeded); } FileInfo fi = new FileInfo(fixZipFullName); if (fi.LastWriteTime != fixZip.FileModTimeStamp) { errorMessage = "File for move to corrupt timestamp not correct " + fixZip.FullName; return(ReturnCode.RescanNeeded); } string corruptDir = Path.Combine(DB.ToSort(), "Corrupt"); if (!Directory.Exists(corruptDir)) { Directory.CreateDirectory(corruptDir); } RvFile toSort = DB.RvFileToSort(); RvFile corruptDirNew = new RvFile(FileType.Dir) { Name = "Corrupt", DatStatus = DatStatus.InToSort }; int found = toSort.ChildNameSearch(corruptDirNew, out int indexcorrupt); if (found != 0) { corruptDirNew.GotStatus = GotStatus.Got; indexcorrupt = toSort.ChildAdd(corruptDirNew); } string toSortFullName = Path.Combine(corruptDir, fixZip.Name); string toSortFileName = fixZip.Name; int fileC = 0; while (File.Exists(toSortFullName)) { fileC++; string fName = Path.GetFileNameWithoutExtension(fixZip.Name); string fExt = Path.GetExtension(fixZip.Name); toSortFullName = Path.Combine(corruptDir, fName + fileC + fExt); toSortFileName = fixZip.Name + fileC; } if (!File.SetAttributes(fixZipFullName, FileAttributes.Normal)) { int error = Error.GetLastError(); Report.ReportProgress(new bgwShowError(fixZipFullName, "Error Setting File Attributes to Normal. Before Moving To Corrupt. Code " + error)); } File.Move(fixZipFullName, toSortFullName); FileInfo toSortCorruptFile = new FileInfo(toSortFullName); RvFile toSortCorruptGame = new RvFile(FileType.Zip) { Name = toSortFileName, DatStatus = DatStatus.InToSort, FileModTimeStamp = toSortCorruptFile.LastWriteTime, GotStatus = GotStatus.Corrupt }; toSort.Child(indexcorrupt).ChildAdd(toSortCorruptGame); FixFileUtils.CheckDeleteFile(fixZip); return(ReturnCode.Good); }
public static ReturnCode FixZip(RvFile fixZip, List <RvFile> fileProcessQueue, ref int totalFixed, out string errorMessage) { errorMessage = ""; //Check for error status if (fixZip.DirStatus.HasUnknown()) { return(ReturnCode.FindFixes); // Error } bool needsTrrntzipped = fixZip.ZipStatus != ZipStatus.TrrntZip && fixZip.GotStatus == GotStatus.Got && fixZip.DatStatus == DatStatus.InDatCollect && (Settings.rvSettings.FixLevel == EFixLevel.TrrntZipLevel1 || Settings.rvSettings.FixLevel == EFixLevel.TrrntZipLevel2 || Settings.rvSettings.FixLevel == EFixLevel.TrrntZipLevel3); // file corrupt and not in tosort // if file cannot be fully fixed copy to corrupt // process zipfile if (fixZip.GotStatus == GotStatus.Corrupt && fixZip.DatStatus != DatStatus.InToSort && !fixZip.DirStatus.HasFixable()) { ReturnCode moveReturnCode = FixAZipFunctions.MoveZipToCorrupt(fixZip, out errorMessage); if (moveReturnCode != ReturnCode.Good) { return(moveReturnCode); } } // has fixable // process zipfile else if (fixZip.DirStatus.HasFixable()) { // do nothing here but continue on to process zip. } // need trrntzipped // process zipfile else if (needsTrrntzipped) { // rv7Zip format is not finalized yet so do not use if (!Settings.rvSettings.ConvertToRV7Z && (fixZip.FileType == FileType.SevenZip)) //if (fixZip.FileType == FileType.SevenZip) { needsTrrntzipped = false; } // do nothing here but continue on to process zip. } // got empty zip that should be deleted // process zipfile else if (fixZip.GotStatus == GotStatus.Got && fixZip.GotStatus != GotStatus.Corrupt && !fixZip.DirStatus.HasAnyFiles()) { // do nothing here but continue on to process zip. } // else // skip this zipfile else { // nothing can be done to return return(ReturnCode.Good); } if (!fixZip.DirStatus.HasFixable() && !needsTrrntzipped) { return(ReturnCode.Good); } string fixZipFullName = fixZip.TreeFullName; if (!fixZip.DirStatus.HasFixable() && needsTrrntzipped) { Report.ReportProgress(new bgwShowFix(Path.GetDirectoryName(fixZipFullName), Path.GetFileName(fixZipFullName), "", 0, "TrrntZipping", "", "", "")); } FixFileUtils.CheckCreateDirectories(fixZip.Parent); string filename = fixZip.FullName; if (fixZip.GotStatus == GotStatus.NotGot) { if (File.Exists(filename)) { errorMessage = "Unexpected file found in directory. Rescan needed.\n" + filename; return(ReturnCode.RescanNeeded); } } ReportError.LogOut(""); ReportError.LogOut(fixZipFullName + " : " + fixZip.RepStatus); ReportError.LogOut("------------------------------------------------------------"); Debug.WriteLine(fixZipFullName + " : " + fixZip.RepStatus); ReportError.LogOut("Zip File Status Before Fix:"); for (int intLoop = 0; intLoop < fixZip.ChildCount; intLoop++) { ReportError.LogOut(fixZip.Child(intLoop)); } ReportError.LogOut(""); ReturnCode returnCode = ReturnCode.Good; RepStatus fileRepStatus = RepStatus.UnSet; ICompress tempFixZip = null; ICompress toSortCorruptOut = null; ICompress toSortZipOut = null; try { RvFile toSortGame = null; RvFile toSortCorruptGame = null; List <RvFile> fixZipTemp = new List <RvFile>(); FileType fixFileType = fixZip.FileType; for (int iRom = 0; iRom < fixZip.ChildCount; iRom++) { RvFile fixZippedFile = new RvFile(DBTypeGet.FileFromDir(fixFileType)); fixZip.Child(iRom).CopyTo(fixZippedFile); fixZipTemp.Add(fixZippedFile); ReportError.LogOut(fixZippedFile.RepStatus + " : " + fixZip.Child(iRom).FullName); fileRepStatus = fixZippedFile.RepStatus; switch (fixZippedFile.RepStatus) { #region Nothing to copy // any file we do not have or do not want in the destination zip case RepStatus.Missing: case RepStatus.NotCollected: case RepStatus.Rename: case RepStatus.Delete: if (! ( // got the file in the original zip but will be deleting it fixZippedFile.DatStatus == DatStatus.NotInDat && fixZippedFile.GotStatus == GotStatus.Got || fixZippedFile.DatStatus == DatStatus.NotInDat && fixZippedFile.GotStatus == GotStatus.Corrupt || fixZippedFile.DatStatus == DatStatus.InDatMerged && fixZippedFile.GotStatus == GotStatus.Got || fixZippedFile.DatStatus == DatStatus.InToSort && fixZippedFile.GotStatus == GotStatus.Got || fixZippedFile.DatStatus == DatStatus.InToSort && fixZippedFile.GotStatus == GotStatus.Corrupt || // do not have this file and cannot fix it here fixZippedFile.DatStatus == DatStatus.InDatCollect && fixZippedFile.GotStatus == GotStatus.NotGot || fixZippedFile.DatStatus == DatStatus.InDatBad && fixZippedFile.GotStatus == GotStatus.NotGot || fixZippedFile.DatStatus == DatStatus.InDatMerged && fixZippedFile.GotStatus == GotStatus.NotGot ) ) { ReportError.SendAndShow("Error in Fix Rom Status " + fixZippedFile.RepStatus + " : " + fixZippedFile.DatStatus + " : " + fixZippedFile.GotStatus); } if (fixZippedFile.RepStatus == RepStatus.Delete) { if (Settings.rvSettings.DetailedFixReporting) { Report.ReportProgress(new bgwShowFix(Path.GetDirectoryName(fixZipFullName), Path.GetFileName(fixZipFullName), fixZippedFile.Name, fixZippedFile.Size, "Delete", "", "", "")); } returnCode = FixFileUtils.DoubleCheckDelete(fixZippedFile, out errorMessage); if (returnCode != ReturnCode.Good) { CloseZipFile(ref tempFixZip); CloseToSortGame(toSortGame, ref toSortZipOut); CloseToSortCorruptGame(toSortGame, ref toSortZipOut); return(returnCode); } } fixZippedFile.GotStatus = GotStatus.NotGot; break; #endregion // any files we are just moving from the original zip to the destination zip case RepStatus.Correct: case RepStatus.InToSort: case RepStatus.NeededForFix: case RepStatus.Corrupt: { returnCode = FixAZipFunctions.CorrectZipFile(fixZip, fixZippedFile, ref tempFixZip, iRom, fileProcessQueue, out errorMessage); if (returnCode != ReturnCode.Good) { CloseZipFile(ref tempFixZip); CloseToSortGame(toSortGame, ref toSortZipOut); CloseToSortCorruptGame(toSortGame, ref toSortZipOut); return(returnCode); } break; } case RepStatus.CanBeFixed: case RepStatus.CorruptCanBeFixed: { returnCode = FixAZipFunctions.CanBeFixed(fixZip, fixZippedFile, ref tempFixZip, fileProcessQueue, ref totalFixed, out errorMessage); if (returnCode != ReturnCode.Good) { CloseZipFile(ref tempFixZip); CloseToSortGame(toSortGame, ref toSortZipOut); CloseToSortCorruptGame(toSortGame, ref toSortZipOut); return(returnCode); } break; } case RepStatus.MoveToSort: { returnCode = FixAZipFunctions.MovetoSort(fixZip, fixZippedFile, ref toSortGame, ref toSortZipOut, iRom); if (returnCode != ReturnCode.Good) { CloseZipFile(ref tempFixZip); CloseToSortGame(toSortGame, ref toSortZipOut); CloseToSortCorruptGame(toSortGame, ref toSortZipOut); return(returnCode); } break; } case RepStatus.MoveToCorrupt: FixAZipFunctions.MoveToCorrupt(fixZip, fixZippedFile, ref toSortCorruptGame, ref toSortCorruptOut, iRom); break; default: ReportError.UnhandledExceptionHandler( "Unknown file status found " + fixZippedFile.RepStatus + " while fixing file " + fixZip.Name + " Dat Status = " + fixZippedFile.DatStatus + " GotStatus " + fixZippedFile.GotStatus); break; } } //if ToSort Zip Made then close the zip and add this new zip to the Database CloseToSortGame(toSortGame, ref toSortZipOut); //if Corrupt Zip Made then close the zip and add this new zip to the Database CloseToSortCorruptGame(toSortCorruptGame, ref toSortCorruptOut); #region Process original Zip if (File.Exists(filename)) { if (!File.SetAttributes(filename, FileAttributes.Normal)) { int error = Error.GetLastError(); Report.ReportProgress(new bgwShowError(filename, "Error Setting File Attributes to Normal. Deleting Original Fix File. Code " + error)); } try { File.Delete(filename); } catch (Exception e) { errorMessage = "Error While trying to delete file " + filename + ". " + e.Message; if (tempFixZip != null && tempFixZip.ZipOpen != ZipOpenType.Closed) { tempFixZip.ZipFileClose(); tempFixZip = null; } return(ReturnCode.RescanNeeded); } } #endregion bool checkDelete = false; #region process the temp Zip rename it to the original Zip if (tempFixZip != null && tempFixZip.ZipOpen != ZipOpenType.Closed) { string tempFilename = tempFixZip.ZipFilename; tempFixZip.ZipFileClose(); if (tempFixZip.LocalFilesCount() > 0) { // now rename the temp fix file to the correct filename File.Move(tempFilename, filename); FileInfo nFile = new FileInfo(filename); RvFile tmpZip = new RvFile(FileType.Zip) { Name = Path.GetFileName(filename), FileModTimeStamp = nFile.LastWriteTime }; tmpZip.SetStatus(fixZip.DatStatus, GotStatus.Got); fixZip.FileAdd(tmpZip, false); fixZip.ZipStatus = tempFixZip.ZipStatus; } else { File.Delete(tempFilename); checkDelete = true; } tempFixZip = null; } else { checkDelete = true; } #endregion #region Now put the New Game Status information into the Database. int intLoopFix = 0; foreach (RvFile tmpZip in fixZipTemp) { tmpZip.CopyTo(fixZip.Child(intLoopFix)); if (fixZip.Child(intLoopFix).RepStatus == RepStatus.Deleted) { if (fixZip.Child(intLoopFix).FileRemove() == EFile.Delete) { fixZip.ChildRemove(intLoopFix); continue; } } intLoopFix++; } #endregion if (checkDelete) { FixFileUtils.CheckDeleteFile(fixZip); } ReportError.LogOut(""); ReportError.LogOut("Zip File Status After Fix:"); for (int intLoop = 0; intLoop < fixZip.ChildCount; intLoop++) { ReportError.LogOut(fixZip.Child(intLoop)); } ReportError.LogOut(""); return(ReturnCode.Good); } catch (ZipFileException ex) { tempFixZip?.ZipFileCloseFailed(); toSortZipOut?.ZipFileCloseFailed(); toSortCorruptOut?.ZipFileCloseFailed(); tempFixZip = null; toSortZipOut = null; toSortCorruptOut = null; errorMessage = ex.Message; return(ex.returnCode); } catch (Exception ex) { tempFixZip?.ZipFileCloseFailed(); toSortZipOut?.ZipFileCloseFailed(); toSortCorruptOut?.ZipFileCloseFailed(); tempFixZip = null; toSortZipOut = null; toSortCorruptOut = null; errorMessage = ex.Message; return(ReturnCode.LogicError); } finally { if (tempFixZip != null) { ReportError.UnhandledExceptionHandler($"{tempFixZip.ZipFilename} tempZipOut was left open, ZipFile= {fixZipFullName} , fileRepStatus= {fileRepStatus} , returnCode= {returnCode}"); } if (toSortZipOut != null) { ReportError.UnhandledExceptionHandler($"{toSortZipOut.ZipFilename} toSortZipOut was left open"); } if (toSortCorruptOut != null) { ReportError.UnhandledExceptionHandler($"{toSortCorruptOut.ZipFilename} toSortCorruptOut was left open"); } } }