Example #1
0
 private static StreamWriter CreateWriter([NotNull] IFileStream stream, [CanBeNull] Encoding encoding = null)
 {
     return(encoding == null ? new StreamWriter(stream.AsStream()) : new StreamWriter(stream.AsStream(), encoding));
 }
        public override FileCopyResult Handle(FileCopyArguments arguments)
        {
            Guard.NotNull(arguments, nameof(arguments));

            pendingContentChanges.Clear();
            pendingContentChanges.Add(FileAccessKinds.Write);

            FileEntry sourceFile = ResolveSourceFile(arguments.SourcePath, arguments.IsCopyAfterMoveFailed);

            FileResolveResult destinationResolveResult            = ResolveDestinationFile(arguments);
            DateTime?         existingDestinationLastWriteTimeUtc = destinationResolveResult.ExistingFileOrNull?.LastWriteTimeUtc;
            FileEntry         destinationFile = PrepareDestinationFile(destinationResolveResult, sourceFile, arguments);

            foreach (FileAccessKinds change in pendingContentChanges)
            {
                Container.ChangeTracker.NotifyContentsAccessed(destinationFile.PathFormatter, change);
            }

            IFileStream sourceStream      = null;
            IFileStream destinationStream = null;

            try
            {
                sourceStream      = sourceFile.Open(FileMode.Open, FileAccess.ReadWrite, arguments.SourcePath, false, false, false);
                destinationStream = destinationFile.Open(FileMode.Open, FileAccess.Write, arguments.DestinationPath, false, false,
                                                         false);

                return(new FileCopyResult(sourceFile, sourceStream.AsStream(), destinationFile, destinationStream.AsStream(),
                                          existingDestinationLastWriteTimeUtc));
            }
            catch (Exception)
            {
                destinationStream?.Dispose();
                sourceStream?.Dispose();

                throw;
            }
        }
Example #3
0
        public override FileCopyResult Handle(FileCopyArguments arguments)
        {
            Guard.NotNull(arguments, nameof(arguments));

            FileEntry sourceFile = ResolveSourceFile(arguments.SourcePath);

            AssertHasExclusiveAccess(sourceFile);

            FileEntry destinationFile = ResolveDestinationFile(arguments.DestinationPath, arguments.Overwrite, sourceFile);

            IFileStream sourceStream      = null;
            IFileStream destinationStream = null;

            try
            {
                sourceStream      = sourceFile.Open(FileMode.Open, FileAccess.ReadWrite, arguments.SourcePath);
                destinationStream = destinationFile.Open(FileMode.Truncate, FileAccess.Write, arguments.DestinationPath);

                return(new FileCopyResult(sourceFile, sourceStream.AsStream(), destinationFile, destinationStream.AsStream()));
            }
            catch (Exception)
            {
                destinationStream?.Dispose();
                sourceStream?.Dispose();

                throw;
            }
        }