public bool IsAlreadyInDB(ScanItemDto si)
 {
     if (_dbContext is null)
     {
         throw new InvalidOperationException("IsAlreadyInDB called without SetupDB!");
     }
     lock (dbContextLock)
     {
         var itemCount = _dbContext.ScanItems?.Where(s => ((s.FilenameAndPath == si.FilenameAndPath) && (s.FileSize == si.FileSize) && (s.ScanExecutionComputer == si.ScanExecutionComputer))).Count();
         if (itemCount is null)
         {
             return(false);
         }
         else if (itemCount == 0)
         {
             return(false);
         }
         else if (itemCount == 1)
         {
             return(true);
         }
         else
         {
             //Strange
             _logger.LogWarning($"IsAlreadyInDB had a strange itemCount of {itemCount}...");
             return(true);
         }
     }
 }
示例#2
0
        public void FilenameTest()
        {
            ScanItemDto scanItem = new ScanItemDto();

            scanItem.FilenameAndPath = @"o:\test\diesIstEinTest\meineDatei.txt";
            Assert.Equal("meineDatei.txt", scanItem.Filename);
            scanItem.FilenameAndPath = @"o:\test\diesIstEinTest\nochEineDatei.txt";
            Assert.Equal("nochEineDatei.txt", scanItem.Filename);
            scanItem.FilenameAndPath = @"o:\test\diesIstEinTest\meineDatei";
            Assert.Equal("meineDatei", scanItem.Filename);
        }
 public void Enqueue(ScanItemDto si)
 {
     if ((_dbContext is null) || (_dbContextOptions is null))
     {
         throw new InvalidOperationException("Enqueue called without SetupDB!");
     }
     lock (dbContextLock)
     {
         _finishedScanItemCollection.Enqueue(si);
         WriteChangesInternal();
     }
 }
        public void EnumerateFiles(int maxFiles = 100)
        {
            if (!HasMore)
            {
                //This way we can dispose the enumerator once we are through
                return;
            }
            int loopCount = 0;

            while (_files.MoveNext())
            {
                try
                {
                    DateTime    currDate = DateTime.UtcNow;
                    ScanItemDto si       = new ScanItemDto
                    {
                        PathBase        = _scanJobDto.BasePath,
                        FilenameAndPath = _files.Current,

                        FirstScanDateUTC      = currDate,
                        LastScanDateUTC       = currDate,
                        LastSha512ScanDateUTC = currDate,
                        OriginComputer        = _scanJobDto.OriginComputer,
                        ScanName = _scanJobDto.ScanName,
                        ScanExecutionComputer   = Environment.MachineName,
                        FileCreationUTC         = File.GetCreationTimeUtc(_files.Current),
                        FileLastModificationUTC = File.GetLastWriteTimeUtc(_files.Current),
                        FileSize = new FileInfo(_files.Current).Length
                    };
                    ScanItemCollection.Enqueue(si);
                    loopCount    += 1;
                    CurrentCount += 1;
                    //Check if we are done
                    if (loopCount >= maxFiles)
                    {
                        _logger.LogDebug($"EnumerateFiles: CurrentCount: {CurrentCount} File: {_files.Current}");
                        //make a break
                        return;
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "There was an exception during file attribute reading of file {file}", _files.Current);
                    ErrorCount += 1;
                }
            }
            //We are done
            HasMore = false;
            _files.Dispose();
        }
        private void CalcHash(ScanItemDto item)
        {
            try
            {
                using (var sha512 = SHA512.Create())
                {
                    using (var stream = File.OpenRead(item.FilenameAndPath))
                    {
                        item.FileSha512Hash = BitConverter.ToString(sha512.ComputeHash(stream)).Replace("-", "", StringComparison.Ordinal);
                        ScanJobDBInserts.Enqueue(item);
                        _logger.LogInformation("File {file} successfull finished", item.FilenameAndPath);
                    }
                }
            }
#pragma warning disable CA1031 // Keine allgemeinen Ausnahmetypen abfangen
            catch (Exception e)
#pragma warning restore CA1031 // Keine allgemeinen Ausnahmetypen abfangen
            {
                ScanJobDBInserts.Enqueue(new ScanErrorItemDto(item, e, _runStarted));
                _logger.LogError(e, "Hashing of {file} failed.", item.FilenameAndPath);
            }
        }
        private void Scan()
        {
            try
            {
                Task taskHash = Task.Run(() =>
                {
                    ParallelOptions po   = new ParallelOptions();
                    po.CancellationToken = CancelToken;
                    var loopResult       = Parallel.ForEach <ScanItemDto>(_scanItemCollection.GetConsumingEnumerable(), po, (item, loopState, _) =>
                    {
                        try
                        {
                            using (var sha512 = SHA512.Create())
                            {
                                using (var stream = File.OpenRead(item.FilenameAndPath))
                                {
                                    item.FileSha512Hash = BitConverter.ToString(sha512.ComputeHash(stream)).Replace("-", "", StringComparison.Ordinal);
                                    ScanJobDBInserts.Enqueue(item);
                                    _logger.LogInformation("File {file} successfull finished", item.FilenameAndPath);
                                }
                            }
                        }
#pragma warning disable CA1031 // Keine allgemeinen Ausnahmetypen abfangen
                        catch (Exception e)
#pragma warning restore CA1031 // Keine allgemeinen Ausnahmetypen abfangen
                        {
                            ScanJobDBInserts.Enqueue(new ScanErrorItemDto(item, e, _runStarted));
                            _logger.LogError(e, "Hashing of {file} failed.", item.FilenameAndPath);
                        }
                    });
                    _logger.LogInformation("Finished hashing files. Successfully hashed files: {successfullCount}, failed: {failedCount}, Queue: {QueueCount}", ScanJobDBInserts.TotalSuccessCount, ScanJobDBInserts.TotalErrorCount, _scanItemCollection.Count);
                    RunnerState = IService.EServiceState.finished;
                });
                var files = Directory.EnumerateFiles(ScanJobDTO.BasePath, "*", SearchOption.AllDirectories);
                foreach (string currentFile in files)
                {
                    if (CancelToken.IsCancellationRequested)
                    {
                        _logger.LogInformation("Current scan is canceled by user request");
                        _scanItemCollection.CompleteAdding();
                        return;
                    }
                    try
                    {
                        DateTime    currDate = DateTime.UtcNow;
                        ScanItemDto si       = new ScanItemDto
                        {
                            PathBase        = ScanJobDTO.BasePath,
                            FilenameAndPath = currentFile,

                            FirstScanDateUTC      = currDate,
                            LastScanDateUTC       = currDate,
                            LastSha512ScanDateUTC = currDate,
                            OriginComputer        = ScanJobDTO.OriginComputer,
                            ScanName = ScanJobDTO.ScanName,
                            ScanExecutionComputer   = Environment.MachineName,
                            FileCreationUTC         = File.GetCreationTimeUtc(currentFile),
                            FileLastModificationUTC = File.GetLastWriteTimeUtc(currentFile),
                            FileSize = new FileInfo(currentFile).Length
                        };
                        _scanItemCollection.TryAdd(si, -1, CancelToken);
                    }
                    catch (OperationCanceledException)
                    {
                        _logger.LogInformation("Current scan is canceled by user request");
                        _scanItemCollection.CompleteAdding();
                        return;
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "There was an exception during file attribute reading of file {file}", currentFile);
                    }
                }
                _scanItemCollection.CompleteAdding();
            }
            catch (Exception e)
            {
                _logger.LogError(e, "There was an exception during file enumeration");
            }
            _scanItemCollection.CompleteAdding();
        }