From 3990ca72d9ab77bd4595d377a32ef0acfbdb032f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Tue, 15 Apr 2025 23:44:27 +0200 Subject: [PATCH] Add directory list output --- ResticLogParser/src/DirectoryNode.cs | 59 ++++++++++++++++++++++++++++ ResticLogParser/src/LogParser.cs | 54 ++++++++++++++++++++++--- 2 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 ResticLogParser/src/DirectoryNode.cs diff --git a/ResticLogParser/src/DirectoryNode.cs b/ResticLogParser/src/DirectoryNode.cs new file mode 100644 index 0000000..e102db0 --- /dev/null +++ b/ResticLogParser/src/DirectoryNode.cs @@ -0,0 +1,59 @@ +namespace ResticLogParser.src +{ + internal class DirectoryNode : IComparable + { + private List _children = new(); + + public DirectoryNode(DirectoryNode? parent, string name, string fullName) + { + Parent = parent; + Name = name; + FullName = fullName; + AddedSize = 0; + FileCount = 0; + } + + public DirectoryNode? Parent { get; private set; } + + public string Name { get; private set; } + + public string FullName { get; private set; } + + public long AddedSize { get; set; } + + public int FileCount { get; set; } + + public DirectoryNode? FindChild(string name) + { + return _children.Find(x => x.Name == name); + } + + public DirectoryNode AddChild(string name, string fullName) + { + var node = new DirectoryNode(this, name, fullName); + _children.Add(node); + return node; + } + + public int CompareTo(DirectoryNode? other) + { + if (other == null) + { + return 1; + } + + int compare = AddedSize.CompareTo(other.AddedSize); + if (compare != 0) + { + return compare; + } + + return FileCount.CompareTo(other.FileCount); + } + + public override string ToString() + { + return $"Added {AddedSize} bytes, {FileCount} files, {FullName}"; + } + } +} diff --git a/ResticLogParser/src/LogParser.cs b/ResticLogParser/src/LogParser.cs index e481e57..f21c869 100644 --- a/ResticLogParser/src/LogParser.cs +++ b/ResticLogParser/src/LogParser.cs @@ -15,6 +15,8 @@ namespace ResticLogParser.src private string _logFileName; private readonly List _files = new(); + private readonly List _directories = new(); + private readonly DirectoryNode _root = new(null, "/", "/"); public LogParser(string logFileName) { @@ -30,8 +32,8 @@ namespace ResticLogParser.src { Console.WriteLine($"Running restic log parser for '{_logFileName}'..."); ParseLogFile(); - _files.Sort(); WriteFileInformation(); + WriteDirectoryInformation(); } private void ParseLogFile() @@ -58,7 +60,7 @@ namespace ResticLogParser.src if (match.Success) { var size = FileSize.GetBytesFromString(match.Groups[2].Value); - _files.Add(new FileLogEntry(match.Groups[1].Value, FileLogEntryType.New, size)); + AddFileLogEntry(new FileLogEntry(match.Groups[1].Value, FileLogEntryType.New, size)); return true; } @@ -67,17 +69,40 @@ namespace ResticLogParser.src { var addedSize = FileSize.GetBytesFromString(match.Groups[2].Value); var storedSize = FileSize.GetBytesFromString(match.Groups[3].Value); - _files.Add(new FileLogEntry(match.Groups[1].Value, FileLogEntryType.Modified, addedSize, storedSize)); + AddFileLogEntry(new FileLogEntry(match.Groups[1].Value, FileLogEntryType.Modified, addedSize, storedSize)); return true; } return false; } + private void AddFileLogEntry(FileLogEntry fileLogEntry) + { + _files.Add(fileLogEntry); + + string[] split = fileLogEntry.FileName.Split('/', StringSplitOptions.RemoveEmptyEntries); + DirectoryNode parent = _root; + // Skips the last one, since it is the file name. + for (int i = 0; i < split.Length - 1; i++) + { + var child = parent.FindChild(split[i]); + if (child == null) + { + child = parent.AddChild(split[i], $"{parent.FullName}{split[i]}/"); + _directories.Add(child); + } + child.AddedSize += fileLogEntry.AddedSize; + parent = child; + } + parent.FileCount++; + } + private void WriteFileInformation() { - string outputFileName = Path.Combine(Path.GetDirectoryName(_logFileName)!, "restic-added-data.txt"); - Console.WriteLine($"Writing output to '{outputFileName}'..."); + string outputFileName = Path.Combine(Path.GetDirectoryName(_logFileName)!, "restic-added-data-files.txt"); + Console.WriteLine($"Writing added files to '{outputFileName}'..."); + + _files.Sort(); using (StreamWriter outputFile = new StreamWriter(outputFileName)) { @@ -87,5 +112,24 @@ namespace ResticLogParser.src } } } + + private void WriteDirectoryInformation() + { + string outputFileName = Path.Combine(Path.GetDirectoryName(_logFileName)!, "restic-added-data-dirs.txt"); + Console.WriteLine($"Writing directory infos to '{outputFileName}'..."); + + _directories.Sort(); + + using (StreamWriter outputFile = new StreamWriter(outputFileName)) + { + for (int i = _directories.Count - 1; i >= 0; i--) + { + if (_directories[i].FileCount > 0) + { + outputFile.WriteLine(_directories[i].ToString()); + } + } + } + } } }