dpkg-source.pl 43 KB

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