Example #1
0
        public static void Write()
        {
            if (File.Exists(Program.rvSettings.CacheFile))
            {
                string bname = Program.rvSettings.CacheFile + "Backup";
                if (File.Exists(bname))
                {
                    File.Delete(bname);
                }
                File.Move(Program.rvSettings.CacheFile, bname);
            }
            FileStream   fs = new FileStream(Program.rvSettings.CacheFile, FileMode.CreateNew, FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(fs);

            DBVersion.VersionNow = DBVersion.Version;
            bw.Write(DBVersion.Version);
            DirTree.Write(bw);

            bw.Write(EndCacheMarker);

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

            fs.Close();
            fs.Dispose();
        }
Example #2
0
        public bool ReadDat(string fullname, out DatHeader rvDat)
        {
            rvDat = null;

            System.Diagnostics.Debug.WriteLine("Reading : " + fullname);

            StreamReader myfile  = File.OpenText(fullname, Enc);
            string       strLine = null;

            while (string.IsNullOrWhiteSpace(strLine) && !myfile.EndOfStream)
            {
                strLine = myfile.ReadLine();
            }
            myfile.Close();

            if (strLine == null)
            {
                return(false);
            }


            if (strLine.ToLower().IndexOf("xml", StringComparison.Ordinal) >= 0)
            {
                if (!ReadXMLDat(fullname, out rvDat))
                {
                    return(false);
                }
            }
            else if ((strLine.ToLower().IndexOf("clrmamepro", StringComparison.Ordinal) >= 0) || (strLine.ToLower().IndexOf("clrmame", StringComparison.Ordinal) >= 0) || (strLine.ToLower().IndexOf("romvault", StringComparison.Ordinal) >= 0) || (strLine.ToLower().IndexOf("game", StringComparison.Ordinal) >= 0) || (strLine.ToLower().IndexOf("machine", StringComparison.Ordinal) >= 0))
            {
                DatCmpReader dcr = new DatCmpReader(ErrorReport);
                if (!dcr.ReadDat(fullname, out rvDat))
                {
                    return(false);
                }
            }
            else if (strLine.ToLower().IndexOf("doscenter", StringComparison.Ordinal) >= 0)
            {
                DatDOSReader ddr = new DatDOSReader(ErrorReport);
                if (!ddr.ReadDat(fullname, out rvDat))
                {
                    return(false);
                }
            }
            else if (strLine.ToLower().IndexOf("[credits]", StringComparison.Ordinal) >= 0)
            {
                DatROMCenterReader drcr = new DatROMCenterReader(ErrorReport);
                if (!drcr.ReadDat(fullname, out rvDat))
                {
                    return(false);
                }
            }
            else
            {
                ErrorReport?.Invoke(fullname, "Invalid DAT File");
                return(false);
            }

            return(true);
        }
Example #3
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);
        }
