{"id":912,"date":"2021-04-01T12:33:14","date_gmt":"2021-04-01T02:33:14","guid":{"rendered":"https:\/\/www.nickdu.com\/?p=912"},"modified":"2021-04-01T12:33:14","modified_gmt":"2021-04-01T02:33:14","slug":"sharpext4-a-net-library-provides-read-write-linux-ext2-3-4-file-system-from-windows-application-continue","status":"publish","type":"post","link":"https:\/\/nickdu.com\/?p=912","title":{"rendered":"SharpExt4, a .Net library, provides read\/write Linux ext2\/3\/4 file system from Windows application (continue)"},"content":{"rendered":"\n<p>Follow my previous post: <a href=\"https:\/\/www.nickdu.com\/?p=896\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.nickdu.com\/?p=896<\/a><\/p>\n\n\n\n<p>Open Ext4 file system.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> \/\/Get the file system\n var fs = ExtFileSystem.Open(disk.Parititions&#91;0]);<\/code><\/pre>\n\n\n\n<p>Sample code to open a file for read<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Open a file for read\nvar file = fs.OpenFile(\"\/etc\/shells\", FileMode.Open, FileAccess.Read);\n\/\/Check the file length\nvar filelen = file.Length;\nvar buf = new byte&#91;filelen];\n\/\/Read the file content\nvar count = file.Read(buf, 0, (int)filelen);\nfile.Close();\nvar content = Encoding.Default.GetString(buf);\nConsole.WriteLine(content);<\/code><\/pre>\n\n\n\n<p>Sample code for listing all files in a folder<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/List all files in \/etc folder\nforeach (var file in fs.GetFiles(\"\/etc\", \"*\", SearchOption.AllDirectories))\n{\n    Console.WriteLine(file);\n}<\/code><\/pre>\n\n\n\n<p>Sample code for file creation<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Open a file for write\nvar file = fs.OpenFile(\"\/etc\/test\", FileMode.Create, FileAccess.Write);\nvar hello = \"Hello World\";\nvar buf = Encoding.ASCII.GetBytes(hello);\n\/\/Write to file\nfile.Write(buf, 0, buf.Length);\nfile.Close();<\/code><\/pre>\n\n\n\n<p>Full ExtDisk APIs<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public sealed class ExtDisk : IDisposable\n{\n    public IList&lt;Partition&gt; Parititions { get; }\n    public Geometry Geometry { get; }\n    public ulong Capacity { get; }\n\n    public static ExtDisk Open(string imagePath);\n    public static ExtDisk Open(int DiskNumber);\n    public sealed override void Dispose();\n    public byte&#91;] GetMasterBootRecord();\n\n}<\/code><\/pre>\n\n\n\n<p>Full ExtFileSystem APIs<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public sealed class ExtFileSystem : IDisposable\n{\n    public static string MountPoint { get; }\n    public string Name { get; }\n    public string VolumeLabel { get; }\n    public bool CanWrite { get; }\n    public string Description { get; }\n\n    public static ExtFileSystem Open(Partition partition);\n    public void CopyFile(string sourceFile, string destinationFile, bool overwrite);\n    public void CreateDirectory(string path);\n    public void CreateHardLink(string target, string path);\n    public void CreateSymLink(string target, string path);\n    public void DeleteDirectory(string path);\n    public void DeleteFile(string path);\n    public bool DirectoryExists(string path);\n    public sealed override void Dispose();\n    public bool FileExists(string path);\n    public ValueType GetCreationTime(string path);\n    public string&#91;] GetDirectories(string path, string searchPattern, SearchOption searchOption);\n    public ulong GetFileLength(string path);\n    public string&#91;] GetFiles(string path, string searchPattern, SearchOption searchOption);\n    public DateTime GetLastAccessTime(string path);\n    public DateTime GetLastWriteTime(string path);\n    public uint GetMode(string path);\n    public Tuple&lt;uint, uint&gt; GetOwner(string path);\n    public void MoveDirectory(string sourceDirectoryName, string destinationDirectoryName);\n    public ExtFileStream OpenFile(string path, FileMode mode, FileAccess access);\n    public string ReadSymLink(string path);\n    public void RenameFile(string sourceFileName, string destFileName);\n    public void SetCreationTime(string path, DateTime newTime);\n    public void SetLastAccessTime(string path, DateTime newTime);\n    public void SetLastWriteTime(string path, DateTime newTime);\n    public void SetMode(string path, uint mode);\n    public void SetOwner(string path, uint uid, uint gid);\n    public sealed override string ToString();\n    public void Truncate(string path, ulong size);\n}<\/code><\/pre>\n\n\n\n<p>Full ExtFileStream APIs<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ExtFileStream : Stream\n{\n    public ExtFileStream(ExtFileSystem fs, string path, FileMode mode, FileAccess access);\n\n    public override long Position { get; set; }\n    public override long Length { get; }\n    public override bool CanWrite { get; }\n    public override bool CanRead { get; }\n    public override bool CanSeek { get; }\n\n    public override void Close();\n    public override void Flush();\n    public override int Read(byte&#91;] array, int offset, int count);\n    public override long Seek(long offset, SeekOrigin origin);\n    public override void SetLength(long value);\n    public override void Write(byte&#91;] array, int offset, int count);\n\n}<\/code><\/pre>\n\n\n\n<p>The full SharpExt4 library can be found at my GitHub.<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/nickdu088\/SharpExt4\">https:\/\/github.com\/nickdu088\/SharpExt4<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Follow my previous post: https:\/\/www.nickdu.com\/?p=896 Open Ext4 file system. Sample code to open a file for read Sample code for listing all files in a folder Sample code for file creation Full ExtDisk APIs Full ExtFileSystem APIs Full ExtFileStream APIs The full SharpExt4 library can be found at my GitHub. https:\/\/github.com\/nickdu088\/SharpExt4<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,12,2],"tags":[],"class_list":["post-912","post","type-post","status-publish","format-standard","hentry","category-net","category-cc","category-it"],"_links":{"self":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/912","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=912"}],"version-history":[{"count":0,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/912\/revisions"}],"wp:attachment":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}