Esempio n. 1
0
        public static void Write()
        {
            string tname = Settings.rvSettings.CacheFile + "_tmp";

            if (File.Exists(tname))
            {
                File.Delete(tname);

                while (File.Exists(tname))
                {
                    Thread.Sleep(50);
                }
            }

            try
            {
                using (FileStream fs = new FileStream(tname, FileMode.CreateNew, FileAccess.Write))
                {
                    using (BinaryWriter bw = new BinaryWriter(fs, Encoding.UTF8, true))
                    {
                        DBVersion.VersionNow = DBVersion.Version;
                        bw.Write(DBVersion.Version);
                        DirRoot.Write(bw);

                        bw.Write(EndCacheMarker);

                        bw.Flush();
                        bw.Close();
                    }

                    fs.Close();
                }
            }
            catch (Exception e)
            {
                ReportError.UnhandledExceptionHandler($"Error Writing Cache File, your cache is now out of date, fix this error and rescan: {e.Message}");
                return;
            }

            if (File.Exists(Settings.rvSettings.CacheFile))
            {
                string bname = Settings.rvSettings.CacheFile + "Backup";
                if (File.Exists(bname))
                {
                    File.Delete(bname);

                    while (File.Exists(bname))
                    {
                        Thread.Sleep(50);
                    }
                }
                File.Move(Settings.rvSettings.CacheFile, bname);
                while (File.Exists(Settings.rvSettings.CacheFile))
                {
                    Thread.Sleep(50);
                }
            }

            File.Move(tname, Settings.rvSettings.CacheFile);
        }
Esempio n. 2
0
        public static void Read(object sender, DoWorkEventArgs e)
        {
            Bgw = sender as BackgroundWorker;
            Program.SyncCont = e.Argument as SynchronizationContext;

            if (!File.Exists(Settings.CacheFile))
            {
                OpenDefaultDB();
                Bgw = null;
                Program.SyncCont = null;
                return;
            }
            DirTree = new RvDir(FileType.Dir);
            FileStream fs = new FileStream(Settings.CacheFile, FileMode.Open, FileAccess.Read);

            if (fs.Length < 4)
            {
                ReportError.UnhandledExceptionHandler("Cache is Corrupt, revert to Backup.");
            }


            BinaryReader br = new BinaryReader(fs);

            if (Bgw != null)
            {
                Bgw.ReportProgress(0, new bgwSetRange((int)fs.Length));
            }

            DBVersion.VersionNow = br.ReadInt32();

            if (DBVersion.VersionNow != DBVersion.Version)
            {
                ReportError.Show(Resources.DB_Read_Data_Cache_version_is_out_of_date_you_should_now_rescan_your_dat_directory_and_roms_directory_);
                OpenDefaultDB();
            }
            else
            {
                DirTree.Read(br, null);
            }

            if (fs.Position > fs.Length - 8)
            {
                ReportError.UnhandledExceptionHandler("Cache is Corrupt, revert to Backup.");
            }

            ulong testEOF = br.ReadUInt64();

            if (testEOF != EndCacheMarker)
            {
                ReportError.UnhandledExceptionHandler("Cache is Corrupt, revert to Backup.");
            }

            br.Close();
            fs.Close();
            fs.Dispose();

            Bgw = null;
            Program.SyncCont = null;
        }
Esempio n. 3
0
        public static void Read(ThreadWorker thWrk)
        {
            ThWrk = thWrk;
            if (!File.Exists(Settings.rvSettings.CacheFile))
            {
                OpenDefaultDB();
                ThWrk = null;
                return;
            }
            DirRoot = new RvFile(FileType.Dir);
            using (FileStream fs = new FileStream(Settings.rvSettings.CacheFile, FileMode.Open, FileAccess.Read))
            {
                if (fs.Length < 4)
                {
                    ReportError.UnhandledExceptionHandler("Cache is Corrupt, revert to Backup.");
                }

                using (BinaryReader br = new BinaryReader(fs, Encoding.UTF8, true))
                {
                    DivideProgress = fs.Length / 1000;

                    DivideProgress = DivideProgress == 0 ? 1 : DivideProgress;

                    ThWrk?.Report(new bgwSetRange(1000));

                    DBVersion.VersionNow = br.ReadInt32();

                    if (DBVersion.VersionNow != DBVersion.Version)
                    {
                        ReportError.Show(
                            "Data Cache version is out of date you should now rescan your dat directory and roms directory.");
                        br.Close();
                        fs.Close();
                        fs.Dispose();

                        OpenDefaultDB();
                        ThWrk = null;
                        return;
                    }
                    else
                    {
                        DirRoot.Read(br, null);
                    }

                    if (fs.Position > fs.Length - 8)
                    {
                        ReportError.UnhandledExceptionHandler("Cache is Corrupt, revert to Backup.");
                    }

                    ulong testEOF = br.ReadUInt64();
                    if (testEOF != EndCacheMarker)
                    {
                        ReportError.UnhandledExceptionHandler("Cache is Corrupt, revert to Backup.");
                    }
                }
            }

            ThWrk = null;
        }
