The main purpose of this SharpExt4 project is to provide full access to Linux ext2/3/4 filesystem from Windows .Net application.
For a day-to-day Windows user, it is not easy to read/write ext2/3/4 filesystem directly from Windows environment. C# .Net programmers espcially find it hard to search for a .Net library, which can provide full access to Linux ext2/3/4 filesystem.
These are the findings so far:
- DiscUtils, is a .NET library to read and write ISO files and Virtual Machine disk files (VHD, VDI, XVA, VMDK, etc). DiscUtils also provides limited access to ext2/3/4 filesystem.
- Ext2Fsd is another Windows file system driver for the Ext2, Ext3, and Ext4 file systems. It allows Windows to read Linux file systems natively, providing access to the file system via a drive letter that any program can access.
- DiskInternals Linux Reader is a freeware application from DiskInternals, developers of data recovery software.
- Ext2explore is an open-source application that works similarly to DiskInternals Linux Reader—but only for Ext4, Ext3, and Ext2 partitions.
- The lwext4 project provides the ext2/3/4 filesystem for microcontrollers.
The lwext4 is a portable C project for microcontrollers and has many cool and unique features. Lwext4 is an excellent choice for SD/MMC cards, USB flash drives or any other wear leveled memory types. In Windows, the author recommended to use MSYS-2
I imported the lwext4 backbone over to MSVC compiler (Visual Studio 2019), and created the lwext4 as a static lib. SharpExt4 is a clr wrapper of lwext4 to provide modem .Net application access. The SharpExt4 borrows the DiscUtils class concept and creates a friendly interface for .Net
Visual Studio 2022 C/C++ (It can be simply modified to be compiled in Visual Studio 2013 and Visual Studio 2019)
How to use SharpExt4 to access Raspberry Pi SD Card Linux partition.
Here's a few simple examples.
...
//Open a Linux ext4 disk image
var disk = ExtDisk.Open(@".\ext4.img");
//Get the Linux partition and open
var fs = ExtFileSystem.Open(disk.Parititions[0]);
//Open a file for read
var file = fs.OpenFile("/etc/shells", FileMode.Open, FileAccess.Read);
//Check the file length
var filelen = file.Length;
var buf = new byte[filelen];
//Read the file content
var count = file.Read(buf, 0, (int)filelen);
file.Close();
var content = Encoding.Default.GetString(buf);
Console.WriteLine(content);
...
...
//Open a Linux ext4 disk image
var disk = ExtDisk.Open(@".\ext4.img");
//Get the Linux partition, and open
var fs = ExtFileSystem.Open(disk.Parititions[0]);
//List all files in /etc folder
foreach(var file in fs.GetFiles("/etc", "*", SearchOption.AllDirectories))
{
Console.WriteLine(file);
}
...
...
//Open a Linux ext4 disk image
var disk = ExtDisk.Open(@".\org.img");
//Get the file system
var fs = ExtFileSystem.Open(disk.Parititions[0]);
//Open a file for write
var file = fs.OpenFile("/etc/test", FileMode.Create, FileAccess.Write);
var hello = "Hello World";
var buf = Encoding.ASCII.GetBytes(hello);
//Write to file
var count = file.Read(buf, 0, buf.Length);
file.Close();
...
...
//Open a Linux ext4 disk assume your SD card/USB is physical disk 1
var disk = ExtDisk.Open(1);
//Get the file system
var fs = ExtFileSystem.Open(disk.Parititions[0]);
//Check file exists
if(fs.FileExists("/etc/hosts"))
{
//0x1FF in HEX, 777 in OCT
fs.SetMode("/etc/hosts", 0x1FF);
}
...
The core library of SharpExt4 was taken from lwext4:
The GPT partition was taken from DiskPartitionInfo: