about summary refs log tree commit diff
path: root/perl/lib/Nix/Store.pm
blob: 8312a732cdeaa915d8dbc36b69efd8bb23d1d354 (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
package Nix::Store;

use strict;
use warnings;
use Nix::Config;

require Exporter;

our @ISA = qw(Exporter);

our %EXPORT_TAGS = ( 'all' => [ qw( ) ] );

our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );

our @EXPORT = qw(
    isValidPath queryReferences queryPathInfo queryDeriver queryPathHash
    topoSortPaths computeFSClosure followLinksToStorePath exportPaths
    hashPath hashFile hashString
    addToStore makeFixedOutputPath
    derivationFromPath
);

our $VERSION = '0.15';

sub backtick {
    open(RES, "-|", @_) or die;
    local $/;
    my $res = <RES> || "";
    close RES or die;
    return $res;
}

if ($Nix::Config::useBindings) {
    require XSLoader;
    XSLoader::load('Nix::Store', $VERSION);
} else {

    # Provide slow fallbacks of some functions on platforms that don't
    # support the Perl bindings.

    use File::Temp;
    use Fcntl qw/F_SETFD/;

    sub hashFile {
        my ($algo, $base32, $path) = @_;
        my $res = backtick("$Nix::Config::binDir/nix-hash", "--flat", $path, "--type", $algo, $base32 ? "--base32" : ());
        chomp $res;
        return $res;
    }
    
    sub hashPath {
        my ($algo, $base32, $path) = @_;
        my $res = backtick("$Nix::Config::binDir/nix-hash", $path, "--type", $algo, $base32 ? "--base32" : ());
        chomp $res;
        return $res;
    }
    
    sub hashString {
        my ($algo, $base32, $s) = @_;
        my $fh = File::Temp->new();
        print $fh $s;
        my $res = backtick("$Nix::Config::binDir/nix-hash", $fh->filename, "--type", $algo, $base32 ? "--base32" : ());
        chomp $res;
        return $res;
    }
    
    sub addToStore {
        my ($srcPath, $recursive, $algo) = @_;
        die "not implemented" if $recursive || $algo ne "sha256";
        my $res = backtick("$Nix::Config::binDir/nix-store", "--add", $srcPath);
        chomp $res;
        return $res;
    }
    
    sub isValidPath {
        my ($path) = @_;
        my $res = backtick("$Nix::Config::binDir/nix-store", "--check-validity", "--print-invalid", $path);
        chomp $res;
        return $res ne $path;
    }
    
    sub queryPathHash {
        my ($path) = @_;
        my $res = backtick("$Nix::Config::binDir/nix-store", "--query", "--hash", $path);
        chomp $res;
        return $res;
    }
}

1;
__END__