Esempio n. 4
0
        public static void CheckDeleteFile(RvFile file)
        {
            if (file.RepStatus == RepStatus.Deleted)
            {
                return;
            }

            // look at the directories childrens status's to figure out if the directory should be deleted.
            if (file.FileType == FileType.Dir)
            {
                RvFile tDir = file;
                if (!tDir.IsDir || tDir.ChildCount != 0)
                {
                    return;
                }
                // check if we are at the root of the tree so that we do not delete RomRoot and ToSort
                if (tDir.Parent == DB.DirTree)
                {
                    return;
                }

                string fullPath = tDir.FullName;
                try
                {
                    if (Directory.Exists(fullPath))
                    {
                        Directory.Delete(fullPath);
                    }
                }
                catch (Exception e)
                {
                    //need to report this to an error window
                    Debug.WriteLine(e.ToString());
                }
            }

            // else check if this file should be removed from the DB
            if (file.FileRemove() == EFile.Delete)
            {
                RvFile parent = file.Parent;

                if (parent == null)
                {
                    ReportError.UnhandledExceptionHandler("Item being deleted had no parent " + file.FullName);
                    return; // this never happens as UnhandledException Terminates the program
                }

                if (!parent.FindChild(file, out int index))
                {
                    ReportError.UnhandledExceptionHandler("Could not find self in delete code " + parent.FullName);
                }
                parent.ChildRemove(index);
                CheckDeleteFile(parent);
            }
        }
Esempio n. 5
0
        public static void ChdManCheck(RvFile tFile, string directory, ThreadWorker thWrk, ref bool fileErrorAbort)
        {
            string filename = Path.Combine(directory, tFile.Name);

            if (tFile.FileStatusIs(FileStatus.AltSHA1FromHeader | FileStatus.AltSHA1Verified))
            {
                return;
            }
            thWrk.Report(new bgwText2(filename));

            CHD.CHDManCheck res = CHD.ChdmanCheck(filename, thWrk, out string error);
            switch (res)
            {
            case CHD.CHDManCheck.Good:
                tFile.FileStatusSet(
                    (tFile.AltSHA1 != null ? FileStatus.AltSHA1Verified : 0) |
                    (tFile.AltMD5 != null ? FileStatus.AltMD5Verified : 0)
                    );
                return;

            case CHD.CHDManCheck.Corrupt:
                thWrk.Report(new bgwShowError(filename, error));
                tFile.GotStatus = GotStatus.Corrupt;
                return;

            case CHD.CHDManCheck.CHDReturnError:
            case CHD.CHDManCheck.CHDUnknownError:
                thWrk.Report(new bgwShowError(filename, error));
                return;

            case CHD.CHDManCheck.ChdmanNotFound:
                return;

            case CHD.CHDManCheck.CHDNotFound:
                ReportError.Show("File: " + filename + " Error: Not Found scan Aborted.");
                fileErrorAbort = true;
                return;

            default:
                ReportError.UnhandledExceptionHandler(error);
                return;
            }
        }
Esempio n. 6
0
        public bool FindChild(RvBase lName, out int index)
        {
            if (ChildNameSearch(lName, out index) != 0)
            {
                ReportError.UnhandledExceptionHandler("Could not find self in Parent " + FullName);
                return(false);
            }

            do
            {
                if (_children[index] == lName)
                {
                    return(true);
                }
                index++;
            } while ((index < _children.Count) && (DBHelper.CompareName(lName, _children[index]) == 0));

            return(false);
        }
