/// <summary>
 /// Copy a file.
 /// </summary>
 /// <param name="filesystem"></param>
 /// <param name="srcPath">source path of a file or directory</param>
 /// <param name="dstPath">target path of a file or directory</param>
 /// <param name="option">(optional) token to authorize or facilitate operation</param>
 /// <exception cref="FileNotFoundException">The specified <paramref name="srcPath"/> is invalid.</exception>
 /// <exception cref="IOException">On unexpected IO error</exception>
 /// <exception cref="SecurityException">If caller did not have permission</exception>
 /// <exception cref="FileNotFoundException">The specified path is invalid.</exception>
 /// <exception cref="ArgumentNullException">path is null</exception>
 /// <exception cref="ArgumentException">path is an empty string (""), contains only white space, or contains one or more invalid characters</exception>
 /// <exception cref="NotSupportedException">The <see cref="IFileSystem"/> doesn't support copy and delete of files</exception>
 /// <exception cref="UnauthorizedAccessException">The access requested is not permitted by the operating system for the specified path, such as when access is Write or ReadWrite and the file or directory is set for read-only access.</exception>
 /// <exception cref="PathTooLongException">The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters.</exception>
 /// <exception cref="InvalidOperationException">path refers to non-file device, or an entry already exists at <paramref name="dstPath"/></exception>
 /// <exception cref="ObjectDisposedException"/>
 public static void CopyFile(this IFileSystem filesystem, string srcPath, string dstPath, IOption option = null)
 {
     using (var s = new OperationSession())
     {
         OperationBase op = new CopyFile(s, filesystem, srcPath, filesystem, dstPath, option, option,
                                         policy: OperationPolicy.SrcThrow | OperationPolicy.DstThrow | OperationPolicy.OmitMountedPackages | OperationPolicy.CancelOnError
                                         );
         op.Estimate();
         op.Run(rollbackOnError: true);
         op.AssertSuccessful();
     }
 }
