public void 射資料結構2() { var executingAssembly = Assembly.GetExecutingAssembly(); var rootPath = Path.GetDirectoryName(executingAssembly.Location); var subPath = "TestFolder"; using var fileSystem = CreateFolder(rootPath, subPath); using var memoryFileSystem = new MemoryFileSystem(); foreach (var line in fileSystem.VisitTree(subPath)) { if (line.Entry.IsDirectory()) { memoryFileSystem.CreateDirectory(line.Path); } if (line.Entry.IsFile()) { fileSystem.CopyFile(line.Path, memoryFileSystem, line.Path); } } memoryFileSystem.CreateDirectory("AAA"); memoryFileSystem.PrintTo(Console.Out); }
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)); }
public static void Main(string[] args) { { #region Snippet_1 IFileSystem ram = new MemoryFileSystem(); ram.CreateDirectory("/tmp/"); ram.CreateDirectory("/mnt/"); ram.CreateDirectory("/usr/lex/"); ram.CreateDirectory("c:/dir/dir/"); ram.CreateFile("/tmp/helloworld.txt", Encoding.UTF8.GetBytes("Hello World!\r\n")); ram.CreateDirectory("file://c:/temp"); foreach (TreeVisit.Line line in ram.VisitTree()) { Console.WriteLine(line); } #endregion Snippet_1 #region Snippet_2 foreach (TreeVisit.Line line in ram.VisitTree(depth: 1)) { Console.WriteLine(line); } #endregion Snippet_2 #region Snippet_3 foreach (TreeVisit.Line line in ram.VisitTree(path: "/tmp/")) { Console.WriteLine(line); } #endregion Snippet_3 } }
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); }
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)); }
public void TestFallback() { // aggregate_fs (fs) // => aggregate_fs (subFs) // => memory_fs (subFsMemFs) // => memory_fs (subMemFs) // => memory_fs (root) var root = new MemoryFileSystem(); var fs = new AggregateFileSystem(root); var subFsMemFs = new MemoryFileSystem(); var subFs = new AggregateFileSystem(subFsMemFs); fs.AddFileSystem(subFs); var subMemFs = new MemoryFileSystem(); fs.AddFileSystem(subMemFs); root.CreateDirectory("/a"); root.CreateDirectory("/b"); root.CreateDirectory("/c"); { using var a = root.OpenFile("/a.txt", FileMode.Create, FileAccess.Write); using var b = root.OpenFile("/b.txt", FileMode.Create, FileAccess.Write); using var c = root.OpenFile("/c.txt", FileMode.Create, FileAccess.Write); } subFsMemFs.CreateDirectory("/b"); { using var b = subFsMemFs.OpenFile("/b.txt", FileMode.Create, FileAccess.Write); } subMemFs.CreateDirectory("/a"); { using var a = subMemFs.OpenFile("/a.txt", FileMode.Create, FileAccess.Write); } var findA = fs.FindFirstFileSystemEntry("/a"); Assert.Equal(subMemFs, findA?.FileSystem); var findB = fs.FindFirstFileSystemEntry("/b"); Assert.Equal(subFsMemFs, findB?.FileSystem); var findC = fs.FindFirstFileSystemEntry("/c"); Assert.Equal(root, findC?.FileSystem); Assert.True(fs.DirectoryExists("/c")); Assert.True(fs.DirectoryExists("/b")); Assert.True(fs.DirectoryExists("/a")); Assert.True(fs.FileExists("/c.txt")); Assert.True(fs.FileExists("/b.txt")); Assert.True(fs.FileExists("/a.txt")); }
public static void Main(string[] args) { { #region Snippet_1 IFileSystem ram = new MemoryFileSystem(); ram.CreateDirectory("/tmp/"); ram.CreateDirectory("/mnt/"); ram.CreateDirectory("/usr/lex/"); ram.CreateDirectory("c:/dir/dir/"); ram.CreateFile("/tmp/helloworld.txt", Encoding.UTF8.GetBytes("Hello World!\r\n")); ram.CreateDirectory("file://c:/temp/"); ram.PrintTo(Console.Out); #endregion Snippet_1 #region Snippet_2 StringBuilder sb = new StringBuilder(); ram.PrintTo(sb); #endregion Snippet_2 Console.WriteLine(sb); #region Snippet_3 Console.WriteLine(ram.Print()); #endregion Snippet_3 #region Snippet_4 Console.WriteLine(ram.Print(depth: 1)); #endregion Snippet_4 #region Snippet_5 Console.WriteLine(ram.Print(path: "/tmp/")); #endregion Snippet_5 #region Snippet_6 string tree = ram.Print(format: PrintTree.Format.Tree | PrintTree.Format.Path | PrintTree.Format.Length | PrintTree.Format.Error); #endregion Snippet_6 Console.WriteLine(tree); } { #region Snippet_10 #endregion Snippet_10 } { #region Snippet_11 #endregion Snippet_11 } { #region Snippet_12 #endregion Snippet_12 } }
public void Initialize() { ram = new MemoryFileSystem(); ram.CreateDirectory("/tmp"); ram.CreateDirectory("/tmp/dir"); ram.CreateDirectory("/mnt"); ram.CreateDirectory("/usr/lex"); ram.CreateDirectory("c:/dir/dir"); ram.CreateFile("/tmp/helloworld.txt", HelloWorld); ram.CreateFile("/tmp/helloworld_100.txt", HelloWorld_x100); ram.CreateFile("/tmp/helloworld_10000.txt", HelloWorld_x10000); //ram.CreateDirectory("file://"); }
private static MemoryFileSystem CreateTestMemoryFile(string folderPath, string content) { var fileSystem = new MemoryFileSystem(); fileSystem.CreateDirectory(folderPath); for (var i = 0; i < 5; i++) { var filePath = $"{folderPath}/{i}.txt"; // via stream using (var outputStream = fileSystem.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { Write(outputStream, $"{i}.{content}"); } // via IFileSystem.Create // var contentBytes = Encoding.UTF8.GetBytes($"{i}.{content}"); // fileSystem.CreateFile(filePath, contentBytes); } var type = typeof(FileEntry); var offset = new DateTimeOffset(DateTime.UtcNow.AddDays(-3)); foreach (var entry in fileSystem.Browse(folderPath)) { var lastAccessPropertyInfo = type.GetProperty("LastAccess"); var lastModifiedPropertyInfo = type.GetProperty("LastModified"); lastAccessPropertyInfo.SetValue(entry, offset); lastModifiedPropertyInfo.SetValue(entry, offset); } return(fileSystem); }
/// <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); }
public void 列舉根路徑內的子資料夾() { using var fileSystem = new MemoryFileSystem(); Console.WriteLine("建立資料夾"); fileSystem.CreateDirectory("dir1/dir2/dir3/"); fileSystem.PrintTo(Console.Out); var content = "This is test string"; var contentBytes = Encoding.UTF8.GetBytes($"{content}"); Console.WriteLine("dir1 底下建立檔案"); using (var outputStream = fileSystem.Open("dir1/1.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { var bytes = Encoding.UTF8.GetBytes(content); outputStream.Write(bytes, 0, bytes.Length); } Console.WriteLine("dir2 底下建立檔案"); fileSystem.CreateFile("dir1/dir2/2.txt", contentBytes); foreach (var entry in fileSystem.Browse("")) { var path = entry.Path; Console.WriteLine(path); } }
public void EnumerateMountsOverride() { var baseFs = new MemoryFileSystem(); baseFs.CreateDirectory("/foo/bar"); baseFs.WriteAllText("/base.txt", "test"); baseFs.WriteAllText("/foo/base.txt", "test"); baseFs.WriteAllText("/foo/bar/base.txt", "test"); var mountedFs = new MemoryFileSystem(); mountedFs.WriteAllText("/mounted.txt", "test"); var deepMountedFs = new MemoryFileSystem(); deepMountedFs.WriteAllText("/deep_mounted.txt", "test"); var mountFs = new MountFileSystem(baseFs); mountFs.Mount("/foo", mountedFs); mountFs.Mount("/foo/bar", deepMountedFs); var expected = new List <UPath> { "/base.txt", "/foo", "/foo/bar", "/foo/mounted.txt", "/foo/bar/deep_mounted.txt" }; var actual = mountFs.EnumeratePaths("/", "*", SearchOption.AllDirectories).ToList(); Assert.Equal(expected, actual); }
public void 列舉根路徑底下所有結構() { using var fileSystem = new MemoryFileSystem(); Console.WriteLine("建立資料夾"); fileSystem.CreateDirectory("dir1/dir2/dir3/"); fileSystem.PrintTo(Console.Out); var content = "This is test string"; var contentBytes = Encoding.UTF8.GetBytes($"{content}"); Console.WriteLine("dir1 底下建立檔案"); using (var outputStream = fileSystem.Open("dir1/1.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { var bytes = Encoding.UTF8.GetBytes(content); outputStream.Write(bytes, 0, bytes.Length); } Console.WriteLine("dir2 底下建立檔案"); fileSystem.CreateFile("dir1/dir2/2.txt", contentBytes); var tree = fileSystem.VisitTree(); foreach (var line in tree) { Console.WriteLine($"name:{line.Name},path:{line.Path}"); } }
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); } }
public void 修改檔案日期() { using var fileSystem = new MemoryFileSystem(); Console.WriteLine("建立資料夾"); fileSystem.CreateDirectory("dir1/dir2/dir3/"); fileSystem.PrintTo(Console.Out); var content = "This is test string"; var contentBytes = Encoding.UTF8.GetBytes($"{content}"); Console.WriteLine("dir2 底下建立檔案"); fileSystem.CreateFile("dir1/dir2/2.txt", contentBytes); var entry = fileSystem.GetEntry("dir1/dir2/2.txt"); Console.WriteLine("檔案修改前的日期"); Console.WriteLine($"LastAccess:{entry.LastAccess}"); Console.WriteLine($"LastModified:{entry.LastModified}"); var type = entry.GetType(); var now = new DateTimeOffset(DateTime.UtcNow.AddDays(-30)); var lastAccessPropertyInfo = type.GetProperty("LastAccess"); var lastModifiedPropertyInfo = type.GetProperty("LastModified"); lastAccessPropertyInfo.SetValue(entry, now); lastModifiedPropertyInfo.SetValue(entry, now); Console.WriteLine("檔案修改後的日期"); Console.WriteLine($"LastAccess:{entry.LastAccess}"); Console.WriteLine($"LastModified:{entry.LastModified}"); }
public void 建立資料夾() { using var fileSystem = new MemoryFileSystem(); Console.WriteLine("建立資料夾"); fileSystem.CreateDirectory("dir1/dir2/dir3/"); fileSystem.PrintTo(Console.Out); }
public virtual void SetUp() { var path = FileSystemPath.Root.AppendDirectory("w"); FileSystem = new MemoryFileSystem(); FileSystem.CreateDirectory(path); Watcher = new MemoryFileSystemWatcher(FileSystem, path); }
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)); }
private static async ValueTask <ISingletonFileSystem> TestFileSystem() { var fileSystem = new MemoryFileSystem(new NullFileEventService()); var root = await fileSystem.CreateFileHandle(Url.Parse("file://test/")); var foo = await fileSystem.CreateDirectory(root, "foo"); var bar = await fileSystem.CreateDirectory(foo, "bar"); var dir = await fileSystem.CreateDirectory(bar, "dir"); await fileSystem.CreateFile(bar, "a", Convert.FromHexString("010203")); await fileSystem.CreateFile(bar, "b", Convert.FromHexString("010203")); await fileSystem.CreateFile(bar, "c", Convert.FromHexString("010203")); await fileSystem.CreateFile(dir, "a", Convert.FromHexString("010203")); await fileSystem.CreateFile(dir, "b", Convert.FromHexString("010203")); return(fileSystem); }
public void Initialize() { ram = new MemoryFileSystem(); ram.CreateDirectory("/tmp"); ram.CreateDirectory("/tmp/dir"); ram.CreateDirectory("/mnt"); ram.CreateDirectory("/usr/lex"); ram.CreateDirectory("c:/dir/dir"); ram.CreateFile("/tmp/helloworld.txt", HelloWorld); ram.CreateFile("/tmp/helloworld_100.txt", HelloWorld_x100); ram.CreateFile("/tmp/helloworld_10000.txt", HelloWorld_x10000); ram.CreateDirectory("file://"); ram2 = new MemoryFileSystem(); ram2.CreateDirectory("/ram2"); ram2.CreateDirectory("/ram2/dir"); ram2.CreateFile("/ram2/dir/helloworld.txt", HelloWorld); ram2.CreateFile("/ram2/dir/helloworld_100.txt", HelloWorld_x100); ram2.CreateFile("/ram2/dir/helloworld_10000.txt", HelloWorld_x10000); fs = ram.Concat(ram2); fs.Observe("**", observer = new Observer()); observer.events.Clear(); }
public void DirectoryEntryPartialMountName() { var fs = new MemoryFileSystem(); fs.CreateDirectory("/w"); var mountFs = new MountFileSystem(); mountFs.Mount("/x/y/z", fs); Assert.NotNull(mountFs.GetDirectoryEntry("/x")); Assert.NotNull(mountFs.GetDirectoryEntry("/x/y")); Assert.NotNull(mountFs.GetDirectoryEntry("/x/y/z")); Assert.NotNull(mountFs.GetDirectoryEntry("/x/y/z/w")); }
internal static void CopyFolder(this MemoryFileSystem fileSystem, string basePath, string source, string destination) { fileSystem.CreateDirectory(destination); // Copy dirs recursively foreach (var child in Directory.EnumerateDirectories(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory(typeof(TestExtensions)), basePath, source)).Select(Path.GetFileName)) { fileSystem.CopyFolder(basePath, Path.Combine(source, child), Path.Combine(destination, child)); } // Copy files foreach (var childFile in Directory.EnumerateFiles(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory(typeof(TestExtensions)), basePath, source)).Select(Path.GetFileName)) { fileSystem.CopyFile(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory(typeof(TestExtensions)), basePath, source, childFile), Path.Combine(destination, childFile)); } }
public void TestFindFileSystemEntries() { var fs = new AggregateFileSystem(); var memfs1 = new MemoryFileSystem(); memfs1.WriteAllText("/a.txt", "content1"); memfs1.WriteAllText("/b", "notused"); fs.AddFileSystem(memfs1); var memfs2 = new MemoryFileSystem(); memfs2.WriteAllText("/a.txt", "content2"); memfs2.CreateDirectory("/b"); fs.AddFileSystem(memfs2); { var entries = fs.FindFileSystemEntries("/a.txt"); Assert.Equal(2, entries.Count); Assert.IsType <FileEntry>(entries[0]); Assert.IsType <FileEntry>(entries[1]); Assert.Equal(memfs2, entries[0].FileSystem); Assert.Equal(memfs1, entries[1].FileSystem); Assert.Equal("/a.txt", entries[0].Path.FullName); Assert.Equal("/a.txt", entries[1].Path.FullName); } { var entries = fs.FindFileSystemEntries("/b"); Assert.Single(entries); Assert.IsType <DirectoryEntry>(entries[0]); Assert.Equal(memfs2, entries[0].FileSystem); Assert.Equal("/b", entries[0].Path.FullName); } { var entry = fs.FindFirstFileSystemEntry("/a.txt"); Assert.NotNull(entry); Assert.IsType <FileEntry>(entry); Assert.Equal(memfs2, entry.FileSystem); Assert.Equal("/a.txt", entry.Path.FullName); } }
public void 射資料結構() { var executingAssembly = Assembly.GetExecutingAssembly(); var rootPath = Path.GetDirectoryName(executingAssembly.Location); var subPath = "TestFolder"; var fileSystem = CreateFolder(rootPath, subPath); var virtualFileSystem = new VirtualFileSystem(); var memoryFileSystem = new MemoryFileSystem(); var appDir = rootPath.Replace('\\', '/'); virtualFileSystem.Mount("", Lexical.FileSystem.FileSystem.OS, Option.SubPath(appDir)); virtualFileSystem.CopyTree($"/{subPath}/", memoryFileSystem, ""); memoryFileSystem.CreateDirectory("AAA"); Console.WriteLine("memoryFileSystem"); memoryFileSystem.PrintTo(Console.Out); }
/// <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); }
public static void Main(string[] args) { { #region Snippet_1 IFileSystem vfs = new VirtualFileSystem(); #endregion Snippet_1 } { #region Snippet_2a IFileSystem vfs = new VirtualFileSystem() .Mount("", FileSystem.OS); #endregion Snippet_2a } { #region Snippet_2b IFileSystem urls = new VirtualFileSystem() .Mount("tmp/", FileSystem.Temp) .Mount("ram/", MemoryFileSystem.Instance); #endregion Snippet_2b } { #region Snippet_2c IFileSystem vfs = new VirtualFileSystem() .Mount("", FileSystem.OS) .Mount("/tmp/", new MemoryFileSystem()); vfs.Browse("/tmp/"); #endregion Snippet_2c } { #region Snippet_3a IFileSystem vfs = new VirtualFileSystem(); vfs.Mount("/tmp/", FileSystem.Temp); vfs.Unmount("/tmp/"); #endregion Snippet_3a } { #region Snippet_3b IFileSystem vfs = new VirtualFileSystem(); vfs.Mount("/tmp/", FileSystem.Temp); vfs.Mount("/tmp/", new MemoryFileSystem()); #endregion Snippet_3b } { #region Snippet_4a IFileSystem vfs = new VirtualFileSystem(); vfs.Mount("/app/", FileSystem.Application, Option.ReadOnly); #endregion Snippet_4a } { #region Snippet_4b IFileSystem vfs = new VirtualFileSystem(); IFileSystem overrides = new MemoryFileSystem(); overrides.CreateFile("important.dat", new byte[] { 12, 23, 45, 67, 89 }); vfs.Mount("/app/", overrides, FileSystem.Application); #endregion Snippet_4b } { #region Snippet_4c IFileSystem overrides = new MemoryFileSystem(); IFileSystem vfs = new VirtualFileSystem(); vfs.Mount("/app/", (overrides, Option.ReadOnly), (FileSystem.Application, Option.ReadOnly) ); #endregion Snippet_4c } { #region Snippet_4d IFileSystem vfs = new VirtualFileSystem(); string appDir = AppDomain.CurrentDomain.BaseDirectory.Replace('\\', '/'); vfs.Mount("/app/", FileSystem.OS, Option.SubPath(appDir)); #endregion Snippet_4d } { #region Snippet_5a IFileSystem vfs = new VirtualFileSystem(); vfs.Mount("/tmp/", filesystem: null); #endregion Snippet_5a vfs.PrintTo(Console.Out); } // Observing { #region Snippet_6a IFileSystem vfs = new VirtualFileSystem(); vfs.Observe("**", new PrintObserver()); IFileSystem ram = new MemoryFileSystem(); ram.CreateDirectory("/dir/"); ram.CreateFile("/dir/file.txt", new byte[] { 32, 65, 66 }); vfs.Mount("", ram); #endregion Snippet_6a } { #region Snippet_6b IFileSystem vfs = new VirtualFileSystem(); vfs.Observe("/dir/*.txt", new PrintObserver()); IFileSystem ram = new MemoryFileSystem(); ram.CreateDirectory("/dir/"); ram.CreateFile("/dir/file.txt", new byte[] { 32, 65, 66 }); ram.CreateFile("/dir/file.dat", new byte[] { 255, 255, 255 }); vfs.Mount("", ram); #endregion Snippet_6b #region Snippet_6c vfs.Unmount(""); #endregion Snippet_6c } { IFileSystem vfs = new VirtualFileSystem(); vfs.Observe("**", new PrintObserver()); IFileSystem ram = new MemoryFileSystem(); ram.CreateDirectory("/dir/"); ram.CreateFile("/dir/file.txt", new byte[] { 32, 65, 66 }); #region Snippet_6d vfs.Mount("", ram, Option.NoObserve); #endregion Snippet_6d } { VirtualFileSystem vfs = new VirtualFileSystem(); #region Snippet_6e IDisposable observerHandle = vfs.Observe("**", new PrintObserver()); observerHandle.Dispose(); #endregion Snippet_6e vfs.Dispose(); } { #region Snippet_6f VirtualFileSystem vfs = new VirtualFileSystem(); IDisposable observerHandle = vfs.Observe("**", new PrintObserver()); vfs.Dispose(); #endregion Snippet_6f } { #region Snippet_10a // Init object obj = new ReaderWriterLockSlim(); IFileSystemDisposable vfs = new VirtualFileSystem().AddDisposable(obj); // ... do work ... // Dispose both vfs.Dispose(); #endregion Snippet_10a } { #region Snippet_10b IFileSystemDisposable vfs = new VirtualFileSystem() .AddDisposeAction(f => Console.WriteLine("Disposed")); #endregion Snippet_10b } { #region Snippet_10c VirtualFileSystem vfs = new VirtualFileSystem().Mount("", FileSystem.OS); vfs.Browse(""); // Postpone dispose IDisposable belateDisposeHandle = vfs.BelateDispose(); // Start concurrent work Task.Run(() => { // Do work Thread.Sleep(1000); vfs.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. vfs.Dispose(); #endregion Snippet_10c } { #region Snippet_10d IFileSystemDisposable vfs = new VirtualFileSystem() .Mount("", new FileSystem("")) .Mount("/tmp/", new MemoryFileSystem()) .AddMountsToBeDisposed(); #endregion Snippet_10d vfs.Dispose(); } { #region Snippet_12a #endregion Snippet_12a #region Snippet_12b VirtualFileSystem.Url.PrintTo(Console.Out, "config://", 2, PrintTree.Format.DefaultPath); VirtualFileSystem.Url.PrintTo(Console.Out, "data://", 1, PrintTree.Format.DefaultPath); VirtualFileSystem.Url.PrintTo(Console.Out, "program-data://", 1, PrintTree.Format.DefaultPath); VirtualFileSystem.Url.PrintTo(Console.Out, "home://", 1, PrintTree.Format.DefaultPath); VirtualFileSystem.Url.PrintTo(Console.Out, "https://github.com/tagcode/Lexical.FileSystem/tree/master/"); #endregion Snippet_12b #region Snippet_12c string config = "[Config]\nUser=ExampleUser\n"; VirtualFileSystem.Url.CreateDirectory("config://ApplicationName/"); VirtualFileSystem.Url.CreateFile("config://ApplicationName/config.ini", UTF8Encoding.UTF8.GetBytes(config)); #endregion Snippet_12c #region Snippet_12d byte[] cacheData = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; VirtualFileSystem.Url.CreateDirectory("data://ApplicationName/"); VirtualFileSystem.Url.CreateFile("data://ApplicationName/cache.db", cacheData); #endregion Snippet_12d #region Snippet_12e string saveGame = "[Save]\nLocation=12.32N 43.43W\n"; VirtualFileSystem.Url.CreateDirectory("document://ApplicationName/"); VirtualFileSystem.Url.CreateFile("document://ApplicationName/save1.txt", UTF8Encoding.UTF8.GetBytes(saveGame)); #endregion Snippet_12e #region Snippet_12f byte[] programData = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; VirtualFileSystem.Url.CreateDirectory("program-data://ApplicationName/"); VirtualFileSystem.Url.CreateFile("program-data://ApplicationName/index.db", programData); #endregion Snippet_12f #region Snippet_12g VirtualFileSystem.Url.PrintTo(Console.Out, "application://", format: PrintTree.Format.DefaultPath); #endregion Snippet_12g } }
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> } }
public static void Main(string[] args) {/* * { #region Snippet_1 * // Get filesystem * IFileSystem filesystem = FileSystem.OS; * * // Create scanner * FileScanner filescanner = new FileScanner(filesystem) * .AddWildcard("c:/Windows/System32/**.dll") * .SetReturnDirectories(true) * .SetDirectoryEvaluator(dir => true); * * // Scan files * foreach (var entry in filescanner) * { * Console.WriteLine(entry.Path); * } #endregion Snippet_1 * } */ { #region Snippet_1a IFileSystem fs = new MemoryFileSystem(); fs.CreateDirectory("myfile.zip/folder"); fs.CreateFile("myfile.zip/folder/somefile.txt"); FileScanner filescanner = new FileScanner(fs); #endregion Snippet_1a #region Snippet_1b filescanner.AddWildcard("*.zip"); #endregion Snippet_1b #region Snippet_1c filescanner.AddRegex(path: "", pattern: new Regex(@".*\.zip")); #endregion Snippet_1c #region Snippet_1d filescanner.AddGlobPattern("**.zip/**.txt"); #endregion Snippet_1d #region Snippet_1d2 filescanner.AddGlobPattern("myfile.zip/**.txt"); #endregion Snippet_1d2 #region Snippet_1e foreach (IEntry entry in filescanner) { Console.WriteLine(entry.Path); } #endregion Snippet_1e #region Snippet_1f // Collect errors filescanner.errors = new ConcurrentBag <Exception>(); // Run scan IEntry[] entries = filescanner.ToArray(); // View errors foreach (Exception e in filescanner.errors) { Console.WriteLine(e); } #endregion Snippet_1f #region Snippet_1g filescanner.ReturnDirectories = true; #endregion Snippet_1g #region Snippet_1h filescanner.SetDirectoryEvaluator(e => e.Name != "tmp"); #endregion Snippet_1h } { #region Snippet_10a Regex globPattern = new GlobPatternRegex("**/*.dll/**.resources", "/"); #endregion Snippet_10a #region Snippet_10b string[] files = new[] { "somefile.zip", "somefile.zip/somefile.ext", "somefile.zip/somefile.ext/someresource", "somefile.zip/somelib.dll", "somefile.zip/somelib.dll/someresource.resources", "somelib.dll", "somelib.dll/someresource.resources", }; foreach (string filename in files.Where(fn => globPattern.IsMatch(fn))) { Console.WriteLine(filename); } #endregion Snippet_10b } }
public void TestCreatingTopFile() { var fs = new MemoryFileSystem(); fs.CreateDirectory("/"); }
public static void Main(string[] args) { { #region Snippet_1a IFileSystem ram = new MemoryFileSystem(); IFileSystem rom = ram.Decorate(Option.ReadOnly); #endregion Snippet_1a } { IFileSystem ram = new MemoryFileSystem(); #region Snippet_1b IFileSystem rom = ram.AsReadOnly(); #endregion Snippet_1b } { IFileSystem ram = new MemoryFileSystem(); #region Snippet_2a IFileSystem invisible = ram.Decorate(Option.NoOpen); #endregion Snippet_2a } { IFileSystem ram = new MemoryFileSystem(); #region Snippet_2b IFileSystem invisible = ram.Decorate(Option.NoBrowse); #endregion Snippet_2b } { #region Snippet_3 IFileSystem ram = new MemoryFileSystem(); ram.CreateDirectory("tmp/dir/"); ram.CreateFile("tmp/dir/file.txt", new byte[] { 32, 32, 32, 32, 32, 32, 32, 32, 32 }); IFileSystem tmp = ram.Decorate(Option.SubPath("tmp/")); tmp.PrintTo(Console.Out, format: PrintTree.Format.DefaultPath); #endregion Snippet_3 } { #region Snippet_4a MemoryFileSystem ram = new MemoryFileSystem(); IFileSystemDisposable rom = ram.Decorate(Option.ReadOnly).AddSourceToBeDisposed(); // Do work ... rom.Dispose(); #endregion Snippet_4a } { #region Snippet_4b MemoryFileSystem ram = new MemoryFileSystem(); ram.CreateDirectory("tmp/dir/"); ram.CreateFile("tmp/dir/file.txt", new byte[] { 32, 32, 32, 32, 32, 32, 32, 32, 32 }); IFileSystemDisposable rom = ram.Decorate(Option.ReadOnly).AddDisposable(ram); // Do work ... rom.Dispose(); #endregion Snippet_4b } { #region Snippet_4c // Create ram filesystem MemoryFileSystem ram = new MemoryFileSystem(); ram.CreateDirectory("tmp/dir/"); ram.CreateFile("tmp/dir/file.txt", new byte[] { 32, 32, 32, 32, 32, 32, 32, 32, 32 }); // Create decorations IFileSystemDisposable rom = ram.Decorate(Option.ReadOnly).AddDisposable(ram.BelateDispose()); IFileSystemDisposable tmp = ram.Decorate(Option.SubPath("tmp/")).AddDisposable(ram.BelateDispose()); ram.Dispose(); // <- is actually postponed // Do work ... // Dispose rom1 and tmp, disposes ram as well rom.Dispose(); tmp.Dispose(); #endregion Snippet_4c } }