From 2ac0bbccdc34d4756b37b88ebfc35e0484b52cc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BE=D0=BD=D1=8C=D0=BA=D0=B8=D0=BD=20=D0=90=D0=BB?= =?UTF-8?q?=D0=B5=D0=BA=D1=81=D0=B5=D0=B9?= Date: Sat, 10 Sep 2022 22:10:40 +0300 Subject: [PATCH] Ext2 disk size autodetection... --- NyaFs/Filesystem/Ext2/Ext2FsBuilder.cs | 14 +++++------ .../Elements/Fs/LinuxFilesystem.cs | 23 +++++++++++++++++++ .../Elements/Fs/Writer/Ext2FsWriter.cs | 4 ++-- .../ImageFormat/Elements/Fs/Writer/Writer.cs | 8 +++++-- NyaFs/ImageFormat/Helper/LogHelper.cs | 3 ++- NyaFs/Processor/Scripting/Commands/Store.cs | 6 ++--- 6 files changed, 43 insertions(+), 15 deletions(-) diff --git a/NyaFs/Filesystem/Ext2/Ext2FsBuilder.cs b/NyaFs/Filesystem/Ext2/Ext2FsBuilder.cs index 61cb511..5280272 100644 --- a/NyaFs/Filesystem/Ext2/Ext2FsBuilder.cs +++ b/NyaFs/Filesystem/Ext2/Ext2FsBuilder.cs @@ -295,7 +295,7 @@ public void Block(string Path, uint Major, uint Minor, uint User, uint Group, ui AddNestedNode(Path, () => { var N = CreateNewINode(Types.ExtINodeType.BLOCK, User, Group, Mode); - Log.Write(0, $"INode {N.Index}: {N.FsNodeType} offset {N.getOffset():x08} {Path}"); + //Log.Write(0, $"INode {N.Index}: {N.FsNodeType} offset {N.getOffset():x08} {Path}"); N.Block[0] = (Major & 0xFF) | ((Minor & 0xFF) << 8); return N; }); @@ -315,7 +315,7 @@ public void Char(string Path, uint Major, uint Minor, uint User, uint Group, uin AddNestedNode(Path, () => { var N = CreateNewINode(Types.ExtINodeType.CHAR, User, Group, Mode); - Log.Write(0, $"INode {N.Index}: {N.FsNodeType} offset {N.getOffset():x08} {Path}"); + //Log.Write(0, $"INode {N.Index}: {N.FsNodeType} offset {N.getOffset():x08} {Path}"); N.Block[0] = (Major & 0xFF) | ((Minor & 0xFF) << 8); return N; }); @@ -335,7 +335,7 @@ public void Directory(string Path, uint User, uint Group, uint Mode) if (Dirs.Count == 0) { var N = CreateNewINode(Types.ExtINodeType.DIR, User, Group, Mode); - Log.Write(0, $"INode {N.Index}: {N.FsNodeType} offset {N.getOffset():x08} {Path}"); + //Log.Write(0, $"INode {N.Index}: {N.FsNodeType} offset {N.getOffset():x08} {Path}"); var D = new BuilderDirectory(Path, N); Dirs.Add(D); @@ -354,7 +354,7 @@ public void Directory(string Path, uint User, uint Group, uint Mode) AddNestedNode(Path, () => { var N = CreateNewINode(Types.ExtINodeType.DIR, User, Group, Mode); - Log.Write(0, $"INode {N.Index}: {N.FsNodeType} offset {N.getOffset():x08} {Path}"); + //Log.Write(0, $"INode {N.Index}: {N.FsNodeType} offset {N.getOffset():x08} {Path}"); return N; }); } @@ -371,7 +371,7 @@ public void Fifo(string Path, uint User, uint Group, uint Mode) AddNestedNode(Path, () => { var N = CreateNewINode(Types.ExtINodeType.FIFO, User, Group, Mode); - Log.Write(0, $"INode {N.Index}: {N.FsNodeType} offset {N.getOffset():x08} {Path}"); + //Log.Write(0, $"INode {N.Index}: {N.FsNodeType} offset {N.getOffset():x08} {Path}"); return N; }); } @@ -391,7 +391,7 @@ public void File(string Path, byte[] Content, uint User, uint Group, uint Mode) var N = CreateNewINode(Types.ExtINodeType.REG, User, Group, Mode); SetNodeBlockContent(N, Content); - Log.Write(0, $"INode {N.Index}: {N.FsNodeType} offset {N.getOffset():x08} {Path}"); + //Log.Write(0, $"INode {N.Index}: {N.FsNodeType} offset {N.getOffset():x08} {Path}"); return N; }); } @@ -427,7 +427,7 @@ public void SymLink(string Path, string Target, uint User, uint Group, uint Mode else SetNodeBlockContent(N, UTF8Encoding.UTF8.GetBytes(Target)); - Log.Write(0, $"INode {N.Index}: {N.FsNodeType} offset {N.getOffset():x08} {Path}"); + //Log.Write(0, $"INode {N.Index}: {N.FsNodeType} offset {N.getOffset():x08} {Path}"); return N; }); } diff --git a/NyaFs/ImageFormat/Elements/Fs/LinuxFilesystem.cs b/NyaFs/ImageFormat/Elements/Fs/LinuxFilesystem.cs index a934f23..4ec1c57 100644 --- a/NyaFs/ImageFormat/Elements/Fs/LinuxFilesystem.cs +++ b/NyaFs/ImageFormat/Elements/Fs/LinuxFilesystem.cs @@ -45,5 +45,28 @@ public class LinuxFilesystem public FilesystemItem GetElement(string Path) => Fs.GetElement(Path); public void Delete(string Path) => Fs.Delete(Path); + + public long GetContentSize() => GetDirSize(Fs.Root); + + private long GetDirSize(Filesystem.Universal.Items.Dir Dir) + { + long Res = 0; + + foreach (var I in Dir.Items) + { + switch(I.ItemType) + { + case Filesystem.Universal.Types.FilesystemItemType.File: + case Filesystem.Universal.Types.FilesystemItemType.SymLink: + Res += I.Size; + break; + case Filesystem.Universal.Types.FilesystemItemType.Directory: + Res += GetDirSize(I as Filesystem.Universal.Items.Dir); + break; + } + } + + return Res; + } } } diff --git a/NyaFs/ImageFormat/Elements/Fs/Writer/Ext2FsWriter.cs b/NyaFs/ImageFormat/Elements/Fs/Writer/Ext2FsWriter.cs index 62ad7c8..9f86a77 100644 --- a/NyaFs/ImageFormat/Elements/Fs/Writer/Ext2FsWriter.cs +++ b/NyaFs/ImageFormat/Elements/Fs/Writer/Ext2FsWriter.cs @@ -7,8 +7,8 @@ namespace NyaFs.ImageFormat.Elements.Fs.Writer public class Ext2FsWriter : BaseFsWriter { // TODO: FS size detector - public Ext2FsWriter() : base(new Filesystem.Ext2.Ext2FsBuilder(0x2000000)) { } + public Ext2FsWriter(uint DiskSize) : base(new Filesystem.Ext2.Ext2FsBuilder(DiskSize)) { } - public Ext2FsWriter(string Filename) : base(new Filesystem.Ext2.Ext2FsBuilder(0x2000000), Filename) { } + public Ext2FsWriter(uint DiskSize, string Filename) : base(new Filesystem.Ext2.Ext2FsBuilder(DiskSize), Filename) { } } } diff --git a/NyaFs/ImageFormat/Elements/Fs/Writer/Writer.cs b/NyaFs/ImageFormat/Elements/Fs/Writer/Writer.cs index 2e955ff..3df92b6 100644 --- a/NyaFs/ImageFormat/Elements/Fs/Writer/Writer.cs +++ b/NyaFs/ImageFormat/Elements/Fs/Writer/Writer.cs @@ -1,4 +1,5 @@ -using System; +using Extension.Array; +using System; using System.Collections.Generic; using System.Text; @@ -34,12 +35,15 @@ internal static bool IsFilesystemSupported(Types.FsType Type) } } + internal static uint DetectFixDiskSize(LinuxFilesystem Fs, uint BlockSize) =>Convert.ToUInt32(Fs.GetContentSize() * 1.5).GetAligned(BlockSize); + internal static Writer GetRawFilesystemWriter(LinuxFilesystem Fs) { switch(Fs.FilesystemType) { case Types.FsType.Cpio: return new CpioFsWriter(); - case Types.FsType.Ext2: return new Ext2FsWriter(); + case Types.FsType.Ext2: + return new Ext2FsWriter(DetectFixDiskSize(Fs, 0x800000)); default: throw new InvalidOperationException($"Unsupported filesystem: {Fs.FilesystemType}"); } } diff --git a/NyaFs/ImageFormat/Helper/LogHelper.cs b/NyaFs/ImageFormat/Helper/LogHelper.cs index d344deb..f4324d8 100644 --- a/NyaFs/ImageFormat/Helper/LogHelper.cs +++ b/NyaFs/ImageFormat/Helper/LogHelper.cs @@ -26,8 +26,9 @@ public static void RamfsInfo(Elements.Fs.LinuxFilesystem Fs) Log.Write(1, $" Compression: {FitHelper.GetCompression(Fs.Info.Compression)}"); Log.Write(1, $" Type: {FitHelper.GetType(Fs.Info.Type)}"); Log.Write(1, $" Filesystem: {FsType}"); - + Log.Write(1, $" Content size: {Fs.GetContentSize()}"); } + public static void DevtreeInfo(Elements.Dtb.DeviceTree Dtb) { Log.Ok(1, "Device tree:"); diff --git a/NyaFs/Processor/Scripting/Commands/Store.cs b/NyaFs/Processor/Scripting/Commands/Store.cs index 71baa0e..e451f0f 100644 --- a/NyaFs/Processor/Scripting/Commands/Store.cs +++ b/NyaFs/Processor/Scripting/Commands/Store.cs @@ -176,13 +176,13 @@ private ScriptStepResult StoreDtb(ImageProcessor Processor) } - private ImageFormat.Elements.Fs.Writer.Writer GetFsWriter() + private ImageFormat.Elements.Fs.Writer.Writer GetFsWriter(ImageFormat.Elements.Fs.LinuxFilesystem Fs) { switch (Format) { case "legacy": return new ImageFormat.Elements.Fs.Writer.LegacyWriter(Path); case "cpio": return new ImageFormat.Elements.Fs.Writer.CpioFsWriter(Path); - case "ext2": return new ImageFormat.Elements.Fs.Writer.Ext2FsWriter(Path); + case "ext2": return new ImageFormat.Elements.Fs.Writer.Ext2FsWriter(ImageFormat.Elements.Fs.Writer.Writer.DetectFixDiskSize(Fs, 0x800000), Path); case "lz4": case "lzma": case "gz": @@ -202,7 +202,7 @@ private ScriptStepResult StoreFs(ImageProcessor Processor) if((Fs == null) || (Fs.Loaded == false)) return new ScriptStepResult(ScriptStepStatus.Error, $"Filesystem is not loaded!"); - var Writer = GetFsWriter(); + var Writer = GetFsWriter(Fs); if(Writer != null) { if (Writer.CheckFilesystem(Fs))