// This Function returns:
        // Good            : Everything Worked Correctly
        // RescanNeeded     : Something unexpectidly changed in the files, so Stop prompt user to rescan.
        // LogicError       : This Should never happen and is a logic problem in the code
        // FileSystemError  : Something is wrong with the files
        /// <summary>
        /// Performs the ROMVault File Copy, with the source and destination being files or zipped files
        /// </summary>
        /// <param name="fileIn">This is the file being copied, it may be a zipped file or a regular file system file</param>
        /// <param name="zipFileOut">This is the zip file that is being writen to, if it is null a new zip file will be made if we are coping to a zip</param>
        /// <param name="zipFilenameOut">This is the name of the .zip file to be made that will be created in zipFileOut</param>
        /// <param name="fileOut">This is the actual output filename</param>
        /// <param name="forceRaw">if true then we will do a raw copy, this is so that we can copy corrupt zips</param>
        /// <param name="error">This is the returned error message if this copy fails</param>
        /// <param name="foundFile">If we are SHA1/MD5 checking the source file for the first time, and it is different from what we expected the correct values for this file are returned in foundFile</param>
        /// <returns>ReturnCode.Good is the valid return code otherwire we have an error</returns>
        public static ReturnCode CopyFile(RvFile fileIn, ref ZipFile zipFileOut, string zipFilenameOut, RvFile fileOut, bool forceRaw, out string error, out RvFile foundFile)
        {
            foundFile = null;
            error = "";

            if (_buffer == null)
                _buffer = new byte[BufferSize];

            bool rawCopy = RawCopy(fileIn, fileOut, forceRaw);

            ulong streamSize = 0;
            ushort compressionMethod = 8;
            bool sourceTrrntzip = false;

            ZipFile zipFileIn = null;
            System.IO.Stream readStream = null;

            bool isZeroLengthFile = DBHelper.IsZeroLengthFile(fileOut);
            if (!isZeroLengthFile)
            {
                #region check that the in and out file match
                if (fileOut.FileStatusIs(FileStatus.SizeFromDAT) && fileOut.Size != null && fileIn.Size != fileOut.Size)
                {
                    error = "Source and destination Size does not match. Logic Error.";
                    return ReturnCode.LogicError;
                }
                if (fileOut.FileStatusIs(FileStatus.CRCFromDAT) && fileOut.CRC != null && !ArrByte.bCompare(fileIn.CRC, fileOut.CRC))
                {
                    error = "Source and destination CRC does not match. Logic Error.";
                    return ReturnCode.LogicError;
                }

                if (fileOut.FileStatusIs(FileStatus.SHA1FromDAT) && fileIn.FileStatusIs(FileStatus.SHA1Verified))
                {
                    if (fileIn.SHA1 != null && fileOut.SHA1 != null && !ArrByte.bCompare(fileIn.SHA1, fileOut.SHA1))
                    {
                        error = "Source and destination SHA1 does not match. Logic Error.";
                        return ReturnCode.LogicError;
                    }
                }
                if (fileOut.FileStatusIs(FileStatus.MD5CHDFromDAT) && fileIn.FileStatusIs(FileStatus.MD5Verified))
                {
                    if (fileIn.MD5 != null && fileOut.MD5 != null && !ArrByte.bCompare(fileIn.MD5, fileOut.MD5))
                    {
                        error = "Source and destination SHA1 does not match. Logic Error.";
                        return ReturnCode.LogicError;
                    }
                }

                #endregion

                #region Find and Check/Open Input Files

                if (fileIn.FileType == FileType.ZipFile) // Input is a ZipFile
                {
                    RvDir zZipFileIn = fileIn.Parent;
                    if (zZipFileIn.FileType != FileType.Zip)
                    {
                        error = "Zip File Open but Source File is not a zip, Logic Error.";
                        return ReturnCode.LogicError;
                    }

                    string fileNameIn = zZipFileIn.FullName;

                    sourceTrrntzip = (zZipFileIn.ZipStatus & ZipStatus.TrrntZip) == ZipStatus.TrrntZip;

                    //if (zZipFileIn.ZipFileType == RvZip.ZipType.Zip)
                    //{
                    zipFileIn = new ZipFile();

                    ZipReturn zr1;
                    if (fileIn.ZipFileHeaderPosition != null)
                        zr1 = zipFileIn.ZipFileOpen(fileNameIn, zZipFileIn.TimeStamp, false);
                    else
                        zr1 = zipFileIn.ZipFileOpen(fileNameIn, zZipFileIn.TimeStamp, true);
                    switch (zr1)
                    {
                        case ZipReturn.ZipGood:
                            break;
                        case ZipReturn.ZipErrorFileNotFound:
                            error = "File not found, Rescan required before fixing " + fileIn.Name;
                            return ReturnCode.FileSystemError;
                        case ZipReturn.ZipErrorTimeStamp:
                            error = "File has changed, Rescan required before fixing " + fileIn.Name;
                            return ReturnCode.FileSystemError;
                        default:
                            error = "Error Open Zip" + zr1 + ", with File " + fileIn.DatFullName;
                            return ReturnCode.FileSystemError;
                    }
                    if (fileIn.ZipFileHeaderPosition != null)
                        zipFileIn.ZipFileOpenReadStreamQuick((ulong)fileIn.ZipFileHeaderPosition, rawCopy, out readStream, out streamSize, out compressionMethod);
                    else
                        zipFileIn.ZipFileOpenReadStream(fileIn.ZipFileIndex, rawCopy, out readStream, out streamSize, out compressionMethod);
                }
                else // Input is a regular file
                {
                    string fileNameIn = fileIn.FullName;
                    if (!IO.File.Exists(fileNameIn))
                    {
                        error = "Rescan needed, File Changed :" + fileNameIn;
                        return ReturnCode.RescanNeeded;
                    }
                    IO.FileInfo fileInInfo = new IO.FileInfo(fileNameIn);
                    if (fileInInfo.LastWriteTime != fileIn.TimeStamp)
                    {
                        error = "Rescan needed, File Changed :" + fileNameIn;
                        return ReturnCode.RescanNeeded;
                    }
                    int errorCode = IO.FileStream.OpenFileRead(fileNameIn, out readStream);
                    if (errorCode != 0)
                    {
                        error = new Win32Exception(errorCode).Message + ". " + fileNameIn;
                        return ReturnCode.FileSystemError;
                    }
                    if (fileIn.Size == null)
                    {
                        error = "Null File Size found in Fixing File :" + fileNameIn;
                        return ReturnCode.LogicError;
                    }
                    streamSize = (ulong)fileIn.Size;
                    if ((ulong)readStream.Length != streamSize)
                    {
                        error = "Rescan needed, File Length Changed :" + fileNameIn;
                        return ReturnCode.RescanNeeded;
                    }
                }
                #endregion
            }
            else
            {
                sourceTrrntzip = true;
            }

            if (!rawCopy && (Settings.FixLevel == eFixLevel.TrrntZipLevel1 || Settings.FixLevel == eFixLevel.TrrntZipLevel2 || Settings.FixLevel == eFixLevel.TrrntZipLevel3))
                compressionMethod = 8;

            #region Find and Check/Open Output Files

            System.IO.Stream writeStream;
            if (fileOut.FileType == FileType.ZipFile)
            {
                // if ZipFileOut == null then we have not open the output zip yet so open it from writing.
                if (zipFileOut == null)
                {
                    if (IO.Path.GetFileName(zipFilenameOut) == "__ROMVault.tmp")
                    {
                        if (IO.File.Exists(zipFilenameOut))
                            IO.File.Delete(zipFilenameOut);
                    }
                    else if (IO.File.Exists(zipFilenameOut))
                    {
                        error = "Rescan needed, File Changed :" + zipFilenameOut;
                        return ReturnCode.RescanNeeded;
                    }

                    zipFileOut = new ZipFile();
                    ZipReturn zrf = zipFileOut.ZipFileCreate(zipFilenameOut);
                    if (zrf != ZipReturn.ZipGood)
                    {
                        error = "Error Opening Write Stream " + zrf;
                        return ReturnCode.FileSystemError;
                    }
                }
                else
                {
                    if (zipFileOut.ZipOpen != ZipOpenType.OpenWrite)
                    {
                        error = "Output Zip File is not set to OpenWrite, Logic Error.";
                        return ReturnCode.LogicError;
                    }

                    if (zipFileOut.ZipFilename != (new IO.FileInfo(zipFilenameOut).FullName))
                    {
                        error = "Output Zip file has changed name from " + zipFileOut.ZipFilename + " to " + zipFilenameOut + ". Logic Error";
                        return ReturnCode.LogicError;
                    }
                }

                if (fileIn.Size == null)
                {
                    error = "Null File Size found in Fixing File :" + fileIn.FullName;
                    return ReturnCode.LogicError;
                }
                ZipReturn zr = zipFileOut.ZipFileOpenWriteStream(rawCopy, sourceTrrntzip, fileOut.Name, (ulong)fileIn.Size, compressionMethod, out writeStream);
                if (zr != ZipReturn.ZipGood)
                {
                    error = "Error Opening Write Stream " + zr;
                    return ReturnCode.FileSystemError;
                }
            }
            else
            {
                if (IO.File.Exists(zipFilenameOut) && fileOut.GotStatus != GotStatus.Corrupt)
                {
                    error = "Rescan needed, File Changed :" + zipFilenameOut;
                    return ReturnCode.RescanNeeded;
                }
                int errorCode = IO.FileStream.OpenFileWrite(zipFilenameOut, out writeStream);
                if (errorCode != 0)
                {
                    error = new Win32Exception(errorCode).Message + ". " + zipFilenameOut;
                    return ReturnCode.FileSystemError;
                }
            }
            #endregion

            byte[] bCRC;
            byte[] bMD5 = null;
            byte[] bSHA1 = null;
            if (!isZeroLengthFile)
            {
                #region Do Data Tranfer

                CRC32Hash crc32 = null;
                MD5 md5 = null;
                SHA1 sha1 = null;

                if (!rawCopy)
                {
                    crc32 = new CRC32Hash();
                    md5 = MD5.Create();
                    sha1 = SHA1.Create();
                }

                ulong sizetogo = streamSize;

                while (sizetogo > 0)
                {
                    int sizenow = sizetogo > BufferSize ? BufferSize : (int)sizetogo;

                    try
                    {
                        readStream.Read(_buffer, 0, sizenow);
                    }
                    catch (ZlibException)
                    {
                        if (fileIn.FileType == FileType.ZipFile && zipFileIn != null)
                        {
                            ZipReturn zr = zipFileIn.ZipFileCloseReadStream();
                            if (zr != ZipReturn.ZipGood)
                            {
                                error = "Error Closing " + zr + " Stream :" + zipFileIn.Filename(fileIn.ReportIndex);
                                return ReturnCode.FileSystemError;
                            }
                            zipFileIn.ZipFileClose();

                        }
                        else
                        {
                            readStream.Close();
                        }

                        if (fileOut.FileType == FileType.ZipFile)
                        {
                            ZipReturn zr = zipFileOut.ZipFileCloseWriteStream(new byte[] { 0, 0, 0, 0 });
                            if (zr != ZipReturn.ZipGood)
                            {
                                error = "Error Closing Stream " + zr;
                                return ReturnCode.FileSystemError;
                            }
                            zipFileOut.ZipFileRollBack();
                        }
                        else
                        {
                            writeStream.Flush();
                            writeStream.Close();
                            IO.File.Delete(zipFilenameOut);
                        }

                        error = "Error in Data Stream";
                        return ReturnCode.SourceCRCCheckSumError;
                    }
                    catch (Exception e)
                    {
                        error = "Error reading Source File " + e.Message;
                        return ReturnCode.FileSystemError;
                    }

                    if (!rawCopy)
                    {
                        crc32.TransformBlock(_buffer, 0, sizenow, null, 0);
                        md5.TransformBlock(_buffer, 0, sizenow, null, 0);
                        sha1.TransformBlock(_buffer, 0, sizenow, null, 0);
                    }
                    try
                    {
                        writeStream.Write(_buffer, 0, sizenow);
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e.Message);
                        error = "Error writing out file. " + Environment.NewLine + e.Message;
                        return ReturnCode.FileSystemError;
                    }
                    sizetogo = sizetogo - (ulong)sizenow;
                }
                writeStream.Flush();

                #endregion

                #region Collect Checksums

                // if we did a full copy then we just calculated all the checksums while doing the copy
                if (!rawCopy)
                {

                    crc32.TransformFinalBlock(_buffer, 0, 0);
                    md5.TransformFinalBlock(_buffer, 0, 0);
                    sha1.TransformFinalBlock(_buffer, 0, 0);

                    bCRC = crc32.Hash;
                    bMD5 = md5.Hash;
                    bSHA1 = sha1.Hash;

                }
                // if we raw copied and the source file has been FileChecked then we can trust the checksums in the source file
                else
                {
                    bCRC = ArrByte.Copy(fileIn.CRC);
                    if (fileIn.FileStatusIs(FileStatus.MD5Verified)) bMD5 = ArrByte.Copy(fileIn.MD5);
                    if (fileIn.FileStatusIs(FileStatus.SHA1Verified)) bSHA1 = ArrByte.Copy(fileIn.SHA1);
                }

                #endregion

                #region close the ReadStream

                if (fileIn.FileType == FileType.ZipFile && zipFileIn != null)
                {
                    ZipReturn zr = zipFileIn.ZipFileCloseReadStream();
                    if (zr != ZipReturn.ZipGood)
                    {
                        error = "Error Closing " + zr + " Stream :" + zipFileIn.Filename(fileIn.ReportIndex);
                        return ReturnCode.FileSystemError;
                    }
                    zipFileIn.ZipFileClose();
                }
                else
                {
                    readStream.Close();

                    //if (IO.File.Exists(tmpFilename))
                    //    IO.File.Delete(tmpFilename);
                }

                #endregion
            }
            else
            {
                // Zero Length File (Directory in a Zip)
                if (fileOut.FileType == FileType.ZipFile)
                {
                    zipFileOut.ZipFileAddDirectory();
                }
                bCRC = VarFix.CleanMD5SHA1("00000000", 8);
                bMD5 = VarFix.CleanMD5SHA1("d41d8cd98f00b204e9800998ecf8427e", 32);
                bSHA1 = VarFix.CleanMD5SHA1("da39a3ee5e6b4b0d3255bfef95601890afd80709", 40);
            }

            #region close the WriteStream
            if (fileOut.FileType == FileType.ZipFile)
            {
                ZipReturn zr = zipFileOut.ZipFileCloseWriteStream(bCRC);
                if (zr != ZipReturn.ZipGood)
                {
                    error = "Error Closing Stream " + zr;
                    return ReturnCode.FileSystemError;
                }
                fileOut.ZipFileIndex = zipFileOut.LocalFilesCount() - 1;
                fileOut.ZipFileHeaderPosition = zipFileOut.LocalHeader(fileOut.ZipFileIndex);
            }
            else
            {
                writeStream.Flush();
                writeStream.Close();
                IO.FileInfo fi = new IO.FileInfo(zipFilenameOut);
                fileOut.TimeStamp = fi.LastWriteTime;
            }
            #endregion

            if (!isZeroLengthFile)
            {

                if (!rawCopy)
                {
                    if (!ArrByte.bCompare(bCRC, fileIn.CRC))
                    {
                        fileIn.GotStatus = GotStatus.Corrupt;
                        error = "Source CRC does not match Source Data stream, corrupt Zip";

                        if (fileOut.FileType == FileType.ZipFile)
                            zipFileOut.ZipFileRollBack();
                        else
                            IO.File.Delete(zipFilenameOut);

                        return ReturnCode.SourceCRCCheckSumError;
                    }

                    fileIn.FileStatusSet(FileStatus.CRCVerified | FileStatus.SizeVerified);

                    bool sourceFailed = false;

                    // check to see if we have a MD5 from the DAT file
                    if (fileIn.FileStatusIs(FileStatus.MD5FromDAT))
                    {
                        if (fileIn.MD5 == null)
                        {
                            error = "Should have an filein MD5 from Dat but not found. Logic Error.";
                            return ReturnCode.LogicError;
                        }

                        if (!ArrByte.bCompare(fileIn.MD5, bMD5))
                            sourceFailed = true;
                        else
                            fileIn.FileStatusSet(FileStatus.MD5Verified);
                    }
                    // check to see if we have an MD5 (not from the DAT) so must be from previously scanning this file.
                    else if (fileIn.MD5 != null)
                    {
                        if (!ArrByte.bCompare(fileIn.MD5, bMD5))
                        {
                            // if we had an MD5 from a preview scan and it now does not match, something has gone really bad.
                            error = "The MD5 found does not match a previously scanned MD5, this should not happen, unless something got corrupt.";
                            return ReturnCode.LogicError;
                        }
                    }
                    else // (FileIn.MD5 == null)
                    {
                        fileIn.MD5 = bMD5;
                        fileIn.FileStatusSet(FileStatus.MD5Verified);
                    }

                    // check to see if we have a SHA1 from the DAT file
                    if (fileIn.FileStatusIs(FileStatus.SHA1FromDAT))
                    {
                        if (fileIn.SHA1 == null)
                        {
                            error = "Should have an filein SHA1 from Dat but not found. Logic Error.";
                            return ReturnCode.LogicError;
                        }

                        if (!ArrByte.bCompare(fileIn.SHA1, bSHA1))
                            sourceFailed = true;
                        else
                            fileIn.FileStatusSet(FileStatus.SHA1Verified);
                    }
                    // check to see if we have an SHA1 (not from the DAT) so must be from previously scanning this file.
                    else if (fileIn.SHA1 != null)
                    {
                        if (!ArrByte.bCompare(fileIn.SHA1, bSHA1))
                        {
                            // if we had an SHA1 from a preview scan and it now does not match, something has gone really bad.
                            error = "The SHA1 found does not match a previously scanned SHA1, this should not happen, unless something got corrupt.";
                            return ReturnCode.LogicError;
                        }
                    }
                    else // (FileIn.SHA1 == null)
                    {
                        fileIn.SHA1 = bSHA1;
                        fileIn.FileStatusSet(FileStatus.SHA1Verified);
                    }

                    if (sourceFailed)
                    {
                        if (fileIn.FileType == FileType.ZipFile)
                        {
                            RvFile tZFile = new RvFile(FileType.ZipFile);
                            foundFile = tZFile;
                            tZFile.ZipFileIndex = fileIn.ZipFileIndex;
                            tZFile.ZipFileHeaderPosition = fileIn.ZipFileHeaderPosition;
                        }
                        else
                        {
                            foundFile = new RvFile(fileIn.FileType);
                        }

                        foundFile.Name = fileIn.Name;
                        foundFile.Size = fileIn.Size;
                        foundFile.CRC = bCRC;
                        foundFile.MD5 = bMD5;
                        foundFile.SHA1 = bSHA1;
                        foundFile.TimeStamp = fileIn.TimeStamp;
                        foundFile.SetStatus(DatStatus.NotInDat, GotStatus.Got);

                        foundFile.FileStatusSet(FileStatus.SizeVerified | FileStatus.CRCVerified | FileStatus.MD5Verified | FileStatus.SHA1Verified);

                        if (fileOut.FileType == FileType.ZipFile)
                            zipFileOut.ZipFileRollBack();
                        else
                            IO.File.Delete(zipFilenameOut);

                        return ReturnCode.SourceCheckSumError;
                    }
                }
            }

            if (fileOut.FileType == FileType.ZipFile)
            {
                fileOut.FileStatusSet(FileStatus.SizeFromHeader | FileStatus.CRCFromHeader);
            }

            if (fileOut.FileStatusIs(FileStatus.CRCFromDAT) && fileOut.CRC != null && !ArrByte.bCompare(fileOut.CRC, bCRC))
            {
                //Rollback the file
                if (fileOut.FileType == FileType.ZipFile)
                    zipFileOut.ZipFileRollBack();
                else
                    IO.File.Delete(zipFilenameOut);

                return ReturnCode.DestinationCheckSumError;
            }

            fileOut.CRC = bCRC;
            if (!rawCopy || fileIn.FileStatusIs(FileStatus.CRCVerified))
                fileOut.FileStatusSet(FileStatus.CRCVerified);

            if (bSHA1 != null)
            {
                if (fileOut.FileStatusIs(FileStatus.SHA1FromDAT) && fileOut.SHA1 != null && !ArrByte.bCompare(fileOut.SHA1, bSHA1))
                {
                    //Rollback the file
                    if (fileOut.FileType == FileType.ZipFile)
                        zipFileOut.ZipFileRollBack();
                    else
                        IO.File.Delete(zipFilenameOut);

                    return ReturnCode.DestinationCheckSumError;
                }

                fileOut.SHA1 = bSHA1;
                fileOut.FileStatusSet(FileStatus.SHA1Verified);
            }

            if (bMD5 != null)
            {
                if (fileOut.FileStatusIs(FileStatus.MD5FromDAT) && fileOut.MD5 != null && !ArrByte.bCompare(fileOut.MD5, bMD5))
                {
                    //Rollback the file
                    if (fileOut.FileType == FileType.ZipFile)
                        zipFileOut.ZipFileRollBack();
                    else
                        IO.File.Delete(zipFilenameOut);

                    return ReturnCode.DestinationCheckSumError;
                }
                fileOut.MD5 = bMD5;
                fileOut.FileStatusSet(FileStatus.MD5Verified);
            }

            if (fileIn.Size != null)
            {
                fileOut.Size = fileIn.Size;
                fileOut.FileStatusSet(FileStatus.SizeVerified);
            }

            if (fileIn.GotStatus == GotStatus.Corrupt)
                fileOut.GotStatus = GotStatus.Corrupt;
            else
                fileOut.GotStatus = GotStatus.Got; // Changes RepStatus to Correct

            fileOut.FileStatusSet(FileStatus.SizeVerified);

            if (fileOut.SHA1CHD == null && fileIn.SHA1CHD != null) fileOut.SHA1CHD = fileIn.SHA1CHD;
            if (fileOut.MD5CHD == null && fileIn.MD5CHD != null) fileOut.MD5CHD = fileIn.MD5CHD;

            fileOut.CHDVersion = fileIn.CHDVersion;

            fileOut.FileStatusSet(FileStatus.SHA1CHDFromHeader | FileStatus.MD5CHDFromHeader | FileStatus.SHA1CHDVerified | FileStatus.MD5CHDVerified, fileIn);

            return ReturnCode.Good;
        }
Example #2
0
        private static void CheckADir(RvDir dbDir, bool report)
        {
            if (_cacheSaveTimer.Elapsed.TotalMinutes > Settings.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:
                    {
                        fileDir = new RvDir(ft);

                        // open the zip file
                        ZipFile checkZ = new ZipFile();

                        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 ".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);
        }