dpkg-source.pl 43 KB

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