about summary refs log tree commit diff
path: root/scripts/optimise-store.pl
blob: 359140073b5b48abd123b83aeac19e549cdfef7a (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
#! /usr/bin/perl -w

use strict;

{ my $ofh = select STDOUT;
  $| = 1;
  select $ofh;
}

#my @paths = ("/nix/store/caef3a49150506d233f474322a824e50-glibc-2.3.3", "/nix/store/a8a9d585d1ad4b1bc911be7743b3b996-glibc-2.3.3");
my @paths = ("/nix/store");

my $tmpfile = "/tmp/nix-optimise-hash-list";
#my $tmpfile = "/data/nix-optimise-hash-list";

system("find @paths -type f -print0 | xargs -0 md5sum -- > $tmpfile") == 0
    or die "cannot hash store files";

system("sort $tmpfile > $tmpfile.sorted") == 0
    or die "cannot sort list";

open LIST, "<$tmpfile.sorted" or die;

my $prevFile;
my $prevHash;

my $totalSpace = 0;
my $savedSpace = 0;

my $files = 0;

while (<LIST>) {
#    print "D";
    /^([0-9a-f]*)\s+(.*)$/ or die;
    my $curFile = $2;
    my $curHash = $1;

#    print "A";
    my $fileSize = (stat $curFile)[7];
#    print "B";
#    my $fileSize = 1;
    $totalSpace += $fileSize;

    if (defined $prevHash && $curHash eq $prevHash) {
        
#        print "$curFile = $prevFile\n";

        $savedSpace += $fileSize;
        
    } else {
        $prevFile = $curFile;
        $prevHash = $curHash;
    }

    print "." if ($files++ % 100 == 0);
    #print ".";

#    print "C";
}

print "\n";

print "total space = $totalSpace\n";
print "saved space = $savedSpace\n";
my $savings = ($savedSpace / $totalSpace) * 100.0;
print "savings = $savings %\n";


close LIST;