dpkg-source.pl 44 KB

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