Esempio n. 7
0
 private static void GeneralError(string error)
 {
     ReportError.UnhandledExceptionHandler(error);
 }
Esempio n. 8
0
        public static void FromAFile(RvFile file, string directory, EScanLevel eScanLevel, ThreadWorker bgw, ref bool fileErrorAbort)
        {
            _bgw = bgw;
            string    filename   = Path.Combine(directory, string.IsNullOrWhiteSpace(file.FileName) ? file.Name : file.FileName);
            ICompress fileToScan = new Compress.File.File();
            ZipReturn zr         = fileToScan.ZipFileOpen(filename, file.FileModTimeStamp);

            if (zr == ZipReturn.ZipFileLocked)
            {
                file.GotStatus = GotStatus.FileLocked;
                return;
            }

            if (zr != ZipReturn.ZipGood)
            {
                ReportError.Show("File: " + filename + " Error: " + zr + ". Scan Aborted.");
                file.GotStatus = GotStatus.FileLocked;
                fileErrorAbort = true;
                return;
            }

            if (_fs == null)
            {
                _fs = new FileScan();
            }
            List <FileScan.FileResults> fr = _fs.Scan(fileToScan, true, eScanLevel == EScanLevel.Level2 || eScanLevel == EScanLevel.Level3);

            file.HeaderFileType = fr[0].HeaderFileType;
            file.Size           = fr[0].Size;
            file.CRC            = fr[0].CRC;
            file.SHA1           = fr[0].SHA1;
            file.MD5            = fr[0].MD5;
            file.AltSize        = fr[0].AltSize;
            file.AltCRC         = fr[0].AltCRC;
            file.AltSHA1        = fr[0].AltSHA1;
            file.AltMD5         = fr[0].AltMD5;


            file.FileStatusSet(
                FileStatus.SizeVerified |
                (file.HeaderFileType != HeaderFileType.Nothing ? FileStatus.HeaderFileTypeFromHeader : 0) |
                (file.CRC != null ? FileStatus.CRCVerified : 0) |
                (file.SHA1 != null ? FileStatus.SHA1Verified : 0) |
                (file.MD5 != null ? FileStatus.MD5Verified : 0) |
                (file.AltSize != null ? FileStatus.AltSizeVerified : 0) |
                (file.AltCRC != null ? FileStatus.AltCRCVerified : 0) |
                (file.AltSHA1 != null ? FileStatus.AltSHA1Verified : 0) |
                (file.AltMD5 != null ? FileStatus.AltMD5Verified : 0)
                );

            if (fr[0].HeaderFileType == HeaderFileType.CHD)
            {
                bool deepCheck = (eScanLevel == EScanLevel.Level2 || eScanLevel == EScanLevel.Level3);
                CHD.fileProcess     = FileProcess;
                CHD.fileProgress    = FileProgress;
                CHD.fileSystemError = FileSystemError;
                CHD.fileError       = FileError;
                CHD.generalError    = GeneralError;
                hdErr result = CHD.CheckFile(file.Name, directory, Settings.isLinux, ref deepCheck, out uint?chdVersion, out byte[] chdSHA1, out byte[] chdMD5, ref fileErrorAbort);
                switch (result)
                {
                case hdErr.HDERR_NONE:
                    file.CHDVersion = chdVersion;
                    if (chdSHA1 != null)
                    {
                        file.AltSHA1 = chdSHA1;
                        file.FileStatusSet(FileStatus.AltSHA1FromHeader);
                        if (deepCheck)
                        {
                            file.FileStatusSet(FileStatus.AltSHA1Verified);
                        }
                    }

                    if (chdMD5 != null)
                    {
                        file.AltMD5 = chdMD5;
                        file.FileStatusSet(FileStatus.AltMD5FromHeader);
                        if (deepCheck)
                        {
                            file.FileStatusSet(FileStatus.AltMD5Verified);
                        }
                    }
                    break;

                case hdErr.HDERR_OUT_OF_MEMORY:
                case hdErr.HDERR_INVALID_FILE:
                case hdErr.HDERR_INVALID_DATA:
                case hdErr.HDERR_READ_ERROR:
                case hdErr.HDERR_DECOMPRESSION_ERROR:
                case hdErr.HDERR_CANT_VERIFY:
                    file.GotStatus = GotStatus.Corrupt;
                    break;

                default:
                    ReportError.UnhandledExceptionHandler(result.ToString());
                    break;
                }
            }
            fileToScan.ZipFileClose();
        }
