summary refs log tree commit diff
path: root/Rhea/Program.cs
blob: a50de4d5ae36806e40e38de9cd2f35f0275a580f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System.Diagnostics;
using System.Reflection;
using System.Text;
using ArcaneLibs;
using ArcaneLibs.Extensions;
using Rhea;
using Rhea.Nar;


// new NarWriter().GetHeader().ToArray().HexDump(64);

foreach (var fieldInfo in typeof(NarConsts).GetFields())
{
    // Console.WriteLine(fieldInfo);
    var rawBytes = (byte[])fieldInfo.GetValue(null)!;
    var sizeBytes = rawBytes[..8];
    var dataBytes = rawBytes[8..];
    Console.WriteLine(
        $$"""
          public static readonly byte[] {{fieldInfo.Name}} = new byte[] {
              {{string.Join(", ", sizeBytes.Select(x => x.ToString()))}}, // {{dataBytes.Trim((byte)0).ToArray().Length}} bytes
              {{string.Join(", ", dataBytes.Select(x => x.ToString()))}}, // "{{Encoding.UTF8.GetString(dataBytes)}}"u8 + padding
              {{string.Join(", ", rawBytes.Select(x => x.ToString()))}}
          };
          """
    );
}

int maxVerifySize = 8_192_000;

// return;
// while (true)
// foreach (var srcPath in (string[])[
//              "/etc/nix/nix.conf", 
//              FileUtils.GetBinDir() + "/Rhea.runtimeconfig.json", 
//              "/etc/nix", 
//              ".",
//              "/nix/store/06d2xkrl3h1pbyg3m1whz1mvhafqgsqq-source",
//              "/nix/store/0l93s9x3z2s9p4r58jls0xiyk64hwkxz-source",
//              "/nix/store/2g4sa9xcir93nckzib9iq1ky6ly6azgw-7j1q60m17gf95k3s0gk2qvjqygnp1q36-source"
//          ])
// foreach (var srcPath in (string[])["/nix/store/7j1q60m17gf95k3s0gk2qvjqygnp1q36-source"])
Parallel.ForEach(FileUtils.EnumerateDirectory("/nix/store")
        // .Where(x => x is not DirectoryInfo)
        .Select(x => x.FullName),
    new() { MaxDegreeOfParallelism = 12 },
    srcPath =>
        // foreach (var srcPath in FileUtils.EnumerateDirectory("/nix/store").Select(x=>x.FullName))
    {
        var sw = Stopwatch.StartNew();
        TimeSpan oldTime, newTime;
        var log = "";
        log += ($"\n - {srcPath}:");
        bool tooLarge = false;
        // Console.WriteLine($"Original {srcPath}:");

        var origBuf = new List<byte>();
        var nixProcess = Process.Start(new ProcessStartInfo("nix", ["nar", "dump-path", srcPath])
        {
            RedirectStandardOutput = true
        });
        int b;
        byte[] oldBuf = new byte[16384];
        while ((b = nixProcess.StandardOutput.BaseStream.Read(oldBuf)) != 0)
        {
            if (!tooLarge)
            {
                if (origBuf.Count < maxVerifySize)
                    origBuf.AddRange(oldBuf[..b]);
                else tooLarge = true;
            }
            // Console.WriteLine(b + " @ " + sw.Elapsed + " -> " + Util.BytesToString(origBuf.Count));
            // Thread.Sleep(1);
        }

        // origBuf.ToArray().HexDump(64);
        oldTime = sw.GetElapsedAndRestart();

        // Console.WriteLine($"\nNew {srcPath}:");
        List<byte> tbuf = [];
        // List<byte> buf = [];
        foreach (var chunk in new NarWriter().GetNarStreamContents(srcPath))
        {
            // Console.WriteLine($"ret Chunk({chunk.Length})");
            if (!tooLarge)
            {
                if (tbuf.Count < maxVerifySize)
                    tbuf.AddRange(chunk);
                else tooLarge = true;
            }
            // buf.AddRange(chunk);
            // while (buf.Count > 64)
            // {
            //     byte[] data = buf[..64].ToArray();
            //     buf = buf[64..];
            //     data.HexDump(64);
            // }
        }

        // buf.ToArray().HexDump(64);
        newTime = sw.GetElapsedAndRestart();

        log += ($"\nOld time: {oldTime}, new time: {newTime}");
        if (!tooLarge)
        {
            log += ("\nIs equal: " + origBuf.SequenceEqual(tbuf));
            if (!origBuf.SequenceEqual(tbuf))
            {
                // File.WriteAllBytes("a", origBuf.ToArray().AsHexString().AsBytes().ToArray());
                // File.WriteAllBytes("b", tbuf.ToArray().AsHexString().AsBytes().ToArray());
                var origStdout = Console.Out;
                using (var fs = File.OpenWrite("a"))
                {
                    using var fsw = new StreamWriter(fs);
                    Console.SetOut(fsw);
                    origBuf.HexDump(width: 16, colorize: false);
                    Console.SetOut(origStdout);
                }
            
                using (var fs = File.OpenWrite("b"))
                {
                    using var fsw = new StreamWriter(fs);
                    Console.SetOut(fsw);
                    tbuf.HexDump(width: 16, colorize: false);
                    Console.SetOut(origStdout);
                }
            
                Console.WriteLine(log);
                Environment.Exit(0);
            }
        }
        else log += "\nToo large to check consistency";

        Console.WriteLine(log);
    }
);

return;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddSingleton<RheaConfiguration>();
builder.Services.AddSingleton<StoreMetaService>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.MapOpenApi();
}

app.UseAuthorization();

app.MapControllers();

// app.Run();