4 my $version = "1.3.0"; # This line modified by Makefile
11 $diff_ignore_default_regexp = '(?:^|/)\.#.*$|(?:^|/).*~$|(?:^|/)\..*\.swp|DEADJOE|\.cvsignore|\.arch-inventory|(?:/(?:CVS|RCS|\.deps|\{arch\}|\.arch-ids|\.svn|_darcs))(?:$|/.*$)';
16 $def_dscformat = "1.0"; # default format for -b
19 use POSIX qw
(:errno_h
:signal_h
);
23 push (@INC, $dpkglibdir);
24 require 'controllib.pl';
26 # Make sure patch doesn't get any funny ideas
27 delete $ENV{'POSIXLY_CORRECT'};
29 my @exit_handlers = ();
31 &$_ foreach ( reverse @exit_handlers );
34 $SIG{'INT'} = \
&exit_handler
;
35 $SIG{'HUP'} = \
&exit_handler
;
36 $SIG{'QUIT'} = \
&exit_handler
;
40 "Debian dpkg-source $version. Copyright (C) 1996
41 Ian Jackson and Klee Dienes. This is free software; see the GNU
42 General Public Licence version 2 or later for copying conditions.
45 Usage: dpkg-source -x <filename>.dsc [<output-directory>]
46 dpkg-source -b <directory> [<orig-directory>|<orig-targz>|\'\']
47 Build options: -c<controlfile> get control info from this file
48 -l<changelogfile> get per-version info from this file
49 -F<changelogformat> force change log format
50 -V<name>=<value> set a substitution variable
51 -T<varlistfile> read variables here, not debian/substvars
52 -D<field>=<value> override or add a .dsc field and value
53 -U<field> remove a field
54 -W Turn certain errors into warnings.
55 -E When -W is enabled, -E disables it.
56 -sa auto select orig source (-sA is default)
57 -i[<regexp>] filter out files to ignore diffs of.
58 Defaults to: '$diff_ignore_default_regexp'
59 -I<filename> filter out files when building tarballs.
60 -sk use packed orig source (unpack & keep)
61 -sp use packed orig source (unpack & remove)
62 -su use unpacked orig source (pack & keep)
63 -sr use unpacked orig source (pack & remove)
64 -ss trust packed & unpacked orig src are same
65 -sn there is no diff, do main tarfile only
66 -sA,-sK,-sP,-sU,-sR like -sa,-sp,-sk,-su,-sr but may overwrite
67 Extract options: -sp (default) leave orig source packed in current dir
68 -sn do not copy original source to current dir
69 -su unpack original source tree too
70 General options: -h print this message
76 return unless $fmt =~ /^(\d+)/; # only check major version
77 return $1 >= $min_dscformat && $1 <= $max_dscformat;
82 grep ($fieldimps {$_} = $i--,
83 qw
(Format Source Version Binary Origin Maintainer Architecture
84 Standards
-Version Build
-Depends Build
-Depends
-Indep Build
-Conflicts
85 Build
-Conflicts
-Indep
));
87 while (@ARGV && $ARGV[0] =~ m/^-/) {
92 &setopmode
('extract');
93 } elsif (m/^-s([akpursnAKPUR])$/) {
99 } elsif (m/^-F([0-9a-z]+)$/) {
101 } elsif (m/^-D([^\=:]+)[=:]/) {
103 } elsif (m/^-U([^\=:]+)$/) {
105 } elsif (m/^-i(.*)$/) {
106 $diff_ignore_regexp = $1 ?
$1 : $diff_ignore_default_regexp;
107 } elsif (m/^-I(.+)$/) {
108 push @tar_ignore, "--exclude=$1";
109 } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:]/) {
114 &usageversion
; exit(0);
122 &usageerr
("unknown option $_");
126 defined($opmode) || &usageerr
("need -x or -b");
128 $SIG{'PIPE'} = 'DEFAULT';
130 if ($opmode eq 'build') {
132 $sourcestyle =~ y/X/A/;
133 $sourcestyle =~ m/[akpursnAKPUR]/ ||
134 &usageerr
("source handling style -s$sourcestyle not allowed with -b");
136 @ARGV || &usageerr
("-b needs a directory");
137 @ARGV<=2 || &usageerr
("-b takes at most a directory and an orig source argument");
139 $dir= "./$dir" unless $dir =~ m
:^/:; $dir =~ s
,/*$,,;
140 stat($dir) || &error
("cannot stat directory $dir: $!");
141 -d
$dir || &error
("directory argument $dir is not a directory");
143 $changelogfile= "$dir/debian/changelog" unless defined($changelogfile);
144 $controlfile= "$dir/debian/control" unless defined($controlfile);
148 $f{"Format"}=$def_dscformat;
154 if (m/^Source$/i) { &setsourcepackage
; }
155 elsif (m/^(Standards-Version|Origin|Maintainer|Uploaders)$/i) { $f{$_}= $v; }
156 elsif (m/^Build-(Depends|Conflicts)(-Indep)?$/i) { $f{$_}= $v; }
157 elsif (s/^X[BC]*S[BC]*-//i) { $f{$_}= $v; }
158 elsif (m/^(Section|Priority|Files|Bugs)$/i || m/^X[BC]+-/i) { }
159 else { &unknown
('general section of control info file'); }
160 } elsif (s/^C(\d+) //) {
161 $i=$1; $p=$fi{"C$i Package"};
162 push(@binarypackages,$p) unless $packageadded{$p}++;
163 if (m/^Architecture$/) {
165 @sourcearch= ('any');
166 } elsif ($v eq 'all') {
167 if (!@sourcearch || $sourcearch[0] eq 'all') {
168 @sourcearch= ('all');
170 @sourcearch= ('any');
173 if (grep($sourcearch[0] eq $_, 'any','all')) {
174 @sourcearch= ('any');
176 for $a (split(/\s+/,$v)) {
177 &error
("architecture $a only allowed on its own".
178 " (list for package $p is `$a')")
179 if grep($a eq $_, 'any','all');
180 push(@sourcearch,$a) unless $archadded{$a}++;
184 $f{'Architecture'}= join(' ',@sourcearch);
185 } elsif (s/^X[BC]*S[BC]*-//i) {
187 } elsif (m/^(Package|Essential|Pre-Depends|Depends|Provides)$/i ||
188 m/^(Recommends|Suggests|Optional|Conflicts|Replaces)$/i ||
189 m/^(Enhances|Description|Section|Priority)$/i ||
192 &unknown
("package's section of control info file");
197 } elsif (m/^Version$/) {
199 } elsif (s/^X[BS]*C[BS]*-//i) {
201 } elsif (m/^(Maintainer|Changes|Urgency|Distribution|Date|Closes)$/i ||
204 &unknown
("parsed version of changelog");
208 &internerr
("value from nowhere, with key >$_< and value >$v<");
212 $f{'Binary'}= join(', ',@binarypackages);
213 for $f (keys %override) { $f{&capit
($f)}= $override{$f}; }
215 for $f (qw(Version)) {
216 defined($f{$f}) || &error
("missing information for critical output field $f");
218 for $f (qw(Maintainer Architecture Standards-Version)) {
219 defined($f{$f}) || &warn("missing information for output field $f");
221 defined($sourcepackage) || &error
("unable to determine source package name !");
222 $f{'Source'}= $sourcepackage;
223 for $f (keys %remove) { delete $f{&capit
($f)}; }
225 $version= $f{'Version'};
226 $version =~ s/^\d+://; $upstreamversion= $version; $upstreamversion =~ s/-[^-]*$//;
227 $basenamerev= $sourcepackage.'_'.$version;
228 $basename= $sourcepackage.'_'.$upstreamversion;
229 $basedirname= $basename;
230 $basedirname =~ s/_/-/;
232 $origdir= "$dir.orig";
233 $origtargz= "$basename.orig.tar.gz";
235 $origarg= shift(@ARGV);
236 if (length($origarg)) {
237 stat($origarg) || &error
("cannot stat orig argument $origarg: $!");
240 $origdir= "./$origdir" unless $origdir =~ m
,^/,; $origdir =~ s
,/*$,,;
241 $sourcestyle =~ y/aA/rR/;
242 $sourcestyle =~ m/[ursURS]/ ||
243 &error
("orig argument is unpacked but source handling style".
244 " -s$sourcestyle calls for packed (.orig.tar.gz)");
246 $origtargz= $origarg;
247 $sourcestyle =~ y/aA/pP/;
248 $sourcestyle =~ m/[kpsKPS]/ ||
249 &error
("orig argument is packed but source handling style".
250 " -s$sourcestyle calls for unpacked (.orig/)");
252 &error
("orig argument $origarg is not a plain file or directory");
255 $sourcestyle =~ y/aA/nn/;
256 $sourcestyle =~ m/n/ ||
257 &error
("orig argument is empty (means no orig, no diff)".
258 " but source handling style -s$sourcestyle wants something");
262 if ($sourcestyle =~ m/[aA]/) {
263 if (stat("$origtargz")) {
264 -f _
|| &error
("packed orig `$origtargz' exists but is not a plain file");
265 $sourcestyle =~ y/aA/pP/;
266 } elsif ($! != ENOENT
) {
267 &syserr
("unable to stat putative packed orig `$origtargz'");
268 } elsif (stat("$origdir")) {
269 -d _
|| &error
("unpacked orig `$origdir' exists but is not a directory");
270 $sourcestyle =~ y/aA/rR/;
271 } elsif ($! != ENOENT
) {
272 &syserr
("unable to stat putative unpacked orig `$origdir'");
274 $sourcestyle =~ y/aA/nn/;
277 $dirbase= $dir; $dirbase =~ s
,/?$,,; $dirbase =~ s,[^/]+$,,; $dirname= $&;
278 $dirname eq $basedirname || &warn("source directory `$dir' is not <sourcepackage>".
279 "-<upstreamversion> `$basedirname'");
281 if ($sourcestyle ne 'n') {
282 $origdirbase= $origdir; $origdirbase =~ s
,/?
$,,;
283 $origdirbase =~ s
,[^/]+$,,; $origdirname= $&;
285 $origdirname eq "$basedirname.orig" ||
286 &warn(".orig directory name $origdirname is not <package>".
287 "-<upstreamversion> (wanted $basedirname.orig)");
288 $tardirbase= $origdirbase; $tardirname= $origdirname;
290 $tarname= $origtargz;
291 $tarname eq "$basename.orig.tar.gz" ||
292 &warn(".orig.tar.gz name $tarname is not <package>_<upstreamversion>".
293 ".orig.tar.gz (wanted $basename.orig.tar.gz)");
295 $tardirbase= $dirbase; $tardirname= $dirname;
296 $tarname= "$basenamerev.tar.gz";
299 if ($sourcestyle =~ m/[nurUR]/) {
301 if (stat($tarname)) {
302 $sourcestyle =~ m/[nUR]/ ||
303 &error
("tarfile `$tarname' already exists, not overwriting,".
304 " giving up; use -sU or -sR to override");
305 } elsif ($! != ENOENT
) {
306 &syserr
("unable to check for existence of `$tarname'");
309 print("$progname: building $sourcepackage in $tarname\n")
310 || &syserr
("write building tar message");
311 &forkgzipwrite
("$tarname.new");
312 defined($c2= fork) || &syserr
("fork for tar");
314 chdir($tardirbase) || &syserr
("chdir to above (orig) source $tardirbase");
315 open(STDOUT
,">&GZIP") || &syserr
("reopen gzip for tar");
316 # FIXME: put `--' argument back when tar is fixed
317 exec('tar',@tar_ignore,'-cf','-',$tardirname) or &syserr
("exec tar");
321 $c2 == waitpid($c2,0) || &syserr
("wait for tar");
322 $?
&& !(WIFSIGNALED
($c2) && WTERMSIG
($c2) == SIGPIPE
) && subprocerr
("tar");
323 rename("$tarname.new",$tarname) ||
324 &syserr
("unable to rename `$tarname.new' (newly created) to `$tarname'");
328 print("$progname: building $sourcepackage using existing $tarname\n")
329 || &syserr
("write using existing tar message");
335 if ($sourcestyle =~ m/[kpKP]/) {
337 if (stat($origdir)) {
338 $sourcestyle =~ m/[KP]/ ||
339 &error
("orig dir `$origdir' already exists, not overwriting,".
340 " giving up; use -sA, -sK or -sP to override");
341 push @exit_handlers, sub { erasedir
($origdir) };
344 } elsif ($! != ENOENT
) {
345 &syserr
("unable to check for existence of orig dir `$origdir'");
348 $expectprefix= $origdir; $expectprefix =~ s
,^\
./,,;
349 # tar checking is disabled, there are too many broken tar archives out there
350 # which we can still handle anyway.
351 # checktarsane($origtargz,$expectprefix);
352 mkdir("$origtargz.tmp-nest",0755) ||
353 &syserr
("unable to create `$origtargz.tmp-nest'");
354 push @exit_handlers, sub { erasedir
("$origtargz.tmp-nest") };
355 extracttar
($origtargz,"$origtargz.tmp-nest",$expectprefix);
356 rename("$origtargz.tmp-nest/$expectprefix",$expectprefix) ||
357 &syserr
("unable to rename `$origtargz.tmp-nest/$expectprefix' to ".
359 rmdir("$origtargz.tmp-nest") ||
360 &syserr
("unable to remove `$origtargz.tmp-nest'");
364 if ($sourcestyle =~ m/[kpursKPUR]/) {
366 print("$progname: building $sourcepackage in $basenamerev.diff.gz\n")
367 || &syserr
("write building diff message");
368 &forkgzipwrite
("$basenamerev.diff.gz");
370 defined($c2= open(FIND
,"-|")) || &syserr
("fork for find");
372 chdir($dir) || &syserr
("chdir to $dir for find");
373 exec('find','.','-print0') or &syserr
("exec find");
378 while (defined($fn= <FIND
>)) {
380 next file
if $fn =~ m/$diff_ignore_regexp/o;
382 lstat("$dir/$fn") || &syserr
("cannot stat file $dir/$fn");
384 $type{$fn}= 'symlink';
385 &checktype
('-l') || next;
386 defined($n= readlink("$dir/$fn")) ||
387 &syserr
("cannot read link $dir/$fn");
388 defined($n2= readlink("$origdir/$fn")) ||
389 &syserr
("cannot read orig link $origdir/$fn");
390 $n eq $n2 || &unrepdiff2
("symlink to $n2","symlink to $n");
392 $type{$fn}= 'plain file';
393 if (!lstat("$origdir/$fn")) {
394 $! == ENOENT
|| &syserr
("cannot stat orig file $origdir/$fn");
395 $ofnread= '/dev/null';
397 $ofnread= "$origdir/$fn";
399 &unrepdiff2
("something else","plain file");
402 defined($c3= open(DIFFGEN
,"-|")) || &syserr
("fork for diff");
408 '-L',"$basedirname.orig/$fn",
409 '-L',"$basedirname/$fn",
410 '--',"$ofnread","$dir/$fn") or &syserr
("exec diff");
416 close(DIFFGEN
); $/= "\0";
417 &unrepdiff
("binary file contents changed");
419 } elsif (m/^[-+\@ ]/) {
421 } elsif (m/^\\ No newline at end of file$/) {
422 &warn("file $fn has no final newline ".
423 "(either original or modified version)");
426 &internerr
("unknown line from diff -u on $fn: `$_'");
428 print(GZIP
$_) || &syserr
("failed to write to gzip");
430 close(DIFFGEN
); $/= "\0";
431 if (WIFEXITED
($?
) && (($es=WEXITSTATUS
($?
))==0 || $es==1)) {
432 if ($es==1 && !$difflinefound) {
433 &unrepdiff
("diff gave 1 but no diff lines found");
436 subprocerr
("diff on $dir/$fn");
441 } elsif (-b _
|| -c _
|| -S _
) {
442 &unrepdiff
("device or socket is not allowed");
444 $type{$fn}= 'directory';
446 &unrepdiff
("unknown file type ($!)");
449 close(FIND
); $?
&& subprocerr
("find on $dir");
450 close(GZIP
) || &syserr
("finish write to gzip pipe");
453 defined($c2= open(FIND
,"-|")) || &syserr
("fork for 2nd find");
455 chdir($origdir) || &syserr
("chdir to $origdir for 2nd find");
456 exec('find','.','-print0') or &syserr
("exec 2nd find");
459 while (defined($fn= <FIND
>)) {
461 next if $fn =~ m/$diff_ignore_regexp/o;
463 next if defined($type{$fn});
464 lstat("$origdir/$fn") || &syserr
("cannot check orig file $origdir/$fn");
466 &warn("ignoring deletion of file $fn");
468 &warn("ignoring deletion of directory $fn");
470 &warn("ignoring deletion of symlink $fn");
472 &unrepdiff2
('not a file, directory or link','nonexistent');
475 close(FIND
); $?
&& subprocerr
("find on $dirname");
477 &addfile
("$basenamerev.diff.gz");
481 if ($sourcestyle =~ m/[prPR]/) {
485 print("$progname: building $sourcepackage in $basenamerev.dsc\n")
486 || &syserr
("write building message");
487 open(STDOUT
,"> $basenamerev.dsc") || &syserr
("create $basenamerev.dsc");
491 print(STDERR
"$progname: unrepresentable changes to source\n")
492 || &syserr
("write error msg: $!");
499 $sourcestyle =~ y/X/p/;
500 $sourcestyle =~ m/[pun]/ ||
501 &usageerr
("source handling style -s$sourcestyle not allowed with -x");
503 @ARGV>=1 || &usageerr
("-x needs at least one argument, the .dsc");
504 @ARGV<=2 || &usageerr
("-x takes no more than two arguments");
506 $dsc= "./$dsc" unless $dsc =~ m
:^/:;
507 $dscdir= $dsc; $dscdir= "./$dscdir" unless $dsc =~ m
,^/|^\
./,;
508 $dscdir =~ s
,/[^/]+$,,;
510 $newdirectory= shift(@ARGV);
511 ! -e
$newdirectory || &error
("unpack target exists: $newdirectory");
514 open(CDATA
,"< $dsc") || &error
("cannot open .dsc file $dsc: $!");
515 &parsecdata
('S',-1,"source control file $dsc");
518 for $f (qw(Source Version Files)) {
519 defined($fi{"S $f"}) ||
520 &error
("missing critical source control field $f");
523 my $dscformat = $def_dscformat;
524 if (defined $fi{'S Format'}) {
525 if (not handleformat
($fi{'S Format'})) {
526 &error
("Unsupported format of .dsc file ($fi{'S Format'})");
528 $dscformat=$fi{'S Format'};
531 $sourcepackage = $fi{'S Source'};
532 $sourcepackage =~ m/[^-+.0-9a-z]/ &&
533 &error
("source package name contains illegal character `$&'");
534 $sourcepackage =~ m/^[0-9a-z]/ ||
535 &error
("source package name starts with non-alphanum");
537 $version= $fi{'S Version'};
538 $version =~ m/[^-+:.0-9a-zA-Z~]/ &&
539 &error
("version number contains illegal character `$&'");
540 $version =~ s/^\d+://;
541 if ($version =~ m/-([^-]+)$/) {
542 $baseversion= $`; $revision= $1;
544 $baseversion= $version; $revision= '';
547 $files = $fi{'S Files'};
552 for $file (split(/\n /,$files)) {
554 $file =~ m/^([0-9a-f]{32})[ \t]+(\d+)[ \t]+([0-9a-zA-Z][-+:.,=0-9a-zA-Z_~]+)$/
555 || &error("Files field contains bad line `$file'");
556 ($md5sum{$3},$size{$3},$file) = ($1,$2,$3);
559 &error("Files field contains invalid filename `$file'")
560 unless s/^\Q$sourcepackage\E_\Q$baseversion\E\b// and
563 &error("repeated file type
- files
`$seen{$_}' and `$file'") if $seen{$_};
568 if (/^\.(orig(-\w+)?\.)?tar$/) {
569 if ($2) { push @tarfiles, $file; } # push orig-foo.tar.gz to the end
570 else { unshift @tarfiles, $file; }
574 if ($revision and s/^-\Q$revision\E\b//) {
579 if (/^\.debian\.tar$/) {
585 &error("unrecognised file suffix `$_'");
588 &error("no tarfile
in Files field
") unless @tarfiles;
589 my $native = !($difffile || $debianfile);
591 &warn("multiple tarfiles
in native
package") if @tarfiles > 1;
592 &warn("native
package with
.orig
.tar
") unless $seen{'.tar'};
594 &warn("no upstream tarfile
in Files field
") unless $seen{'.orig.tar'};
595 if ($dscformat =~ /^1\./) {
596 &warn("multiple upstream tarballs
in $dscformat format dsc
") if @tarfiles > 1;
597 &warn("debian
.tar
in $dscformat format dsc
") if $debianfile;
601 $newdirectory = $sourcepackage.'-'.$baseversion unless defined($newdirectory);
602 $expectprefix = $newdirectory;
603 $expectprefix .= '.orig' if $difffile || $debianfile;
605 checkdiff("$dscdir/$difffile") if $difffile;
606 print("$progname: extracting
$sourcepackage in $newdirectory\n")
607 || &syserr("write extracting message
");
609 &erasedir($newdirectory);
611 || rename("$expectprefix","$newdirectory.tmp
-keep
")
612 || &syserr("unable to
rename `$expectprefix' to `$newdirectory.tmp
-keep
'");
614 push @tarfiles, $debianfile if $debianfile;
615 for my $tarfile (@tarfiles)
618 if ($tarfile =~ /\.orig-(\w+)\.tar/) {
620 $sub =~ s/\d+$// if $sub =~ /\D/;
621 $target = "$expectprefix/$sub";
622 } elsif ($tarfile =~ /\.debian.tar/) {
623 $target = "$expectprefix/debian";
625 $target = $expectprefix;
628 my $tmp = "$target.tmp-nest";
629 (my $t = $target) =~ s!.*/!!;
631 mkdir($tmp,0755) || &syserr("unable to create `$tmp'");
632 system "chmod", "g
-s
", $tmp;
633 print("$progname: unpacking
$tarfile\n");
634 extracttar("$dscdir/$tarfile",$tmp,$t);
635 rename("$tmp/$t",$target)
636 || &syserr("unable to
rename `$tmp/$t' to `$target'");
638 || &syserr("unable to remove `$tmp'");
640 # for the first tar file:
641 if ($tarfile eq $tarfiles[0] and !$native)
643 # -sp: copy the .orig.tar.gz if required
644 if ($sourcestyle =~ /p/) {
645 stat("$dscdir/$tarfile") ||
646 &syserr("failed to
stat `$dscdir/$tarfile' to see if need to copy");
647 ($dsctardev,$dsctarino) = stat _;
648 if (!stat($tarfile)) {
649 $! == ENOENT || &syserr("failed to check destination `$tarfile'".
650 " to see if need to copy");
652 ($dumptardev,$dumptarino) = stat _;
654 unless ($dumptardev == $dsctardev && $dumptarino == $dsctarino) {
655 system('cp
','--',"$dscdir/$tarfile", $tarfile);
656 $? && subprocerr("cp $dscdir/$tarfile to $tarfile");
659 # -su: keep .orig directory unpacked
660 elsif ($sourcestyle =~ /u/ and $expectprefix ne $newdirectory) {
661 ! -e "$newdirectory.tmp-keep"
662 || &error("unable to keep orig directory (already exists)");
663 system('cp
','-ar
','--',$expectprefix,"$newdirectory.tmp-keep");
664 $? && subprocerr("cp $expectprefix to $newdirectory.tmp-keep");
670 push @patches, "$dscdir/$difffile" if $difffile;
672 if ($debianfile and -d (my $pd = "$expectprefix/debian/patches"))
677 while (defined ($_ = readdir D))
679 # patches match same rules as run-parts
680 next unless /^[\w-]+$/ and -f "$pd/$_";
688 push @patches, map "$newdirectory/debian/patches/$_", sort @p;
691 for $dircreate (keys %dirtocreate) {
693 for $dircreatep (split("/",$dirc)) {
694 $dircreatem.= $dircreatep;
695 if (!lstat($dircreatem)) {
696 $! == ENOENT || &syserr("cannot stat $dircreatem");
697 mkdir($dircreatem,0777)
698 || &syserr("failed to create $dircreatem subdirectory");
701 -d _ || &error("diff patches file in directory `$dircreate',"
702 ." but
$dircreatem isn
't a directory !");
707 if ($newdirectory ne $expectprefix)
709 rename($expectprefix,$newdirectory) ||
710 &syserr("failed to rename newly-extracted $expectprefix to $newdirectory");
712 # rename the copied .orig directory
713 ! -e "$newdirectory.tmp-keep"
714 || rename("$newdirectory.tmp-keep",$expectprefix)
715 || &syserr("failed to rename saved $newdirectory.tmp-keep to $expectprefix");
718 for my $patch (@patches) {
719 print("$progname: applying $patch\n");
720 if ($patch =~ /\.(gz|bz2)$/) {
721 &forkgzipread($patch);
724 open DIFF, $patch or &error("can't
open diff
`$patch'");
727 defined($c2= fork) || &syserr("fork for patch");
729 open(STDIN,"<&DIFF") || &syserr("reopen gzip for patch");
730 chdir($newdirectory) || &syserr("chdir to $newdirectory for patch");
733 exec('patch','-s','-t','-F','0','-N','-p1','-u',
734 '-V','never','-g0','-b','-z','.dpkg-orig') or &syserr("exec patch");
737 $c2 == waitpid($c2,0) || &syserr("wait for patch");
738 $? && subprocerr("patch");
740 &reapgzip if $patch =~ /\.(gz|bz2)$/;
743 for $fn (keys %filepatched) {
744 $ftr= "$newdirectory/".substr($fn,length($expectprefix)+1).".dpkg-orig";
745 unlink($ftr) || &syserr("remove patch backup file $ftr");
748 if (!(@s= lstat("$newdirectory/debian/rules"))) {
749 $! == ENOENT || &syserr("cannot stat $newdirectory/debian/rules");
750 &warn("$newdirectory/debian/rules does not exist");
752 chmod($s[2] | 0111, "$newdirectory/debian/rules") ||
753 &syserr("cannot make $newdirectory/debian/rules executable");
755 &warn("$newdirectory/debian/rules is not a plain file");
758 $execmode= 0777 & ~umask;
759 (@s= stat('.')) || &syserr("cannot stat `.'");
760 $dirmode= $execmode | ($s[2] & 02000);
761 $plainmode= $execmode & ~0111;
762 $fifomode= ($plainmode & 0222) | (($plainmode & 0222) << 1);
763 for $fn (@filesinarchive) {
764 $fn=~ s,^$expectprefix,$newdirectory,;
765 (@s= lstat($fn)) || &syserr("cannot stat extracted object `$fn'");
770 $newmode= ($mode & 0111) ? $execmode : $plainmode;
774 &internerr("unknown object
`$fn' after extract (mode ".
775 sprintf("0%o",$mode).")");
777 next if ($mode & 07777) == $newmode;
778 chmod($newmode,$fn) ||
779 &syserr(sprintf("cannot change mode of `%s' to 0%o from 0%o",
780 $fn,$newmode,$mode));
789 open(STDIN,"< $dscdir/$f") || &syserr("cannot read $dscdir/$f");
790 (@s= stat(STDIN)) || &syserr("cannot fstat $dscdir/$f");
791 $s[7] == $size{$f} || &error("file $f has size $s[7] instead of expected $size{$f}");
792 $m= `md5sum`; $? && subprocerr("md5sum $f"); $m =~ s/\n$//;
793 $m =~ s/ *-$//; # Remove trailing spaces and -, to work with GNU md5sum
794 $m =~ m/^[0-9a-f]{32}$/ || &failure("md5sum of $f gave bad output `$m'");
795 $m eq $md5sum{$f} || &error("file
$f has md5sum
$m instead of expected
$md5sum{$f}");
796 open(STDIN,"</dev/null
") || &syserr("reopen stdin from
/dev/null
");
802 $! == ENOENT && return;
803 &syserr("cannot
stat directory
$dir (before removal
)");
805 system 'rm','-rf','--',$dir;
806 $? && subprocerr("rm
-rf
$dir");
808 $! == ENOENT && return;
809 &syserr("unable to check
for removal of dir
`$dir'");
811 &failure("rm -rf failed to remove `$dir'");
818 my ($tarfileread, $wpfx) = @_;
819 my ($tarprefix, $c2);
821 @filesinarchive = ();
823 # make <CPIO> read from the uncompressed archive file
824 &forkgzipread ("$tarfileread");
825 if (! defined ($c2 = open (CPIO,"-|"))) { &syserr ("fork for cpio"); }
829 open (STDIN,"<&GZIP") || &syserr ("reopen gzip for cpio");
831 exec ('cpio
','-0t
') or &syserr ("exec cpio");
836 while (defined ($fn = <CPIO>)) {
840 # store printable name of file for error messages
845 &error ("tarfile `$tarfileread' contains object with
".
846 " newline
in its name
($pname)");
849 next if ($fn eq '././@LongLink');
853 &error("first output from cpio
-0t
(from
`$tarfileread') ".
854 "contains newline - you probably have an out of ".
855 "date version of cpio. GNU cpio 2.4.2-2 is known to work");
857 $tarprefix = ($fn =~ m,((\./)*[^/]*)[/],)[0];
858 # need to check for multiple dots on some operating systems
859 # empty tarprefix (due to regex failer) will match emptry string
860 if ($tarprefix =~ /^[.]*$/) {
861 &error("tarfile `$tarfileread' does not extract into a ".
862 "directory off the current directory ($tarprefix from $pname)");
866 my $fprefix = substr ($fn, 0, length ($tarprefix));
867 my $slash = substr ($fn, length ($tarprefix), 1);
868 if ((($slash ne '/') && ($slash ne '')) || ($fprefix ne $tarprefix)) {
869 &error ("tarfile `$tarfileread' contains object
($pname) ".
870 "not in expected directory
($tarprefix)");
873 # need to check for multiple dots on some operating systems
874 if ($fn =~ m/[.]{2,}/) {
875 &error ("tarfile
`$tarfileread' contains object with".
876 " /../ in its name ($pname)");
878 push (@filesinarchive, $fn);
881 $? && subprocerr ("cpio");
885 my $tarsubst = quotemeta ($tarprefix);
892 my ($tarfileread, $wpfx) = @_;
898 my $tarprefix = &checktarcpio ($tarfileread, $wpfx);
900 # make <TAR> read from the uncompressed archive file
901 &forkgzipread ("$tarfileread");
902 if (! defined ($c2 = open (TAR,"-|"))) { &syserr ("fork for tar -t"); }
906 open (STDIN, "<&GZIP") || &syserr ("reopen gzip for tar -t");
907 exec ('tar', '-vvtf', '-') or &syserr ("exec tar -vvtf -");
916 if (! m,^(\S{10})\s,) {
917 &error("tarfile `$tarfileread' contains unknown object ".
918 "listed by tar as `$_'");
922 $mode =~ s/^([-dpsl])// ||
923 &error("tarfile
`$tarfileread' contains object `$fn' with ".
924 "unknown or forbidden type `".substr($_,0,1)."'");
927 if ($mode =~ /^l/) { $_ =~ s/ -> .*//; }
930 my @tarfields = split(' ', $_, 6);
931 if (@tarfields < 6) {
932 &error ("tarfile
`$tarfileread' contains incomplete entry `$_'\n");
935 my $tarfn = deoctify ($tarfields[5]);
937 # store printable name of file for error messages
941 # fetch name of file as given by cpio
942 $fn = $filesinarchive[$efix++];
945 if (substr ($tarfn, 0, $l + 4) eq "$fn -> ") {
946 # This is a symlink, as listed by tar. cpio doesn't
947 # give us the targets of the symlinks, so we ignore this.
948 $tarfn = substr($tarfn, 0, $l);
951 if ((length ($fn) == 99) && (length ($tarfn) >= 99)
952 && (substr ($fn, 0, 99) eq substr ($tarfn, 0, 99))) {
953 # this file doesn't match because cpio truncated the name
954 # to the first 100 characters. let it slide for now.
955 &warn ("filename `$pname' was truncated by cpio;" .
956 " unable to check full pathname");
957 # Since it didn't match, later checks will not be able
958 # to stat this file, so we replace it with the filename
960 $filesinarchive[$efix-1] = $tarfn;
962 &error
("tarfile `$tarfileread' contains unexpected object".
963 " listed by tar as `$_'; expected `$pname'");
967 # if cpio truncated the name above,
968 # we still can't allow files to expand into /../
969 # need to check for multiple dots on some operating systems
970 if ($tarfn =~ m/[.]{2,}/) {
971 &error
("tarfile `$tarfileread' contains object with".
972 "/../ in its name ($pname)");
975 if ($tarfn =~ /\.dpkg-orig$/) {
976 &error
("tarfile `$tarfileread' contains file with name ending in .dpkg-orig");
979 if ($mode =~ /[sStT]/ && $type ne 'd') {
980 &error
("tarfile `$tarfileread' contains setuid, setgid".
981 " or sticky object `$pname'");
984 if ($tarfn eq "$tarprefix/debian" && $type ne 'd') {
985 &error
("tarfile `$tarfileread' contains object `debian'".
986 " that isn't a directory");
989 if ($type eq 'd') { $tarfn =~ s
,/$,,; }
990 $tarfn =~ s
,(\
./)*,,;
991 my $dirname = $tarfn;
993 if (($dirname =~ s
,/[^/]+$,,) && (! defined ($dirincluded{$dirname}))) {
994 &warnerror
("tarfile `$tarfileread' contains object `$pname' but its containing ".
995 "directory `$dirname' does not precede it");
996 $dirincluded{$dirname} = 1;
998 if ($type eq 'd') { $dirincluded{$tarfn} = 1; }
999 if ($type ne '-') { $notfileobject{$tarfn} = 1; }
1002 $?
&& subprocerr
("tar -vvtf");
1005 my $tarsubst = quotemeta ($tarprefix);
1006 @filesinarchive = map { s/^$tarsubst/$wpfx/; $_ } @filesinarchive;
1007 %dirincluded = map { s/^$tarsubst/$wpfx/; $_=>1 } (keys %dirincluded);
1008 %notfileobject = map { s/^$tarsubst/$wpfx/; $_=>1 } (keys %notfileobject);
1013 # check diff for sanity, find directories to create as a side effect
1017 if ($diff =~ /\.(gz|bz2)$/) {
1018 &forkgzipread
($diff);
1021 open DIFF
, $diff or &error
("can't open diff `$diff'");
1027 while (defined($_) || !eof(DIFF
)) {
1028 # skip cruft leading up to patch (if any)
1030 last HUNK
unless defined ($_ = <DIFF
>);
1032 # read file header (---/+++ pair)
1033 s/\n$// or &error
("diff `$diff' is missing trailing newline");
1034 s/^--- // or &error
("expected ^--- in line $. of diff `$diff'");
1036 $_ eq '/dev/null' or s!^(\./)?[^/]+/!$expectprefix/! or
1037 &error
("diff `$diff' patches file with no subdirectory");
1039 &error
("diff `$diff' patches file with name ending .dpkg-orig");
1042 (defined($_= <DIFF
>) and s/\n$//) or
1043 &error
("diff `$diff' finishes in middle of ---/+++ (line $.)");
1046 (s/^\+\+\+ // and s!^(\./)?[^/]+/!!)
1047 or &error
("line after --- isn't as expected in diff `$diff' (line $.)");
1049 if ($fn eq '/dev/null') {
1050 $fn = "$expectprefix/$_";
1052 $_ eq substr($fn, length($expectprefix)+1)
1053 or &error
("line after --- isn't as expected in diff `$diff' (line $.)");
1057 if ($dirname =~ s
,/[^/]+$,, && !defined($dirincluded{$dirname})) {
1058 $dirtocreate{$dirname} = 1;
1060 defined($notfileobject{$fn}) &&
1061 &error
("diff `$diff' patches something which is not a plain file");
1063 $filepatched{$fn} eq $diff && &error
("diff patches file $fn twice");
1064 $filepatched{$fn} = $diff;
1068 while (defined($_ = <DIFF
>) && !(/^--- / or /^Index:/)) {
1069 # read hunk header (@@)
1070 s/\n$// or &error
("diff `$diff' is missing trailing newline");
1071 next if /^\\ No newline/;
1072 /^@@ -\d+(,(\d+))? \+\d+(,(\d+))? @\@$/ or
1073 &error
("Expected ^\@\@ in line $. of diff `$diff'");
1074 my ($olines, $nlines) = ($1 ?
$2 : 1, $3 ?
$4 : 1);
1077 while ($olines || $nlines) {
1078 defined($_ = <DIFF
>) or &error
("unexpected end of diff `$diff'");
1079 s/\n$// or &error
("diff `$diff' is missing trailing newline");
1080 next if /^\\ No newline/;
1081 if (/^ /) { --$olines; --$nlines; }
1082 elsif (/^-/) { --$olines; }
1083 elsif (/^\+/) { --$nlines; }
1084 else { &error
("expected [ +-] at start of line $. of diff `$diff'"); }
1087 $hunk or &error
("expected ^\@\@ at line $. of diff `$diff'");
1091 &reapgzip
if $diff =~ /\.(gz|bz2)$/;
1095 my ($tarfileread,$dirchdir,$newtopdir) = @_;
1096 &forkgzipread
("$tarfileread");
1097 defined($c2= fork) || &syserr
("fork for tar -xkf -");
1099 open(STDIN
,"<&GZIP") || &syserr
("reopen gzip for tar -xkf -");
1101 chdir($dirchdir) || &syserr
("cannot chdir to `$dirchdir' for tar extract");
1102 exec('tar','-xkf','-') or &syserr
("exec tar -xkf -");
1105 $c2 == waitpid($c2,0) || &syserr
("wait for tar -xkf -");
1106 $?
&& subprocerr
("tar -xkf -");
1109 opendir(D
,"$dirchdir") || &syserr
("Unable to open dir $dirchdir");
1110 @dirchdirfiles = grep($_ ne "." && $_ ne "..",readdir(D
));
1111 closedir(D
) || &syserr
("Unable to close dir $dirchdir");
1112 if (@dirchdirfiles==1 && -d
"$dirchdir/$dirchdirfiles[0]") {
1113 rename("$dirchdir/$dirchdirfiles[0]", "$dirchdir/$newtopdir") ||
1114 &syserr
("Unable to rename $dirchdir/$dirchdirfiles[0] to ".
1115 "$dirchdir/$newtopdir");
1117 mkdir("$dirchdir/$newtopdir.tmp", 0777) or
1118 &syserr
("Unable to mkdir $dirchdir/$newtopdir.tmp");
1119 for (@dirchdirfiles) {
1120 rename("$dirchdir/$_", "$dirchdir/$newtopdir.tmp/$_") or
1121 &syserr
("Unable to rename $dirchdir/$_ to ".
1122 "$dirchdir/$newtopdir.tmp/$_");
1124 rename("$dirchdir/$newtopdir.tmp", "$dirchdir/$newtopdir") or
1125 &syserr
("Unable to rename $dirchdir/$newtopdir.tmp to $dirchdir/$newtopdir");
1130 open(STDERR
,"| grep -E -v '^[0-9]+ blocks\$' >&2") ||
1131 &syserr
("reopen stderr for tar to grep out blocks message");
1135 if (!lstat("$origdir/$fn")) {
1136 &unrepdiff2
("nonexistent",$type{$fn});
1138 $v= eval("$_[0] _ ? 2 : 1"); $v || &internerr
("checktype $@ ($_[0])");
1139 return 1 if $v == 2;
1140 &unrepdiff2
("something else",$type{$fn});
1146 defined($opmode) && &usageerr
("only one of -x or -b allowed, and only once");
1151 print(STDERR
"$progname: cannot represent change to $fn: $_[0]\n")
1152 || &syserr
("write syserr unrep");
1157 print(STDERR
"$progname: cannot represent change to $fn:\n".
1158 "$progname: new version is $_[1]\n".
1159 "$progname: old version is $_[0]\n")
1160 || &syserr
("write syserr unrep");
1165 open(GZIPFILE
,"> $_[0]") || &syserr
("create file $_[0]");
1166 pipe(GZIPREAD
,GZIP
) || &syserr
("pipe for gzip");
1167 defined($cgz= fork) || &syserr
("fork for gzip");
1169 open(STDIN
,"<&GZIPREAD") || &syserr
("reopen gzip pipe"); close(GZIPREAD
);
1170 close(GZIP
); open(STDOUT
,">&GZIPFILE") || &syserr
("reopen tar.gz");
1171 exec('gzip','-9') or &syserr
("exec gzip");
1178 local $SIG{PIPE
} = 'DEFAULT';
1179 my $prog = $_[0] =~ /\.gz$/ ?
'gunzip' : 'bunzip2';
1180 open(GZIPFILE
,"< $_[0]") || &syserr
("read file $_[0]");
1181 pipe(GZIP
,GZIPWRITE
) || &syserr
("pipe for $prog");
1182 defined($cgz= fork) || &syserr
("fork for $prog");
1184 open(STDOUT
,">&GZIPWRITE") || &syserr
("reopen $prog pipe"); close(GZIPWRITE
);
1185 close(GZIP
); open(STDIN
,"<&GZIPFILE") || &syserr
("reopen input file");
1186 exec($prog) or &syserr
("exec $prog");
1193 $cgz == waitpid($cgz,0) || &syserr
("wait for gzip");
1194 !$?
|| ($gzipsigpipeok && WIFSIGNALED
($?
) && WTERMSIG
($?
)==SIGPIPE
) ||
1201 stat($filename) || &syserr
("could not stat output file `$filename'");
1203 my $md5sum= `md5sum <$filename`;
1204 $?
&& &subprocerr
("md5sum $filename");
1205 $md5sum =~ s/^([0-9a-f]{32})\s*-?\s*\n$/$1/ || &failure
("md5sum gave bogus output `$_'");
1206 $f{'Files'}.= "\n $md5sum $size $filename";
1209 # replace \ddd with their corresponding character, refuse \ddd > \377
1215 $backslash= sprintf("\\%03o", unpack("C", "\\")) if !$backslash;
1217 s/\\{2}/$backslash/g;
1218 @_= split(/\\/, $fn);
1222 &failure
("bogus character `\\$1' in `$fn'\n") if oct($1) > 255;
1223 $_= pack("c", oct($1)) . $';
1225 return join("", @_);