Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Sep 8, 2024
1 parent 280cde5 commit 03441a8
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 9 deletions.
39 changes: 33 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,42 @@

<img align="right" width="160px" height="160px" src="https://raw.githubusercontent.com/xoofx/XenoAtom.UnixTools/main/img/XenoAtom.UnixTools.png">

This is a default project description.
This project provides a set of Unix tools for .NET 8.0+.

## ✨ Features

- TODO
> **Note**: This project is still in early development and the API is subject to change.
## 📖 User Guide
## ✨ Features

For more details on how to use XenoAtom.UnixTools, please visit the [user guide](https://github.com/xoofx/XenoAtom.UnixTools/blob/main/doc/readme.md).
- **CPIO Archive**: Read and write CPIO archives (Only the newc format is supported)
- **UnixInMemoryFileSystem**: A simple in-memory file system to manipulate files and directories
- This in memory filesystem can be used to easily manipulate in and out CPIO archives
- .NET 8.0+ compatible and NativeAOT ready

## 📖 Usage

Reading a CPIO archive:

```csharp
var cpioReader = new CpioReader(File.OpenRead("archive.cpio"));
while (cpioReader.TryGetNextEntry(out var entry))
{
Console.WriteLine($"Entry: {entry.Name} {entry.FileType} ({entry.Mode})");
})
```

Writing a CPIO archive with a `UnixInMemoryFileSystem`:

```csharp
var stream = new MemoryStream();
var fs = new UnixInMemoryFileSystem();
fs.CreateDirectory("/dir1");
fs.CreateDirectory("/dir1/dir2");
fs.CreateFile("/dir1/file1.txt", "Hello World");
{
using var writer = new CpioWriter(File.Create("archive.cpio"));
fs.WriteTo(writer);
}
```

## 🪪 License

Expand Down
82 changes: 82 additions & 0 deletions src/XenoAtom.UnixTools.Tests/CpioTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,93 @@
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.

using System.Text;

namespace XenoAtom.UnixTools.Tests;

[TestClass]
public class CpioTests
{
[TestMethod]
public void TestWriterWithFileSystem()
{
var stream = new MemoryStream();
var fs = new UnixInMemoryFileSystem();
fs.CreateDirectory("/dir1");
fs.CreateDirectory("/dir1/dir2");
fs.CreateFile("/dir1/file1.txt", "Hello World");
{
//using var writer = new CpioWriter(File.Create("archive.cpio"));
using var writer = new CpioWriter(stream, true);
fs.WriteTo(writer);
}

stream.Position = 0;
var reader = new CpioReader(stream, true);
var fsOut = new UnixInMemoryFileSystem();
fsOut.ReadFrom(reader);

CollectionAssert.AreEqual(fs.RootDirectory.EnumerateFileSystemEntries(SearchOption.AllDirectories).Select(x => x.FullPath).ToList(), fsOut.RootDirectory.EnumerateFileSystemEntries(SearchOption.AllDirectories).Select(x => x.FullPath).ToList());
}

[TestMethod]
public void TestManualArchiveReadWrite()
{
var entry = new CpioEntry
{
Name = "test.txt",
InodeNumber = 0,
FileType = CpioFileType.RegularFile,
Mode = UnixFileMode.UserRead | UnixFileMode.UserWrite,
Uid = 0,
Gid = 0,
Length = 11,
HardLinkCount = 1,
Device = new DeviceId(1, 3),
ModificationTime = new DateTimeOffset(2010, 1, 1, 0, 0, 0, default),
Checksum = 0,
DataStream = new MemoryStream(Encoding.UTF8.GetBytes("Hello World"))
};

var fileStream = new MemoryStream();
using (var writer = new CpioWriter(fileStream, true))
{
writer.AddEntry(entry);
}

fileStream.Position = 0;
using (var reader = new CpioReader(fileStream))
{
Assert.IsTrue(reader.TryGetNextEntry(out var entryOut));
Assert.AreEqual(entry.Name, entryOut.Name);
Assert.AreEqual(entry.InodeNumber, entryOut.InodeNumber);
Assert.AreEqual(entry.FileType, entryOut.FileType);
Assert.AreEqual(entry.Mode, entryOut.Mode);
Assert.AreEqual(entry.Uid, entryOut.Uid);
Assert.AreEqual(entry.Gid, entryOut.Gid);
Assert.AreEqual(entry.Length, entryOut.Length);
Assert.AreEqual(entry.HardLinkCount, entryOut.HardLinkCount);
Assert.AreEqual(entry.Device, entryOut.Device);
Assert.AreEqual(entry.ModificationTime, entryOut.ModificationTime);
Assert.AreEqual(entry.Checksum, entryOut.Checksum);

Assert.AreEqual(entry.DataStream.Length, entryOut.DataStream.Length);

var contentIn = new MemoryStream();
var contentOut = new MemoryStream();
entry.DataStream.Position = 0;
entry.DataStream.CopyTo(contentIn);
entryOut.DataStream.Position = 0;
entryOut.DataStream.CopyTo(contentOut);

Assert.AreEqual(contentIn.Length, contentOut.Length);
for (int i = 0; i < contentIn.Length; i++)
{
Assert.AreEqual(contentIn.GetBuffer()[i], contentOut.GetBuffer()[i]);
}
}
}

/// <summary>
/// Test reading an existing archive and writing it as-is.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions src/XenoAtom.UnixTools/XenoAtom.UnixTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
</PropertyGroup>

<PropertyGroup>
<Description>This is a default project description</Description>
<Description>This project provides a set of Unix tools for .NET 8.0+.</Description>
<Copyright>Alexandre Mutel</Copyright>
<NeutralLanguage>en-US</NeutralLanguage>
<Authors>Alexandre Mutel</Authors>
<PackageTags>tag1;tag2;tag3</PackageTags>
<PackageTags>unix;cpio</PackageTags>
<PackageReadmeFile>readme.md</PackageReadmeFile>
<PackageIcon>XenoAtom.UnixTools.png</PackageIcon>
<PackageProjectUrl>https://github.com/xoofx/XenoAtom.UnixTools</PackageProjectUrl>
<PackageProjectUrl>https://github.com/XenoAtom/XenoAtom.UnixTools</PackageProjectUrl>
<PackageLicenseExpression>BSD-2-Clause</PackageLicenseExpression>
<!--Add support for sourcelink-->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
Expand Down

0 comments on commit 03441a8

Please sign in to comment.