dpkg-source.pl 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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,0700) || &syserr("unable to create `$tmp'");
  574. system "chmod", "g-s", $tmp;
  575. print("$progname: unpacking $tarfile\n");
  576. extracttar("$dscdir/$tarfile",$tmp,$t);
  577. system "chown", '-R', '-f', join(':',@fowner), "$tmp/$t";
  578. rename("$tmp/$t",$target)
  579. || &syserr("unable to rename `$tmp/$t' to `$target'");
  580. rmdir($tmp)
  581. || &syserr("unable to remove `$tmp'");
  582. # for the first tar file:
  583. if ($tarfile eq $tarfiles[0] and !$native)
  584. {
  585. # -sp: copy the .orig.tar.gz if required
  586. if ($sourcestyle =~ /p/) {
  587. stat("$dscdir/$tarfile") ||
  588. &syserr("failed to stat `$dscdir/$tarfile' to see if need to copy");
  589. ($dsctardev,$dsctarino) = stat _;
  590. if (!stat($tarfile)) {
  591. $! == ENOENT || &syserr("failed to check destination `$tarfile'".
  592. " to see if need to copy");
  593. } else {
  594. ($dumptardev,$dumptarino) = stat _;
  595. }
  596. unless ($dumptardev == $dsctardev && $dumptarino == $dsctarino) {
  597. system('cp','--',"$dscdir/$tarfile", $tarfile);
  598. $? && subprocerr("cp $dscdir/$tarfile to $tarfile");
  599. }
  600. }
  601. # -su: keep .orig directory unpacked
  602. elsif ($sourcestyle =~ /u/ and $expectprefix ne $newdirectory) {
  603. ! -e "$newdirectory.tmp-keep"
  604. || &error("unable to keep orig directory (already exists)");
  605. system('cp','-ar','--',$expectprefix,"$newdirectory.tmp-keep");
  606. $? && subprocerr("cp $expectprefix to $newdirectory.tmp-keep");
  607. }
  608. }
  609. }
  610. my @patches;
  611. push @patches, "$dscdir/$difffile" if $difffile;
  612. if ($debianfile and -d (my $pd = "$expectprefix/debian/patches"))
  613. {
  614. my @p;
  615. opendir D, $pd;
  616. while (defined ($_ = readdir D))
  617. {
  618. # patches match same rules as run-parts
  619. next unless /^[\w-]+$/ and -f "$pd/$_";
  620. my $p = $_;
  621. checkdiff("$pd/$p");
  622. push @p, $p;
  623. }
  624. closedir D;
  625. push @patches, map "$newdirectory/debian/patches/$_", sort @p;
  626. }
  627. for $dircreate (keys %dirtocreate) {
  628. $dircreatem= "";
  629. for $dircreatep (split("/",$dirc)) {
  630. $dircreatem.= $dircreatep;
  631. if (!lstat($dircreatem)) {
  632. $! == ENOENT || &syserr("cannot stat $dircreatem");
  633. mkdir($dircreatem,0777)
  634. || &syserr("failed to create $dircreatem subdirectory");
  635. }
  636. else {
  637. -d _ || &error("diff patches file in directory `$dircreate',"
  638. ." but $dircreatem isn't a directory !");
  639. }
  640. }
  641. }
  642. if ($newdirectory ne $expectprefix)
  643. {
  644. rename($expectprefix,$newdirectory) ||
  645. &syserr("failed to rename newly-extracted $expectprefix to $newdirectory");
  646. # rename the copied .orig directory
  647. ! -e "$newdirectory.tmp-keep"
  648. || rename("$newdirectory.tmp-keep",$expectprefix)
  649. || &syserr("failed to rename saved $newdirectory.tmp-keep to $expectprefix");
  650. }
  651. for my $patch (@patches) {
  652. print("$progname: applying $patch\n");
  653. if ($patch =~ /\.(gz|bz2)$/) {
  654. &forkgzipread($patch);
  655. *DIFF = *GZIP;
  656. } else {
  657. open DIFF, $patch or &error("can't open diff `$patch'");
  658. }
  659. defined($c2= fork) || &syserr("fork for patch");
  660. if (!$c2) {
  661. open(STDIN,"<&DIFF") || &syserr("reopen gzip for patch");
  662. chdir($newdirectory) || &syserr("chdir to $newdirectory for patch");
  663. $ENV{'LC_ALL'}= 'C';
  664. $ENV{'LANG'}= 'C';
  665. exec('patch','-s','-t','-F','0','-N','-p1','-u',
  666. '-V','never','-g0','-b','-z','.dpkg-orig') or &syserr("exec patch");
  667. }
  668. close(DIFF);
  669. $c2 == waitpid($c2,0) || &syserr("wait for patch");
  670. $? && subprocerr("patch");
  671. &reapgzip if $patch =~ /\.(gz|bz2)$/;
  672. }
  673. for $fn (keys %filepatched) {
  674. $ftr= "$newdirectory/".substr($fn,length($expectprefix)+1).".dpkg-orig";
  675. unlink($ftr) || &syserr("remove patch backup file $ftr");
  676. }
  677. if (!(@s= lstat("$newdirectory/debian/rules"))) {
  678. $! == ENOENT || &syserr("cannot stat $newdirectory/debian/rules");
  679. &warn("$newdirectory/debian/rules does not exist");
  680. } elsif (-f _) {
  681. chmod($s[2] | 0111, "$newdirectory/debian/rules") ||
  682. &syserr("cannot make $newdirectory/debian/rules executable");
  683. } else {
  684. &warn("$newdirectory/debian/rules is not a plain file");
  685. }
  686. $execmode= 0777 & ~umask;
  687. (@s= stat('.')) || &syserr("cannot stat `.'");
  688. $dirmode= $execmode | ($s[2] & 02000);
  689. $plainmode= $execmode & ~0111;
  690. $fifomode= ($plainmode & 0222) | (($plainmode & 0222) << 1);
  691. for $fn (@filesinarchive) {
  692. $fn=~ s,^$expectprefix,$newdirectory,;
  693. (@s= lstat($fn)) || &syserr("cannot stat extracted object `$fn'");
  694. $mode= $s[2];
  695. if (-d _) {
  696. $newmode= $dirmode;
  697. } elsif (-f _) {
  698. $newmode= ($mode & 0111) ? $execmode : $plainmode;
  699. } elsif (-p _) {
  700. $newmode= $fifomode;
  701. } elsif (!-l _) {
  702. &internerr("unknown object `$fn' after extract (mode ".
  703. sprintf("0%o",$mode).")");
  704. } else { next; }
  705. next if ($mode & 07777) == $newmode;
  706. chmod($newmode,$fn) ||
  707. &syserr(sprintf("cannot change mode of `%s' to 0%o from 0%o",
  708. $fn,$newmode,$mode));
  709. }
  710. exit(0);
  711. }
  712. sub checkstats {
  713. my ($f) = @_;
  714. my @s;
  715. my $m;
  716. open(STDIN,"< $dscdir/$f") || &syserr("cannot read $dscdir/$f");
  717. (@s= stat(STDIN)) || &syserr("cannot fstat $dscdir/$f");
  718. $s[7] == $size{$f} || &error("file $f has size $s[7] instead of expected $size{$f}");
  719. $m= `md5sum`; $? && subprocerr("md5sum $f"); $m =~ s/\n$//;
  720. $m =~ s/ *-$//; # Remove trailing spaces and -, to work with GNU md5sum
  721. $m =~ m/^[0-9a-f]{32}$/ || &failure("md5sum of $f gave bad output `$m'");
  722. $m eq $md5sum{$f} || &error("file $f has md5sum $m instead of expected $md5sum{$f}");
  723. open(STDIN,"</dev/null") || &syserr("reopen stdin from /dev/null");
  724. }
  725. sub erasedir {
  726. my ($dir) = @_;
  727. if (!lstat($dir)) {
  728. $! == ENOENT && return;
  729. &syserr("cannot stat directory $dir (before removal)");
  730. }
  731. system 'rm','-rf','--',$dir;
  732. $? && subprocerr("rm -rf $dir");
  733. if (!stat($dir)) {
  734. $! == ENOENT && return;
  735. &syserr("unable to check for removal of dir `$dir'");
  736. }
  737. &failure("rm -rf failed to remove `$dir'");
  738. }
  739. use strict 'vars';
  740. sub checktarcpio {
  741. my ($tarfileread, $wpfx) = @_;
  742. my ($tarprefix, $c2);
  743. @filesinarchive = ();
  744. # make <CPIO> read from the uncompressed archive file
  745. &forkgzipread ("$tarfileread");
  746. if (! defined ($c2 = open (CPIO,"-|"))) { &syserr ("fork for cpio"); }
  747. if (!$c2) {
  748. $ENV{'LC_ALL'}= 'C';
  749. $ENV{'LANG'}= 'C';
  750. open (STDIN,"<&GZIP") || &syserr ("reopen gzip for cpio");
  751. &cpiostderr;
  752. exec ('cpio','-0t') or &syserr ("exec cpio");
  753. }
  754. close (GZIP);
  755. $/ = "\0";
  756. while (defined ($fn = <CPIO>)) {
  757. $fn =~ s/\0$//;
  758. # store printable name of file for error messages
  759. my $pname = $fn;
  760. $pname =~ y/ -~/?/c;
  761. if ($fn =~ m/\n/) {
  762. &error ("tarfile `$tarfileread' contains object with".
  763. " newline in its name ($pname)");
  764. }
  765. next if ($fn eq '././@LongLink');
  766. if (! $tarprefix) {
  767. if ($fn =~ m/\n/) {
  768. &error("first output from cpio -0t (from `$tarfileread') ".
  769. "contains newline - you probably have an out of ".
  770. "date version of cpio. GNU cpio 2.4.2-2 is known to work");
  771. }
  772. $tarprefix = ($fn =~ m,((\./)*[^/]*)[/],)[0];
  773. # need to check for multiple dots on some operating systems
  774. # empty tarprefix (due to regex failer) will match emptry string
  775. if ($tarprefix =~ /^[.]*$/) {
  776. &error("tarfile `$tarfileread' does not extract into a ".
  777. "directory off the current directory ($tarprefix from $pname)");
  778. }
  779. }
  780. my $fprefix = substr ($fn, 0, length ($tarprefix));
  781. my $slash = substr ($fn, length ($tarprefix), 1);
  782. if ((($slash ne '/') && ($slash ne '')) || ($fprefix ne $tarprefix)) {
  783. &error ("tarfile `$tarfileread' contains object ($pname) ".
  784. "not in expected directory ($tarprefix)");
  785. }
  786. # need to check for multiple dots on some operating systems
  787. if ($fn =~ m/[.]{2,}/) {
  788. &error ("tarfile `$tarfileread' contains object with".
  789. " /../ in its name ($pname)");
  790. }
  791. push (@filesinarchive, $fn);
  792. }
  793. close (CPIO);
  794. $? && subprocerr ("cpio");
  795. &reapgzip;
  796. $/= "\n";
  797. my $tarsubst = quotemeta ($tarprefix);
  798. return $tarprefix;
  799. }
  800. sub checktarsane {
  801. my ($tarfileread, $wpfx) = @_;
  802. my ($c2);
  803. %dirincluded = ();
  804. %notfileobject = ();
  805. my $tarprefix = &checktarcpio ($tarfileread, $wpfx);
  806. # make <TAR> read from the uncompressed archive file
  807. &forkgzipread ("$tarfileread");
  808. if (! defined ($c2 = open (TAR,"-|"))) { &syserr ("fork for tar -t"); }
  809. if (! $c2) {
  810. $ENV{'LC_ALL'}= 'C';
  811. $ENV{'LANG'}= 'C';
  812. open (STDIN, "<&GZIP") || &syserr ("reopen gzip for tar -t");
  813. exec ('tar', '-vvtf', '-') or &syserr ("exec tar -vvtf -");
  814. }
  815. close (GZIP);
  816. my $efix= 0;
  817. while (<TAR>) {
  818. chomp;
  819. if (! m,^(\S{10})\s,) {
  820. &error("tarfile `$tarfileread' contains unknown object ".
  821. "listed by tar as `$_'");
  822. }
  823. my $mode = $1;
  824. $mode =~ s/^([-dpsl])// ||
  825. &error("tarfile `$tarfileread' contains object `$fn' with ".
  826. "unknown or forbidden type `".substr($_,0,1)."'");
  827. my $type = $&;
  828. if ($mode =~ /^l/) { $_ =~ s/ -> .*//; }
  829. s/ link to .+//;
  830. my @tarfields = split(' ', $_, 6);
  831. if (@tarfields < 6) {
  832. &error ("tarfile `$tarfileread' contains incomplete entry `$_'\n");
  833. }
  834. my $tarfn = deoctify ($tarfields[5]);
  835. # store printable name of file for error messages
  836. my $pname = $tarfn;
  837. $pname =~ y/ -~/?/c;
  838. # fetch name of file as given by cpio
  839. $fn = $filesinarchive[$efix++];
  840. my $l = length($fn);
  841. if (substr ($tarfn, 0, $l + 4) eq "$fn -> ") {
  842. # This is a symlink, as listed by tar. cpio doesn't
  843. # give us the targets of the symlinks, so we ignore this.
  844. $tarfn = substr($tarfn, 0, $l);
  845. }
  846. if ($tarfn ne $fn) {
  847. if ((length ($fn) == 99) && (length ($tarfn) >= 99)
  848. && (substr ($fn, 0, 99) eq substr ($tarfn, 0, 99))) {
  849. # this file doesn't match because cpio truncated the name
  850. # to the first 100 characters. let it slide for now.
  851. &warn ("filename `$pname' was truncated by cpio;" .
  852. " unable to check full pathname");
  853. # Since it didn't match, later checks will not be able
  854. # to stat this file, so we replace it with the filename
  855. # fetched from tar.
  856. $filesinarchive[$efix-1] = $tarfn;
  857. } else {
  858. &error ("tarfile `$tarfileread' contains unexpected object".
  859. " listed by tar as `$_'; expected `$pname'");
  860. }
  861. }
  862. # if cpio truncated the name above,
  863. # we still can't allow files to expand into /../
  864. # need to check for multiple dots on some operating systems
  865. if ($tarfn =~ m/[.]{2,}/) {
  866. &error ("tarfile `$tarfileread' contains object with".
  867. "/../ in its name ($pname)");
  868. }
  869. if ($tarfn =~ /\.dpkg-orig$/) {
  870. &error ("tarfile `$tarfileread' contains file with name ending in .dpkg-orig");
  871. }
  872. if ($mode =~ /[sStT]/ && $type ne 'd') {
  873. &error ("tarfile `$tarfileread' contains setuid, setgid".
  874. " or sticky object `$pname'");
  875. }
  876. if ($tarfn eq "$tarprefix/debian" && $type ne 'd') {
  877. &error ("tarfile `$tarfileread' contains object `debian'".
  878. " that isn't a directory");
  879. }
  880. if ($type eq 'd') { $tarfn =~ s,/$,,; }
  881. $tarfn =~ s,(\./)*,,;
  882. my $dirname = $tarfn;
  883. if (($dirname =~ s,/[^/]+$,,) && (! defined ($dirincluded{$dirname}))) {
  884. &warnerror ("tarfile `$tarfileread' contains object `$pname' but its containing ".
  885. "directory `$dirname' does not precede it");
  886. $dirincluded{$dirname} = 1;
  887. }
  888. if ($type eq 'd') { $dirincluded{$tarfn} = 1; }
  889. if ($type ne '-') { $notfileobject{$tarfn} = 1; }
  890. }
  891. close (TAR);
  892. $? && subprocerr ("tar -vvtf");
  893. &reapgzip;
  894. my $tarsubst = quotemeta ($tarprefix);
  895. @filesinarchive = map { s/^$tarsubst/$wpfx/; $_ } @filesinarchive;
  896. %dirincluded = map { s/^$tarsubst/$wpfx/; $_=>1 } (keys %dirincluded);
  897. %notfileobject = map { s/^$tarsubst/$wpfx/; $_=>1 } (keys %notfileobject);
  898. }
  899. no strict 'vars';
  900. # check diff for sanity, find directories to create as a side effect
  901. sub checkdiff
  902. {
  903. my $diff = shift;
  904. if ($diff =~ /\.(gz|bz2)$/) {
  905. &forkgzipread($diff);
  906. *DIFF = *GZIP;
  907. } else {
  908. open DIFF, $diff or &error("can't open diff `$diff'");
  909. }
  910. $/="\n";
  911. $_ = <DIFF>;
  912. HUNK:
  913. while (defined($_) || !eof(DIFF)) {
  914. # skip cruft leading up to patch (if any)
  915. until (/^--- /) {
  916. last HUNK unless defined ($_ = <DIFF>);
  917. }
  918. # read file header (---/+++ pair)
  919. s/\n$// or &error("diff `$diff' is missing trailing newline");
  920. s/^--- // or &error("expected ^--- in line $. of diff `$diff'");
  921. s/\t.*//;
  922. $_ eq '/dev/null' or s!^(\./)?[^/]+/!$expectprefix/! or
  923. &error("diff `$diff' patches file with no subdirectory");
  924. /\.dpkg-orig$/ and
  925. &error("diff `$diff' patches file with name ending .dpkg-orig");
  926. $fn = $_;
  927. (defined($_= <DIFF>) and s/\n$//) or
  928. &error("diff `$diff' finishes in middle of ---/+++ (line $.)");
  929. s/\t.*//;
  930. (s/^\+\+\+ // and s!^(\./)?[^/]+/!!)
  931. or &error("line after --- isn't as expected in diff `$diff' (line $.)");
  932. if ($fn eq '/dev/null') {
  933. $fn = "$expectprefix/$_";
  934. } else {
  935. $_ eq substr($fn, length($expectprefix)+1)
  936. or &error("line after --- isn't as expected in diff `$diff' (line $.)");
  937. }
  938. $dirname = $fn;
  939. if ($dirname =~ s,/[^/]+$,, && !defined($dirincluded{$dirname})) {
  940. $dirtocreate{$dirname} = 1;
  941. }
  942. defined($notfileobject{$fn}) &&
  943. &error("diff `$diff' patches something which is not a plain file");
  944. $filepatched{$fn} eq $diff && &error("diff patches file $fn twice");
  945. $filepatched{$fn} = $diff;
  946. # read hunks
  947. my $hunk = 0;
  948. while (defined($_ = <DIFF>) && !(/^--- / or /^Index:/)) {
  949. # read hunk header (@@)
  950. s/\n$// or &error("diff `$diff' is missing trailing newline");
  951. next if /^\\ No newline/;
  952. /^@@ -\d+(,(\d+))? \+\d+(,(\d+))? @\@$/ or
  953. &error("Expected ^\@\@ in line $. of diff `$diff'");
  954. my ($olines, $nlines) = ($1 ? $2 : 1, $3 ? $4 : 1);
  955. ++$hunk;
  956. # read hunk
  957. while ($olines || $nlines) {
  958. defined($_ = <DIFF>) or &error("unexpected end of diff `$diff'");
  959. s/\n$// or &error("diff `$diff' is missing trailing newline");
  960. next if /^\\ No newline/;
  961. if (/^ /) { --$olines; --$nlines; }
  962. elsif (/^-/) { --$olines; }
  963. elsif (/^\+/) { --$nlines; }
  964. else { &error("expected [ +-] at start of line $. of diff `$diff'"); }
  965. }
  966. }
  967. $hunk or &error("expected ^\@\@ at line $. of diff `$diff'");
  968. }
  969. close(DIFF);
  970. &reapgzip if $diff =~ /\.(gz|bz2)$/;
  971. }
  972. sub extracttar {
  973. my ($tarfileread,$dirchdir,$newtopdir) = @_;
  974. &forkgzipread("$tarfileread");
  975. defined($c2= fork) || &syserr("fork for tar -xkf -");
  976. if (!$c2) {
  977. open(STDIN,"<&GZIP") || &syserr("reopen gzip for tar -xkf -");
  978. &cpiostderr;
  979. chdir($dirchdir) || &syserr("cannot chdir to `$dirchdir' for tar extract");
  980. exec('tar','-xkf','-') or &syserr("exec tar -xkf -");
  981. }
  982. close(GZIP);
  983. $c2 == waitpid($c2,0) || &syserr("wait for tar -xkf -");
  984. $? && subprocerr("tar -xkf -");
  985. &reapgzip;
  986. opendir(D,"$dirchdir") || &syserr("Unable to open dir $dirchdir");
  987. @dirchdirfiles = grep($_ ne "." && $_ ne "..",readdir(D));
  988. closedir(D) || &syserr("Unable to close dir $dirchdir");
  989. if (@dirchdirfiles==1 && -d "$dirchdir/$dirchdirfiles[0]") {
  990. rename("$dirchdir/$dirchdirfiles[0]", "$dirchdir/$newtopdir") ||
  991. &syserr("Unable to rename $dirchdir/$dirchdirfiles[0] to ".
  992. "$dirchdir/$newtopdir");
  993. } else {
  994. mkdir("$dirchdir/$newtopdir.tmp", 0777) or
  995. &syserr("Unable to mkdir $dirchdir/$newtopdir.tmp");
  996. for (@dirchdirfiles) {
  997. rename("$dirchdir/$_", "$dirchdir/$newtopdir.tmp/$_") or
  998. &syserr("Unable to rename $dirchdir/$_ to ".
  999. "$dirchdir/$newtopdir.tmp/$_");
  1000. }
  1001. rename("$dirchdir/$newtopdir.tmp", "$dirchdir/$newtopdir") or
  1002. &syserr("Unable to rename $dirchdir/$newtopdir.tmp to $dirchdir/$newtopdir");
  1003. }
  1004. }
  1005. sub cpiostderr {
  1006. open(STDERR,"| grep -E -v '^[0-9]+ blocks\$' >&2") ||
  1007. &syserr("reopen stderr for tar to grep out blocks message");
  1008. }
  1009. sub checktype {
  1010. if (!lstat("$origdir/$fn")) {
  1011. &unrepdiff2("nonexistent",$type{$fn});
  1012. } else {
  1013. $v= eval("$_[0] _ ? 2 : 1"); $v || &internerr("checktype $@ ($_[0])");
  1014. return 1 if $v == 2;
  1015. &unrepdiff2("something else",$type{$fn});
  1016. }
  1017. return 0;
  1018. }
  1019. sub setopmode {
  1020. defined($opmode) && &usageerr("only one of -x or -b allowed, and only once");
  1021. $opmode= $_[0];
  1022. }
  1023. sub unrepdiff {
  1024. print(STDERR "$progname: cannot represent change to $fn: $_[0]\n")
  1025. || &syserr("write syserr unrep");
  1026. $ur++;
  1027. }
  1028. sub unrepdiff2 {
  1029. print(STDERR "$progname: cannot represent change to $fn:\n".
  1030. "$progname: new version is $_[1]\n".
  1031. "$progname: old version is $_[0]\n")
  1032. || &syserr("write syserr unrep");
  1033. $ur++;
  1034. }
  1035. sub forkgzipwrite {
  1036. open(GZIPFILE,"> $_[0]") || &syserr("create file $_[0]");
  1037. pipe(GZIPREAD,GZIP) || &syserr("pipe for gzip");
  1038. defined($cgz= fork) || &syserr("fork for gzip");
  1039. if (!$cgz) {
  1040. open(STDIN,"<&GZIPREAD") || &syserr("reopen gzip pipe"); close(GZIPREAD);
  1041. close(GZIP); open(STDOUT,">&GZIPFILE") || &syserr("reopen tar.gz");
  1042. exec('gzip','-9') or &syserr("exec gzip");
  1043. }
  1044. close(GZIPREAD);
  1045. $gzipsigpipeok= 0;
  1046. }
  1047. sub forkgzipread {
  1048. local $SIG{PIPE} = 'DEFAULT';
  1049. my $prog = $_[0] =~ /\.gz$/ ? 'gunzip' : 'bunzip2';
  1050. open(GZIPFILE,"< $_[0]") || &syserr("read file $_[0]");
  1051. pipe(GZIP,GZIPWRITE) || &syserr("pipe for $prog");
  1052. defined($cgz= fork) || &syserr("fork for $prog");
  1053. if (!$cgz) {
  1054. open(STDOUT,">&GZIPWRITE") || &syserr("reopen $prog pipe"); close(GZIPWRITE);
  1055. close(GZIP); open(STDIN,"<&GZIPFILE") || &syserr("reopen input file");
  1056. exec($prog) or &syserr("exec $prog");
  1057. }
  1058. close(GZIPWRITE);
  1059. $gzipsigpipeok= 1;
  1060. }
  1061. sub reapgzip {
  1062. $cgz == waitpid($cgz,0) || &syserr("wait for gzip");
  1063. !$? || ($gzipsigpipeok && WIFSIGNALED($?) && WTERMSIG($?)==SIGPIPE) ||
  1064. subprocerr("gzip");
  1065. close(GZIPFILE);
  1066. }
  1067. sub addfile {
  1068. my ($filename)= @_;
  1069. stat($filename) || &syserr("could not stat output file `$filename'");
  1070. $size= (stat _)[7];
  1071. my $md5sum= `md5sum <$filename`;
  1072. $? && &subprocerr("md5sum $filename");
  1073. $md5sum =~ s/^([0-9a-f]{32})\s*-?\s*\n$/$1/ || &failure("md5sum gave bogus output `$_'");
  1074. $f{'Files'}.= "\n $md5sum $size $filename";
  1075. }
  1076. # replace \ddd with their corresponding character, refuse \ddd > \377
  1077. # modifies $_ (hs)
  1078. {
  1079. my $backslash;
  1080. sub deoctify {
  1081. my $fn= $_[0];
  1082. $backslash= sprintf("\\%03o", unpack("C", "\\")) if !$backslash;
  1083. s/\\{2}/$backslash/g;
  1084. @_= split(/\\/, $fn);
  1085. foreach (@_) {
  1086. /^(\d{3})/ or next;
  1087. &failure("bogus character `\\$1' in `$fn'\n") if oct($1) > 255;
  1088. $_= pack("c", oct($1)) . $';
  1089. }
  1090. return join("", @_);
  1091. } }