Example #4
0
        private static Settings ReadConfig()
        {
            if (!File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RomVault3cfg.xml")))
            {
                return(null);
            }

            Settings retSettings;

            using (StreamReader sr = new StreamReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RomVault3cfg.xml")))
            {
                XmlSerializer x = new XmlSerializer(typeof(Settings));
                retSettings = (Settings)x.Deserialize(sr);
            }

            foreach (var rule in retSettings.DatRules)
            {
                if (rule.Merge == MergeType.CHDsMerge)
                {
                    rule.Merge  = MergeType.Merge;
                    rule.Filter = FilterType.CHDsOnly;
                }
            }

            return(retSettings);
        }
Example #5
0
        private static Settings ReadConfig()
        {
            string configPath = Path.Combine(Environment.CurrentDirectory, "RomVault3cfg.xml");

            if (!File.Exists(configPath))
            {
                Console.WriteLine($"{configPath} not Found");
                return(null);
            }
            Console.WriteLine($"Reading {configPath}");
            string strXml = System.IO.File.ReadAllText(configPath);

            // converting old enum to new:
            strXml = strXml.Replace("TrrntZipLevel", "Level");

            Settings retSettings;

            using (TextReader sr = new StringReader(strXml))
            {
                XmlSerializer x = new XmlSerializer(typeof(Settings));
                retSettings = (Settings)x.Deserialize(sr);
            }

            foreach (var rule in retSettings.DatRules)
            {
                if (rule.Merge == MergeType.CHDsMerge)
                {
                    rule.Merge  = MergeType.Merge;
                    rule.Filter = FilterType.CHDsOnly;
                }
            }

            return(retSettings);
        }
Example #6
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;
        }
Example #7
0
        // if we are fixing a zip/7z file then we use zipFileOut to open an output compressed stream
        // if we are just making a file then we use filenameOut to open an output filestream
        private static ReturnCode OpenOutputStream(RvFile fileOut, RvFile fileIn, ICompress zipFileOut, string filenameOut, ushort compressionMethod, bool rawCopy, bool sourceTrrntzip, long?dateTime, out Stream writeStream, out string error)
        {
            writeStream = null;

            if (fileOut.FileType == FileType.ZipFile || fileOut.FileType == FileType.SevenZipFile)
            {
                // if ZipFileOut == null then we have not open the output zip yet so open it from writing.
                if (zipFileOut == null)
                {
                    error = "zipFileOut cannot be null";
                    return(ReturnCode.FileSystemError);
                }

                if (zipFileOut.ZipOpen != ZipOpenType.OpenWrite)
                {
                    error = "Output Zip File is not set to OpenWrite, Logic Error.";
                    return(ReturnCode.LogicError);
                }

                if (fileIn.Size == null)
                {
                    error = "Null File Size found in Fixing File :" + fileIn.FullName;
                    return(ReturnCode.LogicError);
                }
                TimeStamps ts = null;
                if (dateTime != null)
                {
                    ts = new TimeStamps {
                        ModTime = dateTime
                    };
                }

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


            error = "";
            return(ReturnCode.Good);
        }
Example #8
0
        public static void Read(object sender, DoWorkEventArgs e)
        {
            Bgw = sender as BackgroundWorker;
            Program.SyncCont = e.Argument as SynchronizationContext;

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

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


            BinaryReader br = new BinaryReader(fs);

            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;
        }
Example #9
0
        public static void WriteConfig(Settings settings)
        {
            if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RomVault3cfg.xml")))
            {
                File.Delete(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RomVault3cfg.xml"));
            }

            using (StreamWriter sw = new StreamWriter(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RomVault3cfg.xml")))
            {
                XmlSerializer x = new XmlSerializer(typeof(Settings));
                x.Serialize(sw, settings);
                sw.Flush();
            }
        }
Example #10
0
        public void WriteConfig()
        {
            //if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RomVault2.cfg")))
            //    File.Delete(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RomVault2.cfg"));
            if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RomVault2cfg.xml")))
            {
                File.Delete(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RomVault2cfg.xml"));
            }

            using (StreamWriter sw = new StreamWriter(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RomVault2cfg.xml")))
            {
                XmlSerializer x = new XmlSerializer(Program.rvSettings.GetType());
                x.Serialize(sw, Program.rvSettings);
                sw.Flush();
            }
        }
Example #11
0
        public static void WriteConfig(Settings settings)
        {
            string configPath = Path.Combine(Environment.CurrentDirectory, "RomVault3cfg.xml");

            if (File.Exists(configPath))
            {
                File.Delete(configPath);
            }

            using (StreamWriter sw = new StreamWriter(configPath))
            {
                XmlSerializer x = new XmlSerializer(typeof(Settings));
                x.Serialize(sw, settings);
                sw.Flush();
            }
        }
Example #12
0
        public ZipReturn ZipFileOpen(string filename, long timestamp, bool readHeaders)
        {
            ZipFileClose();
            _memoryZipFile = null;
            Debug.WriteLine(filename);
            #region open file stream

            try
            {
                if (!File.Exists(filename))
                {
                    ZipFileClose();
                    return(ZipReturn.ZipErrorFileNotFound);
                }
                _zipFileInfo = new FileInfo(filename);
                if ((timestamp != -1) && (_zipFileInfo.LastWriteTime != timestamp))
                {
                    ZipFileClose();
                    return(ZipReturn.ZipErrorTimeStamp);
                }
                int errorCode = FileStream.OpenFileRead(filename, out _zipFs);
                if (errorCode != 0)
                {
                    ZipFileClose();
                    return(ZipReturn.ZipErrorOpeningFile);
                }
            }
            catch (PathTooLongException)
            {
                ZipFileClose();
                return(ZipReturn.ZipFileNameToLong);
            }
            catch (IOException)
            {
                ZipFileClose();
                return(ZipReturn.ZipErrorOpeningFile);
            }

            #endregion

            ZipOpen   = ZipOpenType.OpenRead;
            ZipStatus = ZipStatus.None;

            return(ZipFileReadHeaders());
        }
Example #13
0
        public ZipReturn ReadGZip(string filename, bool deepScan)
        {
            _filename = "";
            if (!File.Exists(filename))
            {
                return(ZipReturn.ZipErrorFileNotFound);
            }
            _filename = filename;

            int errorCode = FileStream.OpenFileRead(filename, out _zipFs);

            if (errorCode != 0)
            {
                if (errorCode == 32)
                {
                    return(ZipReturn.ZipFileLocked);
                }
                return(ZipReturn.ZipErrorOpeningFile);
            }
            return(ReadBody(deepScan));
        }
Example #14
0
        public ZipReturn GetRawStream(out Stream st)
        {
            st = null;
            if (!File.Exists(_filename))
            {
                return(ZipReturn.ZipErrorFileNotFound);
            }

            int errorCode = FileStream.OpenFileRead(_filename, out st);

            if (errorCode != 0)
            {
                if (errorCode == 32)
                {
                    return(ZipReturn.ZipFileLocked);
                }
                return(ZipReturn.ZipErrorOpeningFile);
            }

            st.Position = datapos;

            return(ZipReturn.ZipGood);
        }
Example #15
0
        public static void Write()
        {
            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);
                }
            }
            FileStream fs = new FileStream(Settings.rvSettings.CacheFile, FileMode.CreateNew, FileAccess.Write);

            using (BinaryWriter bw = new BinaryWriter(fs, Encoding.UTF8, true))
            {
                DBVersion.VersionNow = DBVersion.Version;
                bw.Write(DBVersion.Version);
                DirTree.Write(bw);

                bw.Write(EndCacheMarker);

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

            fs.Close();
            fs.Dispose();
        }
Example #16
0
        private static void ScanRomRoot(string directory)
        {
            _bgw.ReportProgress(0, new bgwText("Scanning Dir : " + directory));
            DirectoryInfo di = new DirectoryInfo(directory);

            FileInfo[] fi = di.GetFiles();

            _bgw.ReportProgress(0, new bgwRange2Visible(true));
            _bgw.ReportProgress(0, new bgwSetRange2(fi.Count()));

            for (int j = 0; j < fi.Count(); j++)
            {
                if (_bgw.CancellationPending)
                {
                    return;
                }

                FileInfo f = fi[j];
                _bgw.ReportProgress(0, new bgwValue2(j));
                _bgw.ReportProgress(0, new bgwText2(f.Name));
                string ext = Path.GetExtension(f.Name);

                if (ext.ToLower() == ".gz")
                {
                    gZip      gZipTest  = new gZip();
                    ZipReturn errorcode = gZipTest.ZipFileOpen(f.FullName);
                    if (errorcode != ZipReturn.ZipGood)
                    {
                        _bgw.ReportProgress(0, new bgwShowError(f.FullName, "gz File corrupt"));
                        if (!Directory.Exists("corrupt"))
                        {
                            Directory.CreateDirectory("corrupt");
                        }
                        File.Move(f.FullName, Path.Combine("corrupt", f.Name));
                        continue;
                    }

                    RvFile tFile = RvFile.fromGZip(f.FullName, gZipTest.ExtraData, gZipTest.CompressedSize);
                    gZipTest.ZipFileClose();

                    FindStatus res = fileneededTest(tFile);

                    if (res != FindStatus.FoundFileInArchive)
                    {
                        if (deep)
                        {
                            gZipTest = new gZip();

                            try
                            {
                                errorcode = gZipTest.ZipFileOpen(f.FullName);
                                if (errorcode == ZipReturn.ZipGood)
                                {
                                    FileScan fs = new FileScan();
                                    List <FileScan.FileResults> gRes = fs.Scan(gZipTest, true, true);
                                    errorcode = gRes[0].FileStatus;
                                    gZipTest.ZipFileClose();
                                }
                            }
                            catch
                            {
                                gZipTest.ZipFileClose();
                                _bgw.ReportProgress(0, new bgwShowError(f.FullName, "gz Crashed Compression"));
                                if (!Directory.Exists("corrupt"))
                                {
                                    Directory.CreateDirectory("corrupt");
                                }
                                File.Move(f.FullName, Path.Combine("corrupt", f.Name));
                                continue;
                            }

                            if (errorcode != ZipReturn.ZipGood)
                            {
                                _bgw.ReportProgress(0, new bgwShowError(f.FullName, "gz File corrupt"));
                                if (!Directory.Exists("corrupt"))
                                {
                                    Directory.CreateDirectory("corrupt");
                                }
                                File.Move(f.FullName, Path.Combine("corrupt", f.Name));
                                continue;
                            }
                        }
                        tFile.DBWrite();
                    }
                }
                if (_bgw.CancellationPending)
                {
                    return;
                }
            }

            DirectoryInfo[] childdi = di.GetDirectories();
            foreach (DirectoryInfo d in childdi)
            {
                if (_bgw.CancellationPending)
                {
                    return;
                }
                ScanRomRoot(d.FullName);
            }
        }
Example #17
0
        public static TrrntZipStatus ReZipFiles(List <ZippedFile> zippedFiles, ICompress originalZipFile, byte[] buffer, StatusCallback statusCallBack, LogCallback logCallback, int threadId)
        {
            zipType inputType;

            switch (originalZipFile)
            {
            case Zip _:
                inputType = zipType.zip;
                break;

            case SevenZ _:
                inputType = zipType.sevenzip;
                break;

            case Compress.File.File _:
                inputType = zipType.iso;
                break;

            default:
                return(TrrntZipStatus.Unknown);
            }

            zipType outputType = Program.OutZip == zipType.both ? inputType : Program.OutZip;

            if (outputType == zipType.iso)
            {
                outputType = zipType.zip;
            }

            int bufferSize = buffer.Length;

            string filename    = originalZipFile.ZipFilename;
            string tmpFilename = Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename) + ".tmp");

            string outExt      = outputType == zipType.zip ? ".zip" : ".7z";
            string outfilename = Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename) + outExt);

            if (inputType != outputType)
            {
                if (File.Exists(outfilename))
                {
                    logCallback?.Invoke(threadId, "Error output " + outExt + " file already exists");
                    return(TrrntZipStatus.RepeatFilesFound);
                }
            }

            if (File.Exists(tmpFilename))
            {
                File.Delete(tmpFilename);
            }

            ICompress zipFileOut = outputType == zipType.zip ? new Zip() : (ICompress) new SevenZ();

            try
            {
                zipFileOut.ZipFileCreate(tmpFilename);


                ulong fileSizeTotal       = 0;
                ulong fileSizeProgress    = 0;
                int   filePercentReported = 20;
                foreach (ZippedFile f in zippedFiles)
                {
                    fileSizeTotal += f.Size;
                }

                // by now the zippedFiles have been sorted so just loop over them
                foreach (ZippedFile t in zippedFiles)
                {
                    if (Program.VerboseLogging)
                    {
                        logCallback?.Invoke(threadId, $"{t.Size,15}  {t.StringCRC}   {t.Name}");
                    }

                    Stream readStream = null;
                    ulong  streamSize = 0;

                    ZipReturn zrInput = ZipReturn.ZipUntested;
                    switch (originalZipFile)
                    {
                    case Zip z:
                        zrInput = z.ZipFileOpenReadStream(t.Index, false, out readStream, out streamSize, out ushort _);
                        break;

                    case SevenZ z7:
                        zrInput = z7.ZipFileOpenReadStream(t.Index, out readStream, out streamSize);
                        break;

                    case Compress.File.File zf:
                        zrInput = zf.ZipFileOpenReadStream(t.Index, out readStream, out streamSize);
                        break;
                    }


                    ZipReturn zrOutput = zipFileOut.ZipFileOpenWriteStream(false, true, t.Name, streamSize, 8, out Stream writeStream);

                    if ((zrInput != ZipReturn.ZipGood) || (zrOutput != ZipReturn.ZipGood))
                    {
                        //Error writing local File.
                        zipFileOut.ZipFileClose();
                        originalZipFile.ZipFileClose();
                        File.Delete(tmpFilename);
                        return(TrrntZipStatus.CorruptZip);
                    }

                    Stream crcCs = new CrcCalculatorStream(readStream, true);

                    ulong sizetogo = streamSize;
                    while (sizetogo > 0)
                    {
                        int sizenow = sizetogo > (ulong)bufferSize ? bufferSize : (int)sizetogo;

                        fileSizeProgress += (ulong)sizenow;
                        int filePercent = (int)((double)fileSizeProgress / fileSizeTotal * 20);
                        if (filePercent != filePercentReported)
                        {
                            statusCallBack?.Invoke(threadId, filePercent * 5);
                            filePercentReported = filePercent;
                        }

                        crcCs.Read(buffer, 0, sizenow);
                        writeStream.Write(buffer, 0, sizenow);
                        sizetogo = sizetogo - (ulong)sizenow;
                    }
                    writeStream.Flush();

                    crcCs.Close();
                    if (inputType != zipType.sevenzip)
                    {
                        originalZipFile.ZipFileCloseReadStream();
                    }

                    uint crc = (uint)((CrcCalculatorStream)crcCs).Crc;

                    if (t.CRC == null)
                    {
                        t.CRC = crc;
                    }

                    if (crc != t.CRC)
                    {
                        return(TrrntZipStatus.CorruptZip);
                    }

                    zipFileOut.ZipFileCloseWriteStream(t.ByteCRC);
                }
                statusCallBack?.Invoke(threadId, 100);

                zipFileOut.ZipFileClose();
                originalZipFile.ZipFileClose();
                File.Delete(filename);
                File.Move(tmpFilename, outfilename);

                return(TrrntZipStatus.ValidTrrntzip);
            }
            catch (Exception)
            {
                zipFileOut?.ZipFileCloseFailed();
                originalZipFile?.ZipFileClose();
                return(TrrntZipStatus.CorruptZip);
            }
        }
Example #18
0
        public static RvFile FromADir(RvFile dbDir, EScanLevel eScanLevel, ThreadWorker bgw, ref bool fileErrorAbort)
        {
            string    fullDir   = dbDir.FullName;
            DatStatus datStatus = dbDir.IsInToSort ? DatStatus.InToSort : DatStatus.NotInDat;

            RvFile fileDir = new RvFile(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)
            {
                RvFile tDir = new RvFile(FileType.Dir)
                {
                    Name             = dir.Name,
                    FileModTimeStamp = dir.LastWriteTime
                };
                tDir.SetStatus(datStatus, GotStatus.Got);
                fileDir.ChildAdd(tDir);
            }

            // add all the files into scanDir
            foreach (FileInfo oFile in oFiles)
            {
                string fName = oFile.Name;
                if (fName == "__RomVault.tmp")
                {
                    File.Delete(oFile.FullName);
                    continue;
                }
                string fExt = Path.GetExtension(oFile.Name);

                FileType ft = DBTypeGet.fromExtention(fExt);

                if (Settings.rvSettings.FilesOnly)
                {
                    ft = FileType.File;
                }

                RvFile tFile = new RvFile(ft)
                {
                    Name             = oFile.Name,
                    Size             = (ulong)oFile.Length,
                    FileModTimeStamp = oFile.LastWriteTime
                };
                tFile.FileStatusSet(FileStatus.SizeVerified);
                tFile.SetStatus(datStatus, GotStatus.Got);

                if (eScanLevel == EScanLevel.Level3 && tFile.FileType == FileType.File)
                {
                    FromAFile(tFile, fullDir, eScanLevel, bgw, ref fileErrorAbort);
                }

                fileDir.ChildAdd(tFile);
            }
            return(fileDir);
        }
Example #19
0
        private void ReadConfig()
        {
            if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RomVault2cfg.xml")))
            {
                using (StreamReader sr = new StreamReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RomVault2cfg.xml")))
                {
                    XmlSerializer x = new XmlSerializer(Program.rvSettings.GetType());
                    Program.rvSettings = (Settings)x.Deserialize(sr);
                }
                return;
            }

            if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RomVault2.cfg")))
            {
                FileStream   fs = new FileStream(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RomVault2.cfg"), FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);

                int ver = br.ReadInt32();
                if (ver == 1)
                {
                    DatRoot   = br.ReadString();
                    ScanLevel = eScanLevel.Level1;
                    FixLevel  = (eFixLevel)br.ReadInt32();

                    IgnoreFiles = new List <string>();
                    int c = br.ReadInt32();
                    for (int i = 0; i < c; i++)
                    {
                        IgnoreFiles.Add(br.ReadString());
                    }

                    DirPathMap = new List <DirMap>();
                    c          = br.ReadInt32();
                    for (int i = 0; i < c; i++)
                    {
                        DirPathMap.Add(new DirMap(br.ReadString(), br.ReadString()));
                    }
                }
                if (ver == 2)
                {
                    DatRoot   = br.ReadString();
                    ScanLevel = (eScanLevel)br.ReadInt32();
                    FixLevel  = (eFixLevel)br.ReadInt32();

                    IgnoreFiles = new List <string>();
                    int c = br.ReadInt32();
                    for (int i = 0; i < c; i++)
                    {
                        IgnoreFiles.Add(br.ReadString());
                    }

                    DirPathMap = new List <DirMap>();
                    c          = br.ReadInt32();
                    for (int i = 0; i < c; i++)
                    {
                        DirPathMap.Add(new DirMap(br.ReadString(), br.ReadString()));
                    }
                }
                if (ver == 3)
                {
                    DatRoot          = br.ReadString();
                    ScanLevel        = (eScanLevel)br.ReadInt32();
                    FixLevel         = (eFixLevel)br.ReadInt32();
                    DebugLogsEnabled = br.ReadBoolean();

                    IgnoreFiles = new List <string>();
                    int c = br.ReadInt32();
                    for (int i = 0; i < c; i++)
                    {
                        IgnoreFiles.Add(br.ReadString());
                    }

                    DirPathMap = new List <DirMap>();
                    c          = br.ReadInt32();
                    for (int i = 0; i < c; i++)
                    {
                        DirPathMap.Add(new DirMap(br.ReadString(), br.ReadString()));
                    }
                }

                if (ver == 4)
                {
                    DatRoot          = br.ReadString();
                    ScanLevel        = (eScanLevel)br.ReadInt32();
                    FixLevel         = (eFixLevel)br.ReadInt32();
                    DebugLogsEnabled = br.ReadBoolean();

                    IgnoreFiles = new List <string>();
                    int c = br.ReadInt32();
                    for (int i = 0; i < c; i++)
                    {
                        IgnoreFiles.Add(br.ReadString());
                    }

                    DirPathMap = new List <DirMap>();
                    c          = br.ReadInt32();
                    for (int i = 0; i < c; i++)
                    {
                        DirPathMap.Add(new DirMap(br.ReadString(), br.ReadString()));
                    }

                    CacheSaveTimerEnabled = br.ReadBoolean();
                    CacheSaveTimePeriod   = br.ReadInt32();
                }

                if (ver == 5)
                {
                    DatRoot          = br.ReadString();
                    ScanLevel        = (eScanLevel)br.ReadInt32();
                    FixLevel         = (eFixLevel)br.ReadInt32();
                    DebugLogsEnabled = br.ReadBoolean();

                    IgnoreFiles = new List <string>();
                    int c = br.ReadInt32();
                    for (int i = 0; i < c; i++)
                    {
                        IgnoreFiles.Add(br.ReadString());
                    }

                    DirPathMap = new List <DirMap>();
                    c          = br.ReadInt32();
                    for (int i = 0; i < c; i++)
                    {
                        DirPathMap.Add(new DirMap(br.ReadString(), br.ReadString()));
                    }

                    CacheSaveTimerEnabled = br.ReadBoolean();
                    CacheSaveTimePeriod   = br.ReadInt32();

                    DoubleCheckDelete = br.ReadBoolean();
                }

                if (ver == 6)
                {
                    DatRoot          = br.ReadString();
                    ScanLevel        = (eScanLevel)br.ReadInt32();
                    FixLevel         = (eFixLevel)br.ReadInt32();
                    DebugLogsEnabled = br.ReadBoolean();
                    bool UserLongFilenames = br.ReadBoolean();

                    IgnoreFiles = new List <string>();
                    int c = br.ReadInt32();
                    for (int i = 0; i < c; i++)
                    {
                        IgnoreFiles.Add(br.ReadString());
                    }

                    DirPathMap = new List <DirMap>();
                    c          = br.ReadInt32();
                    for (int i = 0; i < c; i++)
                    {
                        DirPathMap.Add(new DirMap(br.ReadString(), br.ReadString()));
                    }

                    CacheSaveTimerEnabled = br.ReadBoolean();
                    CacheSaveTimePeriod   = br.ReadInt32();

                    DoubleCheckDelete = br.ReadBoolean();
                }


                br.Close();
                fs.Close();
            }
        }
Example #20
0
        private static ReturnCode OpenOutputStream(RvFile fileOut, RvFile fileIn, ref ICompress zipFileOut, string zipFilenameOut, ushort compressionMethod, bool rawCopy, bool sourceTrrntzip, out Stream writeStream, out string error)
        {
            writeStream = null;

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

                    if (fileOut.FileType == FileType.ZipFile)
                    {
                        zipFileOut = new ZipFile();
                    }
                    else
                    {
                        zipFileOut = new SevenZ();
                    }

                    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 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 (File.Exists(zipFilenameOut) && (fileOut.GotStatus != GotStatus.Corrupt))
                {
                    error = "Rescan needed, File Changed :" + zipFilenameOut;
                    return(ReturnCode.RescanNeeded);
                }
                int errorCode = FileStream.OpenFileWrite(zipFilenameOut, out writeStream);
                if (errorCode != 0)
                {
                    error = new Win32Exception(errorCode).Message + ". " + zipFilenameOut;
                    return(ReturnCode.FileSystemError);
                }
            }


            error = "";
            return(ReturnCode.Good);
        }
Example #21
0
        private static ReturnCode OpenInputStream(RvFile fileIn, bool rawCopy, out ICompress zipFileIn, out bool sourceTrrntzip, out Stream readStream, out ulong streamSize, out ushort compressionMethod, out string error)
        {
            zipFileIn         = null;
            sourceTrrntzip    = false;
            readStream        = null;
            streamSize        = 0;
            compressionMethod = 0;

            if (fileIn.FileType == FileType.ZipFile || fileIn.FileType == FileType.SevenZipFile) // Input is a ZipFile
            {
                RvFile zZipFileIn = fileIn.Parent;
                if (zZipFileIn.FileType != DBTypeGet.DirFromFile(fileIn.FileType))
                {
                    error = "File Open but Source File is not correct type, Logic Error.";
                    return(ReturnCode.LogicError);
                }

                string    fileNameIn = zZipFileIn.FullNameCase;
                ZipReturn zr1;


                if (zZipFileIn.FileType == FileType.SevenZip)
                {
                    sourceTrrntzip = false;
                    zipFileIn      = new SevenZ();
                    zr1            = zipFileIn.ZipFileOpen(fileNameIn, zZipFileIn.FileModTimeStamp);
                }
                else
                {
                    sourceTrrntzip = (zZipFileIn.ZipStatus & ZipStatus.TrrntZip) == ZipStatus.TrrntZip;
                    zipFileIn      = new Zip();
                    zr1            = zipFileIn.ZipFileOpen(fileNameIn, zZipFileIn.FileModTimeStamp, fileIn.ZipFileHeaderPosition == null);
                }

                switch (zr1)
                {
                case ZipReturn.ZipGood:
                    break;

                case ZipReturn.ZipErrorFileNotFound:
                    error = "File not found, Rescan required before fixing " + fileIn.Name;
                    return(ReturnCode.RescanNeeded);

                case ZipReturn.ZipErrorTimeStamp:
                    error = "File has changed, Rescan required before fixing " + fileIn.Name;
                    return(ReturnCode.RescanNeeded);

                default:
                    error = "Error Open Zip" + zr1 + ", with File " + fileIn.DatTreeFullName;
                    return(ReturnCode.FileSystemError);
                }

                if (fileIn.FileType == FileType.SevenZipFile)
                {
                    ((SevenZ)zipFileIn).ZipFileOpenReadStream(fileIn.ZipFileIndex, out readStream, out streamSize);
                }
                else
                {
                    if (fileIn.ZipFileHeaderPosition != null)
                    {
                        ((Zip)zipFileIn).ZipFileOpenReadStreamQuick((ulong)fileIn.ZipFileHeaderPosition, rawCopy, out readStream, out streamSize, out compressionMethod);
                    }
                    else
                    {
                        ((Zip)zipFileIn).ZipFileOpenReadStream(fileIn.ZipFileIndex, rawCopy, out readStream, out streamSize, out compressionMethod);
                    }
                }
            }
            else // Input is a regular file
            {
                string fileNameIn = fileIn.FullName;
                if (!File.Exists(fileNameIn))
                {
                    error = "Rescan needed, File Not Found :" + fileNameIn;
                    return(ReturnCode.RescanNeeded);
                }
                FileInfo fileInInfo = new FileInfo(fileNameIn);
                if (fileInInfo.LastWriteTime != fileIn.FileModTimeStamp)
                {
                    error = "Rescan needed, File Changed :" + fileNameIn;
                    return(ReturnCode.RescanNeeded);
                }
                int errorCode = 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);
                }
            }

            error = "";
            return(ReturnCode.Good);
        }
Example #22
0
        public static hdErr CheckFile(string file, string directory, bool isLinux, ref bool deepCheck, out uint?chdVersion, out byte[] chdSHA1, out byte[] chdMD5, ref bool fileErrorAbort)
        {
            chdSHA1    = null;
            chdMD5     = null;
            chdVersion = null;
            string filename = Path.Combine(directory, file);

            fileProcess?.Invoke(filename);

            //string ext = Path.GetExtension(filename).ToLower();
            //if (ext != ".chd")
            //{
            //    return hdErr.HDERR_INVALID_FILE;
            //}

            if (!File.Exists(filename))
            {
                fileSystemError?.Invoke("File: " + filename + " Error: File Could not be opened.");
                fileErrorAbort = true;
                return(hdErr.HDERR_CANNOT_OPEN_FILE);
            }

            Stream s;
            int    retval = FileStream.OpenFileRead(filename, out s);

            if (retval != 0)
            {
                fileSystemError?.Invoke("File: " + filename + " Error: File Could not be opened.");
                fileErrorAbort = true;
                return(hdErr.HDERR_CANNOT_OPEN_FILE);
            }
            if (s == null)
            {
                fileSystemError?.Invoke("File: " + filename + " Error: File Could not be opened.");
                fileErrorAbort = true;
                return(hdErr.HDERR_CANNOT_OPEN_FILE);
            }
            if (s.Length < MaxHeader)
            {
                s.Close();
                s.Dispose();
                return(hdErr.HDERR_INVALID_FILE);
            }
            hard_disk_info hdi = new hard_disk_info();
            hdErr          res = ReadCHDHeader(s, ref hdi);

            if (res != hdErr.HDERR_NONE)
            {
                return(res);
            }
            chdVersion = hdi.version;
            chdMD5     = hdi.md5;
            chdSHA1    = hdi.sha1;


            if (!deepCheck)
            {
                s.Close();
                s.Dispose();
                return(res);
            }

            string error = null;

            if (hdi.version < 4 && hdi.compression < 3)
            {
                hdi.file = s;
                CHDLocalCheck clc = new CHDLocalCheck();
                res = clc.ChdCheck(fileProgress, hdi, out error);

                s.Close();
                s.Dispose();
            }
            else
            {
                s.Close();
                s.Dispose();

                CHDManCheck cmc = new CHDManCheck();
                res = cmc.ChdCheck(fileProgress, isLinux, filename, out error);
            }

            switch (res)
            {
            case hdErr.HDERR_NONE:
                break;

            case hdErr.HDERR_CHDMAN_NOT_FOUND:
                deepCheck = false;
                res       = hdErr.HDERR_NONE;
                break;

            case hdErr.HDERR_DECOMPRESSION_ERROR:
                fileError?.Invoke(filename, error);
                break;

            case hdErr.HDERR_FILE_NOT_FOUND:
                fileSystemError?.Invoke("File: " + filename + " Error: Not Found scan Aborted.");
                fileErrorAbort = true;
                break;

            default:
                generalError?.Invoke(res + " " + error);
                break;
            }

            return(res);
        }
Example #23
0
        // This Function returns:
        // Good            : Everything Worked Correctly
        // RescanNeeded     : Something unexpectedly changed in the files, so Stop fixing and prompt user to rescan.
        // LogicError       : This Should never happen and is a logic problem in the code.
        // FileSystemError  : Something is wrong with the files, like it was locked and could not be opened.
        // SourceDataStreamCorrupt : This happens when either zlib returns ZlibException, or the CRC does not match the extracted zip.
        // SourceCheckSumMismatch  : If the extracted files does not match its expected SHA1 or MD5
        // DestinationCheckSumMismatch : If the extracted files does not match the file to be fixed expected CRC,SHA1 or MD5.


        /// <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 written to.</param>
        /// <param name="filenameOut">This is the name of the file to be written to if we are just making a file</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>
        /// <returns>ReturnCode.Good is the valid return code otherwise we have an error</returns>
        public static ReturnCode CopyFile(RvFile fileIn, ICompress zipFileOut, string filenameOut, RvFile fileOut, bool forceRaw, out string error)
        {
            if (zipFileOut == null && filenameOut == null)
            {
                throw new Exception("Error in CopyFile: Both Outputs are null");
            }
            if (zipFileOut != null && filenameOut != null)
            {
                throw new Exception("Error in CopyFile: Both Outputs are set");
            }

            ICompress  zipFileIn  = null;
            Stream     readStream = null;
            ThreadCRC  tcrc32     = null;
            ThreadMD5  tmd5       = null;
            ThreadSHA1 tsha1      = null;


            ReturnCode retC;

            error = "";

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

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

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



            bool isZeroLengthFile = DBHelper.IsZeroLengthFile(fileOut);

            if (!isZeroLengthFile)
            {
                //check that the in and out file match
                retC = CheckInputAndOutputFile(fileIn, fileOut, out error);
                if (retC != ReturnCode.Good)
                {
                    return(retC);
                }

                //Find and Check/Open Input Files
                retC = OpenInputStream(fileIn, rawCopy, out zipFileIn, out sourceTrrntzip, out readStream, out streamSize, out compressionMethod, out error);
                if (retC != ReturnCode.Good)
                {
                    return(retC);
                }
            }
            else
            {
                sourceTrrntzip = true;
            }

            if (!rawCopy)
            {
                compressionMethod = 8;
            }

            //Find and Check/Open Output Files
            retC = OpenOutputStream(fileOut, fileIn, zipFileOut, filenameOut, compressionMethod, rawCopy, sourceTrrntzip, null, out Stream writeStream, out error);
            if (retC != ReturnCode.Good)
            {
                return(retC);
            }

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


                if (!rawCopy)
                {
                    tcrc32 = new ThreadCRC();
                    if (Settings.rvSettings.FixLevel != EFixLevel.Level1)
                    {
                        tmd5  = new ThreadMD5();
                        tsha1 = new ThreadSHA1();
                    }
                }

                ulong sizetogo = streamSize;

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

                    try
                    {
                        readStream.Read(_buffer, 0, sizenow);
                    }
                    catch (Exception ex)
                    {
                        if (ex is ZlibException || ex is DataErrorException)
                        {
                            if ((fileIn.FileType == FileType.ZipFile || fileIn.FileType == FileType.SevenZipFile) && zipFileIn != null)
                            {
                                ZipReturn zr = zipFileIn.ZipFileCloseReadStream();
                                if (zr != ZipReturn.ZipGood)
                                {
                                    error = "Error Closing " + zr + " Stream :" + zipFileIn.ZipFilename;
                                    return(ReturnCode.FileSystemError);
                                }

                                zipFileIn.ZipFileClose();
                            }
                            else
                            {
                                readStream.Close();
                            }

                            writeStream.Flush();
                            writeStream.Close();
                            if (filenameOut != null)
                            {
                                File.Delete(filenameOut);
                            }

                            error = "Unexpected corrupt archive file found:\n" + fileIn.FullName +
                                    "\nRun Find Fixes, and Fix to continue fixing correctly.";
                            return(ReturnCode.SourceDataStreamCorrupt);
                        }

                        error = "Error reading Source File " + ex.Message;
                        return(ReturnCode.FileSystemError);
                    }

                    tcrc32?.Trigger(_buffer, sizenow);
                    tmd5?.Trigger(_buffer, sizenow);
                    tsha1?.Trigger(_buffer, sizenow);
                    try
                    {
                        writeStream.Write(_buffer, 0, sizenow);
                    }
                    catch (Exception e)
                    {
                        error = "Error writing out file. " + Environment.NewLine + e.Message;
                        return(ReturnCode.FileSystemError);
                    }
                    tcrc32?.Wait();
                    tmd5?.Wait();
                    tsha1?.Wait();
                    sizetogo = sizetogo - (ulong)sizenow;


                    if (Report.CancellationPending())
                    {
                        tcrc32?.Dispose();
                        tmd5?.Dispose();
                        tsha1?.Dispose();

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

                        if (fileOut.FileType == FileType.ZipFile || fileOut.FileType == FileType.SevenZipFile)
                        {
                            zipFileOut.ZipFileCloseFailed();
                        }
                        else
                        {
                            writeStream.Flush();
                            writeStream.Close();
                        }

                        return(ReturnCode.Cancel);
                    }
                }
                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)
                {
                    tcrc32.Finish();
                    tmd5?.Finish();
                    tsha1?.Finish();

                    bCRC  = tcrc32.Hash;
                    bMD5  = tmd5?.Hash;
                    bSHA1 = tsha1?.Hash;

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

                #endregion

                #region close the ReadStream

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

                #endregion
            }
            else
            {
                CopyZeroLengthFile(fileOut, zipFileOut, out bCRC, out bMD5, out bSHA1);
            }

            #region close the WriteStream

            if (fileOut.FileType == FileType.ZipFile || fileOut.FileType == FileType.SevenZipFile)
            {
                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.GetLocalFile(fileOut.ZipFileIndex).LocalHead;
                fileOut.FileModTimeStamp      = 629870671200000000;
            }
            else
            {
                writeStream.Flush();
                writeStream.Close();
                FileInfo fi = new FileInfo(filenameOut);
                fileOut.FileModTimeStamp = fi.LastWriteTime;
            }

            #endregion

            if (!isZeroLengthFile)
            {
                if (!rawCopy)
                {
                    retC = ValidateFileIn(fileIn, bCRC, bSHA1, bMD5, out error);
                    if (retC != ReturnCode.Good)
                    {
                        return(retC);
                    }
                }
            }

            retC = ValidateFileOut(fileIn, fileOut, rawCopy, bCRC, bSHA1, bMD5, out error);
            if (retC != ReturnCode.Good)
            {
                return(retC);
            }


            return(ReturnCode.Good);
        }
