dpkg-source.pl 45 KB

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