Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
            }
        }
Exemplo n.º 3
0
        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));
        }
Exemplo n.º 4
0
        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));
            }
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        private void advanceDelayedTask(IndexingTask indexingTask)
        {
            if (indexingTask.ElapsedSinceCreation < ThrottleDelay)
            {
                return;
            }

            if (_delayedTasksQueue.TryRemove(indexingTask.FileEntry))
            {
                _addingToIndexQueue.TryAdd(indexingTask.FileEntry, indexingTask, indexingTask.FileLength);
            }
        }
Exemplo n.º 7
0
        private Encoding getEncoding(IndexingTask task)
        {
            Encoding encoding;

            if (_encodingDetector != null)
            {
                encoding = _encodingDetector(new FileInfo(task.Path));
            }
            else
            {
                encoding = Encoding.UTF8;
            }
            return(encoding);
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        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");
            }
        }
Exemplo n.º 10
0
        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);
            }
        }
Exemplo n.º 11
0
        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();
                }
            }
        }
Exemplo n.º 12
0
        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);
        }