File: dpkg-repack

package info (click to toggle)
dpkg-repack 1.25
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 72 kB
  • ctags: 16
  • sloc: perl: 209; makefile: 31
file content (310 lines) | stat: -rwxr-xr-x 8,478 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/perl -w
#
# This program puts humpty-dumpty back together again.
#
# dpkg-repack is Copyright (c) 1996-2006 by Joey Hess <joeyh@debian.org>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program;  if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave., Cambridge, MA 02139, USA.

use strict;
use File::stat;
use vars qw($error_flag $dirty_flag $build_dir $arch $rootdir $packagename
	    $dpkg_lib $generate);

sub Syntax {
	print STDERR <<eof;
Usage: dpkg-repack [options] packagename [packagename ..]
	--root=dir	Take package from filesystem rooted on <dir>.
	--arch=arch	Force the parch to be built for architecture <arch>.
	--generate	Generate build directory but do not build deb.
	packagename	The name of the package to attempt to repack.
eof
}

sub Info {
	print "dpkg-repack: @_\n";
}

sub Warn {
        print STDERR "dpkg-repack: @_\n";
}

sub Error {
	Warn @_;
	$error_flag=1;
}

sub Die {
        Error('Fatal Error:',@_);
	CleanUp();
        exit 1;
}

# Run a system command, and print an error message if it fails.
sub SafeSystem {
	my $errormessage=pop @_;

	my $ret=system @_;
	if (int($ret/256) > 0) {
		$errormessage="Error running: ".join(' ', @_) if !$errormessage;
		Error($errormessage);
	}
}

# Make the passed directory, print an error message if it fails.
sub SafeMkdir {
	my ($dir,$perms)=@_;
	
	mkdir $dir,$perms || Error("Unable to make directory, \"$dir\": $!");
	# mkdir doesn't do sticky bits and suidness.
	chmod $perms, $dir || Error("Unable to change permissions on \"$dir\": $!");
}

# This removes the temporary directory where we built the package.
sub CleanUp {
	if ($dirty_flag) {
		SafeSystem("rm","-rf",$build_dir,
			"Unable to remove $build_dir ($!). Please remove it by hand.");
	}
}

# This makes the directories we will rebuild the package in.
sub Make_Dirs {
	$dirty_flag=1;
	
	SafeMkdir $build_dir,0755;
	SafeMkdir "$build_dir/DEBIAN",0755;
}

# Get package control file via dpkg -s.
sub Extract_Control {
	my @control=`dpkg --root=$rootdir/ -s $packagename |grep -v "^Status:"`;
	chomp foreach @control;
	
	# Add something to the Description to mention dpkg-repack.
	my $indesc=0;
	my $x;
	for ($x=0; $x < @control; $x++) {
		if ($control[$x] =~/^(\S+):/) {
			last if $indesc; # end of description
			$indesc=1 if lc $1 eq "description";
		}
	}
	if ($indesc) {
		my $date=`822-date`;
		chomp $date;
		$control[$x-1]=$control[$x-1]." .\n"." (Repackaged on $date by dpkg-repack.)";
	}
	
	# Add an Architecture: field
	if (!$arch) {
		$arch=`dpkg --print-architecture`;
		chomp $arch;
	}
	push @control, "Architecture: $arch\n";

	return join("\n", @control);
}
		
# Install the control file. Pass it the text of the file.
sub Install_Control {
	my $control=shift;
	
	open (CONTROL,">$build_dir/DEBIAN/control")
		|| Die "Can't write to $build_dir/DEBIAN/control";
	print CONTROL $control;
	close CONTROL;
	SafeSystem "chown","0:0","$build_dir/DEBIAN/control","";
}

# Install all the files in the DEBIAN directory. (Except control file and
# file list file.)
sub Install_DEBIAN {
	foreach my $fn (glob("$dpkg_lib/info/$packagename.*")) {
		my ($basename)=$fn=~m/^.*\.(.*?)$/;
		if ($basename eq 'conffiles') {
			# If the conffiles file lists conffiles that are
			# not present on the file system, and so were not
			# copied to the build tree, dpkg-deb would cmplain
			# and abort the build. So check the list of
			# conffiles.
			open (CONFFILES, $fn) || Die "$fn: $!";
			open (OUT, ">$build_dir/DEBIAN/$basename") || Die "write conffiles: $!";
			while (<CONFFILES>) {
				chomp;
				if (-e "$build_dir/$_" || -l "$build_dir/$_") {
					print OUT "$_\n";
				}
				else {
					Warn "Skipping missing conffile: $_";
				}
			}
			close OUT;
			close CONFFILES;
			SafeSystem "chown","0:0","$build_dir/DEBIAN/$basename", "";
		}
		elsif ($basename ne 'list') {
			SafeSystem "cp","-p",$fn,"$build_dir/DEBIAN/$basename","";
		}
	}
}

