dpkg-source.pl 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  1. #! /usr/bin/perl
  2. my $dpkglibdir = ".";
  3. my $version = "1.3.0"; # This line modified by Makefile
  4. my @filesinarchive;
  5. my %dirincluded;
  6. my %notfileobject;
  7. my $fn;
  8. $diff_ignore_default_regexp = '
  9. # Ignore general backup files
  10. (?:^|/).*~$|
  11. # Ignore emacs recovery files
  12. (?:^|/)\.#.*$|
  13. # Ignore vi swap files
  14. (?:^|/)\..*\.swp$|
  15. # Ignore baz-style junk files or directories
  16. (?:^|/),,.*(?:$|/.*$)|
  17. # File-names that should be ignored (never directories)
  18. (?:^|/)(?:DEADJOE|\.cvsignore|\.arch-inventory|\.bzrignore)$|
  19. # File or directory names that should be ignored
  20. (?:^|/)(?:CVS|RCS|\.deps|\{arch\}|\.arch-ids|\.svn|_darcs|\.git|\.bzr(?:\.backup)?)(?:$|/.*$)
  21. ';
  22. # Take out comments and newlines
  23. $diff_ignore_default_regexp =~ s/^#.*$//mg;
  24. $diff_ignore_default_regexp =~ s/\n//sg;
  25. $sourcestyle = 'X';
  26. $min_dscformat = 1;
  27. $max_dscformat = 2;
  28. $def_dscformat = "1.0"; # default format for -b
  29. use POSIX;
  30. use Fcntl qw (:mode);
  31. use File::Temp qw (tempfile);
  32. use Cwd;
  33. use strict 'refs';
  34. push (@INC, $dpkglibdir);
  35. require 'controllib.pl';
  36. # Make sure patch doesn't get any funny ideas
  37. delete $ENV{'POSIXLY_CORRECT'};
  38. my @exit_handlers = ();
  39. sub exit_handler {
  40. &$_ foreach ( reverse @exit_handlers );
  41. exit(127);
  42. }
  43. $SIG{'INT'} = \&exit_handler;
  44. $SIG{'HUP'} = \&exit_handler;
  45. $SIG{'QUIT'} = \&exit_handler;
  46. sub usageversion {
  47. print STDERR
  48. "Debian dpkg-source $version. Copyright (C) 1996
  49. Ian Jackson and Klee Dienes. This is free software; see the GNU
  50. General Public Licence version 2 or later for copying conditions.
  51. There is NO warranty.
  52. Usage: dpkg-source -x <filename>.dsc [<output-directory>]
  53. dpkg-source -b <directory> [<orig-directory>|<orig-targz>|\'\']
  54. Build options: -c<controlfile> get control info from this file
  55. -l<changelogfile> get per-version info from this file
  56. -F<changelogformat> force change log format
  57. -V<name>=<value> set a substitution variable
  58. -T<varlistfile> read variables here, not debian/substvars
  59. -D<field>=<value> override or add a .dsc field and value
  60. -U<field> remove a field
  61. -W Turn certain errors into warnings.
  62. -E When -W is enabled, -E disables it.
  63. -q quiet operation, do not print warnings.
  64. -sa auto select orig source (-sA is default)
  65. -i[<regexp>] filter out files to ignore diffs of.
  66. Defaults to: '$diff_ignore_default_regexp'
  67. -I<filename> filter out files when building tarballs.
  68. -sk use packed orig source (unpack & keep)
  69. -sp use packed orig source (unpack & remove)
  70. -su use unpacked orig source (pack & keep)
  71. -sr use unpacked orig source (pack & remove)
  72. -ss trust packed & unpacked orig src are same
  73. -sn there is no diff, do main tarfile only
  74. -sA,-sK,-sP,-sU,-sR like -sa,-sp,-sk,-su,-sr but may overwrite
  75. Extract options: -sp (default) leave orig source packed in current dir
  76. -sn do not copy original source to current dir
  77. -su unpack original source tree too
  78. General options: -h print this message
  79. ";
  80. }
  81. sub handleformat {
  82. my $fmt = shift;
  83. return unless $fmt =~ /^(\d+)/; # only check major version
  84. return $1 >= $min_dscformat && $1 <= $max_dscformat;
  85. }
  86. $i = 100;
  87. grep ($fieldimps {$_} = $i--,
  88. qw (Format Source Version Binary Origin Maintainer Architecture
  89. Standards-Version Build-Depends Build-Depends-Indep Build-Conflicts
  90. Build-Conflicts-Indep));
  91. while (@ARGV && $ARGV[0] =~ m/^-/) {
  92. $_=shift(@ARGV);
  93. if (m/^-b$/) {
  94. &setopmode('build');
  95. } elsif (m/^-x$/) {
  96. &setopmode('extract');
  97. } elsif (m/^-s([akpursnAKPUR])$/) {
  98. &warn( "-s$1 option overrides earlier -s$sourcestyle option" )
  99. if $sourcestyle ne 'X';
  100. $sourcestyle= $1;
  101. } elsif (m/^-c/) {
  102. $controlfile= $';
  103. } elsif (m/^-l/) {
  104. $changelogfile= $';
  105. } elsif (m/^-F([0-9a-z]+)$/) {
  106. $changelogformat=$1;
  107. } elsif (m/^-D([^\=:]+)[=:]/) {
  108. $override{$1}= "$'";
  109. } elsif (m/^-U([^\=:]+)$/) {
  110. $remove{$1}= 1;
  111. } elsif (m/^-i(.*)$/) {
  112. $diff_ignore_regexp = $1 ? $1 : $diff_ignore_default_regexp;
  113. } elsif (m/^-I(.+)$/) {
  114. push @tar_ignore, "--exclude=$1";
  115. } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:]/) {
  116. $substvar{$1}= "$'";
  117. } elsif (m/^-T/) {
  118. $varlistfile= "$'";
  119. } elsif (m/^-h$/) {
  120. &usageversion; exit(0);
  121. } elsif (m/^-W$/) {
  122. $warnable_error= 1;
  123. } elsif (m/^-E$/) {
  124. $warnable_error= 0;
  125. } elsif (m/^-q$/) {
  126. $quiet_warnings = 1;
  127. } elsif (m/^--$/) {
  128. last;
  129. } else {
  130. &usageerr("unknown option $_");
  131. }
  132. }
  133. defined($opmode) || &usageerr("need -x or -b");
  134. $SIG{'PIPE'} = 'DEFAULT';
  135. if ($opmode eq 'build') {
  136. $sourcestyle =~ y/X/A/;
  137. $sourcestyle =~ m/[akpursnAKPUR]/ ||
  138. &usageerr("source handling style -s$sourcestyle not allowed with -b");
  139. @ARGV || &usageerr("-b needs a directory");
  140. @ARGV<=2 || &usageerr("-b takes at most a directory and an orig source argument");
  141. $dir= shift(@ARGV);
  142. $dir= "./$dir" unless $dir =~ m:^/:; $dir =~ s,/*$,,;
  143. stat($dir) || &error("cannot stat directory $dir: $!");
  144. -d $dir || &error("directory argument $dir is not a directory");
  145. $changelogfile= "$dir/debian/changelog" unless defined($changelogfile);
  146. $controlfile= "$dir/debian/control" unless defined($controlfile);
  147. &parsechangelog;
  148. &parsecontrolfile;
  149. $f{"Format"}=$def_dscformat;
  150. $archspecific=0;
  151. for $_ (keys %fi) {
  152. $v= $fi{$_};
  153. if (s/^C //) {
  154. if (m/^Source$/i) { &setsourcepackage; }
  155. elsif (m/^(Standards-Version|Origin|Maintainer)$/i) { $f{$_}= $v; }
  156. elsif (m/^Uploaders$/i) { ($f{$_}= $v) =~ s/[\r\n]//g; }
  157. elsif (m/^Build-(Depends|Conflicts)(-Indep)?$/i) {
  158. my $dep = parsedep(substvars($v),1);
  159. &error("error occurred while parsing $_") unless defined $dep;
  160. $f{$_}= showdep($dep, 1);
  161. }
  162. elsif (s/^X[BC]*S[BC]*-//i) { $f{$_}= $v; }
  163. elsif (m/^(Section|Priority|Files|Bugs)$/i || m/^X[BC]+-/i) { }
  164. else { &unknown('general section of control info file'); }
  165. } elsif (s/^C(\d+) //) {
  166. $i=$1; $p=$fi{"C$i Package"};
  167. push(@binarypackages,$p) unless $packageadded{$p}++;
  168. if (m/^Architecture$/) {
  169. if (debian_arch_eq($v, 'any')) {
  170. @sourcearch= ('any');
  171. } elsif (debian_arch_eq($v, 'all')) {
  172. if (!@sourcearch || $sourcearch[0] eq 'all') {
  173. @sourcearch= ('all');
  174. } else {
  175. @sourcearch= ('any');
  176. }
  177. } else {
  178. if (grep($sourcearch[0] eq $_, 'any','all')) {
  179. @sourcearch= ('any');
  180. } else {
  181. my @arches = map(debian_arch_expand($_),
  182. split(/\s+/, $v));
  183. chomp @arches;
  184. for $a (@arches) {
  185. &error("`$a' is not a legal architecture string")
  186. unless $a =~ /^[\w-]+$/;
  187. &error("architecture $a only allowed on its own".
  188. " (list for package $p is `$a')")
  189. if grep($a eq $_, 'any','all');
  190. push(@sourcearch,$a) unless $archadded{$a}++;
  191. }
  192. }
  193. }
  194. $f{'Architecture'}= join(' ',@sourcearch);
  195. } elsif (s/^X[BC]*S[BC]*-//i) {
  196. $f{$_}= $v;
  197. } elsif (m/^(Package|Essential|Pre-Depends|Depends|Provides)$/i ||
  198. m/^(Recommends|Suggests|Optional|Conflicts|Replaces)$/i ||
  199. m/^(Enhances|Description|Section|Priority)$/i ||
  200. m/^X[CS]+-/i) {
  201. } else {
  202. &unknown("package's section of control info file");
  203. }
  204. } elsif (s/^L //) {
  205. if (m/^Source$/) {
  206. &setsourcepackage;
  207. } elsif (m/^Version$/) {
  208. checkversion( $v );
  209. $f{$_}= $v;
  210. } elsif (s/^X[BS]*C[BS]*-//i) {
  211. $f{$_}= $v;
  212. } elsif (m/^(Maintainer|Changes|Urgency|Distribution|Date|Closes)$/i ||
  213. m/^X[BS]+-/i) {
  214. } else {
  215. &unknown("parsed version of changelog");
  216. }
  217. } elsif (m/^o:.*/) {
  218. } else {
  219. &internerr("value from nowhere, with key >$_< and value >$v<");
  220. }
  221. }
  222. $f{'Binary'}= join(', ',@binarypackages);
  223. for $f (keys %override) { $f{&capit($f)}= $override{$f}; }
  224. for $f (qw(Version)) {
  225. defined($f{$f}) || &error("missing information for critical output field $f");
  226. }
  227. for $f (qw(Maintainer Architecture Standards-Version)) {
  228. defined($f{$f}) || &warn("missing information for output field $f");
  229. }
  230. defined($sourcepackage) || &error("unable to determine source package name !");
  231. $f{'Source'}= $sourcepackage;
  232. for $f (keys %remove) { delete $f{&capit($f)}; }
  233. $version= $f{'Version'};
  234. $version =~ s/^\d+://; $upstreamversion= $version; $upstreamversion =~ s/-[^-]*$//;
  235. $basenamerev= $sourcepackage.'_'.$version;
  236. $basename= $sourcepackage.'_'.$upstreamversion;
  237. $basedirname= $basename;
  238. $basedirname =~ s/_/-/;
  239. $origdir= "$dir.orig";
  240. $origtargz= "$basename.orig.tar.gz";
  241. if (@ARGV) {
  242. $origarg= shift(@ARGV);
  243. if (length($origarg)) {
  244. stat($origarg) || &error("cannot stat orig argument $origarg: $!");
  245. if (-d _) {
  246. $origdir= $origarg;
  247. $origdir= "./$origdir" unless $origdir =~ m,^/,; $origdir =~ s,/*$,,;
  248. $sourcestyle =~ y/aA/rR/;
  249. $sourcestyle =~ m/[ursURS]/ ||
  250. &error("orig argument is unpacked but source handling style".
  251. " -s$sourcestyle calls for packed (.orig.tar.gz)");
  252. } elsif (-f _) {
  253. $origtargz= $origarg;
  254. $sourcestyle =~ y/aA/pP/;
  255. $sourcestyle =~ m/[kpsKPS]/ ||
  256. &error("orig argument is packed but source handling style".
  257. " -s$sourcestyle calls for unpacked (.orig/)");
  258. } else {
  259. &error("orig argument $origarg is not a plain file or directory");
  260. }
  261. } else {
  262. $sourcestyle =~ y/aA/nn/;
  263. $sourcestyle =~ m/n/ ||
  264. &error("orig argument is empty (means no orig, no diff)".
  265. " but source handling style -s$sourcestyle wants something");
  266. }
  267. }
  268. if ($sourcestyle =~ m/[aA]/) {
  269. if (stat("$origtargz")) {
  270. -f _ || &error("packed orig `$origtargz' exists but is not a plain file");
  271. $sourcestyle =~ y/aA/pP/;
  272. } elsif ($! != ENOENT) {
  273. &syserr("unable to stat putative packed orig `$origtargz'");
  274. } elsif (stat("$origdir")) {
  275. -d _ || &error("unpacked orig `$origdir' exists but is not a directory");
  276. $sourcestyle =~ y/aA/rR/;
  277. } elsif ($! != ENOENT) {
  278. &syserr("unable to stat putative unpacked orig `$origdir'");
  279. } else {
  280. $sourcestyle =~ y/aA/nn/;
  281. }
  282. }
  283. $dirbase= $dir; $dirbase =~ s,/?$,,; $dirbase =~ s,[^/]+$,,; $dirname= $&;
  284. $dirname eq $basedirname || &warn("source directory `$dir' is not <sourcepackage>".
  285. "-<upstreamversion> `$basedirname'");
  286. if ($sourcestyle ne 'n') {
  287. $origdirbase= $origdir; $origdirbase =~ s,/?$,,;
  288. $origdirbase =~ s,[^/]+$,,; $origdirname= $&;
  289. $origdirname eq "$basedirname.orig" ||
  290. &warn(".orig directory name $origdirname is not <package>".
  291. "-<upstreamversion> (wanted $basedirname.orig)");
  292. $tardirbase= $origdirbase; $tardirname= $origdirname;
  293. $tarname= $origtargz;
  294. $tarname eq "$basename.orig.tar.gz" ||
  295. &warn(".orig.tar.gz name $tarname is not <package>_<upstreamversion>".
  296. ".orig.tar.gz (wanted $basename.orig.tar.gz)");
  297. } else {
  298. $tardirbase= $dirbase; $tardirname= $dirname;
  299. $tarname= "$basenamerev.tar.gz";
  300. }
  301. if ($sourcestyle =~ m/[nurUR]/) {
  302. if (stat($tarname)) {
  303. $sourcestyle =~ m/[nUR]/ ||
  304. &error("tarfile `$tarname' already exists, not overwriting,".
  305. " giving up; use -sU or -sR to override");
  306. } elsif ($! != ENOENT) {
  307. &syserr("unable to check for existence of `$tarname'");
  308. }
  309. print("$progname: building $sourcepackage in $tarname\n")
  310. || &syserr("write building tar message");
  311. my ($ntfh, $newtar) = tempfile( "$tarname.new.XXXXXX",
  312. DIR => &getcwd, UNLINK => 0 );
  313. &forkgzipwrite($newtar);
  314. defined($c2= fork) || &syserr("fork for tar");
  315. if (!$c2) {
  316. chdir($tardirbase) || &syserr("chdir to above (orig) source $tardirbase");
  317. open(STDOUT,">&GZIP") || &syserr("reopen gzip for tar");
  318. # FIXME: put `--' argument back when tar is fixed
  319. exec('tar',@tar_ignore,'-cf','-',$tardirname) or &syserr("exec tar");
  320. }
  321. close(GZIP);
  322. &reapgzip;
  323. $c2 == waitpid($c2,0) || &syserr("wait for tar");
  324. $? && !(WIFSIGNALED($c2) && WTERMSIG($c2) == SIGPIPE) && subprocerr("tar");
  325. rename($newtar,$tarname) ||
  326. &syserr("unable to rename `$newtar' (newly created) to `$tarname'");
  327. chmod(0666 &~ umask(), $tarname) ||
  328. &syserr("unable to change permission of `$tarname'");
  329. } else {
  330. print("$progname: building $sourcepackage using existing $tarname\n")
  331. || &syserr("write using existing tar message");
  332. }
  333. addfile("$tarname");
  334. if ($sourcestyle =~ m/[kpKP]/) {
  335. if (stat($origdir)) {
  336. $sourcestyle =~ m/[KP]/ ||
  337. &error("orig dir `$origdir' already exists, not overwriting,".
  338. " giving up; use -sA, -sK or -sP to override");
  339. push @exit_handlers, sub { erasedir($origdir) };
  340. erasedir($origdir);
  341. pop @exit_handlers;
  342. } elsif ($! != ENOENT) {
  343. &syserr("unable to check for existence of orig dir `$origdir'");
  344. }
  345. $expectprefix= $origdir; $expectprefix =~ s,^\./,,;
  346. $expectprefix_dirname = $origdirname;
  347. # tar checking is disabled, there are too many broken tar archives out there
  348. # which we can still handle anyway.
  349. # checktarsane($origtargz,$expectprefix);
  350. mkdir("$origtargz.tmp-nest",0755) ||
  351. &syserr("unable to create `$origtargz.tmp-nest'");
  352. push @exit_handlers, sub { erasedir("$origtargz.tmp-nest") };
  353. extracttar($origtargz,"$origtargz.tmp-nest",$expectprefix_dirname);
  354. rename("$origtargz.tmp-nest/$expectprefix_dirname",$expectprefix) ||
  355. &syserr("unable to rename `$origtargz.tmp-nest/$expectprefix_dirname' to ".
  356. "`$expectprefix'");
  357. rmdir("$origtargz.tmp-nest") ||
  358. &syserr("unable to remove `$origtargz.tmp-nest'");
  359. pop @exit_handlers;
  360. }
  361. if ($sourcestyle =~ m/[kpursKPUR]/) {
  362. print("$progname: building $sourcepackage in $basenamerev.diff.gz\n")
  363. || &syserr("write building diff message");
  364. my ($ndfh, $newdiffgz) = tempfile( "$basenamerev.diff.gz.new.XXXXXX",
  365. DIR => &getcwd, UNLINK => 0 );
  366. &forkgzipwrite($newdiffgz);
  367. defined($c2= open(FIND,"-|")) || &syserr("fork for find");
  368. if (!$c2) {
  369. chdir($dir) || &syserr("chdir to $dir for find");
  370. exec('find','.','-print0') or &syserr("exec find");
  371. }
  372. $/= "\0";
  373. file:
  374. while (defined($fn= <FIND>)) {
  375. $fn =~ s/\0$//;
  376. next file if $fn =~ m/$diff_ignore_regexp/o;
  377. $fn =~ s,^\./,,;
  378. lstat("$dir/$fn") || &syserr("cannot stat file $dir/$fn");
  379. my $mode = S_IMODE((lstat(_))[2]);
  380. if (-l _) {
  381. $type{$fn}= 'symlink';
  382. &checktype('-l') || next;
  383. defined($n= readlink("$dir/$fn")) ||
  384. &syserr("cannot read link $dir/$fn");
  385. defined($n2= readlink("$origdir/$fn")) ||
  386. &syserr("cannot read orig link $origdir/$fn");
  387. $n eq $n2 || &unrepdiff2("symlink to $n2","symlink to $n");
  388. } elsif (-f _) {
  389. $type{$fn}= 'plain file';
  390. if (!lstat("$origdir/$fn")) {
  391. $! == ENOENT || &syserr("cannot stat orig file $origdir/$fn");
  392. $ofnread= '/dev/null';
  393. if( $mode & ( S_IXUSR | SIXGRP | S_IXOTH ) ) {
  394. &warn( sprintf( "executable mode %04o of `$fn' will not be represented in diff", $mode ) )
  395. unless $fn eq 'debian/rules';
  396. }
  397. if( $mode & ( S_ISUID | S_IGID | S_ISVTX ) ) {
  398. &warn( sprintf( "special mode %04o of `$fn' will not be represented in diff", $mode ) );
  399. }
  400. } elsif (-f _) {
  401. $ofnread= "$origdir/$fn";
  402. } else {
  403. &unrepdiff2("something else","plain file");
  404. next;
  405. }
  406. defined($c3= open(DIFFGEN,"-|")) || &syserr("fork for diff");
  407. if (!$c3) {
  408. $ENV{'LC_ALL'}= 'C';
  409. $ENV{'LANG'}= 'C';
  410. $ENV{'TZ'}= 'UTC0';
  411. exec('diff','-u',
  412. '-L',"$basedirname.orig/$fn",
  413. '-L',"$basedirname/$fn",
  414. '--',"$ofnread","$dir/$fn") or &syserr("exec diff");
  415. }
  416. $difflinefound= 0;
  417. $/= "\n";
  418. while (<DIFFGEN>) {
  419. if (m/^binary/i) {
  420. close(DIFFGEN); $/= "\0";
  421. &unrepdiff("binary file contents changed");
  422. next file;
  423. } elsif (m/^[-+\@ ]/) {
  424. $difflinefound=1;
  425. } elsif (m/^\\ No newline at end of file$/) {
  426. &warn("file $fn has no final newline ".
  427. "(either original or modified version)");
  428. } else {
  429. s/\n$//;
  430. &internerr("unknown line from diff -u on $fn: `$_'");
  431. }
  432. print(GZIP $_) || &syserr("failed to write to gzip");
  433. }
  434. close(DIFFGEN); $/= "\0";
  435. if (WIFEXITED($?) && (($es=WEXITSTATUS($?))==0 || $es==1)) {
  436. if ($es==1 && !$difflinefound) {
  437. &unrepdiff("diff gave 1 but no diff lines found");
  438. }
  439. } else {
  440. subprocerr("diff on $dir/$fn");
  441. }
  442. } elsif (-p _) {
  443. $type{$fn}= 'pipe';
  444. &checktype('-p');
  445. } elsif (-b _ || -c _ || -S _) {
  446. &unrepdiff("device or socket is not allowed");
  447. } elsif (-d _) {
  448. $type{$fn}= 'directory';
  449. if (!lstat("$origdir/$fn")) {
  450. $! == ENOENT
  451. || &syserr("cannot stat orig file $origdir/$fn");
  452. } elsif (! -d _) {
  453. &unrepdiff2('not a directory', 'directory');
  454. }
  455. } else {
  456. &unrepdiff("unknown file type ($!)");
  457. }
  458. }
  459. close(FIND); $? && subprocerr("find on $dir");
  460. close(GZIP) || &syserr("finish write to gzip pipe");
  461. &reapgzip;
  462. rename($newdiffgz,"$basenamerev.diff.gz") ||
  463. &syserr("unable to rename `$newdiffgz' (newly created) to `$basenamerev.diff.gz'");
  464. chmod(0666 &~ umask(), "$basenamerev.diff.gz") ||
  465. &syserr("unable to change permission of `$basenamerev.diff.gz'");
  466. defined($c2= open(FIND,"-|")) || &syserr("fork for 2nd find");
  467. if (!$c2) {
  468. chdir($origdir) || &syserr("chdir to $origdir for 2nd find");
  469. exec('find','.','-print0') or &syserr("exec 2nd find");
  470. }
  471. $/= "\0";
  472. while (defined($fn= <FIND>)) {
  473. $fn =~ s/\0$//;
  474. next if $fn =~ m/$diff_ignore_regexp/o;
  475. $fn =~ s,^\./,,;
  476. next if defined($type{$fn});
  477. lstat("$origdir/$fn") || &syserr("cannot check orig file $origdir/$fn");
  478. if (-f _) {
  479. &warn("ignoring deletion of file $fn");
  480. } elsif (-d _) {
  481. &warn("ignoring deletion of directory $fn");
  482. } elsif (-l _) {
  483. &warn("ignoring deletion of symlink $fn");
  484. } else {
  485. &unrepdiff2('not a file, directory or link','nonexistent');
  486. }
  487. }
  488. close(FIND); $? && subprocerr("find on $dirname");
  489. &addfile("$basenamerev.diff.gz");
  490. }
  491. if ($sourcestyle =~ m/[prPR]/) {
  492. erasedir($origdir);
  493. }
  494. print("$progname: building $sourcepackage in $basenamerev.dsc\n")
  495. || &syserr("write building message");
  496. open(STDOUT,"> $basenamerev.dsc") || &syserr("create $basenamerev.dsc");
  497. &outputclose(1);
  498. if ($ur) {
  499. print(STDERR "$progname: unrepresentable changes to source\n")
  500. || &syserr("write error msg: $!");
  501. exit(1);
  502. }
  503. exit(0);
  504. } else { # -> opmode ne 'build'
  505. $sourcestyle =~ y/X/p/;
  506. $sourcestyle =~ m/[pun]/ ||
  507. &usageerr("source handling style -s$sourcestyle not allowed with -x");
  508. @ARGV>=1 || &usageerr("-x needs at least one argument, the .dsc");
  509. @ARGV<=2 || &usageerr("-x takes no more than two arguments");
  510. $dsc= shift(@ARGV);
  511. $dsc= "./$dsc" unless $dsc =~ m:^/:;
  512. ! -d $dsc
  513. || &usageerr("-x needs the .dsc file as first argument, not a directory");
  514. $dscdir= $dsc; $dscdir= "./$dscdir" unless $dsc =~ m,^/|^\./,;
  515. $dscdir =~ s,/[^/]+$,,;
  516. if (@ARGV) {
  517. $newdirectory= shift(@ARGV);
  518. ! -e $newdirectory || &error("unpack target exists: $newdirectory");
  519. }
  520. my $is_signed = 0;
  521. open(DSC,"< $dsc") || &error("cannot open .dsc file $dsc: $!");
  522. while (<DSC>) {
  523. next if /^\s*$/o;
  524. $is_signed = 1 if /^-----BEGIN PGP SIGNED MESSAGE-----$/o;
  525. last;
  526. }
  527. close(DSC);
  528. if ($is_signed) {
  529. if (-x '/usr/bin/gpg') {
  530. my $gpg_command = 'gpg -q --verify '.quotemeta($dsc).' 2>&1';
  531. my @gpg_output = `$gpg_command`;
  532. my $gpg_status = $? >> 8;
  533. if ($gpg_status) {
  534. print STDERR join("",@gpg_output);
  535. &error("failed to verify signature on $dsc")
  536. if ($gpg_status == 1);
  537. }
  538. } else {
  539. &warn("could not verify signature on $dsc since gpg isn't installed");
  540. }
  541. } else {
  542. &warn("extracting unsigned source package ($dsc)");
  543. }
  544. open(CDATA,"< $dsc") || &error("cannot open .dsc file $dsc: $!");
  545. &parsecdata('S',-1,"source control file $dsc");
  546. close(CDATA);
  547. for $f (qw(Source Version Files)) {
  548. defined($fi{"S $f"}) ||
  549. &error("missing critical source control field $f");
  550. }
  551. my $dscformat = $def_dscformat;
  552. if (defined $fi{'S Format'}) {
  553. if (not handleformat($fi{'S Format'})) {
  554. &error("Unsupported format of .dsc file ($fi{'S Format'})");
  555. }
  556. $dscformat=$fi{'S Format'};
  557. }
  558. $sourcepackage = $fi{'S Source'};
  559. checkpackagename( $sourcepackage );
  560. $version= $fi{'S Version'};
  561. checkversion( $version );
  562. $version =~ s/^\d+://;
  563. if ($version =~ m/-([^-]+)$/) {
  564. $baseversion= $`; $revision= $1;
  565. } else {
  566. $baseversion= $version; $revision= '';
  567. }
  568. $files = $fi{'S Files'};
  569. my @tarfiles;
  570. my $difffile;
  571. my $debianfile;
  572. my %seen;
  573. for $file (split(/\n /,$files)) {
  574. next if $file eq '';
  575. $file =~ m/^([0-9a-f]{32})[ \t]+(\d+)[ \t]+([0-9a-zA-Z][-+:.,=0-9a-zA-Z_~]+)$/
  576. || &error("Files field contains bad line `$file'");
  577. ($md5sum{$3},$size{$3},$file) = ($1,$2,$3);
  578. local $_ = $file;
  579. &error("Files field contains invalid filename `$file'")
  580. unless s/^\Q$sourcepackage\E_\Q$baseversion\E(?=[.-])// and
  581. s/\.(gz|bz2)$//;
  582. s/^-\Q$revision\E(?=\.)// if length $revision;
  583. &error("repeated file type - files `$seen{$_}' and `$file'") if $seen{$_};
  584. $seen{$_} = $file;
  585. checkstats($file);
  586. if (/^\.(?:orig(-\w+)?\.)?tar$/) {
  587. if ($1) { push @tarfiles, $file; } # push orig-foo.tar.gz to the end
  588. else { unshift @tarfiles, $file; }
  589. } elsif (/^\.debian\.tar$/) {
  590. $debianfile = $file;
  591. } elsif (/^\.diff$/) {
  592. $difffile = $file;
  593. } else {
  594. &error("unrecognised file type - `$file'");
  595. }
  596. }
  597. &error("no tarfile in Files field") unless @tarfiles;
  598. my $native = !($difffile || $debianfile);
  599. if ($native) {
  600. &warn("multiple tarfiles in native package") if @tarfiles > 1;
  601. &warn("native package with .orig.tar")
  602. unless $seen{'.tar'} or $seen{"-$revision.tar"};
  603. } else {
  604. &warn("no upstream tarfile in Files field") unless $seen{'.orig.tar'};
  605. if ($dscformat =~ /^1\./) {
  606. &warn("multiple upstream tarballs in $dscformat format dsc") if @tarfiles > 1;
  607. &warn("debian.tar in $dscformat format dsc") if $debianfile;
  608. }
  609. }
  610. $newdirectory = $sourcepackage.'-'.$baseversion unless defined($newdirectory);
  611. $expectprefix = $newdirectory;
  612. $expectprefix .= '.orig' if $difffile || $debianfile;
  613. checkdiff("$dscdir/$difffile") if $difffile;
  614. print("$progname: extracting $sourcepackage in $newdirectory\n")
  615. || &syserr("write extracting message");
  616. &erasedir($newdirectory);
  617. ! -e "$expectprefix"
  618. || rename("$expectprefix","$newdirectory.tmp-keep")
  619. || &syserr("unable to rename `$expectprefix' to `$newdirectory.tmp-keep'");
  620. push @tarfiles, $debianfile if $debianfile;
  621. for my $tarfile (@tarfiles)
  622. {
  623. my $target;
  624. if ($tarfile =~ /\.orig-(\w+)\.tar/) {
  625. my $sub = $1;
  626. $sub =~ s/\d+$// if $sub =~ /\D/;
  627. $target = "$expectprefix/$sub";
  628. } elsif ($tarfile =~ /\.debian.tar/) {
  629. $target = "$expectprefix/debian";
  630. } else {
  631. $target = $expectprefix;
  632. }
  633. my $tmp = "$target.tmp-nest";
  634. (my $t = $target) =~ s!.*/!!;
  635. mkdir($tmp,0700) || &syserr("unable to create `$tmp'");
  636. system "chmod", "g-s", $tmp;
  637. print("$progname: unpacking $tarfile\n");
  638. extracttar("$dscdir/$tarfile",$tmp,$t);
  639. system "chown", '-R', '-f', join(':',@fowner), "$tmp/$t";
  640. rename("$tmp/$t",$target)
  641. || &syserr("unable to rename `$tmp/$t' to `$target'");
  642. rmdir($tmp)
  643. || &syserr("unable to remove `$tmp'");
  644. # for the first tar file:
  645. if ($tarfile eq $tarfiles[0] and !$native)
  646. {
  647. # -sp: copy the .orig.tar.gz if required
  648. if ($sourcestyle =~ /p/) {
  649. stat("$dscdir/$tarfile") ||
  650. &syserr("failed to stat `$dscdir/$tarfile' to see if need to copy");
  651. ($dsctardev,$dsctarino) = stat _;
  652. if (!stat($tarfile)) {
  653. $! == ENOENT || &syserr("failed to check destination `$tarfile'".
  654. " to see if need to copy");
  655. } else {
  656. ($dumptardev,$dumptarino) = stat _;
  657. }
  658. unless ($dumptardev == $dsctardev && $dumptarino == $dsctarino) {
  659. system('cp','--',"$dscdir/$tarfile", $tarfile);
  660. $? && subprocerr("cp $dscdir/$tarfile to $tarfile");
  661. }
  662. }
  663. # -su: keep .orig directory unpacked
  664. elsif ($sourcestyle =~ /u/ and $expectprefix ne $newdirectory) {
  665. ! -e "$newdirectory.tmp-keep"
  666. || &error("unable to keep orig directory (already exists)");
  667. system('cp','-ar','--',$expectprefix,"$newdirectory.tmp-keep");
  668. $? && subprocerr("cp $expectprefix to $newdirectory.tmp-keep");
  669. }
  670. }
  671. }
  672. my @patches;
  673. push @patches, "$dscdir/$difffile" if $difffile;
  674. if ($debianfile and -d (my $pd = "$expectprefix/debian/patches"))
  675. {
  676. my @p;
  677. opendir D, $pd;
  678. while (defined ($_ = readdir D))
  679. {
  680. # patches match same rules as run-parts
  681. next unless /^[\w-]+$/ and -f "$pd/$_";
  682. my $p = $_;
  683. checkdiff("$pd/$p");
  684. push @p, $p;
  685. }
  686. closedir D;
  687. push @patches, map "$newdirectory/debian/patches/$_", sort @p;
  688. }
  689. for $dircreate (keys %dirtocreate) {
  690. $dircreatem= "";
  691. for $dircreatep (split("/",$dirc)) {
  692. $dircreatem.= $dircreatep;
  693. if (!lstat($dircreatem)) {
  694. $! == ENOENT || &syserr("cannot stat $dircreatem");
  695. mkdir($dircreatem,0777)
  696. || &syserr("failed to create $dircreatem subdirectory");
  697. }
  698. else {
  699. -d _ || &error("diff patches file in directory `$dircreate',"
  700. ." but $dircreatem isn't a directory !");
  701. }
  702. }
  703. }
  704. if ($newdirectory ne $expectprefix)
  705. {
  706. rename($expectprefix,$newdirectory) ||
  707. &syserr("failed to rename newly-extracted $expectprefix to $newdirectory");
  708. # rename the copied .orig directory
  709. ! -e "$newdirectory.tmp-keep"
  710. || rename("$newdirectory.tmp-keep",$expectprefix)
  711. || &syserr("failed to rename saved $newdirectory.tmp-keep to $expectprefix");
  712. }
  713. for my $patch (@patches) {
  714. print("$progname: applying $patch\n");
  715. if ($patch =~ /\.(gz|bz2)$/) {
  716. &forkgzipread($patch);
  717. *DIFF = *GZIP;
  718. } else {
  719. open DIFF, $patch or &error("can't open diff `$patch'");
  720. }
  721. defined($c2= fork) || &syserr("fork for patch");
  722. if (!$c2) {
  723. open(STDIN,"<&DIFF") || &syserr("reopen gzip for patch");
  724. chdir($newdirectory) || &syserr("chdir to $newdirectory for patch");
  725. $ENV{'LC_ALL'}= 'C';
  726. $ENV{'LANG'}= 'C';
  727. exec('patch','-s','-t','-F','0','-N','-p1','-u',
  728. '-V','never','-g0','-b','-z','.dpkg-orig') or &syserr("exec patch");
  729. }
  730. close(DIFF);
  731. $c2 == waitpid($c2,0) || &syserr("wait for patch");
  732. $? && subprocerr("patch");
  733. &reapgzip if $patch =~ /\.(gz|bz2)$/;
  734. }
  735. my $now = time;
  736. for $fn (keys %filepatched) {
  737. $ftr= "$newdirectory/".substr($fn,length($expectprefix)+1);
  738. utime($now, $now, $ftr) || &syserr("cannot change timestamp for $ftr");
  739. $ftr.= ".dpkg-orig";
  740. unlink($ftr) || &syserr("remove patch backup file $ftr");
  741. }
  742. if (!(@s= lstat("$newdirectory/debian/rules"))) {
  743. $! == ENOENT || &syserr("cannot stat $newdirectory/debian/rules");
  744. &warn("$newdirectory/debian/rules does not exist");
  745. } elsif (-f _) {
  746. chmod($s[2] | 0111, "$newdirectory/debian/rules") ||
  747. &syserr("cannot make $newdirectory/debian/rules executable");
  748. } else {
  749. &warn("$newdirectory/debian/rules is not a plain file");
  750. }
  751. $execmode= 0777 & ~umask;
  752. (@s= stat('.')) || &syserr("cannot stat `.'");
  753. $dirmode= $execmode | ($s[2] & 02000);
  754. $plainmode= $execmode & ~0111;
  755. $fifomode= ($plainmode & 0222) | (($plainmode & 0222) << 1);
  756. for $fn (@filesinarchive) {
  757. $fn=~ s,^$expectprefix,$newdirectory,;
  758. (@s= lstat($fn)) || &syserr("cannot stat extracted object `$fn'");
  759. $mode= $s[2];
  760. if (-d _) {
  761. $newmode= $dirmode;
  762. } elsif (-f _) {
  763. $newmode= ($mode & 0111) ? $execmode : $plainmode;
  764. } elsif (-p _) {
  765. $newmode= $fifomode;
  766. } elsif (!-l _) {
  767. &internerr("unknown object `$fn' after extract (mode ".
  768. sprintf("0%o",$mode).")");
  769. } else { next; }
  770. next if ($mode & 07777) == $newmode;
  771. chmod($newmode,$fn) ||
  772. &syserr(sprintf("cannot change mode of `%s' to 0%o from 0%o",
  773. $fn,$newmode,$mode));
  774. }
  775. exit(0);
  776. }
  777. sub checkstats {
  778. my ($f) = @_;
  779. my @s;
  780. my $m;
  781. open(STDIN,"< $dscdir/$f") || &syserr("cannot read $dscdir/$f");
  782. (@s= stat(STDIN)) || &syserr("cannot fstat $dscdir/$f");
  783. $s[7] == $size{$f} || &error("file $f has size $s[7] instead of expected $size{$f}");
  784. $m= `md5sum`; $? && subprocerr("md5sum $f"); $m =~ s/\n$//;
  785. $m = readmd5sum( $m );
  786. $m eq $md5sum{$f} || &error("file $f has md5sum $m instead of expected $md5sum{$f}");
  787. open(STDIN,"</dev/null") || &syserr("reopen stdin from /dev/null");
  788. }
  789. sub erasedir {
  790. my ($dir) = @_;
  791. if (!lstat($dir)) {
  792. $! == ENOENT && return;
  793. &syserr("cannot stat directory $dir (before removal)");
  794. }
  795. system 'rm','-rf','--',$dir;
  796. $? && subprocerr("rm -rf $dir");
  797. if (!stat($dir)) {
  798. $! == ENOENT && return;
  799. &syserr("unable to check for removal of dir `$dir'");
  800. }
  801. &failure("rm -rf failed to remove `$dir'");
  802. }
  803. use strict 'vars';
  804. sub checktarcpio {
  805. my ($tarfileread, $wpfx) = @_;
  806. my ($tarprefix, $c2);
  807. @filesinarchive = ();
  808. # make <CPIO> read from the uncompressed archive file
  809. &forkgzipread ("$tarfileread");
  810. if (! defined ($c2 = open (CPIO,"-|"))) { &syserr ("fork for cpio"); }
  811. if (!$c2) {
  812. $ENV{'LC_ALL'}= 'C';
  813. $ENV{'LANG'}= 'C';
  814. open (STDIN,"<&GZIP") || &syserr ("reopen gzip for cpio");
  815. &cpiostderr;
  816. exec ('cpio','-0t') or &syserr ("exec cpio");
  817. }
  818. close (GZIP);
  819. $/ = "\0";
  820. while (defined ($fn = <CPIO>)) {
  821. $fn =~ s/\0$//;
  822. # store printable name of file for error messages
  823. my $pname = $fn;
  824. $pname =~ y/ -~/?/c;
  825. if ($fn =~ m/\n/) {
  826. &error ("tarfile `$tarfileread' contains object with".
  827. " newline in its name ($pname)");
  828. }
  829. next if ($fn eq '././@LongLink');
  830. if (! $tarprefix) {
  831. if ($fn =~ m/\n/) {
  832. &error("first output from cpio -0t (from `$tarfileread') ".
  833. "contains newline - you probably have an out of ".
  834. "date version of cpio. GNU cpio 2.4.2-2 is known to work");
  835. }
  836. $tarprefix = ($fn =~ m,((\./)*[^/]*)[/],)[0];
  837. # need to check for multiple dots on some operating systems
  838. # empty tarprefix (due to regex failer) will match emptry string
  839. if ($tarprefix =~ /^[.]*$/) {
  840. &error("tarfile `$tarfileread' does not extract into a ".
  841. "directory off the current directory ($tarprefix from $pname)");
  842. }
  843. }
  844. my $fprefix = substr ($fn, 0, length ($tarprefix));
  845. my $slash = substr ($fn, length ($tarprefix), 1);
  846. if ((($slash ne '/') && ($slash ne '')) || ($fprefix ne $tarprefix)) {
  847. &error ("tarfile `$tarfileread' contains object ($pname) ".
  848. "not in expected directory ($tarprefix)");
  849. }
  850. # need to check for multiple dots on some operating systems
  851. if ($fn =~ m/[.]{2,}/) {
  852. &error ("tarfile `$tarfileread' contains object with".
  853. " /../ in its name ($pname)");
  854. }
  855. push (@filesinarchive, $fn);
  856. }
  857. close (CPIO);
  858. $? && subprocerr ("cpio");
  859. &reapgzip;
  860. $/= "\n";
  861. my $tarsubst = quotemeta ($tarprefix);
  862. return $tarprefix;
  863. }
  864. sub checktarsane {
  865. my ($tarfileread, $wpfx) = @_;
  866. my ($c2);
  867. %dirincluded = ();
  868. %notfileobject = ();
  869. my $tarprefix = &checktarcpio ($tarfileread, $wpfx);
  870. # make <TAR> read from the uncompressed archive file
  871. &forkgzipread ("$tarfileread");
  872. if (! defined ($c2 = open (TAR,"-|"))) { &syserr ("fork for tar -t"); }
  873. if (! $c2) {
  874. $ENV{'LC_ALL'}= 'C';
  875. $ENV{'LANG'}= 'C';
  876. open (STDIN, "<&GZIP") || &syserr ("reopen gzip for tar -t");
  877. exec ('tar', '-vvtf', '-') or &syserr ("exec tar -vvtf -");
  878. }
  879. close (GZIP);
  880. my $efix= 0;
  881. while (<TAR>) {
  882. chomp;
  883. if (! m,^(\S{10})\s,) {
  884. &error("tarfile `$tarfileread' contains unknown object ".
  885. "listed by tar as `$_'");
  886. }
  887. my $mode = $1;
  888. $mode =~ s/^([-dpsl])// ||
  889. &error("tarfile `$tarfileread' contains object `$fn' with ".
  890. "unknown or forbidden type `".substr($_,0,1)."'");
  891. my $type = $&;
  892. if ($mode =~ /^l/) { $_ =~ s/ -> .*//; }
  893. s/ link to .+//;
  894. my @tarfields = split(' ', $_, 6);
  895. if (@tarfields < 6) {
  896. &error ("tarfile `$tarfileread' contains incomplete entry `$_'\n");
  897. }
  898. my $tarfn = deoctify ($tarfields[5]);
  899. # store printable name of file for error messages
  900. my $pname = $tarfn;
  901. $pname =~ y/ -~/?/c;
  902. # fetch name of file as given by cpio
  903. $fn = $filesinarchive[$efix++];
  904. my $l = length($fn);
  905. if (substr ($tarfn, 0, $l + 4) eq "$fn -> ") {
  906. # This is a symlink, as listed by tar. cpio doesn't
  907. # give us the targets of the symlinks, so we ignore this.
  908. $tarfn = substr($tarfn, 0, $l);
  909. }
  910. if ($tarfn ne $fn) {
  911. if ((length ($fn) == 99) && (length ($tarfn) >= 99)
  912. && (substr ($fn, 0, 99) eq substr ($tarfn, 0, 99))) {
  913. # this file doesn't match because cpio truncated the name
  914. # to the first 100 characters. let it slide for now.
  915. &warn ("filename `$pname' was truncated by cpio;" .
  916. " unable to check full pathname");
  917. # Since it didn't match, later checks will not be able
  918. # to stat this file, so we replace it with the filename
  919. # fetched from tar.
  920. $filesinarchive[$efix-1] = $tarfn;
  921. } else {
  922. &error ("tarfile `$tarfileread' contains unexpected object".
  923. " listed by tar as `$_'; expected `$pname'");
  924. }
  925. }
  926. # if cpio truncated the name above,
  927. # we still can't allow files to expand into /../
  928. # need to check for multiple dots on some operating systems
  929. if ($tarfn =~ m/[.]{2,}/) {
  930. &error ("tarfile `$tarfileread' contains object with".
  931. "/../ in its name ($pname)");
  932. }
  933. if ($tarfn =~ /\.dpkg-orig$/) {
  934. &error ("tarfile `$tarfileread' contains file with name ending in .dpkg-orig");
  935. }
  936. if ($mode =~ /[sStT]/ && $type ne 'd') {
  937. &error ("tarfile `$tarfileread' contains setuid, setgid".
  938. " or sticky object `$pname'");
  939. }
  940. if ($tarfn eq "$tarprefix/debian" && $type ne 'd') {
  941. &error ("tarfile `$tarfileread' contains object `debian'".
  942. " that isn't a directory");
  943. }
  944. if ($type eq 'd') { $tarfn =~ s,/$,,; }
  945. $tarfn =~ s,(\./)*,,;
  946. my $dirname = $tarfn;
  947. if (($dirname =~ s,/[^/]+$,,) && (! defined ($dirincluded{$dirname}))) {
  948. &warnerror ("tarfile `$tarfileread' contains object `$pname' but its containing ".
  949. "directory `$dirname' does not precede it");
  950. $dirincluded{$dirname} = 1;
  951. }
  952. if ($type eq 'd') { $dirincluded{$tarfn} = 1; }
  953. if ($type ne '-') { $notfileobject{$tarfn} = 1; }
  954. }
  955. close (TAR);
  956. $? && subprocerr ("tar -vvtf");
  957. &reapgzip;
  958. my $tarsubst = quotemeta ($tarprefix);
  959. @filesinarchive = map { s/^$tarsubst/$wpfx/; $_ } @filesinarchive;
  960. %dirincluded = map { s/^$tarsubst/$wpfx/; $_=>1 } (keys %dirincluded);
  961. %notfileobject = map { s/^$tarsubst/$wpfx/; $_=>1 } (keys %notfileobject);
  962. }
  963. no strict 'vars';
  964. # check diff for sanity, find directories to create as a side effect
  965. sub checkdiff
  966. {
  967. my $diff = shift;
  968. if ($diff =~ /\.(gz|bz2)$/) {
  969. &forkgzipread($diff);
  970. *DIFF = *GZIP;
  971. } else {
  972. open DIFF, $diff or &error("can't open diff `$diff'");
  973. }
  974. $/="\n";
  975. $_ = <DIFF>;
  976. HUNK:
  977. while (defined($_) || !eof(DIFF)) {
  978. # skip cruft leading up to patch (if any)
  979. until (/^--- /) {
  980. last HUNK unless defined ($_ = <DIFF>);
  981. }
  982. # read file header (---/+++ pair)
  983. s/\n$// or &error("diff `$diff' is missing trailing newline");
  984. s/^--- // or &error("expected ^--- in line $. of diff `$diff'");
  985. s/\t.*//;
  986. $_ eq '/dev/null' or s!^(\./)?[^/]+/!$expectprefix/! or
  987. &error("diff `$diff' patches file with no subdirectory");
  988. /\.dpkg-orig$/ and
  989. &error("diff `$diff' patches file with name ending .dpkg-orig");
  990. $fn = $_;
  991. (defined($_= <DIFF>) and s/\n$//) or
  992. &error("diff `$diff' finishes in middle of ---/+++ (line $.)");
  993. s/\t.*//;
  994. (s/^\+\+\+ // and s!^(\./)?[^/]+/!!)
  995. or &error("line after --- isn't as expected in diff `$diff' (line $.)");
  996. if ($fn eq '/dev/null') {
  997. $fn = "$expectprefix/$_";
  998. } else {
  999. $_ eq substr($fn, length($expectprefix)+1)
  1000. or &error("line after --- isn't as expected in diff `$diff' (line $.)");
  1001. }
  1002. $dirname = $fn;
  1003. if ($dirname =~ s,/[^/]+$,, && !defined($dirincluded{$dirname})) {
  1004. $dirtocreate{$dirname} = 1;
  1005. }
  1006. defined($notfileobject{$fn}) &&
  1007. &error("diff `$diff' patches something which is not a plain file");
  1008. $filepatched{$fn} eq $diff && &error("diff patches file $fn twice");
  1009. $filepatched{$fn} = $diff;
  1010. # read hunks
  1011. my $hunk = 0;
  1012. while (defined($_ = <DIFF>) && !(/^--- / or /^Index:/)) {
  1013. # read hunk header (@@)
  1014. s/\n$// or &error("diff `$diff' is missing trailing newline");
  1015. next if /^\\ No newline/;
  1016. /^@@ -\d+(,(\d+))? \+\d+(,(\d+))? @\@( .*)?$/ or
  1017. &error("Expected ^\@\@ in line $. of diff `$diff'");
  1018. my ($olines, $nlines) = ($1 ? $2 : 1, $3 ? $4 : 1);
  1019. ++$hunk;
  1020. # read hunk
  1021. while ($olines || $nlines) {
  1022. defined($_ = <DIFF>) or &error("unexpected end of diff `$diff'");
  1023. s/\n$// or &error("diff `$diff' is missing trailing newline");
  1024. next if /^\\ No newline/;
  1025. if (/^ /) { --$olines; --$nlines; }
  1026. elsif (/^-/) { --$olines; }
  1027. elsif (/^\+/) { --$nlines; }
  1028. else { &error("expected [ +-] at start of line $. of diff `$diff'"); }
  1029. }
  1030. }
  1031. $hunk or &error("expected ^\@\@ at line $. of diff `$diff'");
  1032. }
  1033. close(DIFF);
  1034. &reapgzip if $diff =~ /\.(gz|bz2)$/;
  1035. }
  1036. sub extracttar {
  1037. my ($tarfileread,$dirchdir,$newtopdir) = @_;
  1038. &forkgzipread("$tarfileread");
  1039. defined($c2= fork) || &syserr("fork for tar -xkf -");
  1040. if (!$c2) {
  1041. open(STDIN,"<&GZIP") || &syserr("reopen gzip for tar -xkf -");
  1042. &cpiostderr;
  1043. chdir($dirchdir) || &syserr("cannot chdir to `$dirchdir' for tar extract");
  1044. exec('tar','-xkf','-') or &syserr("exec tar -xkf -");
  1045. }
  1046. close(GZIP);
  1047. $c2 == waitpid($c2,0) || &syserr("wait for tar -xkf -");
  1048. $? && subprocerr("tar -xkf -");
  1049. &reapgzip;
  1050. opendir(D,"$dirchdir") || &syserr("Unable to open dir $dirchdir");
  1051. @dirchdirfiles = grep($_ ne "." && $_ ne "..",readdir(D));
  1052. closedir(D) || &syserr("Unable to close dir $dirchdir");
  1053. if (@dirchdirfiles==1 && -d "$dirchdir/$dirchdirfiles[0]") {
  1054. rename("$dirchdir/$dirchdirfiles[0]", "$dirchdir/$newtopdir") ||
  1055. &syserr("Unable to rename $dirchdir/$dirchdirfiles[0] to ".
  1056. "$dirchdir/$newtopdir");
  1057. } else {
  1058. mkdir("$dirchdir/$newtopdir.tmp", 0777) or
  1059. &syserr("Unable to mkdir $dirchdir/$newtopdir.tmp");
  1060. for (@dirchdirfiles) {
  1061. rename("$dirchdir/$_", "$dirchdir/$newtopdir.tmp/$_") or
  1062. &syserr("Unable to rename $dirchdir/$_ to ".
  1063. "$dirchdir/$newtopdir.tmp/$_");
  1064. }
  1065. rename("$dirchdir/$newtopdir.tmp", "$dirchdir/$newtopdir") or
  1066. &syserr("Unable to rename $dirchdir/$newtopdir.tmp to $dirchdir/$newtopdir");
  1067. }
  1068. }
  1069. sub cpiostderr {
  1070. open(STDERR,"| grep -E -v '^[0-9]+ blocks\$' >&2") ||
  1071. &syserr("reopen stderr for tar to grep out blocks message");
  1072. }
  1073. sub checktype {
  1074. if (!lstat("$origdir/$fn")) {
  1075. &unrepdiff2("nonexistent",$type{$fn});
  1076. } else {
  1077. $v= eval("$_[0] _ ? 2 : 1"); $v || &internerr("checktype $@ ($_[0])");
  1078. return 1 if $v == 2;
  1079. &unrepdiff2("something else",$type{$fn});
  1080. }
  1081. return 0;
  1082. }
  1083. sub setopmode {
  1084. defined($opmode) && &usageerr("only one of -x or -b allowed, and only once");
  1085. $opmode= $_[0];
  1086. }
  1087. sub unrepdiff {
  1088. print(STDERR "$progname: cannot represent change to $fn: $_[0]\n")
  1089. || &syserr("write syserr unrep");
  1090. $ur++;
  1091. }
  1092. sub unrepdiff2 {
  1093. print(STDERR "$progname: cannot represent change to $fn:\n".
  1094. "$progname: new version is $_[1]\n".
  1095. "$progname: old version is $_[0]\n")
  1096. || &syserr("write syserr unrep");
  1097. $ur++;
  1098. }
  1099. sub forkgzipwrite {
  1100. open(GZIPFILE,"> $_[0]") || &syserr("create file $_[0]");
  1101. pipe(GZIPREAD,GZIP) || &syserr("pipe for gzip");
  1102. defined($cgz= fork) || &syserr("fork for gzip");
  1103. if (!$cgz) {
  1104. open(STDIN,"<&GZIPREAD") || &syserr("reopen gzip pipe"); close(GZIPREAD);
  1105. close(GZIP); open(STDOUT,">&GZIPFILE") || &syserr("reopen tar.gz");
  1106. exec('gzip','-9') or &syserr("exec gzip");
  1107. }
  1108. close(GZIPREAD);
  1109. $gzipsigpipeok= 0;
  1110. }
  1111. sub forkgzipread {
  1112. local $SIG{PIPE} = 'DEFAULT';
  1113. my $prog = $_[0] =~ /\.gz$/ ? 'gunzip' : 'bunzip2';
  1114. open(GZIPFILE,"< $_[0]") || &syserr("read file $_[0]");
  1115. pipe(GZIP,GZIPWRITE) || &syserr("pipe for $prog");
  1116. defined($cgz= fork) || &syserr("fork for $prog");
  1117. if (!$cgz) {
  1118. open(STDOUT,">&GZIPWRITE") || &syserr("reopen $prog pipe"); close(GZIPWRITE);
  1119. close(GZIP); open(STDIN,"<&GZIPFILE") || &syserr("reopen input file");
  1120. exec($prog) or &syserr("exec $prog");
  1121. }
  1122. close(GZIPWRITE);
  1123. $gzipsigpipeok= 1;
  1124. }
  1125. sub reapgzip {
  1126. $cgz == waitpid($cgz,0) || &syserr("wait for gzip");
  1127. !$? || ($gzipsigpipeok && WIFSIGNALED($?) && WTERMSIG($?)==SIGPIPE) ||
  1128. subprocerr("gzip");
  1129. close(GZIPFILE);
  1130. }
  1131. my %added_files;
  1132. sub addfile {
  1133. my ($filename)= @_;
  1134. $added_files{$filename}++ &&
  1135. &internerr( "tried to add file `$filename' twice" );
  1136. stat($filename) || &syserr("could not stat output file `$filename'");
  1137. $size= (stat _)[7];
  1138. my $md5sum= `md5sum <$filename`;
  1139. $? && &subprocerr("md5sum $filename");
  1140. $md5sum = readmd5sum( $md5sum );
  1141. $f{'Files'}.= "\n $md5sum $size $filename";
  1142. }
  1143. # replace \ddd with their corresponding character, refuse \ddd > \377
  1144. # modifies $_ (hs)
  1145. {
  1146. my $backslash;
  1147. sub deoctify {
  1148. my $fn= $_[0];
  1149. $backslash= sprintf("\\%03o", unpack("C", "\\")) if !$backslash;
  1150. s/\\{2}/$backslash/g;
  1151. @_= split(/\\/, $fn);
  1152. foreach (@_) {
  1153. /^(\d{3})/ or next;
  1154. &failure("bogus character `\\$1' in `$fn'\n") if oct($1) > 255;
  1155. $_= pack("c", oct($1)) . $';
  1156. }
  1157. return join("", @_);
  1158. } }