Example #1
0
        public void TestDispose()
        {
            var memfs = new MemoryFileSystem();

            memfs.Dispose();
            Assert.Throws <ObjectDisposedException>(() => memfs.DirectoryExists("/"));
        }
Example #2
0
        /// <summary>
        /// Creates a <see cref="MemoryFileSystem"/> based on the given <see cref="Stream"/>.
        /// </summary>
        /// <param name="streamFile">The in-memory file to add to the file system.</param>
        /// <param name="streamManager">The stream manager for this file system.</param>
        /// <returns>The created <see cref="IFileSystem"/> for this stream.</returns>
        public static IFileSystem CreateMemoryFileSystem(StreamFile streamFile, IStreamManager streamManager)
        {
            var stream    = streamFile.Stream;
            var directory = streamFile.Path.GetDirectory();

            // 1. Create file system
            var fileSystem = new MemoryFileSystem(streamManager);

            if (!directory.IsEmpty && !fileSystem.DirectoryExists(directory))
            {
                fileSystem.CreateDirectory(directory);
            }

            var createdStream = fileSystem.OpenFile(streamFile.Path.ToAbsolute(), FileMode.CreateNew, FileAccess.Write, FileShare.Write);

            // 2. Copy data
            var bkPos = stream.Position;

            stream.Position = 0;
            stream.CopyTo(createdStream);
            stream.Position        = bkPos;
            createdStream.Position = 0;
            createdStream.Close();

            return(fileSystem);
        }
Example #3
0
        private void RegisterPluginFiles(OpenModFileSystemModule module)
        {
            foreach (var plugin in _pluginActivator.ActivatedPlugins)
            {
                var memoryFileSystem = new MemoryFileSystem();
                var workingDirectory = plugin.WorkingDirectory;
                var webDirectory     = Path.Combine(workingDirectory, "web");

                if (!Directory.Exists(webDirectory))
                {
                    continue;
                }

                foreach (var file in Directory.GetFiles(webDirectory, "*", SearchOption.AllDirectories))
                {
                    var path = Path.GetFullPath(file).Replace(Path.GetFullPath(webDirectory), string.Empty);

                    var directory = Path.GetDirectoryName(path);
                    if (directory != null && !memoryFileSystem.DirectoryExists(directory))
                    {
                        memoryFileSystem.CreateDirectory(directory);
                    }

                    memoryFileSystem.WriteAllBytes(path, File.ReadAllBytes(file));
                }

                module.FileSystem.AddFileSystem(memoryFileSystem);
            }
        }
Example #4
0
        public void TestGetOrCreateFileSystem()
        {
            var          fs            = new MemoryFileSystem();
            const string subFolder     = "/sub";
            var          subFileSystem = fs.GetOrCreateSubFileSystem(subFolder);

            Assert.True(fs.DirectoryExists(subFolder));
            subFileSystem.WriteAllText("/test.txt", "yo");
            var text = fs.ReadAllText(subFolder + "/test.txt");

            Assert.Equal("yo", text);
        }
Example #5
0
        public void 建立資料夾()
        {
            var rootUPath = CreateRootPath();

            using var fileSystem = new MemoryFileSystem();

            var subName      = "TestFolder";
            var subPath      = $"{rootUPath}/{subName}";
            var subPath1     = $"{subPath}/1";
            var subPath1_1   = $"{subPath}/1/1_1";
            var subPath1_1_1 = $"{subPath}/1/1_1/1_1_1";
            var subPath2     = $"{subPath}/2";
            var content      = "This is test string";

            if (fileSystem.DirectoryExists(subPath1_1_1) == false)
            {
                fileSystem.CreateDirectory(subPath1_1_1);
            }

            if (fileSystem.DirectoryExists(subPath2) == false)
            {
                fileSystem.CreateDirectory(subPath2);
            }

            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath2));
        }
