Example #1
0
        static void CreateMemoryFile()
        {
            FileSystemPath MemRootFilePath = FileSystemPath.Root.AppendFile("x");
            var            MemFileSystem   = new MemoryFileSystem();

            // File shouldn’t exist prior to creation.
            Assert.False(MemFileSystem.Exists(MemRootFilePath));

            var content = new byte[] { 0xde, 0xad, 0xbe, 0xef, };

            using (var xStream = MemFileSystem.CreateFile(MemRootFilePath))
            {
                // File now should exist.
                Assert.True(MemFileSystem.Exists(MemRootFilePath));

                xStream.Write(content, 0, content.Length);
            }

            // File should still exist and have content.
            Assert.True(MemFileSystem.Exists(MemRootFilePath));
            using (var xStream = MemFileSystem.OpenFile(MemRootFilePath, FileAccess.Read))
            {
                var readContent = new byte[2 * content.Length];
                Assert.Equal(content.Length, xStream.Read(readContent, 0, readContent.Length));
                Assert.Equal(
                    content,
                    // trim to the length that was read.
                    readContent.Take(content.Length).ToArray());

                // Trying to read beyond end of file should return 0.
                Assert.Equal(0, xStream.Read(readContent, 0, readContent.Length));
            }
        }
Example #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"));
                        }
                    }
                }
            }
        }