Added some code to peer into a data structure in Maemian/Schedule.pm. Also added the
[maemian] / nokia-lintian / collection / changelog-file
1 #!/usr/bin/perl -w
2 # changelog-file -- lintian collector script
3
4 # Copyright (C) 1998 Richard Braakman
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, you can find it on the World Wide
18 # Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
19 # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
20 # MA 02110-1301, USA.
21
22 use strict;
23
24 ($#ARGV == 1) or fail("syntax: changelog-file <pkg> <type>");
25 my $pkg = shift;
26 my $type = shift;
27
28 -f "fields/package" or fail("changelog-file invoked in wrong directory");
29
30 unlink("changelog");
31
32 # Pick the first of these files that exists.
33 my @changelogs = ("unpacked/usr/share/doc/$pkg/changelog.Debian.gz",
34                "unpacked/usr/share/doc/$pkg/changelog.Debian",
35                "unpacked/usr/share/doc/$pkg/changelog.debian.gz",
36                "unpacked/usr/share/doc/$pkg/changelog.debian",
37                "unpacked/usr/share/doc/$pkg/changelog.gz",
38                "unpacked/usr/share/doc/$pkg/changelog",
39                "unpacked/usr/doc/$pkg/changelog.Debian.gz",
40                "unpacked/usr/doc/$pkg/changelog.Debian",
41                "unpacked/usr/doc/$pkg/changelog.debian.gz",
42                "unpacked/usr/doc/$pkg/changelog.debian",
43                "unpacked/usr/doc/$pkg/changelog.gz",
44                "unpacked/usr/doc/$pkg/changelog");
45
46 my $chl;
47
48 for (@changelogs) {
49     if (-l $_ || -f $_) {
50         $chl = $_;
51         last;
52     }
53 }
54
55 # If the changelog file we found was a symlink, we have to be careful.  It
56 # could be a symlink to some file outside of the laboratory and we don't want
57 # to end up reading that file by mistake.  Relative links within the same
58 # directory or to a subdirectory we accept; anything else is replaced by an
59 # intentinally broken symlink so that checks can do the right thing.
60 if (defined ($chl) && -l $chl) {
61     my $link = readlink $chl or fail("cannot readlink $chl: $!");
62     if ($link =~ /\.\./ || ($link =~ m%/% && $link !~ m%^[^/]+(/+[^/]+)*\z%)) {
63         symlink('file-is-in-another-package', 'changelog')
64             or fail("cannot create changelog symlink: $!");
65         undef $chl;
66     } elsif (! -f $chl) {
67         undef $chl;
68     }
69 }
70
71 # If the changelog was a broken symlink, it will be undefined and we'll now
72 # treat it the same as if we didn't find a changelog and do nothing.  If it
73 # was a symlink, copy the file, since otherwise the relative symlinks are
74 # going to break things.
75 if (not defined $chl) {
76     # no changelog found
77 } elsif ($chl =~ /\.gz$/) {
78     use lib "$ENV{'LINTIAN_ROOT'}/lib";
79     use Pipeline;
80
81     pipeline((sub { exec 'gzip', '-dc', $chl }), "changelog") == 0
82         or fail("error in gzip");
83 } elsif (-f $chl && -l $chl) {
84     local $_;
85     open (CHL, '<', $chl) or fail("cannot open $chl: $!");
86     open (COPY, '>', 'changelog') or fail("cannot create changelog: $!");
87     print COPY while <CHL>;
88     close CHL;
89     close COPY;
90 } else {
91     link($chl, "changelog")
92         or fail("cannot link $chl to changelog: $!");
93 }
94
95 # Extract NEWS.Debian files as well, with similar precautious.  Ignore any
96 # symlinks to other packages here; in that case, we just won't check the file.
97 my $news = "unpacked/usr/share/doc/$pkg/NEWS.Debian.gz";
98 if (-f $news) {
99     if (-l $news) {
100         my $link = readlink $news or fail("cannot readlink $chl: $!");
101         if ($link =~ /\.\./ || ($link =~ m%/% && $link !~ m%^[^/]+(/+[^/]+)*\z%)) {
102             undef $news;
103         } elsif (! -f $news) {
104             undef $news;
105         }
106     }
107     if ($news) {
108         use lib "$ENV{'LINTIAN_ROOT'}/lib";
109         use Pipeline;
110
111         pipeline((sub { exec 'gzip', '-dc', $news }), "NEWS.Debian") == 0
112             or fail("error in gzip");
113     }
114 }
115
116 exit 0;
117
118 # -----------------------------------
119
120 sub fail {
121     if ($_[0]) {
122         print STDERR "internal error: $_[0]\n";
123     } elsif ($!) {
124         print STDERR "internal error: $!\n";
125     } else {
126         print STDERR "internal error.\n";
127     }
128     exit 1;
129 }