Example #6
0
        public void 列舉根路徑內的子資料夾()
        {
            var rootUPath = CreateRootPath();

            using var fileSystem = new MemoryFileSystem();
            var subName = "../../path";

            var subPath      = $"{rootUPath}/{subName}";
            var subPath1     = $"{subPath}/1";
            var subFile1     = $"{subPath}/1/1.txt";
            var subPath1_1   = $"{subPath}/1/1_1";
            var subFile1_1   = $"{subPath}/1/1_1/1_1.txt";
            var subPath1_1_1 = $"{subPath}/1/1_1/1_1_1";
            var subPath2     = $"{subPath}/2";

            var uPath = UPath.Combine(rootUPath, "..");

            var content      = "This is test string";
            var contentBytes = Encoding.UTF8.GetBytes(content);

            if (fileSystem.DirectoryExists(subPath1_1_1) == false)
            {
                fileSystem.CreateDirectory(subPath1_1_1);
            }

            if (fileSystem.DirectoryExists(subPath2) == false)
            {
                fileSystem.CreateDirectory(subPath2);
            }

            if (fileSystem.FileExists(subFile1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            if (fileSystem.FileExists(subFile1_1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1_1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            var directoryEntries = fileSystem.EnumerateDirectoryEntries(subPath);

            foreach (var entry in directoryEntries)
            {
                Console.WriteLine(entry.Path);
            }

            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath2));
        }
Example #7
0
        public void 修改檔案日期()
        {
            var rootUPath = CreateRootPath();

            using var fileSystem = new MemoryFileSystem();

            var subName = "TestFolder";

            var subPath      = $"{rootUPath}/{subName}";
            var subPath1     = $"{subPath}/1";
            var subFile1     = $"{subPath}/1/1.txt";
            var subFile2     = $"{subPath}/1/2.txt";
            var subPath1_1   = $"{subPath}/1/1_1";
            var subFile1_1   = $"{subPath}/1/1_1/1_1.txt";
            var subPath1_1_1 = $"{subPath}/1/1_1/1_1_1";
            var subPath2     = $"{subPath}/2";
            var content      = "This is test string";
            var contentBytes = Encoding.UTF8.GetBytes(content);

            if (fileSystem.DirectoryExists(subPath1_1_1) == false)
            {
                fileSystem.CreateDirectory(subPath1_1_1);
            }

            if (fileSystem.DirectoryExists(subPath2) == false)
            {
                fileSystem.CreateDirectory(subPath2);
            }

            if (fileSystem.FileExists(subFile1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            if (fileSystem.FileExists(subFile1_1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1_1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            var fileEntry = fileSystem.GetFileEntry(subFile1);

            fileEntry.CreationTime   = new DateTime(1900, 1, 1);
            fileEntry.LastWriteTime  = new DateTime(1900, 1, 2);
            fileEntry.LastAccessTime = new DateTime(1900, 1, 3);

            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath2));

            fileSystem.DeleteDirectory(subPath, true);
        }
Example #8
0
        public void 在資料夾建立檔案()
        {
            var rootUPath = CreateRootPath();

            using var fileSystem = new MemoryFileSystem();

            var subName      = "TestFolder";
            var subPath      = $"{rootUPath}/{subName}";
            var subPath1     = $"{subPath}/1";
            var subFile1     = $"{subPath}/1/1.txt";
            var subPath1_1   = $"{subPath}/1/1_1";
            var subFile1_1   = $"{subPath}/1/1_1/1_1.txt";
            var subPath1_1_1 = $"{subPath}/1/1_1/1_1_1";
            var subPath2     = $"{subPath}/2";
            var content      = "This is test string";
            var contentBytes = Encoding.UTF8.GetBytes(content);

            if (fileSystem.DirectoryExists(subPath1_1_1) == false)
            {
                fileSystem.CreateDirectory(subPath1_1_1);
            }

            if (fileSystem.DirectoryExists(subPath2) == false)
            {
                fileSystem.CreateDirectory(subPath2);
            }

            if (fileSystem.FileExists(subFile1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            if (fileSystem.FileExists(subFile1_1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1_1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath2));
        }
Example #9
0
        /// <summary>
        /// Creates a <see cref="MemoryFileSystem"/> based on the given <see cref="Stream"/>.
        /// </summary>
        /// <param name="stream">The <see cref="Stream"/> to add to the file system.</param>
        /// <param name="streamName">The path of the stream in the file system.</param>
        /// <param name="streamManager">The stream manager for this file system.</param>
        /// <returns>The created <see cref="IFileSystem"/> for this stream.</returns>
        public static IFileSystem CreateMemoryFileSystem(Stream stream, UPath streamName, IStreamManager streamManager)
        {
            // 1. Create file system
            var fileSystem = new MemoryFileSystem(streamManager);
            var directory  = streamName.GetDirectory();

            if (!directory.IsEmpty && !fileSystem.DirectoryExists(streamName.GetDirectory()))
            {
                fileSystem.CreateDirectory(streamName.GetDirectory());
            }

            var createdStream = fileSystem.OpenFile(streamName, FileMode.CreateNew, FileAccess.Write);

            // 2. Copy data
            var bkPos = stream.Position;

            stream.Position = 0;
            stream.CopyTo(createdStream);
            stream.Position        = bkPos;
            createdStream.Position = 0;
            createdStream.Close();

            return(fileSystem);
        }