buildEnv: build the whole tree of directories to pathsToLink

This patch fixes #16614 and #16741.

The first issue was caused by the fact that both `/share` and
`/share/fish/vendor_completions.d` end in the `pathsToLink`. The
`pkgs/build-support/buildenv/builder.pl` creates `/share`, then links
`/share/fish` under `/share` and then tries to create the directory
`/share/fish/vendor_completions.d` and fails because it already exists.

The simplest way to reproduce the issue is to build the next Nix
expression:

```nix
let pkgs = import <nixpkgs> { };
in pkgs.buildEnv {
name = "buildenv-issue";

paths = [
pkgs.fish
pkgs.vim
];

pathsToLink = [
"/share"
"/share/fish/vendor_completions.d"
];
}
```

The second issue is more critical and was caused by the fact findFiles
doesn't recurse deep enough. It stops at first unique directory for the
package (e.g., "/share" or even "/") and later the scripts decides it
shouldn't link it as it doesn't match pathsToLink (e.g., "/share/fish"),
so the result is empty.

The test:
```nix
let pkgs = import <nixpkgs> { };
in pkgs.buildEnv {
name = "buildenv-issue";

paths = [
pkgs.fish
pkgs.vim
];

pathsToLink = [
"/share/fish/functions"
];
}
```

or

```nix
let pkgs = import <nixpkgs> { };
in pkgs.buildEnv {
name = "buildenv-issue";

paths = [
pkgs.vim
];

pathsToLink = [
"/share"
];
}
```

authored by Alexey Shmalko and committed by Nikolay Amiantov 0172558e f56a319e

+16 -2
+16 -2
pkgs/build-support/buildenv/builder.pl
··· 31 31 32 32 my %symlinks; 33 33 34 + # Add all pathsToLink and all parent directories. 35 + # 36 + # For "/a/b/c" that will include 37 + # [ "", "/a", "/a/b", "/a/b/c" ] 38 + # 39 + # That ensures the whole directory tree needed by pathsToLink is 40 + # created as directories and not symlinks. 41 + $symlinks{""} = ["", 0]; 34 42 for my $p (@pathsToLink) { 35 - $p = "" if $p eq "/"; 36 - $symlinks{$p} = ["", 0]; 43 + my @parts = split '/', $p; 44 + 45 + my $cur = ""; 46 + for my $x (@parts) { 47 + $cur = $cur . "/$x"; 48 + $cur = "" if $cur eq "/"; 49 + $symlinks{$cur} = ["", 0]; 50 + } 37 51 } 38 52 39 53 sub findFiles;