/// <summary> /// Retrieve file information for a single file /// </summary> /// <param name="input">Filename to get information from</param> /// <param name="header">Populated string representing the name of the skipper to use, a blank string to use the first available checker, null otherwise</param> /// <param name="hashes">Hashes to include in the information</param> /// <param name="asFiles">TreatAsFiles representing special format scanning</param> /// <returns>Populated BaseFile object if success, empty one on error</returns> public static BaseFile GetInfo(string input, string header = null, Hash hashes = Hash.Standard, TreatAsFile asFiles = 0x00) { // Add safeguard if file doesn't exist if (!File.Exists(input)) { return(null); } // Get input information var fileType = GetFileType(input); Stream inputStream = File.OpenRead(input); // Try to match the supplied header skipper if (header != null) { SkipperMatch.Init(); var rule = SkipperMatch.GetMatchingRule(input, Path.GetFileNameWithoutExtension(header)); // If there's a match, transform the stream before getting info if (rule.Tests != null && rule.Tests.Count != 0) { // Create the output stream MemoryStream outputStream = new MemoryStream(); // Transform the stream and get the information from it rule.TransformStream(inputStream, outputStream, keepReadOpen: false, keepWriteOpen: true); inputStream = outputStream; } } // Get the info in the proper manner BaseFile baseFile; if (fileType == FileType.AaruFormat && !asFiles.HasFlag(TreatAsFile.AaruFormat)) { baseFile = AaruFormat.Create(inputStream); } else if (fileType == FileType.CHD && !asFiles.HasFlag(TreatAsFile.CHD)) { baseFile = CHDFile.Create(inputStream); } else { baseFile = GetInfo(inputStream, hashes: hashes, keepReadOpen: false); } // Dispose of the input stream inputStream?.Dispose(); // Add unique data from the file baseFile.Filename = Path.GetFileName(input); baseFile.Date = new FileInfo(input).LastWriteTime.ToString("yyyy/MM/dd HH:mm:ss"); return(baseFile); }
/// <summary> /// Check a given file for hashes, based on current settings /// </summary> /// <param name="datFile">Current DatFile object to add to</param> /// <param name="item">Filename of the item to be checked</param> /// <param name="basePath">Base folder to be used in creating the DAT</param> /// <param name="asFiles">TreatAsFiles representing CHD and Archive scanning</param> /// <param name="skipFileType">Type of files that should be skipped</param> /// <param name="addBlanks">True if blank items should be created for empty folders, false otherwise</param> /// <param name="hashes">Hashes to include in the information</param> private static void CheckFileForHashes( DatFile datFile, string item, string basePath, TreatAsFile asFiles, SkipFileType skipFileType, bool addBlanks, Hash hashes) { // If we're in depot mode, process it separately if (CheckDepotFile(datFile, item)) { return; } // Initialize possible archive variables BaseArchive archive = BaseArchive.Create(item); // Process archives according to flags if (archive != null) { // Set the archive flags archive.AvailableHashes = hashes; // Skip if we're treating archives as files and skipping files if (asFiles.HasFlag(TreatAsFile.Archive) && skipFileType == SkipFileType.File) { return; } // Skip if we're skipping archives else if (skipFileType == SkipFileType.Archive) { return; } // Process as archive if we're not treating archives as files else if (!asFiles.HasFlag(TreatAsFile.Archive)) { var extracted = archive.GetChildren(); // If we have internal items to process, do so if (extracted != null) { ProcessArchive(datFile, item, basePath, extracted); } // Now find all folders that are empty, if we are supposed to if (addBlanks) { ProcessArchiveBlanks(datFile, item, basePath, archive); } } // Process as file if we're treating archives as files else { ProcessFile(datFile, item, basePath, hashes, asFiles); } } // Process non-archives according to flags else { // Skip if we're skipping files if (skipFileType == SkipFileType.File) { return; } // Process as file else { ProcessFile(datFile, item, basePath, hashes, asFiles); } } }
/// <summary> /// Attempt to add a file to the output if it matches /// </summary> /// <param name="datFile">Current DatFile object to rebuild from</param> /// <param name="file">Name of the file to process</param> /// <param name="outDir">Output directory to use to build to</param> /// <param name="quickScan">True to enable external scanning of archives, false otherwise</param> /// <param name="date">True if the date from the DAT should be used if available, false otherwise</param> /// <param name="inverse">True if the DAT should be used as a filter instead of a template, false otherwise</param> /// <param name="outputFormat">Output format that files should be written to</param> /// <param name="asFiles">TreatAsFiles representing special format scanning</param> /// <returns>True if the file was used to rebuild, false otherwise</returns> private static bool RebuildGenericHelper( DatFile datFile, string file, string outDir, bool quickScan, bool date, bool inverse, OutputFormat outputFormat, TreatAsFile asFiles) { // If we somehow have a null filename, return if (file == null) { return(false); } // Set the deletion variables bool usedExternally = false, usedInternally = false; // Create an empty list of BaseFile for archive entries List <BaseFile> entries = null; // Get the TGZ and TXZ status for later GZipArchive tgz = new GZipArchive(file); XZArchive txz = new XZArchive(file); bool isSingleTorrent = tgz.IsTorrent() || txz.IsTorrent(); // Get the base archive first BaseArchive archive = BaseArchive.Create(file); // Now get all extracted items from the archive if (archive != null) { archive.AvailableHashes = quickScan ? Hash.CRC : Hash.Standard; entries = archive.GetChildren(); } // If the entries list is null, we encountered an error or have a file and should scan externally if (entries == null && File.Exists(file)) { BaseFile internalFileInfo = BaseFile.GetInfo(file, asFiles: asFiles); // Create the correct DatItem DatItem internalDatItem; if (internalFileInfo.Type == FileType.AaruFormat && !asFiles.HasFlag(TreatAsFile.AaruFormat)) { internalDatItem = new Media(internalFileInfo); } else if (internalFileInfo.Type == FileType.CHD && !asFiles.HasFlag(TreatAsFile.CHD)) { internalDatItem = new Disk(internalFileInfo); } else { internalDatItem = new Rom(internalFileInfo); } usedExternally = RebuildIndividualFile(datFile, internalDatItem, file, outDir, date, inverse, outputFormat); } // Otherwise, loop through the entries and try to match else { foreach (BaseFile entry in entries) { DatItem internalDatItem = DatItem.Create(entry); usedInternally |= RebuildIndividualFile(datFile, internalDatItem, file, outDir, date, inverse, outputFormat, !isSingleTorrent /* isZip */); } } return(usedExternally || usedInternally); }