/// <summary> /// /// </summary> /// <param name="file"></param> /// <param name="fullPathForFile"></param> /// <exception cref="TaskCancelledException"></exception> /// <exception cref="CannotGetFileContentsException"></exception> /// <exception cref="ObjectDisposedException"></exception> /// <exception cref="InvalidPathException">Если указанный путь невалиден</exception> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="FolderNotFoundException"></exception> /// <exception cref="FileAlreadyExistsException"></exception> /// <exception cref="InsufficientSpaceException"></exception> /// <exception cref="MaximumFileCountReachedException"></exception> private void CopyFileContents(FileAddressable file, string fullPathForFile) { using (var stream = _targetFileSystem.CreateAndOpenFileForWriting(fullPathForFile)) { try { IEnumerator <byte[]> contentsEnumerator = _fileContentsBufferFactory.GetBufferEnumeratorFor(file.FullPath); while (contentsEnumerator.MoveNext()) { if (_taskToken.HasBeenCancelled) { throw new TaskCancelledException(); } stream.Write(contentsEnumerator.Current, 0, contentsEnumerator.Current.Length); } } catch (Exception) { stream.Dispose(); try { _targetFileSystem.DeleteFile(fullPathForFile); } catch (FileNotFoundException) { } catch (FileLockedException) { } catch (ObjectDisposedException) { } throw; } } }
/// <summary> /// /// </summary> /// <param name="file"></param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="InsufficientSpaceException"></exception> /// <exception cref="ObjectDisposedException"></exception> public void VisitFile(FileAddressable file) { if (file == null) { throw new ArgumentNullException("file"); } try { if (_taskToken.HasBeenCancelled) { _fileTaskResults.Add(new FileTaskResult(file, null, "Задача прервана")); return; } // если бы DirectorySeparatorChar не был установлен в Path.DirectorySeparatorChar, здесь надо было бы использовать разные стратегии для виртуальной и реальной файловой системы string newFilePathRelative = _targetFileSystem.PathBuilder.GetRelativePath(_sourceFolderPath, file.FullPath); string fullPathForFile; try { fullPathForFile = _targetFileSystem.PathBuilder.CombinePaths(_destinationFolder, newFilePathRelative); } catch (ArgumentException exception) { _fileTaskResults.Add(new FileTaskResult(file, null, "Не удалось создать эквивалент файла \"{0}\" в виртуальной файловой системе.{1}{2}".FormatWith(file.FullPath, Environment.NewLine, exception.Message))); return; } string fileName = _targetFileSystem.PathBuilder.GetFileOrFolderName(fullPathForFile); CopyFileContents(file, fullPathForFile); _fileTaskResults.Add(new FileTaskResult(file, new FileAddressable(fullPathForFile, fileName), String.Empty)); } catch (TaskCancelledException) { _fileTaskResults.Add(new FileTaskResult(file, null, "Задача прервана")); } catch (CannotGetFileContentsException exception) { _fileTaskResults.Add(new FileTaskResult(file, null, "{0}{1}{2}".FormatWith(exception.Message, Environment.NewLine, exception.InnerException.Message))); } catch (InvalidPathException exception) { AddNewErrorToFileTaskResults(file, exception); } catch (FolderNotFoundException exception) { AddNewErrorToFileTaskResults(file, exception); } catch (FileAlreadyExistsException exception) { AddNewErrorToFileTaskResults(file, exception); } catch (InsufficientSpaceException exception) { AddNewErrorToFileTaskResults(file, exception); throw; // продолжать не будем. } catch (MaximumFileCountReachedException exception) { AddNewErrorToFileTaskResults(file, exception); } catch (ObjectDisposedException exception) { AddNewErrorToFileTaskResults(file, exception); throw; // продолжать не будем. } finally { CalculateAndReportProgress(); } }
private void AddNewErrorToFileTaskResults(FileAddressable file, Exception exception) { _fileTaskResults.Add(new FileTaskResult(file, null, exception.Message)); }