dpkg-source.pl 44 KB

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