Exemplo n.º 1
0
        private static void ProcessFile(string fileName)
        {
            _logger.Debug($"Checking if '{fileName}' is a SQLite file");
            if (SQLiteFile.IsSQLiteFile(fileName) == false)
            {
                if (_fluentCommandLineParser.Object.Hunt == false)
                {
                    _logger.Error($"\t'{fileName}' is not a SQLite file! Skipping...");
                }
                else
                {
                    _logger.Debug($"\t'{fileName}' is not a SQLite file! Skipping...");
                }

                return;
            }

            _logger.Debug($"'{fileName}' is a SQLite file!");

            if (_fluentCommandLineParser.Object.Dedupe)
            {
                var sha = File.GetHash(fileName, HashType.SHA1);

                if (SeenHashes.Contains(sha))
                {
                    _logger.Error($"Skipping '{fileName}' as a file with SHA-1 '{sha}' has already been processed");
                    Console.WriteLine();
                    return;
                }

                _logger.Debug($"Adding '{fileName}' SHA-1 '{sha}' to seen hashes collection");
                SeenHashes.Add(sha);
            }

            _logger.Warn($"Processing '{fileName}'...");

            ProcessedFiles.Add(fileName);

            var maps = new List <MapFile>();

            if (_fluentCommandLineParser.Object.Hunt)
            {
                maps = SQLMap.MapFiles.Values.ToList();
            }
            else
            {
                //only find maps tht match the db filename
                maps = SQLMap.MapFiles.Values.Where(t => string.Equals(t.FileName, Path.GetFileName(fileName),
                                                                       StringComparison.InvariantCultureIgnoreCase)).ToList();
            }

            var foundMap = false;

            //need to run thru each map and see if we get any IdentityQuery matches
            //process each one that matches
            foreach (var map in maps)
            {
                _logger.Debug($"Processing map '{map.Description}' with Id '{map.Id}'");

                var dbFactory = new OrmLiteConnectionFactory($"{fileName}", SqliteDialect.Provider);

                var baseTime = DateTimeOffset.UtcNow;

                using (var db = dbFactory.Open())
                {
                    _logger.Debug($"\tVerifying database via '{map.IdentifyQuery}'");

                    var id = db.ExecuteScalar <string>(map.IdentifyQuery);

                    if (string.Equals(id, map.IdentifyValue, StringComparison.InvariantCultureIgnoreCase) == false)
                    {
                        _logger.Error($"\tFor map w/ description '{map.Description}', got value '{id}' from IdentityQuery, but expected '{map.IdentifyValue}'. Queries will not be processed!");
                        continue;
                    }

                    //if we find any matches, its not unmatched anymore
                    foundMap = true;

                    _logger.Error($"\tMap queries found: {map.Queries.Count:N0}. Processing queries...");
                    foreach (var queryInfo in map.Queries)
                    {
                        _logger.Debug($"Processing query '{queryInfo.Name}'");

                        try
                        {
                            var results = db.Query <dynamic>(queryInfo.Query).ToList();

                            var outName =
                                $"{baseTime:yyyyMMddHHmmssffffff}_{map.CSVPrefix}_{queryInfo.BaseFileName}_{map.Id}.csv";

                            var fullOutName = Path.Combine(_fluentCommandLineParser.Object.CsvDirectory, outName);

                            if (results.Any() == false)
                            {
                                _logger.Warn($"\t '{queryInfo.Name}' did not return any results. CSV will not be saved.");
                                continue;
                            }

                            _logger.Info($"\tDumping '{queryInfo.Name}' to '{fullOutName}'");

                            using (var writer = new StreamWriter(new FileStream(fullOutName, FileMode.CreateNew)))
                            {
                                using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                                {
                                    //   csv.WriteRecords(results);

                                    foreach (var result in results)
                                    {
                                        result.SourceFile = fileName;
                                        csv.WriteRecord(result);
                                        csv.NextRecord();
                                    }

                                    // csv.WriteComment("");
                                    // csv.NextRecord();
                                    // csv.WriteComment($"SourceFile: {fileName}");
                                    // csv.NextRecord();
                                    csv.Flush();
                                    writer.Flush();
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            _logger.Fatal($"Error processing map '{map.Description}' with Id '{map.Id}' for query '{queryInfo.Name}':");
                            _logger.Error($"\t{e.Message.Replace("\r\n","\r\n\t")}");
                        }
                    }
                }
            }

            if (foundMap == false)
            {
                _logger.Info($"\tNo maps found for '{fileName}'. Adding to unmatched database list");
                UnmatchedDbs.Add(fileName);
            }

            Console.WriteLine();
        }