Add directory list output

This commit is contained in:
Stefan Müller 2025-04-15 23:44:27 +02:00
parent afcfc0d8c7
commit 3990ca72d9
2 changed files with 108 additions and 5 deletions

View File

@ -0,0 +1,59 @@
namespace ResticLogParser.src
{
internal class DirectoryNode : IComparable<DirectoryNode>
{
private List<DirectoryNode> _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}";
}
}
}

View File

@ -15,6 +15,8 @@ namespace ResticLogParser.src
private string _logFileName; private string _logFileName;
private readonly List<FileLogEntry> _files = new(); private readonly List<FileLogEntry> _files = new();
private readonly List<DirectoryNode> _directories = new();
private readonly DirectoryNode _root = new(null, "/", "/");
public LogParser(string logFileName) public LogParser(string logFileName)
{ {
@ -30,8 +32,8 @@ namespace ResticLogParser.src
{ {
Console.WriteLine($"Running restic log parser for '{_logFileName}'..."); Console.WriteLine($"Running restic log parser for '{_logFileName}'...");
ParseLogFile(); ParseLogFile();
_files.Sort();
WriteFileInformation(); WriteFileInformation();
WriteDirectoryInformation();
} }
private void ParseLogFile() private void ParseLogFile()
@ -58,7 +60,7 @@ namespace ResticLogParser.src
if (match.Success) if (match.Success)
{ {
var size = FileSize.GetBytesFromString(match.Groups[2].Value); 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; return true;
} }
@ -67,17 +69,40 @@ namespace ResticLogParser.src
{ {
var addedSize = FileSize.GetBytesFromString(match.Groups[2].Value); var addedSize = FileSize.GetBytesFromString(match.Groups[2].Value);
var storedSize = FileSize.GetBytesFromString(match.Groups[3].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 true;
} }
return false; 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() private void WriteFileInformation()
{ {
string outputFileName = Path.Combine(Path.GetDirectoryName(_logFileName)!, "restic-added-data.txt"); string outputFileName = Path.Combine(Path.GetDirectoryName(_logFileName)!, "restic-added-data-files.txt");
Console.WriteLine($"Writing output to '{outputFileName}'..."); Console.WriteLine($"Writing added files to '{outputFileName}'...");
_files.Sort();
using (StreamWriter outputFile = new StreamWriter(outputFileName)) 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());
}
}
}
}
} }
} }