Skip to content

Commit

Permalink
File mode change
Browse files Browse the repository at this point in the history
  • Loading branch information
teplofizik committed Feb 22, 2022
1 parent 80e0d01 commit 0447cb8
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 18 deletions.
14 changes: 7 additions & 7 deletions CpioDump/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static void ModifyCpio(ArgParser Args)
{
var Cpio = Args.GetArg("cpio");
var Root = Args.GetArg("root");
var Delete = Args.GetArg("delete");
var Cmds = Args.GetArg("commands");
var Out = Args.GetArg("out");

if (Cpio != null)
Expand All @@ -43,11 +43,11 @@ static void ModifyCpio(ArgParser Args)

if (Root != null)
{
var DeleteList = GetDeleteList(Delete);
var Commands = GetCommands(Cmds);
// SD LOADER: C:\Users\Professional\Documents\antminer\cpio\Angstrom-antminer_m-eglibc-ipk-v2013.06-beaglebone.rootfs.cpio
//var Packed = CpioParser.Load(@"C:\Users\Professional\Documents\antminer\cpio\Angstrom-antminer_m-eglibc-ipk-v2013.06-beaglebone.rootfs.packed.cpio");

CpioUpdater.UpdateArchive(ref Original, Root, DeleteList);
CpioUpdater.UpdateArchive(ref Original, Root, Commands);

if (Out != null)
CpioPacker.Save(Original, Out);
Expand All @@ -72,7 +72,7 @@ static void ModifyInitramfs(ArgParser Args)
{
var RamFsFile = Args.GetArg("ramfs");
var Root = Args.GetArg("root");
var Delete = Args.GetArg("delete");
var Cmds = Args.GetArg("commands");
var Out = Args.GetArg("out");

if (RamFsFile != null)
Expand All @@ -89,9 +89,9 @@ static void ModifyInitramfs(ArgParser Args)

if (Root != null)
{
var DeleteList = GetDeleteList(Delete);
var Commands = GetCommands(Cmds);

CpioUpdater.UpdateArchive(ref Original, Root, DeleteList);
CpioUpdater.UpdateArchive(ref Original, Root, Commands);

if (Out != null)
{
Expand All @@ -117,7 +117,7 @@ static void ModifyInitramfs(ArgParser Args)
}
}

static string[] GetDeleteList(string Filename)
static string[] GetCommands(string Filename)
{
if ((Filename != null) && File.Exists(Filename))
return File.ReadAllLines(Filename);
Expand Down
2 changes: 1 addition & 1 deletion CpioDump/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"CpioDump": {
"commandName": "Project",
"commandLineArgs": "ramfs=./example/initramfs.bin.SD root=./root/ delete=delete.txt out=initramfs.bin.SD"
"commandLineArgs": "ramfs=./example/initramfs.bin.SD root=./root/ commands=commands.txt out=initramfs.bin.SD"
}
}
}
73 changes: 64 additions & 9 deletions CpioLib/IO/CpioUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,14 @@ public static void Info(ref CpioArchive Archive)
}
}

public static void UpdateArchive(ref CpioArchive Archive, string RootDir, string[] DeleteList)
public static void UpdateArchive(ref CpioArchive Archive, string RootDir, string[] CommandsList)
{
var Processed = new List<string>();
var Filenames = Array.ConvertAll(Archive.Files.ToArray(), F => F.Path);
foreach(var F in Filenames)
{
var LocalPath = Path.Combine(RootDir, F);

if(DeleteList.Contains(F))
{
Console.WriteLine($"Delete {F}");

Archive.Delete(F);
Processed.Add(F);
}

if(File.Exists(LocalPath))
{
Console.WriteLine($"Update {F}");
Expand Down Expand Up @@ -76,6 +68,69 @@ public static void UpdateArchive(ref CpioArchive Archive, string RootDir, string
}
}
}

foreach (var Cmd in CommandsList)
{
var Parts = Cmd.Split(new char[] { ' ' });
ProcessCommand(Parts, Archive);
}
}

private static UInt32 ConvertMode(string Mode)
{
UInt32 ModeX = 0;
for(int i = 0; i < 3; i++)
{
int Offset = i * 3;

for(int c = 0; c < 3; c++)
{
var C = Mode[Offset + c];

if (C == 'r') ModeX |= 4U << ((2 - i) * 4);
if (C == 'w') ModeX |= 2U << ((2 - i) * 4);
if (C == 'x') ModeX |= 1U << ((2 - i) * 4);
}
}
return ModeX;
}

private static string ConvertModeToString(UInt32 Mode)
{
var Res = "";
for(int i = 0; i < 3; i++)
{
UInt32 Part = (Mode >> (2 - i) * 4) & 0x7;

Res += ((Part & 0x04) != 0) ? "r" : "-";
Res += ((Part & 0x02) != 0) ? "w" : "-";
Res += ((Part & 0x01) != 0) ? "x" : "-";
}
return Res;
}

private static void ProcessCommand(string[] Command, CpioArchive Archive)
{
if(Command.Length>= 2)
{
var Cmd = Command[0];
var Path = Command[1];
switch (Cmd)
{
case "rm":
Archive.Delete(Path);
Console.WriteLine($"Delete {Path}");
break;
case "chmod":
if (Command.Length == 3)
{
var Mode = (Command[2].Length == 9) ? ConvertMode(Command[2]) : Convert.ToUInt32(Command[2], 16) & 0xFFF;
Archive.ChMod(Path, Mode);
Console.WriteLine($"ChMod {Path}: {ConvertModeToString(Mode)}");
}
break;
}
}
}
}
}
16 changes: 16 additions & 0 deletions CpioLib/Types/CpioArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ public void Delete(string Filename)
Files.RemoveAll(F => (F.Path == Filename));
}

public void ChMod(string Filename, UInt32 Mode)
{
var F = GetFile(Filename);
if(F != null)
{
// rwxr-x---
var OldMode = F.Mode & ~0x1FFU;

OldMode |= (Mode & 0x7);
OldMode |= ((Mode >> 4) & 0x7) << 3;
OldMode |= ((Mode >> 8) & 0x7) << 6;

F.Mode = OldMode;
}
}

public void AddDir(string Filename, string LocalPath)
{
Files.Add(new CpioFile(Filename, LocalPath, true));
Expand Down
13 changes: 12 additions & 1 deletion CpioLib/Types/CpioFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,18 @@ private void SetAsciiValue(long HeaderOffset, int Size, UInt32 value)

// https://developer.adobe.com/experience-manager/reference-materials/6-4/javadoc/org/apache/commons/compress/archivers/cpio/CpioArchiveEntry.html
public UInt32 INode => GetAsciiValue(6, 8);
public UInt32 Mode => GetAsciiValue(14, 8);
public UInt32 Mode
{
get
{
return GetAsciiValue(14, 8);
}
set
{
SetAsciiValue(14, 8, value);
}
}

public UInt32 UserId => GetAsciiValue(22, 8);
public UInt32 GroupId => GetAsciiValue(30, 8);
public UInt32 NumLink => GetAsciiValue(38, 8);
Expand Down

0 comments on commit 0447cb8

Please sign in to comment.