Esempio n. 1
0
        public void 刪除資料夾()
        {
            using var fileSystem = new MemoryFileSystem();
            Console.WriteLine("建立資料夾");
            fileSystem.CreateDirectory("dir1/dir2/dir3/");
            fileSystem.PrintTo(Console.Out);

            Console.WriteLine("刪除 dir2 資料夾");
            fileSystem.Delete("dir1/dir2/", true);
            fileSystem.PrintTo(Console.Out);
        }
Esempio n. 2
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>
            }
        }
Esempio n. 3
0
        public static void Main(string[] args)
        {
            {
                #region Snippet_1
                IFileSystem filesystem = new MemoryFileSystem();
                #endregion Snippet_1
            }
            {
                #region Snippet_1b
                IFileSystem filesystem = new MemoryFileSystem(blockSize: 4096);
                #endregion Snippet_1b
            }
            {
                IFileSystem filesystem = new MemoryFileSystem();
                #region Snippet_2
                foreach (var entry in filesystem.Browse(""))
                {
                    Console.WriteLine(entry.Path);
                }
                #endregion Snippet_2
            }
            {
                IFileSystem filesystem = new MemoryFileSystem();
                filesystem.CreateFile("file.txt");
                #region Snippet_3a
                using (Stream s = filesystem.Open("file.txt", FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    Console.WriteLine(s.Length);
                }
                #endregion Snippet_3a
            }
            {
                IFileSystem filesystem = new MemoryFileSystem();
                filesystem.CreateFile("file.txt");
                #region Snippet_3b
                using (Stream s = filesystem.Open("file.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    s.WriteByte(32);
                }
                #endregion Snippet_3b
            }
            {
                IFileSystem filesystem = new MemoryFileSystem();
                #region Snippet_4
                IObserver <IEvent> observer = new Observer();
                using (IDisposable handle = filesystem.Observe("**", observer))
                {
                }
                #endregion Snippet_4
            }
            {
                IFileSystem filesystem = new MemoryFileSystem();
                #region Snippet_5
                filesystem.CreateDirectory("dir/");
                #endregion Snippet_5
            }

            {
                IFileSystem filesystem = new MemoryFileSystem();
                #region Snippet_5a
                filesystem.CreateDirectory("dir1/dir2/dir3/");
                filesystem.PrintTo(Console.Out);
                #endregion Snippet_5a
            }
            {
                IFileSystem filesystem = new MemoryFileSystem();
                #region Snippet_5b
                filesystem.CreateDirectory("/tmp/dir/");
                #endregion Snippet_5b
                filesystem.PrintTo(Console.Out);
            }
            {
                IFileSystem filesystem = new MemoryFileSystem();
                #region Snippet_5c
                filesystem.CreateDirectory("/tmp/dir/");
                #endregion Snippet_5c
                filesystem.PrintTo(Console.Out);
            }
            {
                IFileSystem filesystem = new MemoryFileSystem();
                #region Snippet_5d
                filesystem.CreateDirectory("file://");
                #endregion Snippet_5d
                filesystem.PrintTo(Console.Out);
            }

            {
                IFileSystem filesystem = new MemoryFileSystem();
                filesystem.CreateDirectory("dir/");
                #region Snippet_6
                filesystem.Delete("dir/", recurse: true);
                #endregion Snippet_6
            }
            {
                IFileSystem filesystem = new MemoryFileSystem();
                filesystem.CreateDirectory("dir/");
                #region Snippet_7
                filesystem.CreateDirectory("dir/");
                filesystem.Move("dir/", "new-name/");
                #endregion Snippet_7
                filesystem.Delete("new-name/");
            }

            {
                #region Snippet_8a
                IFileSystem filesystem = new MemoryFileSystem();
                #endregion Snippet_8a
                foreach (var entry in filesystem.Browse(""))
                {
                    Console.WriteLine(entry.Path);
                }
            }
            {
                #region Snippet_8b
                #endregion Snippet_8b
            }
            {
                #region Snippet_10a
                // Init
                object obj = new ReaderWriterLockSlim();
                IFileSystemDisposable filesystem = new FileSystem("").AddDisposable(obj);

                // ... do work ...

                // Dispose both
                filesystem.Dispose();
                #endregion Snippet_10a
            }
            {
                #region Snippet_10b
                IFileSystemDisposable filesystem = new FileSystem("")
                                                   .AddDisposeAction(f => Console.WriteLine("Disposed"));
                #endregion Snippet_10b
            }
            {
                #region Snippet_10c
                MemoryFileSystem filesystem = new MemoryFileSystem();
                filesystem.CreateDirectory("/tmp/dir/");

                // Postpone dispose
                IDisposable belateDisposeHandle = filesystem.BelateDispose();
                // Start concurrent work
                Task.Run(() =>
                {
                    // Do work
                    Thread.Sleep(1000);
                    filesystem.GetEntry("");
                    // Release belate handle. Disposes here or below, depending which thread runs last.
                    belateDisposeHandle.Dispose();
                });

                // Start dispose, but postpone it until belatehandle is disposed in another thread.
                filesystem.Dispose();
                #endregion Snippet_10c
            }

            {
                // <Snippet_20a>
                IFileSystem ms = new MemoryFileSystem(blockSize: 1024, maxSpace: 1L << 34);
                // </Snippet_20a>
            }
            {
                // <Snippet_20b>
                IFileSystem ms = new MemoryFileSystem(blockSize: 1024, maxSpace: 1L << 34);
                ms.CreateFile("file", new byte[1 << 30]);
                ms.PrintTo(Console.Out, format: PrintTree.Format.AllWithName);
                // </Snippet_20b>
            }
            {
                try
                {
                    // <Snippet_20c>
                    IFileSystem ms = new MemoryFileSystem(blockSize: 1024, maxSpace: 2048);
                    ms.CreateFile("file1", new byte[1024]);
                    ms.CreateFile("file2", new byte[1024]);

                    // throws FileSystemExceptionOutOfDiskSpace
                    ms.CreateFile("file3", new byte[1024]);
                    // </Snippet_20c>
                }
                catch (FileSystemExceptionOutOfDiskSpace) { }
            }
            {
                try
                {
                    // <Snippet_20d>
                    IBlockPool  pool = new BlockPool(blockSize: 1024, maxBlockCount: 3, maxRecycleQueue: 3);
                    IFileSystem ms1  = new MemoryFileSystem(pool);
                    IFileSystem ms2  = new MemoryFileSystem(pool);

                    // Reserve 2048 from shared pool
                    ms1.CreateFile("file1", new byte[2048]);

                    // Not enough for another 3072, throws FileSystemExceptionOutOfDiskSpace
                    ms2.CreateFile("file2", new byte[2048]);
                    // </Snippet_20d>
                }
                catch (FileSystemExceptionOutOfDiskSpace) { }
            }
            {
                try
                {
                    // <Snippet_20e>
                    IBlockPool  pool = new BlockPool(blockSize: 1024, maxBlockCount: 3, maxRecycleQueue: 3);
                    IFileSystem ms   = new MemoryFileSystem(pool);
                    Stream      s    = ms.Open("file", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
                    s.Write(new byte[3072], 0, 3072);
                    ms.Delete("file");

                    Console.WriteLine(pool.BytesAvailable); // Prints 0
                    s.Dispose();
                    Console.WriteLine(pool.BytesAvailable); // Prints 3072
                    // </Snippet_20e>
                }
                catch (FileSystemExceptionOutOfDiskSpace) { }
            }
        }