Exemplo n.º 2
0
        public void CopyFile()
        {
            //
            {
                // Create 4GB file
                using (var s = ram.Open("bigfile", FileMode.Create, FileAccess.ReadWrite, FileShare.None))
                {
                    byte[] buf = new byte[4096];
                    for (int i = 0; i < 4096; i++)
                    {
                        buf[i] = (byte)(i & 0xff);
                    }
                    for (int i = 0; i < blocks; i++)
                    {
                        s.Write(buf, 0, buf.Length);
                    }
                }

                // TODO TEst | FileOperation.Policy.CancelIfError

                // File session
                using (var session = new OperationSession())
                {
                    // Copy file
                    {
                        // Operation
                        OperationBase op = new CopyFile(session, ram, "bigfile", ram, "bigfile.copy");
                        //
                        op.Estimate();
                        //
                        Assert.IsTrue(op.CanRollback);
                        //
                        op.Run();
                        // Assert
                        using (var s = ram.Open("bigfile.copy", FileMode.Open, FileAccess.Read, FileShare.None))
                        {
                            byte[] buf = new byte[4096];
                            for (int i = 0; i < blocks; i++)
                            {
                                int x = s.Read(buf, 0, buf.Length);
                                Assert.AreEqual(buf.Length, x);
                                for (int j = 0; j < buf.Length; j++)
                                {
                                    Assert.AreEqual(buf[j], (byte)(j & 0xff));
                                }
                            }
                        }
                    }

                    // Copy to existing file
                    {
                        // Operation
                        OperationBase op = new CopyFile(session, ram, "bigfile", ram, "bigfile.copy");
                        try
                        {
                            // Estimate
                            op.Estimate();
                            //
                            Assert.Fail("Estimate should have failed");
                        } catch (FileSystemException)
                        {
                            // Catched
                        }
                    }

                    // Copy, run out of memory, rollback
                    {
                        IFileSystem dst = new MemoryFileSystem(blockSize: 1024, maxSpace: 2048);
                        // Operation
                        OperationBase op = new CopyFile(session, ram, "bigfile", dst, "bigfile.copy");
                        //
                        op.Estimate();
                        //
                        Assert.IsTrue(op.CanRollback);
                        try
                        {
                            //
                            op.Run(rollbackOnError: true);
                        } catch (FileSystemExceptionOutOfDiskSpace)
                        {
                            Assert.IsFalse(dst.Exists("bigfile.copy"));
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        public static void Main(string[] args)
        {
            {
                // <Snippet_1>
                IFileSystem ms = new MemoryFileSystem();
                ms.CreateFile("file", new byte[1024 * 1024]);

                using (var s = new OperationSession())
                {
                    new CopyFile(s, ms, "file", ms, "file.copy")
                    .Estimate()
                    .AssertCanRollback()
                    .Run()
                    .AssertSuccessful();
                }
                // </Snippet_1>

                ms.PrintTo(Console.Out, format: PrintTree.Format.Default | PrintTree.Format.Length);
            }

            {
                IFileSystem ms = new MemoryFileSystem();
                ms.CreateFile("file", new byte[1024 * 1024]);
                // <Snippet_1b>
                var s  = new OperationSession();
                var op = new CopyFile(s, ms, "file", ms, "file.copy");
                op.Estimate();
                // </Snippet_1b>
                // <Snippet_1b1>
                Console.WriteLine(op.CanRollback);
                // </Snippet_1b1>


                // <Snippet_1b2>
                op.Estimate().AssertCanRollback();
                // </Snippet_1b2>

                // <Snippet_1c>
                op.Run();
                // </Snippet_1c>

                // <Snippet_1c1>
                if (op.CurrentState == OperationState.Completed)
                {
                    Console.WriteLine("ok");
                }
                // </Snippet_1c1>

                ms.Delete("file.copy");
                op = (CopyFile) new CopyFile(s, ms, "file", ms, "file.copy").Estimate();
                // <Snippet_1c2>
                op.Run().AssertSuccessful();
                // </Snippet_1c2>

                ms.Delete("file.copy");
                op = (CopyFile) new CopyFile(s, ms, "file", ms, "file.copy").Estimate();
                // <Snippet_1c3>
                try
                {
                    op.Run();
                }
                catch (Exception)
                {
                    // Rollback
                    op.CreateRollback()?.Run();
                }
                // </Snippet_1c3>

                // <Snippet_1c4>
                op.Run(rollbackOnError: true);
                // </Snippet_1c4>
            }

            {
                // <Snippet_2>
                IFileSystem ms = new MemoryFileSystem();
                ms.CreateDirectory("dir/dir/dir/");
                ms.CreateFile("dir/dir/dir/file", new byte[1024 * 1024]);

                using (var s = new OperationSession())
                {
                    var op = new CopyTree(s, ms, "dir", ms, "dir.copy");

                    op.Estimate().AssertCanRollback().Run().AssertSuccessful();
                }
                // </Snippet_2>
                ms.PrintTo(Console.Out);
            }
            {
                // <Snippet_3>
                IFileSystem ms = new MemoryFileSystem();
                using (var s = new OperationSession(policy: OperationPolicy.EstimateOnRun /*important*/))
                {
                    Batch batch = new Batch(s, OperationPolicy.Unset);

                    batch.Ops.Add(new CreateDirectory(s, ms, "dir/dir/dir"));
                    batch.Ops.Add(new CopyTree(s, ms, "dir", ms, "dir.copy"));
                    batch.Ops.Add(new Delete(s, ms, "dir/dir", true, policy: OperationPolicy.EstimateOnRun));
                    batch.Estimate().Run().AssertSuccessful();
                }
                // </Snippet_3>
                ms.PrintTo(Console.Out);
            }
            {
                // <Snippet_4>
                IFileSystem ms_src = new MemoryFileSystem();
                ms_src.CreateDirectory("dir/dir/dir/");
                ms_src.CreateFile("dir/dir/dir/file", new byte[1024 * 1024]);

                IFileSystem ms_dst = new MemoryFileSystem();

                using (var s = new OperationSession())
                {
                    var op = new TransferTree(s, ms_src, "dir", ms_dst, "dir.elsewhere");
                    op.Estimate().AssertCanRollback().Run().AssertSuccessful();
                }
                // </Snippet_4>
                ms_dst.PrintTo(Console.Out);
            }
            {
                // <Snippet_5>
                IFileSystem ms = new MemoryFileSystem();
                ms.CreateFile("file", new byte[1024 * 1024]);

                using (var s = new OperationSession())
                {
                    new CopyFile(s, ms, "file", ms, "file.copy")
                    .Estimate()
                    .AssertCanRollback()
                    .Run()
                    .AssertSuccessful();

                    foreach (var @event in s.Events)
                    {
                        Console.WriteLine(@event);
                    }
                }
                ms.PrintTo(Console.Out);
                // </Snippet_5>
            }
            {
                // <Snippet_6>
                IFileSystem ms = new MemoryFileSystem();
                ms.CreateFile("file", new byte[1024 * 20]);

                using (var s = new OperationSession().SetProgressInterval(1024))
                {
                    s.Subscribe(new OpEventPrinter());

                    new CopyFile(s, ms, "file", ms, "file.copy")
                    .Estimate()
                    .AssertCanRollback()
                    .Run()
                    .AssertSuccessful();
                }
                ms.PrintTo(Console.Out);
                // </Snippet_6>
            }
            {
                try
                {
                    // <Snippet_7>
                    IFileSystem ms = new MemoryFileSystem();
                    ms.CreateFile("file", new byte[1024 * 10]);
                    CancellationTokenSource cancelSrc = new CancellationTokenSource();

                    using (var s = new OperationSession(cancelSrc: cancelSrc))
                    {
                        cancelSrc.Cancel();
                        new Move(s, ms, "file", ms, "file.moved")
                        .Estimate()
                        .AssertCanRollback()
                        .Run()
                        .AssertSuccessful();
                    }
                    // </Snippet_7>
                } catch (Exception e)
                {
                }
            }
            {
                // <Snippet_8>
                // </Snippet_8>
            }
            {
                // <Snippet_9>
                // </Snippet_9>
            }
            {
                // <Snippet_10>
                // </Snippet_10>
            }
            {
                // <Snippet_11>
                // </Snippet_11>
            }
            {
                // <Snippet_12>
                // </Snippet_12>
            }
            {
                // <Snippet_13>
                // </Snippet_13>
            }
            {
                // <Snippet_14>
                // </Snippet_14>
            }
            {
                // <Snippet_15>
                // </Snippet_15>
            }
            {
                // <Snippet_16>
                // </Snippet_16>
            }
        }
Exemplo n.º 4
0
 public LogRepository(OperationSession session)
     : base(session)
 {
 }