in src/Ingestor.cs [634:682]
private void ListAndFilterFiles(string sourcePath, string pattern, int filesToTake, ITargetBlock<DataSource> targetBlock)
{
try
{
m_logger.LogVerbose($"ListAndFilterFiles: enumerating files under '{sourcePath}'");
// sourcePath is a file path
if (File.Exists(sourcePath))
{
m_logger.LogVerbose($"ListAndFilterFiles: found 1 file: '{sourcePath}'");
Interlocked.Increment(ref m_objectsListed);
Interlocked.Increment(ref m_objectsAccepted);
targetBlock.SendAsync(new DataSource { FileSystemPath = sourcePath, SizeInBytes = Utilities.TryGetFileSize(sourcePath, m_estimatedCompressionRatio) });
return;
}
// sourcePath is a directory path
var files = Directory.EnumerateFiles(sourcePath, pattern, SearchOption.AllDirectories);
if (files.SafeFastNone())
{
m_logger.LogWarning($"ListAndFilterFiles: files matching the pattern '{pattern}' found under '{sourcePath}' path.");
throw new FileNotFoundException($"No files matching the pattern '{pattern}' found under '{sourcePath}' path.");
}
int fileCount = (int)files.SafeFastCount();
Interlocked.Add(ref m_objectsListed, fileCount);
filesToTake = (filesToTake >= 0 ? Math.Min(filesToTake, fileCount) : fileCount);
files.SafeFastTake(filesToTake).ForEach((f) =>
{
long fileSize = Utilities.TryGetFileSize(f, m_estimatedCompressionRatio);
DateTime? fileCreationTime = Utilities.InferFileCreationTimeUtc(f, m_creationTimeInNamePattern);
targetBlock.SendAsync(new DataSource
{
FileSystemPath = f,
SizeInBytes = fileSize,
CreationTimeUtc = fileCreationTime
});
Interlocked.Increment(ref m_objectsAccepted);
});
}
catch (Exception ex)
{
m_logger.LogError($"ListAndFilterFiles failed: {ex.Message}");
}
}