public static void EnsureDirectoryExists(this FileSystemManager fs, string path) { path = PathTools.Normalize(path); if (fs.DirectoryExists(path)) { return; } PathTools.GetMountNameLength(path, out int mountNameLength).ThrowIfFailure(); // Find the first subdirectory in the path that doesn't exist int i; for (i = path.Length - 1; i > mountNameLength + 2; i--) { if (path[i] == '/') { string subPath = path.Substring(0, i); if (fs.DirectoryExists(subPath)) { break; } } } // path[i] will be a '/', so skip that character i++; for (; i < path.Length; i++) { if (path[i] == '/') { string subPath = path.Substring(0, i); fs.CreateDirectory(subPath); } } fs.CreateDirectory(path); }
public static void CopyFile(this FileSystemManager fs, string sourcePath, string destPath, IProgressReport logger = null) { using (FileHandle sourceHandle = fs.OpenFile(sourcePath, OpenMode.Read)) using (FileHandle destHandle = fs.OpenFile(destPath, OpenMode.Write | OpenMode.Append)) { const int maxBufferSize = 0x10000; long fileSize = fs.GetFileSize(sourceHandle); int bufferSize = (int)Math.Min(maxBufferSize, fileSize); logger?.SetTotal(fileSize); byte[] buffer = ArrayPool <byte> .Shared.Rent(bufferSize); try { for (long offset = 0; offset < fileSize; offset += bufferSize) { int toRead = (int)Math.Min(fileSize - offset, bufferSize); Span <byte> buf = buffer.AsSpan(0, toRead); fs.ReadFile(sourceHandle, buf, offset); fs.WriteFile(destHandle, buf, offset); logger?.ReportAdd(toRead); } } finally { ArrayPool <byte> .Shared.Return(buffer); logger?.SetTotal(0); } fs.FlushFile(destHandle); } }
public static IEnumerable <DirectoryEntry> EnumerateEntries(this FileSystemManager fs, string path, string searchPattern) { return(fs.EnumerateEntries(path, searchPattern, SearchOptions.RecurseSubdirectories)); }
public static IEnumerable <DirectoryEntry> EnumerateEntries(this FileSystemManager fs, string path) { return(fs.EnumerateEntries(path, "*")); }
public static void CreateOrOverwriteFile(this FileSystemManager fs, string path, long size) { fs.CreateOrOverwriteFile(path, size, CreateFileOptions.None); }
public static bool FileExists(this FileSystemManager fs, string path) { return(fs.GetEntryType(path) == DirectoryEntryType.File); }