Example #24
0
        public static ReturnCode MoveFile(RvFile fileIn, RvFile fileOut, string outFilename, out bool fileMoved, out string error)
        {
            error     = "";
            fileMoved = false;

            bool fileMove = TestFileMove(fileIn, fileOut);

            if (!fileMove)
            {
                return(ReturnCode.Good);
            }

            byte[] bCRC;
            byte[] bMD5  = null;
            byte[] bSHA1 = null;

            string fileNameIn = fileIn.FullName;

            if (!File.Exists(fileNameIn))
            {
                error = "Rescan needed, File Not Found :" + fileNameIn;
                return(ReturnCode.RescanNeeded);
            }
            FileInfo fileInInfo = new FileInfo(fileNameIn);

            if (fileInInfo.LastWriteTime != fileIn.FileModTimeStamp)
            {
                error = "Rescan needed, File Changed :" + fileNameIn;
                return(ReturnCode.RescanNeeded);
            }

            string fileNameOut = outFilename ?? fileOut.FullName;

            File.Move(fileNameIn, fileNameOut);

            bCRC = fileIn.CRC.Copy();
            if (fileIn.FileStatusIs(FileStatus.MD5Verified))
            {
                bMD5 = fileIn.MD5.Copy();
            }

            if (fileIn.FileStatusIs(FileStatus.SHA1Verified))
            {
                bSHA1 = fileIn.SHA1.Copy();
            }

            FileInfo fi = new FileInfo(fileNameOut);

            fileOut.FileModTimeStamp = fi.LastWriteTime;

            ReturnCode retC = ValidateFileOut(fileIn, fileOut, true, bCRC, bSHA1, bMD5, out error);

            if (retC != ReturnCode.Good)
            {
                return(retC);
            }

            CheckDeleteFile(fileIn);

            fileMoved = true;
            return(ReturnCode.Good);
        }
