dpkg-source.pl 45 KB

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