Esempio n. 9
0
        public static ReturnCode DoubleCheckDelete(RvFile fileDeleting, out string errorMessage)
        {
            errorMessage = "";

            if (!Settings.rvSettings.DoubleCheckDelete)
            {
                return(ReturnCode.Good);
            }


            ReportError.LogOut("Double Check deleting file ");
            ReportError.LogOut(fileDeleting);

            if (DBHelper.IsZeroLengthFile(fileDeleting))
            {
                return(ReturnCode.Good);
            }

            List <RvFile> lstFixRomTable = new List <RvFile>();
            List <RvFile> family         = fileDeleting.FileGroup.Files;

            foreach (RvFile file in family)
            {
                if (file.GotStatus == GotStatus.Got && DBHelper.CheckIfMissingFileCanBeFixedByGotFile(fileDeleting, file))
                {
                    lstFixRomTable.Add(file);
                }
            }

            RvFile fileToCheck = null;
            int    i           = 0;

            while (i < lstFixRomTable.Count && fileToCheck == null)
            {
                switch (lstFixRomTable[i].RepStatus)
                {
                case RepStatus.Delete:
                    i++;
                    break;

                case RepStatus.Unknown:
                case RepStatus.Correct:
                case RepStatus.InToSort:
                case RepStatus.Rename:
                case RepStatus.NeededForFix:
                case RepStatus.MoveToSort:
                case RepStatus.Ignore:
                    fileToCheck = lstFixRomTable[i];
                    break;

                default:

                    ReportError.LogOut("Double Check Delete Error Unknown " + lstFixRomTable[i].FullName + " " + lstFixRomTable[i].RepStatus);
                    ReportError.UnhandledExceptionHandler("Unknown double check delete status " + lstFixRomTable[i].RepStatus);
                    break;
                }
            }
            //ReportError.LogOut("Found Files when double check deleting");
            //foreach (RvFile t in lstFixRomTable)
            //    ReportError.LogOut(t);

            if (fileToCheck == null)
            {
                ReportError.UnhandledExceptionHandler("Double Check Delete could not find the correct file. (" + fileDeleting.FullName + ")");
                //this line of code never gets called because the above line terminates the program.
                return(ReturnCode.LogicError);
            }

            //if it is a file then
            // check it exists and the filestamp matches
            //if it is a ZipFile then
            // check the parent zip exists and the filestamp matches

            switch (fileToCheck.FileType)
            {
            case FileType.ZipFile:
            case FileType.SevenZipFile:
            {
                string fullPathCheckDelete = fileToCheck.Parent.FullNameCase;
                if (!File.Exists(fullPathCheckDelete))
                {
                    errorMessage = "Deleting " + fileDeleting.FullName + " Correct file not found. Resan for " + fullPathCheckDelete;
                    return(ReturnCode.RescanNeeded);
                }
                FileInfo fi = new FileInfo(fullPathCheckDelete);
                if (fi.LastWriteTime != fileToCheck.Parent.FileModTimeStamp)
                {
                    errorMessage = "Deleting " + fileDeleting.FullName + " Correct file timestamp not found. Resan for " + fileToCheck.FullName;
                    return(ReturnCode.RescanNeeded);
                }
                break;
            }

            case FileType.File:
            {
                string fullPathCheckDelete = fileToCheck.FullNameCase;
                if (!File.Exists(fullPathCheckDelete))
                {
                    errorMessage = "Deleting " + fileDeleting.FullName + " Correct file not found. Resan for " + fullPathCheckDelete;
                    return(ReturnCode.RescanNeeded);
                }
                FileInfo fi = new FileInfo(fullPathCheckDelete);
                if (fi.LastWriteTime != fileToCheck.FileModTimeStamp)
                {
                    errorMessage = "Deleting " + fileDeleting.FullName + " Correct file timestamp not found. Resan for " + fileToCheck.FullName;
                    return(ReturnCode.RescanNeeded);
                }
                break;
            }

            default:
                ReportError.UnhandledExceptionHandler("Unknown double check delete status " + fileToCheck.RepStatus);
                break;
            }

            return(ReturnCode.Good);
        }
