Skip to content

Commit 62475fa

Browse files
committed
Try around iterate directory to prevent popping out of external loop (#472)
* Try around iterate directory to prevent popping out of external loop * Switch to local function for iterate on directory.
1 parent a427627 commit 62475fa

File tree

1 file changed

+60
-54
lines changed

1 file changed

+60
-54
lines changed

Lib/Collectors/FileSystemCollector.cs

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -72,78 +72,84 @@ public override void ExecuteInternal()
7272
Roots.Add("/");
7373
}
7474
}
75-
Action<string>? IterateOnDirectory = null;
76-
IterateOnDirectory = Path =>
77-
{
78-
Log.Verbose("Started parsing {0}", Path);
7975

80-
// To optimize calls to du on non-windows platforms we run du on the whole directory ahead of time
81-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
76+
void TryIterateOnDirectory(string Path)
77+
{
78+
try
8279
{
83-
var exitCode = ExternalCommandRunner.RunExternalCommand("du", Path, out string StdOut, out string StdErr);
84-
if (exitCode == 0)
80+
Log.Verbose("Started parsing {0}", Path);
81+
82+
// To optimize calls to du on non-windows platforms we run du on the whole directory ahead of time
83+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
8584
{
86-
foreach (var line in StdOut.Split(Environment.NewLine))
85+
var exitCode = ExternalCommandRunner.RunExternalCommand("du", Path, out string StdOut, out string StdErr);
86+
if (exitCode == 0)
8787
{
88-
var fields = line.Split('\t');
89-
if (long.TryParse(fields[0], out long result))
88+
foreach (var line in StdOut.Split(Environment.NewLine))
9089
{
91-
sizesOnDisk[fields[1]] = result;
90+
var fields = line.Split('\t');
91+
if (long.TryParse(fields[0], out long result))
92+
{
93+
sizesOnDisk[fields[1]] = result;
94+
}
9295
}
9396
}
9497
}
95-
}
96-
97-
var files = Directory.EnumerateFiles(Path, "*", new System.IO.EnumerationOptions()
98-
{
99-
IgnoreInaccessible = true
100-
});
101-
foreach (var file in files)
102-
{
103-
Log.Verbose("Started parsing {0}", file);
104-
FileSystemObject obj = FilePathToFileSystemObject(file);
105-
if (obj != null)
106-
{
107-
HandleChange(obj);
10898

109-
// If we know how to handle this as an archive, and crawling archives is enabled
110-
if (opts.CrawlArchives && MiniMagic.DetectFileType(file) != ArchiveFileType.UNKNOWN)
99+
var files = Directory.EnumerateFiles(Path, "*", new System.IO.EnumerationOptions()
100+
{
101+
IgnoreInaccessible = true
102+
});
103+
foreach (var file in files)
104+
{
105+
Log.Verbose("Started parsing {0}", file);
106+
FileSystemObject obj = FilePathToFileSystemObject(file);
107+
if (obj != null)
111108
{
112-
Extractor extractor = new Extractor(new ExtractorOptions() { ExtractSelfOnFail = false });
113-
foreach (var fso in extractor.ExtractFile(file, !opts.SingleThread).Select(fileEntry => FileEntryToFileSystemObject(fileEntry)))
109+
HandleChange(obj);
110+
111+
// If we know how to handle this as an archive, and crawling archives is enabled
112+
if (opts.CrawlArchives && MiniMagic.DetectFileType(file) != ArchiveFileType.UNKNOWN)
114113
{
115-
HandleChange(fso);
114+
Extractor extractor = new Extractor(new ExtractorOptions() { ExtractSelfOnFail = false });
115+
foreach (var fso in extractor.ExtractFile(file, !opts.SingleThread).Select(fileEntry => FileEntryToFileSystemObject(fileEntry)))
116+
{
117+
HandleChange(fso);
118+
}
116119
}
117-
}
118120

119-
// TODO: Also try parse .DER as a key
120-
if (Path.EndsWith(".cer", StringComparison.CurrentCulture) ||
121-
Path.EndsWith(".der", StringComparison.CurrentCulture) ||
122-
Path.EndsWith(".p7b", StringComparison.CurrentCulture) ||
123-
Path.EndsWith(".pfx", StringComparison.CurrentCulture))
124-
{
125-
try
121+
// TODO: Also try parse .DER as a key
122+
if (Path.EndsWith(".cer", StringComparison.CurrentCulture) ||
123+
Path.EndsWith(".der", StringComparison.CurrentCulture) ||
124+
Path.EndsWith(".p7b", StringComparison.CurrentCulture) ||
125+
Path.EndsWith(".pfx", StringComparison.CurrentCulture))
126126
{
127-
using var certificate = new X509Certificate2(Path);
127+
try
128+
{
129+
using var certificate = new X509Certificate2(Path);
128130

129-
var certObj = new CertificateObject(
130-
StoreLocation: StoreLocation.LocalMachine.ToString(),
131-
StoreName: StoreName.Root.ToString(),
132-
Certificate: new SerializableCertificate(certificate));
131+
var certObj = new CertificateObject(
132+
StoreLocation: StoreLocation.LocalMachine.ToString(),
133+
StoreName: StoreName.Root.ToString(),
134+
Certificate: new SerializableCertificate(certificate));
133135

134-
HandleChange(certObj);
135-
}
136-
catch (Exception e)
137-
{
138-
Log.Verbose($"Could not parse certificate from file: {file}, {e.GetType().ToString()}");
136+
HandleChange(certObj);
137+
}
138+
catch (Exception e)
139+
{
140+
Log.Verbose("Could not parse certificate from file: {0} ({1}:{2})", file, e.GetType(), e.Message);
141+
}
139142
}
140143
}
144+
Log.Verbose("Finished parsing {0}", file);
141145
}
142-
Log.Verbose("Finished parsing {0}", file);
143146
}
144-
147+
catch (Exception e)
148+
{
149+
Log.Verbose("Error parsing Directory {0} ({1}:{2})", Path, e.GetType(), e.Message);
150+
}
145151
Log.Verbose("Finished parsing {0}", Path);
146-
};
152+
}
147153

148154
foreach (var root in Roots)
149155
{
@@ -156,20 +162,20 @@ public override void ExecuteInternal()
156162
});
157163

158164
//First do root
159-
IterateOnDirectory?.Invoke(root);
165+
TryIterateOnDirectory(root);
160166

161167
if (!opts.SingleThread == true)
162168
{
163169
Parallel.ForEach(directories, filePath =>
164170
{
165-
IterateOnDirectory?.Invoke(filePath);
171+
TryIterateOnDirectory(filePath);
166172
});
167173
}
168174
else
169175
{
170176
foreach (var filePath in directories)
171177
{
172-
IterateOnDirectory?.Invoke(filePath);
178+
TryIterateOnDirectory(filePath);
173179
}
174180
}
175181
}

0 commit comments

Comments
 (0)