dpkg-source.pl 42 KB

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