Esempio n. 1
0
        /// <summary>
        /// Print files from the store
        /// </summary>
        /// <param name="options">Print options, including filters</param>
        public void PrintFiles(QueryMetadataOptions options)
        {
            if (!string.IsNullOrEmpty(options.FileHash))
            {
                var hashBytes = new byte[options.FileHash.Length / 2];
                for (var i = 0; i < hashBytes.Length; i++)
                {
                    hashBytes[i] = System.Convert.ToByte(options.FileHash.Substring(i * 2, 2), 16);
                }

                var hashBase64 = Convert.ToBase64String(hashBytes);

                if (!MetadataSource.HasFile(hashBase64))
                {
                    ConsoleOutput.WriteRed($"Cannot find file hash {options.FileHash}");
                }

                var file = MetadataSource.GetFile(hashBase64);

                foreach (var update in MetadataSource.UpdatesIndex.Values)
                {
                    if (update.HasFiles)
                    {
                        foreach (var updateFile in update.Files)
                        {
                            if (updateFile.Digests.Any(d => d.HexString.Equals(options.FileHash, StringComparison.OrdinalIgnoreCase)))
                            {
                                PrintUpdateMetadata(update, MetadataSource);
                                break;
                            }
                        }
                    }
                }
            }
            else if (options.IdFilter.Count() > 0)
            {
                var idFilter = FilterBuilder.StringGuidsToGuids(options.IdFilter);
                if (idFilter == null)
                {
                    ConsoleOutput.WriteRed("The update ID filter must contain only GUIDs!");
                    return;
                }

                var update = MetadataSource.UpdatesIndex.Values.FirstOrDefault(u => u.Identity.ID.Equals(idFilter[0]));
                if (update == null)
                {
                    ConsoleOutput.WriteRed($"Cannot find update with GUID {idFilter[0]}");
                    return;
                }

                var files = GetAllUpdateFiles(MetadataSource, update);
                PrintFileDetails(files);
            }
        }
Esempio n. 2
0
        /// <summary>
        ///  Runs a query command against a metadata source
        /// </summary>
        /// <param name="options">Query options (filters)</param>
        public static void Query(QueryMetadataOptions options)
        {
            var source = Program.LoadMetadataSourceFromOptions(options as IMetadataSourceOptions);

            if (source == null)
            {
                return;
            }

            var repoQuery = new MetadataQuery(source, options);

            repoQuery.Query();
        }
Esempio n. 3
0
        /// <summary>
        /// Print updates from the store
        /// </summary>
        /// <param name="options">Print options, including filters</param>
        public void PrintUpdates(QueryMetadataOptions options)
        {
            var filter = FilterBuilder.MetadataFilterFromCommandLine(options as IMetadataFilterOptions);

            if (filter == null)
            {
                return;
            }

            filter.FirstX = options.FirstX;

            // Apply filters specified on the command line
            List <Update> filteredUpdates;

            if (options.Classifications || options.Products || options.Detectoids)
            {
                filteredUpdates = MetadataSource.GetCategories(filter);
                if (!options.Classifications)
                {
                    filteredUpdates.RemoveAll(u => u is Classification);
                }

                if (!options.Products)
                {
                    filteredUpdates.RemoveAll(u => u is Product);
                }

                if (!options.Detectoids)
                {
                    filteredUpdates.RemoveAll(u => u is Detectoid);
                }
            }
            else if (options.Updates || options.Drivers)
            {
                filteredUpdates = MetadataSource.GetUpdates(filter);

                if (options.Drivers)
                {
                    filteredUpdates.RemoveAll(u => !(u is DriverUpdate));
                }
            }
            else
            {
                filteredUpdates = new List <Update>();
            }

            if (filteredUpdates.Count == 0)
            {
                Console.WriteLine("No data found");
            }
            else
            {
                Console.Write("\r\nQuery results:\r\n-----------------------------");

                if (!options.CountOnly)
                {
                    foreach (var update in filteredUpdates)
                    {
                        PrintUpdateMetadata(update, MetadataSource);
                    }
                }

                Console.WriteLine("-----------------------------\r\nMatched {0} entries", filteredUpdates.Count);
            }
        }
Esempio n. 4
0
 private MetadataQuery(IMetadataSource metadataSource, QueryMetadataOptions options)
 {
     MetadataSource = metadataSource;
     Options        = options;
 }