SharpExt4, a .Net library, provides read/write Linux ext2/3/4 file system from Windows application (continue)

Follow my previous post: https://www.nickdu.com/?p=896

Open Ext4 file system.

 //Get the file system
 var fs = ExtFileSystem.Open(disk.Parititions[0]);

Sample code to open a file for read

//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);

Sample code for listing all files in a folder

//List all files in /etc folder
foreach (var file in fs.GetFiles("/etc", "*", SearchOption.AllDirectories))
{
    Console.WriteLine(file);
}

Sample code for file creation

//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
file.Write(buf, 0, buf.Length);
file.Close();

Full ExtDisk APIs

public sealed class ExtDisk : IDisposable
{
    public IList<Partition> Parititions { get; }
    public Geometry Geometry { get; }
    public ulong Capacity { get; }

    public static ExtDisk Open(string imagePath);
    public static ExtDisk Open(int DiskNumber);
    public sealed override void Dispose();
    public byte[] GetMasterBootRecord();

}

Full ExtFileSystem APIs

public sealed class ExtFileSystem : IDisposable
{
    public static string MountPoint { get; }
    public string Name { get; }
    public string VolumeLabel { get; }
    public bool CanWrite { get; }
    public string Description { get; }

    public static ExtFileSystem Open(Partition partition);
    public void CopyFile(string sourceFile, string destinationFile, bool overwrite);
    public void CreateDirectory(string path);
    public void CreateHardLink(string target, string path);
    public void CreateSymLink(string target, string path);
    public void DeleteDirectory(string path);
    public void DeleteFile(string path);
    public bool DirectoryExists(string path);
    public sealed override void Dispose();
    public bool FileExists(string path);
    public ValueType GetCreationTime(string path);
    public string[] GetDirectories(string path, string searchPattern, SearchOption searchOption);
    public ulong GetFileLength(string path);
    public string[] GetFiles(string path, string searchPattern, SearchOption searchOption);
    public DateTime GetLastAccessTime(string path);
    public DateTime GetLastWriteTime(string path);
    public uint GetMode(string path);
    public Tuple<uint, uint> GetOwner(string path);
    public void MoveDirectory(string sourceDirectoryName, string destinationDirectoryName);
    public ExtFileStream OpenFile(string path, FileMode mode, FileAccess access);
    public string ReadSymLink(string path);
    public void RenameFile(string sourceFileName, string destFileName);
    public void SetCreationTime(string path, DateTime newTime);
    public void SetLastAccessTime(string path, DateTime newTime);
    public void SetLastWriteTime(string path, DateTime newTime);
    public void SetMode(string path, uint mode);
    public void SetOwner(string path, uint uid, uint gid);
    public sealed override string ToString();
    public void Truncate(string path, ulong size);
}

Full ExtFileStream APIs

public class ExtFileStream : Stream
{
    public ExtFileStream(ExtFileSystem fs, string path, FileMode mode, FileAccess access);

    public override long Position { get; set; }
    public override long Length { get; }
    public override bool CanWrite { get; }
    public override bool CanRead { get; }
    public override bool CanSeek { get; }

    public override void Close();
    public override void Flush();
    public override int Read(byte[] array, int offset, int count);
    public override long Seek(long offset, SeekOrigin origin);
    public override void SetLength(long value);
    public override void Write(byte[] array, int offset, int count);

}

The full SharpExt4 library can be found at my GitHub.

https://github.com/nickdu088/SharpExt4

Leave a Reply

Your email address will not be published.