Esempio n. 10
0
        public static void UpdateDat(ThreadWorker thWrk)
        {
            try
            {
                _thWrk = thWrk;
                if (_thWrk == null)
                {
                    return;
                }

                _thWrk.Report(new bgwText("Clearing DB Status"));
                RepairStatus.ReportStatusReset(DB.DirTree);

                _datCount = 0;

                _thWrk.Report(new bgwText("Finding Dats"));
                RvFile datRoot = new RvFile(FileType.Dir)
                {
                    Name = "RomVault", DatStatus = DatStatus.InDatCollect
                };

                // build a datRoot tree of the DAT's in DatRoot, and count how many dats are found
                if (!RecursiveDatTree(datRoot, out _datCount))
                {
                    _thWrk.Report(new bgwText("Dat Update Complete"));
                    _thWrk.Finished = true;
                    _thWrk          = null;
                    return;
                }

                _thWrk.Report(new bgwText("Scanning Dats"));
                _datsProcessed = 0;

                // now compare the database DAT's with datRoot removing any old DAT's
                RemoveOldDats(DB.DirTree.Child(0), datRoot);

                // next clean up the File status removing any old DAT's
                RemoveOldDatsCleanUpFiles(DB.DirTree.Child(0));

                _thWrk.Report(new bgwSetRange(_datCount - 1));

                // next add in new DAT and update the files
                UpdateDatList(DB.DirTree.Child(0), datRoot);

                // finally remove any unneeded directories from the TreeView
                RemoveOldTree(DB.DirTree.Child(0));

                _thWrk.Report(new bgwText("Updating Cache"));
                DB.Write();

                _thWrk.Report(new bgwText("Dat Update Complete"));
                _thWrk.Finished = true;
                _thWrk          = null;
            }
            catch (Exception exc)
            {
                ReportError.UnhandledExceptionHandler(exc);

                _thWrk?.Report(new bgwText("Updating Cache"));
                DB.Write();
                _thWrk?.Report(new bgwText("Complete"));

                if (_thWrk != null)
                {
                    _thWrk.Finished = true;
                }
                _thWrk = null;
            }
        }
