dpkg-source.pl 44 KB

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