private FileStream openFileDirectly(IndexingTask task) { try { var stream = new FileStream( task.Path, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Write | FileShare.Delete, BufferSize, FileOptions.SequentialScan); return(stream); } catch (IOException ex) { handleFailedAccess(task, ex); } catch (SecurityException ex) { handleFailedAccess(task, ex); } catch (UnauthorizedAccessException ex) { handleFailedAccess(task, ex); } return(null); }
public void SetFilePropertiesOf(IndexingTask task) { task.Path = task.FileEntry.GetPath(); // file was deleted if (task.Path == null) { handleFailedAccess(task, exception: null); return; } if (isIndexFileOrDirectory(task.Path)) { return; } try { var fileInfo = new FileInfo(task.Path); task.FileEntry.Data.Length = fileInfo.Length; } catch (Exception ex) when(ex is SecurityException || ex is UnauthorizedAccessException || ex is FileNotFoundException) { // FileNotFoundException may be ok if the file was renamed / moved after GetPath call task.FileEntry.Data.Length = long.MaxValue; handleFailedAccess(task, ex); } }
private StreamReader openFile(IndexingTask task) { var encoding = getEncoding(task); string hardlinkPath = task.HardlinkPath ?? tryCreateHardlink(task.Path); FileStream stream; if (hardlinkPath != null) { task.HardlinkPath = hardlinkPath; stream = openFileThroughHardLink(task); } else { stream = openFileDirectly(task); } if (stream == null) { return(null); } return(new StreamReader(stream, encoding)); }
private void handleFailedAccess(IndexingTask task, Exception exception) { task.HasToBeRepeated = task.Attempts < MaxReadAttempts; task.FileAccessException = exception; if (exception != null) { FileAccessError?.Invoke(this, new EntryAccessError(EntryType.File, task.Path, exception)); } }
private void processRemoveFromIndexTask(FileEntry <Metadata> fileToRemove) { var indexingTask = new IndexingTask(IndexingAction.RemoveContent, fileToRemove, CancellationToken); ProcessingTaskStarted?.Invoke(this, indexingTask); _currentTask = indexingTask; _indexingTaskProcessor.ProcessTask(indexingTask); _currentTask = null; ProcessingTaskFinished?.Invoke(this, indexingTask); }
private void advanceDelayedTask(IndexingTask indexingTask) { if (indexingTask.ElapsedSinceCreation < ThrottleDelay) { return; } if (_delayedTasksQueue.TryRemove(indexingTask.FileEntry)) { _addingToIndexQueue.TryAdd(indexingTask.FileEntry, indexingTask, indexingTask.FileLength); } }
private Encoding getEncoding(IndexingTask task) { Encoding encoding; if (_encodingDetector != null) { encoding = _encodingDetector(new FileInfo(task.Path)); } else { encoding = Encoding.UTF8; } return(encoding); }
private void addDelayedTask(FileEntry <Metadata> fileEntry) { var indexingTask = new IndexingTask(IndexingAction.AddContent, fileEntry, CancellationToken); _indexingTaskProcessor.SetFilePropertiesOf(indexingTask); // file was deleted if (indexingTask.Path == null) { return; } _delayedTasksQueue.TryEnqueue(fileEntry, indexingTask); _filesByContentId.TryAdd(fileEntry.Data.ContentId, fileEntry); }
public void ProcessTask(IndexingTask task) { task.Reset(); switch (task.Action) { case IndexingAction.AddContent: updateFile(task); break; case IndexingAction.RemoveContent: _indexEngine.Remove(task.ContentId, task.CancellationToken); break; default: throw new NotSupportedException($"{nameof(IndexingAction)} {task.Action} is not supported"); } }
private void processAddToIndexTask(IndexingTask indexingTask) { ProcessingTaskStarted?.Invoke(this, indexingTask); _currentTask = indexingTask; _indexingTaskProcessor.ProcessTask(indexingTask); _currentTask = null; ProcessingTaskFinished?.Invoke(this, indexingTask); if (indexingTask.HasToBeRepeated) { _delayedTasksQueue.TryEnqueue(indexingTask.FileEntry, indexingTask); } if (indexingTask.FileAccessException != null && indexingTask.HardlinkPath != null) { _failedHardLinksQueue.TryEnqueue(indexingTask.HardlinkPath); } }
private void updateFile(IndexingTask task) { task.BeginScan(); SetFilePropertiesOf(task); if (task.FileAccessException != null) { return; } if (task.Path == null) { return; } if (task.FileLength >= MaxFileLength) { _indexEngine.Remove(task.ContentId, task.CancellationToken); task.EndScan(); return; } var textReader = openFile(task); if (textReader == null) { return; } using (textReader) { FileOpened?.Invoke(this, task); _indexEngine.Update(task.ContentId, textReader, task.CancellationToken); if (!task.CancellationToken.IsCancellationRequested) { task.EndScan(); } } }
private FileStream openFileThroughHardLink(IndexingTask task) { try { var stream = new FileStream( task.HardlinkPath, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Write | FileShare.Delete, BufferSize, FileOptions.SequentialScan | FileOptions.DeleteOnClose); return(stream); } catch (IOException ex) when(!(ex is FileNotFoundException) && !(ex is DirectoryNotFoundException)) { handleFailedAccess(task, ex); } return(null); }