public static string FindExePath(ConfigurationTemplate template) { if (template != null) { foreach (string path in GetSearchPaths(template.HintPath)) { FileFilter fileParams = new FileFilter(path, template.ExeFileName, string.Empty, false, false, false, true, -1, true, true, false, false, 0, 0, FileDateFilter.None, null, null, true); var exePath = SafeDirectory.EnumerateFiles(path, new string[] { template.ExeFileName }, null, fileParams).FirstOrDefault(); if (!string.IsNullOrEmpty(exePath)) { return(exePath); } } return(template.ExeFileName); } return(string.Empty); }
public static IList <FileData> EnumerateFiles(Stream input, string file, IList <string> includeSearchPatterns, FileFilter filter) { if (filter == null) { throw new ArgumentNullException(nameof(filter)); } List <FileData> results = new List <FileData>(); HashSet <string> hiddenDirectories = new HashSet <string>(); try { using (SevenZipExtractor extractor = new SevenZipExtractor(input)) { foreach (var fileInfo in extractor.ArchiveFileData) { FileData fileData = new FileData(file, fileInfo); var attr = (FileAttributes)fileInfo.Attributes; string innerFileName = fileInfo.FileName; int index = fileInfo.Index; if (innerFileName == "[no name]" && extractor.ArchiveFileData.Count == 1) { index = 0; innerFileName = Path.GetFileNameWithoutExtension(file); ArchiveFileInfo temp = Copy(fileInfo); temp.FileName = innerFileName; fileData = new FileData(file, temp); } if (fileInfo.IsDirectory) { if (!filter.IncludeHidden && attr.HasFlag(FileAttributes.Hidden) && !hiddenDirectories.Contains(innerFileName)) { hiddenDirectories.Add(innerFileName); } continue; } if (!filter.IncludeHidden) { string path = Path.GetDirectoryName(innerFileName); if (hiddenDirectories.Contains(path)) { continue; } } if (!filter.IncludeBinary) { using (Stream stream = new MemoryStream()) { extractor.ExtractFile(index, stream); stream.Seek(0, SeekOrigin.Begin); fileData.IsBinary = Utils.IsBinary(stream); } } if (includeSearchPatterns != null && includeSearchPatterns.Count > 0) { foreach (string pattern in includeSearchPatterns) { if (SafeDirectory.WildcardMatch(innerFileName, pattern, true)) { results.Add(fileData); } } } else { results.Add(fileData); } if (Utils.IsArchive(innerFileName)) { using (Stream stream = new MemoryStream()) { extractor.ExtractFile(index, stream); foreach (var item in EnumerateFiles(stream, file + ArchiveSeparator + innerFileName, includeSearchPatterns, filter)) { results.Add(item); } } } if (Utils.CancelSearch) { break; } } } } catch (Exception ex) { logger.Error(ex, string.Format(CultureInfo.CurrentCulture, "Failed to search inside archive '{0}'", file)); } return(results); }
/// <summary> /// Evaluates if a file should be included in the search results /// only checks filters that do not extract/read the file... /// call NeedsIncludeFileStream and IncludeFileStream if file needs /// to be extracted and read to evaluate /// </summary> public static bool IncludeFile(string fileName, string compositeFileName, FileFilter filter, FileData fileData, IList <string> includeSearchPatterns, IList <Regex> includeRegexPatterns, IList <Regex> excludeRegexPatterns) { try { if (includeSearchPatterns != null && includeSearchPatterns.Count > 0) { bool include = false; foreach (string pattern in includeSearchPatterns) { if (SafeDirectory.WildcardMatch(fileName, pattern, true)) { include = true; break; } } if (!include) { return(false); } } if (includeRegexPatterns != null && includeRegexPatterns.Count > 0) { bool include = false; foreach (var pattern in includeRegexPatterns) { if (pattern.IsMatch(fileName)) { include = true; break; } } if (!include) { return(false); } } // exclude this file? // wildcard exclude files are converted to regex foreach (var pattern in excludeRegexPatterns) { if (pattern.IsMatch(fileName)) { return(false); } } if (filter.SizeFrom > 0 || filter.SizeTo > 0) { long sizeKB = fileData.Length / 1000; if (filter.SizeFrom > 0 && sizeKB < filter.SizeFrom) { return(false); } if (filter.SizeTo > 0 && sizeKB > filter.SizeTo) { return(false); } } if (filter.DateFilter != FileDateFilter.None) { DateTime fileDate = filter.DateFilter == FileDateFilter.Created ? fileData.CreationTime : fileData.LastWriteTime; if (filter.StartTime.HasValue && fileDate < filter.StartTime.Value) { return(false); } if (filter.EndTime.HasValue && fileDate >= filter.EndTime.Value) { return(false); } } } catch (Exception ex) { logger.Error(ex, string.Format(CultureInfo.CurrentCulture, "Failed to search inside archive '{0}'", compositeFileName)); } return(true); }