Esempio n. 11
0
        public static void ScanFiles(ThreadWorker thWrk)
        {
            try
            {
                _thWrk = thWrk;
                if (_thWrk == null)
                {
                    return;
                }
                _progressCounter = 0;


                Stopwatch sw = new Stopwatch();
                sw.Reset();
                sw.Start();

                _thWrk.Report(new bgwSetRange(12));

                _thWrk.Report(new bgwText("Clearing DB Status"));
                _thWrk.Report(_progressCounter++);
                RepairStatus.ReportStatusReset(DB.DirTree);
                ResetFileGroups(DB.DirTree);

                _thWrk.Report(new bgwText("Getting Selected Files"));
                _thWrk.Report(_progressCounter++);


                Debug.WriteLine("Start " + sw.ElapsedMilliseconds);
                List <RvFile> filesGot     = new List <RvFile>();
                List <RvFile> filesMissing = new List <RvFile>();
                GetSelectedFiles(DB.DirTree, true, filesGot, filesMissing);
                Debug.WriteLine("GetSelected " + sw.ElapsedMilliseconds);


                _thWrk.Report(new bgwText("Sorting on CRC"));
                _thWrk.Report(_progressCounter++);
                RvFile[] filesGotSortedCRC = FindFixesSort.SortCRC(filesGot);
                Debug.WriteLine("SortCRC " + sw.ElapsedMilliseconds);

                // take the fileGot list and fileGroups list
                // this groups all the got files using there CRC

                _thWrk.Report(new bgwText("Index creation on got CRC"));
                _thWrk.Report(_progressCounter++);
                MergeGotFiles(filesGotSortedCRC, out FileGroup[] fileGroupsCRCSorted);

                Debug.WriteLine("Merge " + sw.ElapsedMilliseconds);

                _thWrk.Report(new bgwText("Index creation on got SHA1"));
                _thWrk.Report(_progressCounter++);
                FindFixesSort.SortFamily(fileGroupsCRCSorted, FindSHA1, FamilySortSHA1, out FileGroup[] fileGroupsSHA1Sorted);
                _thWrk.Report(new bgwText("Index creation on got MD5"));
                _thWrk.Report(_progressCounter++);
                FindFixesSort.SortFamily(fileGroupsCRCSorted, FindMD5, FamilySortMD5, out FileGroup[] fileGroupsMD5Sorted);

                // next make another sorted list of got files on the AltCRC
                // these are the same FileGroup classes as in the fileGroupsCRCSorted List, just sorted by AltCRC
                // if the files does not have an altCRC then it is not added to this list.
                _thWrk.Report(new bgwText("Index creation on got AltCRC"));
                _thWrk.Report(_progressCounter++);
                FindFixesSort.SortFamily(fileGroupsCRCSorted, FindAltCRC, FamilySortAltCRC, out FileGroup[] fileGroupsAltCRCSorted);
                _thWrk.Report(new bgwText("Index creation on got AltSHA1"));
                _thWrk.Report(_progressCounter++);
                FindFixesSort.SortFamily(fileGroupsCRCSorted, FindAltSHA1, FamilySortAltSHA1, out FileGroup[] fileGroupsAltSHA1Sorted);
                _thWrk.Report(new bgwText("Index creation on got AltMD5"));
                _thWrk.Report(_progressCounter++);
                FindFixesSort.SortFamily(fileGroupsCRCSorted, FindAltMD5, FamilySortAltMD5, out FileGroup[] fileGroupsAltMD5Sorted);

                _thWrk.Report(new bgwText("Merging in missing file list"));
                _thWrk.Report(_progressCounter++);
                // try and merge the missing File list into the FileGroup classes
                // using the altCRC sorted list and then the CRCSorted list
                MergeInMissingFiles(fileGroupsCRCSorted, fileGroupsSHA1Sorted, fileGroupsMD5Sorted, fileGroupsAltCRCSorted, fileGroupsAltSHA1Sorted, fileGroupsAltMD5Sorted, filesMissing);

                int totalAfterMerge = fileGroupsCRCSorted.Length;

                _thWrk.Report(new bgwText("Finding Fixes"));
                _thWrk.Report(_progressCounter++);
                FindFixesListCheck.GroupListCheck(fileGroupsCRCSorted);

                _thWrk.Report(new bgwText("Complete (Unique Files " + totalAfterMerge + ")"));
                _thWrk.Finished = true;
                _thWrk          = null;
            }
            catch (Exception exc)
            {
                ReportError.UnhandledExceptionHandler(exc);

                _thWrk?.Report(new bgwText("Updating Cache"));
                DB.Write();
                _thWrk?.Report(new bgwText("Complete"));
                if (_thWrk != null)
                {
                    _thWrk.Finished = true;
                }
                _thWrk = null;
            }
        }
Esempio n. 12
0
        public static ReturnCode FixFile(RvFile fixFile, List <RvFile> fileProcessQueue, ref int totalFixed, out string errorMessage)
        {
            errorMessage = "";

            switch (fixFile.RepStatus)
            {
            case RepStatus.Unknown:
                return(ReturnCode.FindFixes);


            case RepStatus.UnScanned:
                return(ReturnCode.Good);

            case RepStatus.Missing:
                // nothing can be done so moving right along
                return(ReturnCode.Good);


            case RepStatus.Correct:
                // this is correct nothing to be done here
                FixFileCheckName(fixFile);
                return(ReturnCode.Good);


            case RepStatus.NotCollected:
                // this is correct nothing to be done here
                return(ReturnCode.Good);

            // Unknown

            case RepStatus.Ignore:
                // this is correct nothing to be done here
                return(ReturnCode.Good);

            // Corrupt

            case RepStatus.InToSort:
                // this is correct nothing to be done here
                return(ReturnCode.Good);


            case RepStatus.Delete:
                return(FixFileDelete(fixFile, out errorMessage));

            case RepStatus.MoveToSort:
                return(FixFileMoveToSort(fixFile, out errorMessage));

            case RepStatus.MoveToCorrupt:
                return(FixFileMoveToCorrupt(fixFile, out errorMessage));

            case RepStatus.CanBeFixed:
            case RepStatus.CorruptCanBeFixed:
                return(FixFileCanBeFixed(fixFile, fileProcessQueue, ref totalFixed, out errorMessage));

            case RepStatus.NeededForFix:
                // this file can be left as is, it will be used to fix a file, and then marked to be deleted.
                return(ReturnCode.Good);

            // this is for a corrupt CHD already in ToSort
            case RepStatus.Corrupt:
                return(ReturnCode.Good);

            case RepStatus.Rename:
                // this file will be used and mark to be deleted in the CanBeFixed
                // so nothing to be done to it here
                return(ReturnCode.Good);


            default:
                ReportError.UnhandledExceptionHandler("Unknown fix file type " + fixFile.RepStatus + " Dat Status = " + fixFile.DatStatus + " GotStatus " + fixFile.GotStatus);
                return(ReturnCode.LogicError);
            }
        }
