2006-11-28 17:46:12 +01:00
|
|
|
#! @perl@ -w
|
|
|
|
|
|
|
|
use strict;
|
2010-04-14 21:26:31 +02:00
|
|
|
use Cwd 'abs_path';
|
2006-11-28 17:46:12 +01:00
|
|
|
use IO::Handle;
|
|
|
|
use File::Path;
|
|
|
|
use File::Basename;
|
2014-11-23 20:42:14 +01:00
|
|
|
use File::Compare;
|
2015-08-25 00:37:54 +02:00
|
|
|
use JSON::PP;
|
2006-11-28 17:46:12 +01:00
|
|
|
|
|
|
|
STDOUT->autoflush(1);
|
|
|
|
|
|
|
|
my $out = $ENV{"out"};
|
|
|
|
|
|
|
|
my @pathsToLink = split ' ', $ENV{"pathsToLink"};
|
|
|
|
|
|
|
|
sub isInPathsToLink {
|
|
|
|
my $path = shift;
|
|
|
|
$path = "/" if $path eq "";
|
|
|
|
foreach my $elem (@pathsToLink) {
|
2010-08-09 18:08:02 +02:00
|
|
|
return 1 if
|
2015-08-25 00:37:54 +02:00
|
|
|
$elem eq "/" ||
|
2010-08-09 18:08:02 +02:00
|
|
|
(substr($path, 0, length($elem)) eq $elem
|
|
|
|
&& (($path eq $elem) || (substr($path, length($elem), 1) eq "/")));
|
2006-11-28 17:46:12 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-08 14:52:17 +02:00
|
|
|
# For each activated package, determine what symlinks to create.
|
2006-11-28 17:46:12 +01:00
|
|
|
|
2010-07-08 14:52:17 +02:00
|
|
|
my %symlinks;
|
2015-08-25 00:37:54 +02:00
|
|
|
|
2016-07-09 22:47:15 +02:00
|
|
|
# Add all pathsToLink and all parent directories.
|
|
|
|
#
|
|
|
|
# For "/a/b/c" that will include
|
|
|
|
# [ "", "/a", "/a/b", "/a/b/c" ]
|
|
|
|
#
|
|
|
|
# That ensures the whole directory tree needed by pathsToLink is
|
|
|
|
# created as directories and not symlinks.
|
|
|
|
$symlinks{""} = ["", 0];
|
2015-08-25 11:11:51 +02:00
|
|
|
for my $p (@pathsToLink) {
|
2016-07-09 22:47:15 +02:00
|
|
|
my @parts = split '/', $p;
|
|
|
|
|
|
|
|
my $cur = "";
|
|
|
|
for my $x (@parts) {
|
|
|
|
$cur = $cur . "/$x";
|
|
|
|
$cur = "" if $cur eq "/";
|
|
|
|
$symlinks{$cur} = ["", 0];
|
|
|
|
}
|
2015-08-25 11:11:51 +02:00
|
|
|
}
|
2006-11-28 17:46:12 +01:00
|
|
|
|
2010-07-08 14:52:17 +02:00
|
|
|
sub findFiles;
|
2006-11-28 17:46:12 +01:00
|
|
|
|
2010-07-08 14:52:17 +02:00
|
|
|
sub findFilesInDir {
|
2014-11-23 20:42:14 +01:00
|
|
|
my ($relName, $target, $ignoreCollisions, $checkCollisionContents, $priority) = @_;
|
2006-11-28 17:46:12 +01:00
|
|
|
|
2010-07-08 14:52:17 +02:00
|
|
|
opendir DIR, "$target" or die "cannot open `$target': $!";
|
|
|
|
my @names = readdir DIR or die;
|
|
|
|
closedir DIR;
|
2015-08-25 00:37:54 +02:00
|
|
|
|
2010-07-08 14:52:17 +02:00
|
|
|
foreach my $name (@names) {
|
|
|
|
next if $name eq "." || $name eq "..";
|
2014-11-23 20:42:14 +01:00
|
|
|
findFiles("$relName/$name", "$target/$name", $name, $ignoreCollisions, $checkCollisionContents, $priority);
|
2010-07-08 14:52:17 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-25 00:37:54 +02:00
|
|
|
|
2014-11-23 20:42:14 +01:00
|
|
|
sub checkCollision {
|
|
|
|
my ($path1, $path2) = @_;
|
|
|
|
|
|
|
|
my $stat1 = (stat($path1))[2];
|
|
|
|
my $stat2 = (stat($path2))[2];
|
|
|
|
|
|
|
|
if ($stat1 != $stat2) {
|
|
|
|
warn "different permissions in `$path1' and `$path2': "
|
|
|
|
. sprintf("%04o", $stat1 & 07777) . " <-> "
|
|
|
|
. sprintf("%04o", $stat2 & 07777);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return compare($path1, $path2) == 0;
|
|
|
|
}
|
|
|
|
|
2010-07-08 14:52:17 +02:00
|
|
|
sub findFiles {
|
2014-11-23 20:42:14 +01:00
|
|
|
my ($relName, $target, $baseName, $ignoreCollisions, $checkCollisionContents, $priority) = @_;
|
2010-07-08 14:52:17 +02:00
|
|
|
|
|
|
|
# Urgh, hacky...
|
|
|
|
return if
|
|
|
|
$relName eq "/propagated-build-inputs" ||
|
|
|
|
$relName eq "/nix-support" ||
|
|
|
|
$relName =~ /info\/dir/ ||
|
|
|
|
( $relName =~ /^\/share\/mime\// && !( $relName =~ /^\/share\/mime\/packages/ ) ) ||
|
|
|
|
$baseName eq "perllocal.pod" ||
|
|
|
|
$baseName eq "log";
|
|
|
|
|
2015-08-25 00:37:54 +02:00
|
|
|
my ($oldTarget, $oldPriority) = @{$symlinks{$relName} // [undef, undef]};
|
2010-07-08 14:52:17 +02:00
|
|
|
|
2015-08-25 00:37:54 +02:00
|
|
|
# If target doesn't exist, create it. If it already exists as a
|
|
|
|
# symlink to a file (not a directory) in a lower-priority package,
|
|
|
|
# overwrite it.
|
|
|
|
if (!defined $oldTarget || ($priority < $oldPriority && ($oldTarget ne "" && ! -d $oldTarget))) {
|
|
|
|
$symlinks{$relName} = [$target, $priority];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
# If target already exists as a symlink to a file (not a
|
|
|
|
# directory) in a higher-priority package, skip.
|
|
|
|
if (defined $oldTarget && $priority > $oldPriority && $oldTarget ne "" && ! -d $oldTarget) {
|
2010-07-08 14:52:17 +02:00
|
|
|
return;
|
|
|
|
}
|
2006-11-28 17:46:12 +01:00
|
|
|
|
2010-07-08 14:52:17 +02:00
|
|
|
unless (-d $target && ($oldTarget eq "" || -d $oldTarget)) {
|
2014-11-23 23:11:06 +01:00
|
|
|
if ($ignoreCollisions) {
|
2015-08-25 00:37:54 +02:00
|
|
|
warn "collision between `$target' and `$oldTarget'\n" if $ignoreCollisions == 1;
|
2010-07-08 14:52:17 +02:00
|
|
|
return;
|
2014-11-23 23:11:06 +01:00
|
|
|
} elsif ($checkCollisionContents && checkCollision($oldTarget, $target)) {
|
|
|
|
return;
|
2010-07-08 14:52:17 +02:00
|
|
|
} else {
|
2015-08-25 00:37:54 +02:00
|
|
|
die "collision between `$target' and `$oldTarget'\n";
|
2006-11-28 17:46:12 +01:00
|
|
|
}
|
|
|
|
}
|
2010-07-08 14:52:17 +02:00
|
|
|
|
2014-11-23 20:42:14 +01:00
|
|
|
findFilesInDir($relName, $oldTarget, $ignoreCollisions, $checkCollisionContents, $oldPriority) unless $oldTarget eq "";
|
|
|
|
findFilesInDir($relName, $target, $ignoreCollisions, $checkCollisionContents, $priority);
|
2015-08-25 00:37:54 +02:00
|
|
|
|
|
|
|
$symlinks{$relName} = ["", $priority]; # denotes directory
|
2006-11-28 17:46:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
my %done;
|
|
|
|
my %postponed;
|
|
|
|
|
2015-08-25 00:37:54 +02:00
|
|
|
sub addPkg {
|
2014-11-23 20:42:14 +01:00
|
|
|
my ($pkgDir, $ignoreCollisions, $checkCollisionContents, $priority) = @_;
|
2006-11-28 17:46:12 +01:00
|
|
|
|
|
|
|
return if (defined $done{$pkgDir});
|
|
|
|
$done{$pkgDir} = 1;
|
|
|
|
|
2014-11-23 20:42:14 +01:00
|
|
|
findFiles("", $pkgDir, "", $ignoreCollisions, $checkCollisionContents, $priority);
|
2006-11-28 17:46:12 +01:00
|
|
|
|
|
|
|
my $propagatedFN = "$pkgDir/nix-support/propagated-user-env-packages";
|
|
|
|
if (-e $propagatedFN) {
|
|
|
|
open PROP, "<$propagatedFN" or die;
|
2017-08-01 09:41:06 +02:00
|
|
|
my $propagated = <PROP>;
|
|
|
|
close PROP;
|
|
|
|
my @propagated = split ' ', $propagated;
|
|
|
|
foreach my $p (@propagated) {
|
2006-11-28 17:46:12 +01:00
|
|
|
$postponed{$p} = 1 unless defined $done{$p};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:11:33 +02:00
|
|
|
# Read packages list.
|
|
|
|
my $pkgs;
|
|
|
|
|
|
|
|
if (exists $ENV{"pkgsPath"}) {
|
|
|
|
open FILE, $ENV{"pkgsPath"};
|
|
|
|
$pkgs = <FILE>;
|
|
|
|
close FILE;
|
|
|
|
} else {
|
|
|
|
$pkgs = $ENV{"pkgs"}
|
|
|
|
}
|
2006-11-28 17:46:12 +01:00
|
|
|
|
2015-08-25 00:37:54 +02:00
|
|
|
# Symlink to the packages that have been installed explicitly by the
|
|
|
|
# user.
|
2015-10-15 15:11:33 +02:00
|
|
|
for my $pkg (@{decode_json $pkgs}) {
|
2015-08-25 00:37:54 +02:00
|
|
|
for my $path (@{$pkg->{paths}}) {
|
2014-11-23 20:42:14 +01:00
|
|
|
addPkg($path,
|
|
|
|
$ENV{"ignoreCollisions"} eq "1",
|
|
|
|
$ENV{"checkCollisionContents"} eq "1",
|
|
|
|
$pkg->{priority})
|
|
|
|
if -e $path;
|
2015-08-25 00:37:54 +02:00
|
|
|
}
|
2006-11-28 17:46:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Symlink to the packages that have been "propagated" by packages
|
2015-08-25 00:37:54 +02:00
|
|
|
# installed by the user (i.e., package X declares that it wants Y
|
2006-11-28 17:46:12 +01:00
|
|
|
# installed as well). We do these later because they have a lower
|
|
|
|
# priority in case of collisions.
|
2015-08-25 00:37:54 +02:00
|
|
|
my $priorityCounter = 1000; # don't care about collisions
|
2006-11-28 17:46:12 +01:00
|
|
|
while (scalar(keys %postponed) > 0) {
|
|
|
|
my @pkgDirs = keys %postponed;
|
|
|
|
%postponed = ();
|
|
|
|
foreach my $pkgDir (sort @pkgDirs) {
|
2016-01-27 12:44:56 +01:00
|
|
|
addPkg($pkgDir, 2, $ENV{"checkCollisionContents"} eq "1", $priorityCounter++);
|
2006-11-28 17:46:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-08 14:52:17 +02:00
|
|
|
|
|
|
|
# Create the symlinks.
|
2015-09-17 17:43:18 +02:00
|
|
|
my $extraPrefix = $ENV{"extraPrefix"};
|
2010-07-08 14:52:17 +02:00
|
|
|
my $nrLinks = 0;
|
|
|
|
foreach my $relName (sort keys %symlinks) {
|
2015-08-25 00:37:54 +02:00
|
|
|
my ($target, $priority) = @{$symlinks{$relName}};
|
2015-09-17 17:43:18 +02:00
|
|
|
my $abs = "$out" . "$extraPrefix" . "/$relName";
|
2010-07-08 14:52:17 +02:00
|
|
|
next unless isInPathsToLink $relName;
|
|
|
|
if ($target eq "") {
|
|
|
|
#print "creating directory $relName\n";
|
|
|
|
mkpath $abs or die "cannot create directory `$abs': $!";
|
|
|
|
} else {
|
|
|
|
#print "creating symlink $relName to $target\n";
|
|
|
|
symlink $target, $abs ||
|
|
|
|
die "error creating link `$abs': $!";
|
|
|
|
$nrLinks++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
print STDERR "created $nrLinks symlinks in user environment\n";
|
2006-11-28 17:46:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
my $manifest = $ENV{"manifest"};
|
2010-07-08 14:52:17 +02:00
|
|
|
if ($manifest) {
|
2006-11-28 17:46:12 +01:00
|
|
|
symlink($manifest, "$out/manifest") or die "cannot create manifest";
|
|
|
|
}
|