/
SearchFile.cs
212 lines (180 loc) · 6.38 KB
/
SearchFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using DotNet.Globbing;
using System.IO.Compression;
using System.Xml;
namespace Searcher;
internal static class SearchFile
{
/// <summary>
/// Wrapper to pick the correct search function. Special cases for docx, pdf and zip files
/// </summary>
public static SearchResult FileContainsStringWrapper(string path, string text, IReadOnlyList<Glob> innerpatterns, StringComparison comparer, CancellationToken token)
{
if (path.EndsWith(".docx", CliOptions.FilenameComparison)) {
return DocxContainsString(path, text, comparer);
}
if (path.EndsWith(".pdf", CliOptions.FilenameComparison)) {
return PdfCheck.CheckPdfFile(path, text, comparer, token);
}
if (path.EndsWith(".zip", CliOptions.FilenameComparison) || Utils.IsZipArchive(path)) {
return CheckZipFile(path, text, innerpatterns, comparer, token);
}
return FileContainsString(path, text, comparer);
}
/// <summary>
/// General function to check if a file contains a given string
/// </summary>
private static SearchResult FileContainsString(string path, string text, StringComparison comparer)
{
if (string.IsNullOrEmpty(text)) {
return SearchResult.NotFound;
}
try {
using var file = new StreamReader(path);
while (!file.EndOfStream) {
var line = file.ReadLine();
if (line == null) {
continue;
}
if (line.Contains(text, comparer)) {
return SearchResult.Found;
}
}
}
catch {
// exceptions are not thrown by my code here, but potentially by the StreamReader
return SearchResult.Error;
}
return SearchResult.NotFound;
}
/// <summary>
/// Function to check if a docx contains a given string
/// </summary>
private static SearchResult DocxContainsString(string path, string text, StringComparison comparer)
{
if (string.IsNullOrEmpty(text)) {
return SearchResult.NotFound;
}
if (!path.EndsWith(".docx", CliOptions.FilenameComparison)) {
throw new Exception("Not a docx file");
}
try {
using var archive = ZipFile.OpenRead(path);
var documentEntry = archive.GetEntry("word/document.xml");
if (documentEntry == null) {
return SearchResult.NotFound;
}
using var stream = documentEntry.Open();
using var reader = XmlReader.Create(stream);
while (reader.Read()) {
if (reader.NodeType == XmlNodeType.Text && reader.Value.Contains(text, comparer)) {
return SearchResult.Found;
}
}
}
catch {
// exceptions are not thrown by my code here, but potentially by libraries
return SearchResult.Error;
}
return SearchResult.NotFound;
}
/// <summary>
/// Wrapper around zip search to handle nested zips
/// </summary>
private static SearchResult CheckZipFile(string path, string text, IReadOnlyList<Glob> innerpatterns, StringComparison comparer, CancellationToken token)
{
if (string.IsNullOrEmpty(text)) {
return SearchResult.NotFound;
}
try {
// this is an actual zip file, so open it as archive and then use the recursive function
using var archive = ZipFile.OpenRead(path);
return ZipInternals.RecursiveArchiveCheck(archive, text, innerpatterns, comparer, token) ? SearchResult.Found : SearchResult.NotFound;
}
catch (OperationCanceledException) {
// this is thrown when the user cancels the search
return SearchResult.NotFound;
}
catch {
return SearchResult.Error;
}
}
}
internal static class ZipInternals
{
/// <summary>
/// Given a zip archive, loop through and check the contents. Recursively calls for nested zips
/// </summary>
public static bool RecursiveArchiveCheck(ZipArchive archive, string text, IReadOnlyList<Glob> innerpatterns, StringComparison comparer, CancellationToken token)
{
foreach (var nestedEntry in archive.Entries) {
// loop through all entries in the nested zip file
token.ThrowIfCancellationRequested();
var found = false;
if (nestedEntry.FullName.EndsWith(".zip", CliOptions.FilenameComparison)) {
// its another nested zip file, we need to open it and search inside
using var nestedStream = nestedEntry.Open();
using var nestedArchive = new ZipArchive(nestedStream);
found = RecursiveArchiveCheck(nestedArchive, text, innerpatterns, comparer, token);
} else if (nestedEntry.FullName.EndsWith(".docx", CliOptions.FilenameComparison)) {
// this is a DOCX inside a zip
using var nestedStream = nestedEntry.Open();
using var nestedArchive = new ZipArchive(nestedStream);
found = DocxContainsString(nestedArchive, text, comparer);
} else if (nestedEntry.FullName.EndsWith(".pdf", CliOptions.FilenameComparison)) {
// this is a PDF inside a zip
found = PdfCheck.CheckStream(nestedEntry, text, comparer, token);
} else if (nestedEntry.FullName.EndsWith('/')) {
// its a folder, we can skip it
continue;
} else {
// this is an actual file, not a nested zip
//Debug.WriteLine($"Checking {nestedEntry.Name}");
// This fails when using FullName, because the '/' separator screws up the globbing
if (innerpatterns.Count == 0 || innerpatterns.Any(p => p.IsMatch(nestedEntry.Name))) {
found = GeneralContainsString(nestedEntry, text, comparer, token);
}
}
if (found) {
return true;
}
}
return false;
}
/// <summary>
/// Function to check if a docx inside ZIP contains a given string
/// </summary>
public static bool DocxContainsString(ZipArchive archive, string text, StringComparison comparer)
{
var documentEntry = archive.GetEntry("word/document.xml");
if (documentEntry == null) {
return false;
}
using var stream = documentEntry.Open();
using var reader = XmlReader.Create(stream);
while (reader.Read()) {
if (reader.NodeType == XmlNodeType.Text && reader.Value.Contains(text, comparer)) {
return true;
}
}
return false;
}
/// <summary>
/// Check an entry in a zip file for a given string
/// </summary>
public static bool GeneralContainsString(ZipArchiveEntry entry, string text, StringComparison comparer, CancellationToken token)
{
using var stream = entry.Open();
using var file = new StreamReader(stream);
while (!file.EndOfStream) {
var line = file.ReadLine();
if (line == null) {
continue;
}
if (line.Contains(text, comparer)) {
return true;
}
token.ThrowIfCancellationRequested();
}
return false;
}
}