]> git.donarmstrong.com Git - debhelper.git/blob - dh_compress
d4ec3cfda591d660286f60f0544fad319779c188
[debhelper.git] / dh_compress
1 #!/usr/bin/perl -w
2 #
3 # Compresses files and makes sure that symlinks pointing to the 
4 # compressed files get fixed.
5
6 use Cwd;
7 BEGIN { push @INC, "debian", "/usr/lib/debhelper" }
8 use Dh_Lib;
9 init();
10
11 foreach $PACKAGE (@{$dh{DOPACKAGES}}) {
12         $TMP=tmpdir($PACKAGE);
13         $compress=pkgfile($PACKAGE,"compress");
14
15         # Run the file name gathering commands from within the directory
16         # structure that will be effected.
17         $olddir=getcwd();
18         verbose_print("cd $TMP");
19         chdir($TMP) || error("Can't cd to $TMP: $!");
20
21         # Figure out what files to compress.
22         @files=();
23         # First of all, deal with any files specified right on the command line.
24         if (($PACKAGE eq $dh{FIRSTPACKAGE} || $dh{PARAMS_ALL}) && @ARGV) {
25                 push @files,#ARGV;
26         }
27         if ($compress) {
28                 # The config file is a sh script that outputs the files to be compressed
29                 # (typically using find).
30                 push @files, split(/\n/,`sh $olddir/$compress 2>/dev/null`);
31         }
32         else {
33                 # By default, fall back to what the policy manual says to compress.
34                 push @files, split(/\n/,`
35                         find usr/info usr/man usr/X11*/man -type f ! -name "*.gz" 2>/dev/null || true;
36                         find usr/doc -type f \\( -size +4k -or -name "changelog*" \\) \\
37                                 ! -name "*.htm*" ! -name "*.gif" ! -name "*.gz" \\
38                                 ! -name "copyright" 2>/dev/null || true
39                 `);
40         }
41
42         # Exclude files from compression.
43         if (@files && defined($dh{EXCLUDE}) && $dh{EXCLUDE}) {
44                 @new=();
45                 foreach (@files) {
46                         $ok=1;
47                         foreach $x (@{$dh{EXCLUDE}}) {
48                                 if (/\Q$x\E/) {
49                                         $ok='';
50                                         last;
51                                 }
52                         }
53                         push @new,$_ if $ok;
54                 }
55                 @files=@new;
56         }
57         
58         # Look for files with hard links. If we are going to compress both,
59         # we can preserve the hard link across the compression and save
60         # space in the end.
61         my @f=();
62         my %hardlinks;
63         foreach (@files) {
64                 ($dev, $inode, undef, $nlink)=stat($_);
65                 if ($nlink > 1) {
66                         if (! $seen{"$inode.$dev"}) {
67                                 $seen{"$inode.$dev"}=$_;
68                                 push @f, $_;
69                         }
70                         else {
71                                 # This is a hardlink.
72                                 $hardlinks{$_}=$seen{"$inode.$dev"};
73                         }
74                 }
75                 else {
76                         push @f, $_;
77                 }
78         }
79
80         if (@f) {
81                 xargs(\@f,"gzip","-9f");
82         }
83         
84         # Now change over any files we can that used to be hard links so
85         # they are again.
86         foreach (keys %hardlinks) {
87                 # Remove old file.
88                 doit("rm","-f","$_");
89                 # Make new hardlink.
90                 doit("ln","$hardlinks{$_}.gz","$_.gz");
91         }
92
93         verbose_print("cd $olddir");
94         chdir($olddir);
95
96         # Fix up symlinks that were pointing to the uncompressed files.
97         open (FIND,"find $TMP -type l |");
98         while (<FIND>) {
99                 chomp;
100                 ($directory)=m:(.*)/:;
101                 $linkval=readlink($_);
102                 if (! -e "$directory/$linkval" && -e "$directory/$linkval.gz") {
103                         doit("rm","-f",$_);
104                         doit("ln","-sf","$linkval.gz","$_.gz");
105                 }
106         }
107 }