# This looks at the list of files in this package, and places them
# all on the directory tree.
sub Install_Files {
	my $control=shift;

	# There are two types of conffiles. Obsolete conffiles should be
	# skipped, while other conffiles should be included if present.
	my @conffiles;
	my @obsolete_conffiles;
	my $in_conffiles=0;
	foreach my $line (split /\n/,$control) {
		if ($line=~/^Conffiles:/) {
			$in_conffiles=1;
		}
		elsif ($in_conffiles && $line=~/^ (.*)\s+([^\s]+)\s+obsolete$/) {
			push @obsolete_conffiles, $1;
		}
		elsif ($in_conffiles && $line=~/^ (.*)\s+([^\s]+)$/) {
			push @conffiles, $1;
		}
		else {
			$in_conffiles=0;
		}
	}
	
	# I need a list of all the files, for later lookups
	# when I test to see where symlinks point to.
	# Note that because I parse the output of the command (for
	# diversions, below) it's important to make sure it runs with English
	# language output.
	my $lc_all=$ENV{LC_ALL};
	$ENV{LC_ALL}='C';
	my @filelist=split /\n/,`dpkg --root=$rootdir/ -L $packagename`;
	$ENV{LC_ALL}=$lc_all if defined $lc_all; # important to reset it.

	# Set up a hash for easy lookups.
	my %filelist = map { $_ => 1 } @filelist;

	my $fn;
	for (my $x=0;$x<=$#filelist;$x++) {
		my $origfn=$filelist[$x];

		# dpkg -L spits out extra lines to report diversions.
		# we have to parse those (ugly..), to find out where the
		# file was diverted to, and use the diverted file.
		if (defined $filelist[$x+1] &&
		    ($filelist[$x+1]=~m/locally diverted to: (.*)/ ||
		     $filelist[$x+1]=~m/diverted by .*? to: (.*)/)) {
			$fn="$rootdir/$1";
			$x++; # skip over that line.
		}
		elsif ($origfn=~m/package diverts others to: (.*)/) {
			# not a file at all, skip over it
			next;
		}
		else {
			$fn=$rootdir.$origfn;
		}

		if (grep { $_ eq $fn } @obsolete_conffiles) {
			Warn "Skipping obsolete conffile $fn\n";
			next;
		}

		if (!-e $fn && !-l $fn) {
			Error "File not found: $fn" unless grep { $_ eq $fn } @conffiles;
		}
		elsif ((-d $fn and !-l $fn) or
		       (-d $fn and -l $fn and !$filelist{readlink($fn)}
		        and ($x+1 <= $#filelist and $filelist[$x+1]=~m/^\Q$origfn\E\//))) {
			# See the changelog for version 0.17 for an
			# explanation of what I'm doing here with
			# directory symlinks. I rely on the order of the
			# filelist listing parent directories first, and 
			# then their contents.
			# There has to be a better way to do this!
			my $st=stat($fn);
			SafeMkdir "$build_dir/$origfn",$st->mode;
			chown($st->uid, $st->gid, "$build_dir/$origfn");
		}
		elsif (-p $fn) {
			# Copy a named pipe with cp -a.
			SafeSystem "cp","-a",$fn,"$build_dir/$origfn","";
		}
		else {
			SafeSystem "cp","-pd",$fn,"$build_dir/$origfn","";
		}
	}
}

# Parse parameters.
use Getopt::Long;
$rootdir='';
my $ret=&GetOptions(
	"root|r=s", \$rootdir,
	"arch|a=s", \$arch,
	"generate|g" , \$generate,
);

if (!@ARGV || !$ret) {
	Syntax();
	exit 1;
}	
$dpkg_lib=$rootdir.'/var/lib/dpkg';
$build_dir="./dpkg-repack-$$";

# Some sanity checks.
if ($> ne 0) { Die "This program should be run as root (or you could use fakeroot -u). Aborting." }
if (exists $ENV{FAKED_MODE} && $ENV{FAKED_MODE} ne 'unknown-is-real') {
	Warn "fakeroot run without its -u flag may corrupt some file permissions.";
}

foreach $packagename (@ARGV) {
	if (! -f "$dpkg_lib/info/$packagename.list") {
		Error("Package $packagename not installed");
		next;
	}
	
	my $control=Extract_Control();
	if (!$control) { Die "Unable to locate $packagename in the package list." }

	# If the umask is set wrong, the directories will end up with the wrong
	# perms. (Is this still needed?)
	umask 022;

	# Generate the directory tree.
	Make_Dirs();
	Install_Files($control);
	Install_DEBIAN();
	Install_Control($control);

	# Stop here?
	if ($generate) {
		Info "dpkg-repack: created $build_dir for $packagename";
		next;
	}
	
	# Let dpkg do its magic.
	SafeSystem("dpkg","--build",$build_dir,".","");

	# Finish up.
	CleanUp();
	if ($error_flag) {
	        Error("Problems were encountered in processing.");
	        Error("The package may be broken.");
		$error_flag=0;
	}
}