]> git.donarmstrong.com Git - debhelper.git/blob - dh_link
r591: * dh_python typo fix Closes: #197679
[debhelper.git] / dh_link
1 #!/usr/bin/perl -w
2
3 =head1 NAME
4
5 dh_link - create symlinks in package build directories
6
7 =cut
8
9 use strict;
10 use File::Find;
11 use Debian::Debhelper::Dh_Lib;
12
13 =head1 SYNOPSIS
14
15 B<dh_link> [S<I<debhelper options>>] [B<-A>] [S<I<source destination ...>>]
16
17 =head1 DESCRIPTION
18
19 dh_link is a debhelper program that creates symlinks in package build
20 directories.
21
22 dh_link accepts a list of pairs of source and destination files. The source
23 files are the already existing files that will be symlinked from. The
24 destination files are the symlinks that will be created. There B<must> be
25 an equal number of source and destination files specified.
26
27 The list can be specified in two ways. A file named debian/package.links
28 can list pairs of files. If you use this file, you should put each pair
29 of files on its own line, and separate the files within the pair with
30 whitespace. Also, pairs of files can be specified as parameters - these
31 pairs will only be created in the package build directory of the first
32 package dh_link is told to act on. By default, this is the first binary
33 package in debian/control, but if you use -p, -i, or -a flags, it will be
34 the first package specified by those flags.
35
36 Be sure you B<do> specify the full filename to both the source and
37 destination files (unlike you would do if you were using something like
38 L<ln(1)>).
39
40 dh_link will generate symlinks that comply with debian policy - absolute
41 when policy says they should be absolute, and relative links with as short
42 a path as possible. It will also create any subdirectories it needs to to put
43 the symlinks in.
44
45 dh_link also scans the package build tree for existing symlinks which do not
46 conform to debian policy, and corrects them (v4 only).
47
48 =head1 OPTIONS
49
50 =over 4
51
52 =item B<-A>, B<--all>
53
54 Create any links specified by command line parameters in ALL packages
55 acted on, not just the first.
56
57 =item I<source destination ...>
58
59 Create a file named "destination" as a link to a file named "source". Do
60 this in the package build directory of the first package acted on.
61 (Or in all packages if -A is specified.)
62
63 =back
64
65 =head1 EXAMPLES
66
67  dh_link usr/share/man/man1/foo.1 usr/share/man/man1/bar.1
68
69 Make bar.1 be a symlink to foo.1
70
71  dh_link var/lib/foo usr/lib/foo \
72    usr/X11R6/man/man1/foo.1x usr/share/man/man1/bar.1
73
74 Make /usr/lib/foo/ be a link to /var/lib/foo/, and bar.1 be a symlink to
75 the X man page foo.1x
76
77 =cut
78
79 init();
80
81 foreach my $package (@{$dh{DOPACKAGES}}) {
82         my $tmp=tmpdir($package);
83         my $file=pkgfile($package,"links");
84
85         my @links;
86         if ($file) {
87                 @links=filearray($file);
88         }
89
90         # Make sure it has pairs of symlinks and destinations. If it
91         # doesn't, $#links will be _odd_ (not even, -- it's zero-based).
92         if (int($#links/2) eq $#links/2) {
93                 error("$file lists a link without a destination.");
94         }
95
96         if (($package eq $dh{FIRSTPACKAGE} || $dh{PARAMS_ALL}) && @ARGV) {
97                 push @links, @ARGV;
98         }
99
100         # Same test as above, including arguments this time.
101         if (int($#links/2) eq $#links/2) {
102                 error("parameters list a link without a destination.");
103         }
104
105         # v4 only and only if there is a temp dir already
106         if (! compat(3) && -e $tmp) {
107                 # Scan for existing links and add them to @links, so they
108                 # are recreated policy conformant.
109                 find(
110                         sub {
111                                 return unless -l;
112                                 my $dir=$File::Find::dir;
113                                 $dir=~s/^\Q$tmp\E//;
114                                 my $target = readlink($_);
115                                 if ($target=~/^\//) {
116                                         push @links, $target;
117                                 }
118                                 else {
119                                         push @links, "$dir/$target";
120                                 }
121                                 push @links, "$dir/$_";
122                                 doit("rm","-f",$_);
123                                 
124                         },
125                         $tmp);
126         }
127         
128         while (@links) {
129                 my $dest=pop @links;
130                 my $src=pop @links;
131
132                 # Relavatize src and dest.
133                 $src=~s:^/::;
134                 $dest=~s:^/::;
135
136                 # Make sure the directory the link will be in exists.
137                 my $basedir=dirname("$tmp/$dest");
138                 if (! -e $basedir) {
139                         doit("install","-d",$basedir);
140                 }
141                 
142                 # Policy says that if the link is all within one toplevel
143                 # directory, it should be relative. If it's between
144                 # top level directories, leave it absolute.
145                 my @src_dirs=split(m:/+:,$src);
146                 my @dest_dirs=split(m:/+:,$dest);
147                 if ($src_dirs[0] eq $dest_dirs[0]) {
148                         # Figure out how much of a path $src and $dest
149                         # share in common.
150                         my $x;
151                         for ($x=0; $x<$#src_dirs && $src_dirs[$x] eq $dest_dirs[$x]; $x++) {}
152                         # Build up the new src.
153                         $src="";
154                         for (1..$#dest_dirs - $x) {
155                                 $src.="../";
156                         }
157                         for ($x .. $#src_dirs) {
158                                 $src.=$src_dirs[$_]."/";
159                         }
160                         $src=~s:/$::;
161                 }
162                 else {
163                         # Make sure it's properly absolute.
164                         $src="/$src";
165                 }
166                 
167                 doit("ln","-sf",$src,"$tmp/$dest");
168         }
169 }
170
171 =head1 SEE ALSO
172
173 L<debhelper(7)>
174
175 This program is a part of debhelper.
176
177 =head1 AUTHOR
178
179 Joey Hess <joeyh@debian.org>
180
181 =cut