Esempio n. 13
0
        public static void ScanFiles(ThreadWorker e)
        {
#if !DEBUG
            try
            {
#endif
            _fileErrorAbort = false;
            _cacheSaveTimer = new Stopwatch();
            _cacheSaveTimer.Reset();
            if (Settings.rvSettings.CacheSaveTimerEnabled)
            {
                _cacheSaveTimer.Start();
            }

            _thWrk = e;
            if (_thWrk == null)
            {
                return;
            }



            _thWrk.Report(new bgwText("Clearing DB Status"));
            RepairStatus.ReportStatusReset(DB.DirRoot);

            _thWrk.Report(new bgwText("Finding Dir's to Scan"));
            //Next get a list of all the directories to be scanned
            List <RvFile> lstDir = new List <RvFile>();
            DBHelper.GetSelectedDirListStart(ref lstDir, StartAt);


            _thWrk.Report(new bgwText("Scanning Dir's"));
            _thWrk.Report(new bgwSetRange(lstDir.Count));
            //Scan the list of directories.
            for (int i = 0; i < lstDir.Count; i++)
            {
                _thWrk.Report(i + 1);
                _thWrk.Report(new bgwText("Scanning Dir : " + lstDir[i].FullName));
                string lDir = lstDir[i].FullName;
                Console.WriteLine(lDir);
                if (Directory.Exists(lDir))
                {
                    lstDir[i].GotStatus = GotStatus.Got;
                    CheckADir(lstDir[i], true);
                }
                else
                {
                    MarkAsMissing(lstDir[i]);
                }

                if (_thWrk.CancellationPending || _fileErrorAbort)
                {
                    break;
                }
            }

            _thWrk.Report(new bgwText("Updating Cache"));
            DB.Write();


            _thWrk.Report(new bgwText("File Scan Complete"));
            _thWrk.Finished = true;

            _thWrk = null;
#if !DEBUG
        }

        catch (Exception exc)
        {
            ReportError.UnhandledExceptionHandler(exc);

            _thWrk?.Report(new bgwText("Updating Cache"));
            DB.Write();
            _thWrk?.Report(new bgwText("Complete"));
            if (_thWrk != null)
            {
                _thWrk.Finished = true;
            }
            _thWrk = null;
        }
#endif
        }
Esempio n. 14
0
        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");
                }
            }
        }
