public static RvFile FromAZipFile(RvFile dbDir, EScanLevel eScanLevel, ThreadWorker thWrk) { RvFile fileDir = new RvFile(dbDir.FileType); DatStatus chechingDatStatus = dbDir.IsInToSort ? DatStatus.InToSort : DatStatus.NotInDat; string filename = dbDir.FullNameCase; ICompress checkZ = dbDir.FileType == FileType.Zip ? new Zip() : (ICompress) new SevenZ(); ZipReturn zr = checkZ.ZipFileOpen(filename, dbDir.FileModTimeStamp); if (zr == ZipReturn.ZipGood) { dbDir.ZipStatus = checkZ.ZipStatus; // to be Scanning a ZIP file means it is either new or has changed. // as the code below only calls back here if that is true. // // Level1: Only use header CRC's // Just get the CRC for the ZIP headers. // // Level2: Fully checksum changed only files // We know this file has been changed to do a full checksum scan. // // Level3: Fully checksum everything // So do a full checksum scan. if (_fs == null) { _fs = new FileScan(); } List <FileScan.FileResults> fr = _fs.Scan(checkZ, false, eScanLevel == EScanLevel.Level2 || eScanLevel == EScanLevel.Level3); // add all of the file information from the zip file into scanDir for (int i = 0; i < checkZ.LocalFilesCount(); i++) { RvFile tFile = new RvFile(DBTypeGet.FileFromDir(dbDir.FileType)) { Name = checkZ.Filename(i), ZipFileIndex = i, ZipFileHeaderPosition = checkZ.LocalHeader(i), Size = checkZ.UncompressedSize(i), CRC = checkZ.CRC32(i), FileModTimeStamp = checkZ.LastModified(i) }; // all levels read the CRC from the ZIP header tFile.SetStatus(chechingDatStatus, GotStatus.Got); tFile.FileStatusSet(FileStatus.SizeFromHeader | FileStatus.CRCFromHeader); if (fr[i].FileStatus != ZipReturn.ZipGood) { thWrk.Report(new bgwShowCorrupt(fr[i].FileStatus, filename + " : " + checkZ.Filename(i))); tFile.GotStatus = GotStatus.Corrupt; } else { tFile.HeaderFileType = fr[i].HeaderFileType; tFile.SHA1 = fr[i].SHA1; tFile.MD5 = fr[i].MD5; tFile.AltSize = fr[i].AltSize; tFile.AltCRC = fr[i].AltCRC; tFile.AltSHA1 = fr[i].AltSHA1; tFile.AltMD5 = fr[i].AltMD5; tFile.FileStatusSet( FileStatus.SizeVerified | (fr[i].HeaderFileType != HeaderFileType.Nothing ? FileStatus.HeaderFileTypeFromHeader : 0) | (fr[i].CRC != null ? FileStatus.CRCVerified : 0) | (fr[i].SHA1 != null ? FileStatus.SHA1Verified : 0) | (fr[i].MD5 != null ? FileStatus.MD5Verified : 0) | (fr[i].AltSize != null ? FileStatus.AltSizeVerified : 0) | (fr[i].AltCRC != null ? FileStatus.AltCRCVerified : 0) | (fr[i].AltSHA1 != null ? FileStatus.AltSHA1Verified : 0) | (fr[i].AltMD5 != null ? FileStatus.AltMD5Verified : 0) ); } fileDir.ChildAdd(tFile); } } else if (zr == ZipReturn.ZipFileLocked) { thWrk.Report(new bgwShowError(filename, "Zip File Locked")); dbDir.FileModTimeStamp = 0; dbDir.GotStatus = GotStatus.FileLocked; } else { thWrk.Report(new bgwShowCorrupt(zr, filename)); dbDir.GotStatus = GotStatus.Corrupt; } checkZ.ZipFileClose(); return(fileDir); }
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.ConvertToTrrntzip; // 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); } } if (Settings.rvSettings.DebugLogsEnabled) { ReportError.LogOut(""); ReportError.LogOut(fixZipFullName + " : " + fixZip.RepStatus); ReportError.LogOut("------------------------------------------------------------"); Debug.WriteLine(fixZipFullName + " : " + fixZip.RepStatus); ReportError.LogOut("Zip File Status Before Fix:"); int deleteCount = 0; int moveToSortCount = 0; int notThereCount = 0; for (int intLoop = 0; intLoop < fixZip.ChildCount; intLoop++) { var fixZipFile = fixZip.Child(intLoop); ReportError.LogOut(fixZipFile); if (fixZipFile.RepStatus == RepStatus.MoveToSort || fixZipFile.RepStatus == RepStatus.MoveToCorrupt) { moveToSortCount++; } if (fixZipFile.RepStatus == RepStatus.Delete) { deleteCount++; } if (fixZipFile.RepStatus == RepStatus.Missing || fixZip.RepStatus == RepStatus.NotCollected) { notThereCount++; } } ReportError.LogOut($"MoveToSortCount {moveToSortCount} , DeleteCount {deleteCount} , NotThereCount {notThereCount}"); 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; Dictionary <string, RvFile> filesUsedForFix = new Dictionary <string, RvFile>(); 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(fixZip.Child(iRom), 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 = FixAZipCorrectZipFile.CorrectZipFile(fixZip, fixZippedFile, ref tempFixZip, iRom, filesUsedForFix, 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 = FixAZipCanBeFixed.CanBeFixed(fixZip, fixZippedFile, ref tempFixZip, filesUsedForFix, 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 = FixAZipMoveToSort.MovetoSort(fixZip, fixZippedFile, ref toSortGame, ref toSortZipOut, iRom, filesUsedForFix); if (returnCode != ReturnCode.Good) { CloseZipFile(ref tempFixZip); CloseToSortGame(toSortGame, ref toSortZipOut); CloseToSortCorruptGame(toSortGame, ref toSortZipOut); return(returnCode); } break; } case RepStatus.MoveToCorrupt: returnCode = FixAZipFunctions.MoveToCorrupt(fixZip, fixZippedFile, ref toSortCorruptGame, ref toSortCorruptOut, iRom); if (returnCode != ReturnCode.Good) { CloseZipFile(ref tempFixZip); CloseToSortGame(toSortGame, ref toSortZipOut); CloseToSortCorruptGame(toSortGame, ref toSortZipOut); return(returnCode); } break; default: ReportError.UnhandledExceptionHandler($"Unknown file status found {fixZippedFile.RepStatus} while fixing file {fixZip.Name} Dat Status = {fixZippedFile.DatStatus} GotStatus {fixZippedFile.GotStatus}"); break; } if (Report.CancellationPending()) { tempFixZip?.ZipFileCloseFailed(); toSortZipOut?.ZipFileCloseFailed(); toSortCorruptOut?.ZipFileCloseFailed(); tempFixZip = null; toSortZipOut = null; toSortCorruptOut = null; errorMessage = "Cancel"; return(ReturnCode.Cancel); } } //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 List <RvFile> usedFiles = filesUsedForFix.Values.ToList(); FixFileUtils.CheckFilesUsedForFix(usedFiles, fileProcessQueue, false); 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"); } } }
private static void CheckADir(RvDir dbDir, bool report) { if (_cacheSaveTimer.Elapsed.TotalMinutes > Program.rvSettings.CacheSaveTimePeriod) { _bgw.ReportProgress(0, "Saving Cache"); DB.Write(); _bgw.ReportProgress(0, "Saving Cache Complete"); _cacheSaveTimer.Reset(); _cacheSaveTimer.Start(); } string fullDir = dbDir.FullName; if (report) { _bgw.ReportProgress(0, new bgwText2(fullDir)); } DatStatus chechingDatStatus = dbDir.IsInToSort ? DatStatus.InToSort : DatStatus.NotInDat; // this is a temporary rvDir structure to store the data about the actual directory/files we are scanning // we will first populate this variable with the real file data from the directory, and then compare it // with the data in dbDir. RvDir fileDir = null; Debug.WriteLine(fullDir); FileType ft = dbDir.FileType; #region "Populate fileDir" // if we are scanning a ZIP file then populate scanDir from the ZIP file switch (ft) { case FileType.Zip: case FileType.SevenZip: { fileDir = new RvDir(ft); // open the zip file ICompress checkZ; if (ft == FileType.Zip) { checkZ = new ZipFile(); } else { checkZ = new SevenZ(); } ZipReturn zr = checkZ.ZipFileOpen(fullDir, dbDir.TimeStamp, true); if (zr == ZipReturn.ZipGood) { dbDir.ZipStatus = checkZ.ZipStatus; // to be Scanning a ZIP file means it is either new or has changed. // as the code below only calls back here if that is true. // // Level1: Only use header CRC's // Just get the CRC for the ZIP headers. // // Level2: Fully checksum changed only files // We know this file has been changed to do a full checksum scan. // // Level3: Fully checksum everything // So do a full checksum scan. if ((EScanLevel == eScanLevel.Level2) || (EScanLevel == eScanLevel.Level3)) { checkZ.DeepScan(); } // add all of the file information from the zip file into scanDir for (int i = 0; i < checkZ.LocalFilesCount(); i++) { RvFile tFile = new RvFile(DBTypeGet.FileFromDir(ft)) { Name = checkZ.Filename(i), ZipFileIndex = i, ZipFileHeaderPosition = checkZ.LocalHeader(i), Size = checkZ.UncompressedSize(i), CRC = checkZ.CRC32(i) }; // all 3 levels read the CRC from the ZIP header tFile.SetStatus(chechingDatStatus, GotStatus.Got); tFile.FileStatusSet(FileStatus.SizeFromHeader | FileStatus.CRCFromHeader); // if we are in level 2 or level 3 then do a full CheckSum Scan. if ((EScanLevel == eScanLevel.Level2) || (EScanLevel == eScanLevel.Level3)) { // DeepScan will return ZipReturn.ZipCRCDecodeError if the headers CRC and // the actual CRC do not match. // So we just need to set the MD5 and SHA1 from the ZIP file. zr = checkZ.FileStatus(i); if (zr == ZipReturn.ZipUntested) { _bgw.ReportProgress(0, new bgwShowCorrupt(zr, fullDir + " : " + checkZ.Filename(i))); } else if (zr != ZipReturn.ZipGood) { _bgw.ReportProgress(0, new bgwShowCorrupt(zr, fullDir + " : " + checkZ.Filename(i))); tFile.GotStatus = GotStatus.Corrupt; } else { tFile.MD5 = checkZ.MD5(i); tFile.SHA1 = checkZ.SHA1(i); tFile.FileStatusSet(FileStatus.SizeVerified | FileStatus.CRCVerified | FileStatus.SHA1Verified | FileStatus.MD5Verified); } } fileDir.ChildAdd(tFile); } } else if (zr == ZipReturn.ZipFileLocked) { _bgw.ReportProgress(0, new bgwShowError(fullDir, "Zip File Locked")); dbDir.TimeStamp = 0; dbDir.GotStatus = GotStatus.FileLocked; } else { _bgw.ReportProgress(0, new bgwShowCorrupt(zr, fullDir)); dbDir.GotStatus = GotStatus.Corrupt; } checkZ.ZipFileClose(); } break; case FileType.Dir: { fileDir = new RvDir(FileType.Dir); DirectoryInfo oDir = new DirectoryInfo(fullDir); DirectoryInfo[] oDirs = oDir.GetDirectories(); FileInfo[] oFiles = oDir.GetFiles(); // add all the subdirectories into scanDir foreach (DirectoryInfo dir in oDirs) { RvBase tDir = new RvDir(FileType.Dir) { Name = dir.Name, TimeStamp = dir.LastWriteTime }; tDir.SetStatus(chechingDatStatus, GotStatus.Got); fileDir.ChildAdd(tDir); } // add all the files into scanDir foreach (FileInfo oFile in oFiles) { // if we find any zip files add them as zip files. string fExt = Path.GetExtension(oFile.Name); switch (fExt.ToLower()) { case ".7z": { RvDir tGame = new RvDir(FileType.SevenZip) { Name = Path.GetFileNameWithoutExtension(oFile.Name), TimeStamp = oFile.LastWriteTime }; tGame.SetStatus(chechingDatStatus, GotStatus.Got); fileDir.ChildAdd(tGame); } break; case ".zip": { RvDir tGame = new RvDir(FileType.Zip) { Name = Path.GetFileNameWithoutExtension(oFile.Name), TimeStamp = oFile.LastWriteTime }; tGame.SetStatus(chechingDatStatus, GotStatus.Got); fileDir.ChildAdd(tGame); } break; default: { string fName = oFile.Name; if (fName == "__RomVault.tmp") { File.Delete(oFile.FullName); continue; } // Scanning a file // // Level1 & 2 : (are the same for files) Fully checksum changed only files // Here we are just getting the TimeStamp of the File, and later // if the TimeStamp was not matched we will have to read the files CRC, MD5 & SHA1 // // Level3: Fully checksum everything // Get everything about the file right here so // read CRC, MD5 & SHA1 // add all the files in the sub-directory to scanDir RvFile tFile = new RvFile(FileType.File) { Name = fName, Size = (ulong)oFile.Length, TimeStamp = oFile.LastWriteTime }; tFile.FileStatusSet(FileStatus.SizeVerified); int errorCode = CHD.CheckFile(oFile, out tFile.SHA1CHD, out tFile.MD5CHD, out tFile.CHDVersion); if (errorCode == 0) { if (tFile.SHA1CHD != null) { tFile.FileStatusSet(FileStatus.SHA1CHDFromHeader); } if (tFile.MD5CHD != null) { tFile.FileStatusSet(FileStatus.MD5CHDFromHeader); } tFile.SetStatus(chechingDatStatus, GotStatus.Got); // if we are scanning at Level3 then we get all the info here if (EScanLevel == eScanLevel.Level3) { DeepScanFile(fullDir, tFile); ChdManCheck(fullDir, tFile); } } else if (errorCode == 32) { tFile.GotStatus = GotStatus.FileLocked; _bgw.ReportProgress(0, new bgwShowError(fullDir, "File Locked")); } else { string filename = Path.Combine(fullDir, tFile.Name); ReportError.Show("File: " + filename + " Error: " + new Win32Exception(errorCode).Message + ". Scan Aborted."); _fileErrorAbort = true; return; } fileDir.ChildAdd(tFile); } break; } } } break; default: ReportError.SendAndShow("Un supported file type in CheckADir " + ft); break; } #endregion if (fileDir == null) { ReportError.SendAndShow("Unknown Reading File Type in Dir Scanner"); return; } if (report) { _bgw.ReportProgress(0, new bgwSetRange2(fileDir.ChildCount - 1)); _bgw.ReportProgress(0, new bgwRange2Visible(true)); } if (!DBTypeGet.isCompressedDir(ft) && _bgw.CancellationPending) { return; } Compare(dbDir, fileDir, report, true); }