Example #25
0
        public static RvFile FromADir(RvFile dbDir, EScanLevel eScanLevel, ThreadWorker bgw, ref bool fileErrorAbort)
        {
            string    fullDir   = dbDir.FullName;
            DatStatus datStatus = dbDir.IsInToSort ? DatStatus.InToSort : DatStatus.NotInDat;

            RvFile fileDir = new RvFile(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)
            {
                RvFile tDir = new RvFile(FileType.Dir)
                {
                    Name             = dir.Name,
                    FileModTimeStamp = dir.LastWriteTime
                };
                tDir.SetStatus(datStatus, GotStatus.Got);
                fileDir.ChildAdd(tDir);
            }

            // add all the files into scanDir
            foreach (FileInfo oFile in oFiles)
            {
                string fName = oFile.Name;
                if (fName == "__RomVault.tmp")
                {
                    File.Delete(oFile.FullName);
                    continue;
                }
                string fExt = Path.GetExtension(oFile.Name);

                FileType ft = DBTypeGet.fromExtention(fExt);

                if (Settings.rvSettings.FilesOnly)
                {
                    ft = FileType.File;
                }

                RvFile tFile = new RvFile(ft)
                {
                    Name             = oFile.Name,
                    Size             = (ulong)oFile.Length,
                    FileModTimeStamp = oFile.LastWriteTime
                };
                tFile.FileStatusSet(FileStatus.SizeVerified);
                tFile.SetStatus(datStatus, GotStatus.Got);

                if (eScanLevel == EScanLevel.Level3 && tFile.FileType == FileType.File)
                {
                    FromAFile(tFile, fullDir, eScanLevel, bgw, ref fileErrorAbort);
                }

                fileDir.ChildAdd(tFile);

                /*
                 * // if we find a zip file add it as zip files.
                 * // else
                 * if (ft == FileType.File)
                 * {
                 * // 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
                 *
                 * errorCode = CHD.CheckFile(oFile, out tFile.AltSHA1, out tFile.AltMD5, out tFile.CHDVersion);
                 *
                 * if (errorCode == 0)
                 * {
                 *  if (tFile.AltSHA1 != null)
                 *  {
                 *      tFile.FileStatusSet(FileStatus.AltSHA1FromHeader);
                 *  }
                 *  if (tFile.AltMD5 != null)
                 *  {
                 *      tFile.FileStatusSet(FileStatus.AltMD5FromHeader);
                 *  }
                 *
                 *  // if we are scanning at Level3 then we get all the info here
                 *  if (EScanLevel == EScanLevel.Level3)
                 *  {
                 *      FileResults(fullDir, tFile);
                 *      ChdManCheck(fullDir, tFile);
                 *  }
                 * }
                 * else if (errorCode == 32)
                 * {
                 *  tFile.GotStatus = GotStatus.FileLocked;
                 *  _bgw.Report(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;
                 * }
                 * }
                 */
            }
            return(fileDir);
        }
