dpkg-source.pl 44 KB

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