Esempio n. 15
0
        private static ReturnCode FixBase(RvFile child, bool thisSelected, List <RvFile> fileProcessQueue, ref int totalFixed, ref int reportedFixed, Stopwatch cacheSaveTimer)
        {
            // skip any files that have already been deleted
            if (child.RepStatus == RepStatus.Deleted)
            {
                return(ReturnCode.Good);
            }

            CheckDBWrite(cacheSaveTimer);

            string     errorMessage = "";
            ReturnCode returnCode   = ReturnCode.LogicError;

            switch (child.FileType)
            {
            case FileType.Zip:
            case FileType.SevenZip:
                if (!thisSelected)
                {
                    return(ReturnCode.Good);
                }

                if (!string.IsNullOrEmpty(child.FileName))
                {
                    string strDir = child.Parent.FullNameCase;

                    Report.ReportProgress(new bgwShowFix(strDir, child.Name, null, null, "Rename", null, child.FileName, null));

                    File.Move(Path.Combine(strDir, child.FileName), Path.Combine(strDir, child.Name));
                    child.FileName = null;
                }

                returnCode = FixAZip.FixZip(child, fileProcessQueue, ref totalFixed, out errorMessage);
                break;

            case FileType.Dir:
                if (thisSelected)
                {
                    if (!string.IsNullOrEmpty(child.FileName))
                    {
                        string strDir = child.Parent.FullName;
                        Directory.Move(Path.Combine(strDir, child.FileName), Path.Combine(strDir, "__RomVault.tmpDir"));
                        Directory.Move(Path.Combine(strDir, "__RomVault.tmpDir"), Path.Combine(strDir, child.Name));
                        child.FileName = null;
                    }
                }

                returnCode = FixDir(child, thisSelected, fileProcessQueue, ref totalFixed, ref reportedFixed, cacheSaveTimer);
                return(returnCode);

            case FileType.File:
                if (!thisSelected)
                {
                    return(ReturnCode.Good);
                }

                returnCode = FixAFile.FixFile(child, fileProcessQueue, ref totalFixed, out errorMessage);
                break;
            }
            switch (returnCode)
            {
            case ReturnCode.Good:
                // all good, move alone.
                break;

            case ReturnCode.Cancel:
                break;

            case ReturnCode.RescanNeeded:
            case ReturnCode.TreeStructureError:
                ReportError.Show(errorMessage);
                break;

            case ReturnCode.LogicError:
                ReportError.UnhandledExceptionHandler(errorMessage);
                break;

            case ReturnCode.FileSystemError:
                ReportError.Show(errorMessage);
                break;

            case ReturnCode.FindFixes:
                ReportError.Show("You Need to Find Fixes before Fixing. (Incorrect File Status's found for fixing.)");
                break;

            case ReturnCode.SourceCheckSumMismatch:
            case ReturnCode.DestinationCheckSumMismatch:
                ReportError.Show(errorMessage);
                break;

            case ReturnCode.SourceDataStreamCorrupt:
                ReportError.Show(errorMessage);
                break;

            case ReturnCode.ToSortNotFound:
                ReportError.Show("Your Primary ToSort directory could not be found.");
                break;

            default:
                ReportError.UnhandledExceptionHandler("Unknown result type " + returnCode);
                break;
            }
            return(returnCode);
        }
Esempio n. 16
0
        public static void PerformFixes(ThreadWorker thWrk)
        {
            try
            {
                Stopwatch cacheSaveTimer = new Stopwatch();
                cacheSaveTimer.Reset();
                if (Settings.rvSettings.CacheSaveTimerEnabled)
                {
                    cacheSaveTimer.Start();
                }

                if (!Report.Set(thWrk))
                {
                    return;
                }


                Report.ReportProgress(new bgwText("Fixing Files"));

                int totalFixes    = 0;
                int totalFixed    = 0;
                int reportedFixed = 0;
                for (int i = 0; i < DB.DirRoot.ChildCount; i++)
                {
                    RvFile tdir = DB.DirRoot.Child(i);
                    totalFixes += CountFixDir(tdir, tdir.Tree.Checked == RvTreeRow.TreeSelect.Selected);
                }
                Report.ReportProgress(new bgwSetRange(totalFixes));

                List <RvFile> fileProcessQueue = new List <RvFile>();

                for (int i = 0; i < DB.DirRoot.ChildCount; i++)
                {
                    RvFile     tdir       = DB.DirRoot.Child(i);
                    ReturnCode returnCode = FixDir(tdir, tdir.Tree.Checked == RvTreeRow.TreeSelect.Selected, fileProcessQueue, ref totalFixed, ref reportedFixed, cacheSaveTimer);
                    if (returnCode != ReturnCode.Good && returnCode != ReturnCode.Cancel)
                    {
                        RepairStatus.ReportStatusReset(DB.DirRoot);
                        break;
                    }

                    if (Report.CancellationPending())
                    {
                        break;
                    }
                }

                Report.ReportProgress(new bgwText("Updating Cache"));
                DB.Write();
                Report.ReportProgress(new bgwText("Complete"));

                Report.Set(null);
            }
            catch (Exception exc)
            {
                ReportError.UnhandledExceptionHandler(exc);

                Report.ReportProgress(new bgwText("Updating Cache"));

                DB.Write();
                Report.ReportProgress(new bgwText("Complete"));


                Report.Set(null);
            }
        }