Example #26
0
        private static bool LoadBytes(RvFile tGame, string filename, out byte[] memBuffer)
        {
            memBuffer = null;

            int cCount = tGame.ChildCount;

            if (cCount == 0)
            {
                return(false);
            }

            int found = -1;

            for (int i = 0; i < cCount; i++)
            {
                RvFile rvf = tGame.Child(i);
                if (rvf.Name != filename || rvf.GotStatus != GotStatus.Got)
                {
                    continue;
                }
                found = i;
                break;
            }

            if (found == -1)
            {
                return(false);
            }

            try
            {
                switch (tGame.FileType)
                {
                case FileType.Zip:
                {
                    RvFile imagefile = tGame.Child(found);
                    if (imagefile.ZipFileHeaderPosition == null)
                    {
                        return(false);
                    }

                    Zip zf = new Zip();
                    if (zf.ZipFileOpen(tGame.FullNameCase, tGame.FileModTimeStamp, false) != ZipReturn.ZipGood)
                    {
                        return(false);
                    }

                    if (zf.ZipFileOpenReadStreamQuick((ulong)imagefile.ZipFileHeaderPosition, false,
                                                      out Stream stream, out ulong streamSize, out ushort _) != ZipReturn.ZipGood)
                    {
                        zf.ZipFileClose();
                        return(false);
                    }

                    memBuffer = new byte[streamSize];
                    stream.Read(memBuffer, 0, (int)streamSize);
                    zf.ZipFileClose();
                    return(true);
                }

                case FileType.Dir:
                {
                    string dirPath = tGame.FullNameCase;
                    string artwork = Path.Combine(dirPath, filename);
                    if (!File.Exists(artwork))
                    {
                        return(false);
                    }

                    RVIO.FileStream.OpenFileRead(artwork, out Stream stream);
                    memBuffer = new byte[stream.Length];
                    stream.Read(memBuffer, 0, memBuffer.Length);
                    stream.Close();
                    stream.Dispose();
                    return(true);
                }

                default:
                    return(false);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(false);
            }
        }
Example #27
0
        // 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 ICompress 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;

            ICompress  zipFileIn  = null;
            Stream     readStream = null;
            ReturnCode retC;


            bool isZeroLengthFile = DBHelper.IsZeroLengthFile(fileOut);

            if (!isZeroLengthFile)
            {
                //check that the in and out file match
                retC = CheckInputAndOutputFile(fileIn, fileOut, out error);
                if (retC != ReturnCode.Good)
                {
                    return(retC);
                }

                //Find and Check/Open Input Files
                retC = OpenInputStream(fileIn, rawCopy, out zipFileIn, out sourceTrrntzip, out readStream, out streamSize, out compressionMethod, out error);
                if (retC != ReturnCode.Good)
                {
                    return(retC);
                }
            }
            else
            {
                sourceTrrntzip = true;
            }

            if (!rawCopy && ((Program.rvSettings.FixLevel == eFixLevel.TrrntZipLevel1) || (Program.rvSettings.FixLevel == eFixLevel.TrrntZipLevel2) || (Program.rvSettings.FixLevel == eFixLevel.TrrntZipLevel3)))
            {
                compressionMethod = 8;
            }

            //Find and Check/Open Output Files
            Stream writeStream;

            retC = OpenOutputStream(fileOut, fileIn, ref zipFileOut, zipFilenameOut, compressionMethod, rawCopy, sourceTrrntzip, out writeStream, out error);
            if (retC != ReturnCode.Good)
            {
                return(retC);
            }

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

                ThreadCRC  tcrc32 = null;
                ThreadMD5  tmd5   = null;
                ThreadSHA1 tsha1  = null;

                if (!rawCopy)
                {
                    tcrc32 = new ThreadCRC();
                    tmd5   = new ThreadMD5();
                    tsha1  = new ThreadSHA1();
                }

                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();
                            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)
                    {
                        tcrc32.Trigger(_buffer, sizenow);
                        tmd5.Trigger(_buffer, sizenow);
                        tsha1.Trigger(_buffer, sizenow);

                        tcrc32.Wait();
                        tmd5.Wait();
                        tsha1.Wait();
                    }
                    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)
                {
                    tcrc32.Finish();
                    tmd5.Finish();
                    tsha1.Finish();

                    bCRC  = tcrc32.Hash;
                    bMD5  = tmd5.Hash;
                    bSHA1 = tsha1.Hash;

                    tcrc32.Dispose();
                    tmd5.Dispose();
                    tsha1.Dispose();
                }
                // 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) || (fileIn.FileType == FileType.SevenZipFile)) && (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 (RVIO.File.Exists(tmpFilename))
                    //    RVIO.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) || (fileOut.FileType == FileType.SevenZipFile))
            {
                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();
                FileInfo fi = new 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
                        {
                            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) || (fileIn.FileType == FileType.SevenZipFile))
                        {
                            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
                        {
                            File.Delete(zipFilenameOut);
                        }

                        return(ReturnCode.SourceCheckSumError);
                    }
                }
            }

            if ((fileOut.FileType == FileType.ZipFile) || (fileOut.FileType == FileType.SevenZipFile))
            {
                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
                {
                    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
                    {
                        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
                    {
                        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);
        }