dpkg-source.pl 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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. $sourcestyle = 'X';
  9. use POSIX;
  10. use POSIX qw (:errno_h :signal_h);
  11. use strict 'refs';
  12. push (@INC, $dpkglibdir);
  13. require 'controllib.pl';
  14. sub usageversion {
  15. print STDERR
  16. "Debian GNU/Linux dpkg-source $version. Copyright (C) 1996
  17. Ian Jackson and Klee Dienes. This is free software; see the GNU
  18. General Public Licence version 2 or later for copying conditions.
  19. There is NO warranty.
  20. Usage: dpkg-source -x <filename>.dsc
  21. dpkg-source -b <directory> [<orig-directory>|<orig-targz>|\'\']
  22. Build options: -c<controlfile> get control info from this file
  23. -l<changelogfile> get per-version info from this file
  24. -F<changelogformat> force change log format
  25. -V<name>=<value> set a substitution variable
  26. -T<varlistfile> read variables here, not debian/substvars
  27. -D<field>=<value> override or add a .dsc field and value
  28. -U<field> remove a field
  29. -sa auto select orig source (-sA is default)
  30. -sk use packed orig source (unpack & keep)
  31. -sp use packed orig source (unpack & remove)
  32. -su use unpacked orig source (pack & keep)
  33. -sr use unpacked orig source (pack & remove)
  34. -ss trust packed & unpacked orig src are same
  35. -sn there is no diff, do main tarfile only
  36. -sA,-sK,-sP,-sU,-sR like -sa,-sp,-sk,-su,-sr but may overwrite
  37. Extract options: -sp (default) leave orig source packed in current dir
  38. -sn do not copy original source to current dir
  39. -su unpack original source tree too
  40. General options: -h print this message
  41. ";
  42. }
  43. $i = 100;
  44. grep ($fieldimps {$_} = $i--,
  45. qw (Source Version Binary Maintainer Architecture Standards-Version));
  46. while (@ARGV && $ARGV[0] =~ m/^-/) {
  47. $_=shift(@ARGV);
  48. if (m/^-b$/) {
  49. &setopmode('build');
  50. } elsif (m/^-x$/) {
  51. &setopmode('extract');
  52. } elsif (m/^-s([akpursnAKPUR])$/) {
  53. $sourcestyle= $1;
  54. } elsif (m/^-c/) {
  55. $controlfile= $';
  56. } elsif (m/^-l/) {
  57. $changelogfile= $';
  58. } elsif (m/^-F([0-9a-z]+)$/) {
  59. $changelogformat=$1;
  60. } elsif (m/^-D([^\=:]+)[=:]/) {
  61. $override{$1}= $';
  62. } elsif (m/^-U([^\=:]+)$/) {
  63. $remove{$1}= 1;
  64. } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:]/) {
  65. $substvar{$1}= $';
  66. } elsif (m/^-T/) {
  67. $varlistfile= $';
  68. } elsif (m/^-h$/) {
  69. &usageversion; exit(0);
  70. } elsif (m/^--$/) {
  71. last;
  72. } else {
  73. &usageerr("unknown option $_");
  74. }
  75. }
  76. defined($opmode) || &usageerr("need -x or -b");
  77. if ($opmode eq 'build') {
  78. $sourcestyle =~ y/X/A/;
  79. $sourcestyle =~ m/[akpursnAKPUR]/ ||
  80. &usageerr("source handling style -s$sourcestyle not allowed with -b");
  81. @ARGV || &usageerr("-b needs a directory");
  82. @ARGV<=2 || &usageerr("-b takes at most a directory and an orig source argument");
  83. $dir= shift(@ARGV);
  84. $dir= "./$dir" unless $dir =~ m:^/:; $dir =~ s,/*$,,;
  85. stat($dir) || &error("cannot stat directory $dir: $!");
  86. -d $dir || &error("directory argument $dir is not a directory");
  87. $changelogfile= "$dir/debian/changelog" unless defined($changelogfile);
  88. $controlfile= "$dir/debian/control" unless defined($controlfile);
  89. &parsechangelog;
  90. &parsecontrolfile;
  91. $archspecific=0;
  92. for $_ (keys %fi) {
  93. $v= $fi{$_};
  94. if (s/^C //) {
  95. #print STDERR "G key >$_< value >$v<\n";
  96. if (m/^Source$/) { &setsourcepackage; }
  97. elsif (m/^Standards-Version$|^Maintainer$/) { $f{$_}= $v; }
  98. elsif (s/^X[BC]*S[BC]*-//i) { $f{$_}= $v; }
  99. elsif (m/^(Section|Priority|Files)$/ || m/^X[BC]+-/i) { }
  100. else { &unknown('general section of control info file'); }
  101. } elsif (s/^C(\d+) //) {
  102. #print STDERR "P key >$_< value >$v<\n";
  103. $i=$1; $p=$fi{"C$i Package"};
  104. push(@binarypackages,$p) unless $packageadded{$p}++;
  105. if (m/^Architecture$/) {
  106. #print STDERR "$p >$v< >".join(' ',@sourcearch)."<\n";
  107. if ($v eq 'any') {
  108. @sourcearch= ('any');
  109. } elsif ($v eq 'all') {
  110. if (!@sourcearch || $sourcearch[0] eq 'all') {
  111. @sourcearch= ('all');
  112. } else {
  113. @sourcearch= ('any');
  114. }
  115. } else {
  116. if (grep($sourcearch[0] eq $_, 'any','all')) {
  117. @sourcearch= ('any');
  118. } else {
  119. for $a (split(/\s+/,$v)) {
  120. &error("architecture $a only allowed on its own".
  121. " (list for package $p is \`$a')")
  122. if grep($a eq $_, 'any','all');
  123. push(@sourcearch,$a) unless $archadded{$a}++;
  124. }
  125. }
  126. }
  127. $f{'Architecture'}= join(' ',@sourcearch);
  128. } elsif (s/^X[BC]*S[BC]*-//i) {
  129. $f{$_}= $v;
  130. } elsif (m/^(Package|Essential|Pre-Depends|Depends|Provides)$/ ||
  131. m/^(Recommends|Suggests|Optional|Conflicts|Replaces)$/ ||
  132. m/^(Description|Section|Priority)$/ ||
  133. m/^X[CS]+-/i) {
  134. } else {
  135. &unknown("package's section of control info file");
  136. }
  137. } elsif (s/^L //) {
  138. #print STDERR "L key >$_< value >$v<\n";
  139. if (m/^Source$/) {
  140. &setsourcepackage;
  141. } elsif (m/^Version$/) {
  142. $f{$_}= $v;
  143. } elsif (s/^X[BS]*C[BS]*-//i) {
  144. $f{$_}= $v;
  145. } elsif (m/^(Maintainer|Changes|Urgency|Distribution|Date|Closes)$/ ||
  146. m/^X[BS]+-/i) {
  147. } else {
  148. &unknown("parsed version of changelog");
  149. }
  150. } else {
  151. &internerr("value from nowhere, with key >$_< and value >$v<");
  152. }
  153. }
  154. $f{'Binary'}= join(', ',@binarypackages);
  155. for $f (keys %override) { $f{&capit($f)}= $override{$f}; }
  156. for $f (qw(Version)) {
  157. defined($f{$f}) || &error("missing information for critical output field $f");
  158. }
  159. for $f (qw(Maintainer Architecture Standards-Version)) {
  160. defined($f{$f}) || &warn("missing information for output field $f");
  161. }
  162. defined($sourcepackage) || &error("unable to determine source package name !");
  163. $f{'Source'}= $sourcepackage;
  164. for $f (keys %remove) { delete $f{&capit($f)}; }
  165. $version= $f{'Version'};
  166. $version =~ s/^\d+://; $upstreamversion= $version; $upstreamversion =~ s/-[^-]*$//;
  167. $basenamerev= $sourcepackage.'_'.$version;
  168. $basename= $sourcepackage.'_'.$upstreamversion;
  169. $basedirname= $basename;
  170. #print STDERR ">$basedirname<\n";
  171. $basedirname =~ s/_/-/;
  172. #print STDERR ">$basedirname<\n";
  173. $origdir= "$dir.orig";
  174. $origtargz= "$basename.orig.tar.gz";
  175. if (@ARGV) {
  176. $origarg= shift(@ARGV);
  177. if (length($origarg)) {
  178. stat($origarg) || &error("cannot stat orig argument $origarg: $!");
  179. if (-d _) {
  180. $origdir= $origarg;
  181. $origdir= "./$origdir" unless $origdir =~ m,^/,; $origdir =~ s,/*$,,;
  182. $sourcestyle =~ y/aA/rR/;
  183. $sourcestyle =~ m/[ursURS]/ ||
  184. &error("orig argument is unpacked but source handling style".
  185. " -s$sourcestyle calls for packed (.orig.tar.gz)");
  186. } elsif (-f _) {
  187. $origtargz= $origarg;
  188. $sourcestyle =~ y/aA/pP/;
  189. $sourcestyle =~ m/[kpsKPS]/ ||
  190. &error("orig argument is packed but source handling style".
  191. " -s$sourcestyle calls for unpacked (.orig/)");
  192. } else {
  193. &error("orig argument $origarg is not a plain file or directory");
  194. }
  195. } else {
  196. $sourcestyle =~ y/aA/nn/;
  197. $sourcestyle =~ m/n/ ||
  198. &error("orig argument is empty (means no orig, no diff)".
  199. " but source handling style -s$sourcestyle wants something");
  200. }
  201. }
  202. if ($sourcestyle =~ m/[aA]/) {
  203. if (stat("$origtargz")) {
  204. -f _ || &error("packed orig \`$origtargz' exists but is not a plain file");
  205. $sourcestyle =~ y/aA/pP/;
  206. } elsif ($! != ENOENT) {
  207. &syserr("unable to stat putative packed orig \`$origtargz'");
  208. } elsif (stat("$origdir")) {
  209. -d _ || &error("unpacked orig \`$origdir' exists but is not a directory");
  210. $sourcestyle =~ y/aA/rR/;
  211. } elsif ($! != ENOENT) {
  212. &syserr("unable to stat putative unpacked orig \`$origdir'");
  213. } else {
  214. $sourcestyle =~ y/aA/nn/;
  215. }
  216. }
  217. $dirbase= $dir; $dirbase =~ s,/?$,,; $dirbase =~ s,[^/]+$,,; $dirname= $&;
  218. $dirname eq $basedirname || &warn("source directory \`$dir' is not <sourcepackage>".
  219. "-<upstreamversion> \`$basedirname'");
  220. if ($sourcestyle ne 'n') {
  221. $origdirbase= $origdir; $origdirbase =~ s,/?$,,;
  222. $origdirbase =~ s,[^/]+$,,; $origdirname= $&;
  223. $origdirname eq "$basedirname.orig" ||
  224. &warn(".orig directory name $origdirname is not <package>".
  225. "-<upstreamversion> (wanted $basedirname.orig)");
  226. $tardirbase= $origdirbase; $tardirname= $origdirname;
  227. $tarname= $origtargz;
  228. $tarname eq "$basename.orig.tar.gz" ||
  229. &warn(".orig.tar.gz name $tarname is not <package>_<upstreamversion>".
  230. ".orig.tar.gz (wanted $basename.orig.tar.gz)");
  231. } else {
  232. $tardirbase= $dirbase; $tardirname= $dirname;
  233. $tarname= "$basenamerev.tar.gz";
  234. }
  235. #print STDERR ">$dir|$origdir|$origtargz|$sourcestyle<\n";
  236. if ($sourcestyle =~ m/[nurUR]/) {
  237. if (stat($tarname)) {
  238. $sourcestyle =~ m/[nUR]/ ||
  239. &error("tarfile \`$tarname' already exists, not overwriting,".
  240. " giving up; use -sU or -sR to override");
  241. } elsif ($! != ENOENT) {
  242. &syserr("unable to check for existence of \`$tarname'");
  243. }
  244. #print STDERR ">$tarname|$tardirbase|$tardirname<\n";
  245. print("$progname: building $sourcepackage in $tarname\n")
  246. || &syserr("write building tar message");
  247. &forkgzipwrite("$tarname.new");
  248. defined($c2= fork) || &syserr("fork for tar");
  249. if (!$c2) {
  250. chdir($tardirbase) || &syserr("chdir to above (orig) source $tardirbase");
  251. #system('pwd && ls');
  252. open(STDOUT,">&GZIP") || &syserr("reopen gzip for tar");
  253. # FIXME: put `--' argument back when tar is fixed
  254. exec('tar','-cf','-',$tardirname); &syserr("exec tar");
  255. }
  256. close(GZIP);
  257. &reapgzip;
  258. $c2 == waitpid($c2,0) || &syserr("wait for tar");
  259. $? && !(WIFSIGNALED($c2) && WTERMSIG($c2) == SIGPIPE) && subprocerr("tar");
  260. rename("$tarname.new",$tarname) ||
  261. &syserr("unable to rename \`$tarname.new' (newly created) to \`$tarname'");
  262. } else {
  263. print("$progname: building $sourcepackage using existing $tarname\n")
  264. || &syserr("write using existing tar message");
  265. }
  266. addfile("$tarname");
  267. if ($sourcestyle =~ m/[kpKP]/) {
  268. if (stat($origdir)) {
  269. $sourcestyle =~ m/[KP]/ ||
  270. &error("orig dir \`$origdir' already exists, not overwriting,".
  271. " giving up; use -sA, -sK or -sP to override");
  272. erasedir($origdir);
  273. } elsif ($! != ENOENT) {
  274. &syserr("unable to check for existence of orig dir \`$origdir'");
  275. }
  276. $expectprefix= $origdir; $expectprefix =~ s,^\./,,;
  277. checktarsane($origtargz,$expectprefix);
  278. mkdir("$origtargz.tmp-nest",0755) ||
  279. &syserr("unable to create \`$origtargz.tmp-nest'");
  280. extracttar($origtargz,"$origtargz.tmp-nest",$expectprefix);
  281. rename("$origtargz.tmp-nest/$expectprefix",$expectprefix) ||
  282. &syserr("unable to rename \`$origtargz.tmp-nest/$expectprefix' to ".
  283. "\`$expectprefix'");
  284. rmdir("$origtargz.tmp-nest") ||
  285. &syserr("unable to remove \`$origtargz.tmp-nest'");
  286. }
  287. if ($sourcestyle =~ m/[kpursKPUR]/) {
  288. print("$progname: building $sourcepackage in $basenamerev.diff.gz\n")
  289. || &syserr("write building diff message");
  290. &forkgzipwrite("$basenamerev.diff.gz");
  291. defined($c2= open(FIND,"-|")) || &syserr("fork for find");
  292. if (!$c2) {
  293. chdir($dir) || &syserr("chdir to $dir for find");
  294. exec('find','.','-print0'); &syserr("exec find");
  295. }
  296. $/= "\0";
  297. file:
  298. while (defined($fn= <FIND>)) {
  299. $fn =~ s/\0$//; $fn =~ s,^\./,,;
  300. lstat("$dir/$fn") || &syserr("cannot stat file $dir/$fn");
  301. if (-l _) {
  302. $type{$fn}= 'symlink';
  303. &checktype('-l') || next;
  304. defined($n= readlink("$dir/$fn")) ||
  305. &syserr("cannot read link $dir/$fn");
  306. defined($n2= readlink("$origdir/$fn")) ||
  307. &syserr("cannot read orig link $origdir/$fn");
  308. $n eq $n2 || &unrepdiff2("symlink to $n2","symlink to $n");
  309. } elsif (-f _) {
  310. $type{$fn}= 'plain file';
  311. if (!lstat("$origdir/$fn")) {
  312. $! == ENOENT || &syserr("cannot stat orig file $origdir/$fn");
  313. $ofnread= '/dev/null';
  314. } elsif (-f _) {
  315. $ofnread= "$origdir/$fn";
  316. } else {
  317. &unrepdiff2("something else","plain file");
  318. next;
  319. }
  320. defined($c3= open(DIFFGEN,"-|")) || &syserr("fork for diff");
  321. if (!$c3) {
  322. $ENV{'LANG'}= 'C';
  323. exec('diff','-u',
  324. '-L',"$basedirname.orig/$fn",
  325. '-L',"$basedirname/$fn",
  326. '--',"$ofnread","$dir/$fn"); &syserr("exec diff");
  327. }
  328. $difflinefound= 0;
  329. $/= "\n";
  330. while (<DIFFGEN>) {
  331. if (m/^binary/i) {
  332. close(DIFFGEN); $/= "\0";
  333. &unrepdiff("binary file contents changed");
  334. next file;
  335. } elsif (m/^[-+\@ ]/) {
  336. $difflinefound=1;
  337. } elsif (m/^\\ No newline at end of file$/) {
  338. &warn("file $fn has no final newline ".
  339. "(either original or modified version)");
  340. } else {
  341. s/\n$//;
  342. &internerr("unknown line from diff -u on $fn: \`$_'");
  343. }
  344. print(GZIP $_) || &syserr("failed to write to gzip");
  345. }
  346. close(DIFFGEN); $/= "\0";
  347. if (WIFEXITED($?) && (($es=WEXITSTATUS($?))==0 || $es==1)) {
  348. if ($es==1 && !$difflinefound) {
  349. &unrepdiff("diff gave 1 but no diff lines found");
  350. }
  351. } else {
  352. subprocerr("diff on $dir/$fn");
  353. }
  354. } elsif (-p _) {
  355. $type{$fn}= 'pipe';
  356. &checktype('-p');
  357. } elsif (-b _ || -c _ || -S _) {
  358. &unrepdiff("device or socket is not allowed");
  359. } elsif (-d _) {
  360. $type{$fn}= 'directory';
  361. } else {
  362. &unrepdiff("unknown file type ($!)");
  363. }
  364. }
  365. close(FIND); $? && subprocerr("find on $dir");
  366. close(GZIP) || &syserr("finish write to gzip pipe");
  367. &reapgzip;
  368. defined($c2= open(FIND,"-|")) || &syserr("fork for 2nd find");
  369. if (!$c2) {
  370. chdir($origdir) || &syserr("chdir to $origdir for 2nd find");
  371. exec('find','.','-print0'); &syserr("exec 2nd find");
  372. }
  373. $/= "\0";
  374. while (defined($fn= <FIND>)) {
  375. $fn =~ s/\0$//; $fn =~ s,^\./,,;
  376. next if defined($type{$fn});
  377. lstat("$origdir/$fn") || &syserr("cannot check orig file $origdir/$fn");
  378. if (-f _) {
  379. &warn("ignoring deletion of file $fn");
  380. } elsif (-d _) {
  381. &warn("ignoring deletion of directory $fn");
  382. } elsif (-l _) {
  383. &warn("ignoring deletion of symlink $fn");
  384. } else {
  385. &unrepdiff2('not a file, directory or link','nonexistent');
  386. }
  387. }
  388. close(FIND); $? && subprocerr("find on $dirname");
  389. &addfile("$basenamerev.diff.gz");
  390. }
  391. if ($sourcestyle =~ m/[prPR]/) {
  392. erasedir($origdir);
  393. }
  394. print("$progname: building $sourcepackage in $basenamerev.dsc\n")
  395. || &syserr("write building message");
  396. open(STDOUT,"> $basenamerev.dsc") || &syserr("create $basenamerev.dsc");
  397. &outputclose(1);
  398. if ($ur) {
  399. print(STDERR "$progname: unrepresentable changes to source\n")
  400. || &syserr("write error msg: $!");
  401. exit(1);
  402. }
  403. exit(0);
  404. } else {
  405. $sourcestyle =~ y/X/p/;
  406. $sourcestyle =~ m/[pun]/ ||
  407. &usageerr("source handling style -s$sourcestyle not allowed with -x");
  408. @ARGV==1 || &usageerr("-x needs exactly one argument, the .dsc");
  409. $dsc= shift(@ARGV);
  410. $dsc= "./$dsc" unless $dsc =~ m:^/:;
  411. $dscdir= $dsc; $dscdir= "./$dscdir" unless $dsc =~ m,^/|^\./,;
  412. $dscdir =~ s,/[^/]+$,,;
  413. open(CDATA,"< $dsc") || &error("cannot open .dsc file $dsc: $!");
  414. &parsecdata('S',-1,"source control file $dsc");
  415. close(CDATA);
  416. for $f (qw(Source Version Files)) {
  417. defined($fi{"S $f"}) ||
  418. &error("missing critical source control field $f");
  419. }
  420. $sourcepackage= $fi{'S Source'};
  421. $sourcepackage =~ m/[^-+.0-9a-z]/ &&
  422. &error("source package name contains illegal character \`$&'");
  423. $sourcepackage =~ m/^[0-9a-z]./ ||
  424. &error("source package name is too short or starts with non-alphanum");
  425. $version= $fi{'S Version'};
  426. $version =~ m/[^-+:.0-9a-zA-Z]/ &&
  427. &error("version number contains illegal character \`$&'");
  428. $version =~ s/^\d+://;
  429. if ($version =~ m/-([^-]+)$/) {
  430. $baseversion= $`; $revision= $1;
  431. } else {
  432. $baseversion= $version; $revision= '';
  433. }
  434. $files= $fi{'S Files'};
  435. for $file (split(/\n /,$files)) {
  436. next if $file eq '';
  437. $file =~ m/^([0-9a-f]{32})[ \t]+(\d+)[ \t]+([0-9a-zA-Z][-+:.,=0-9a-zA-Z_]+)$/
  438. || &error("Files field contains bad line \`$file'");
  439. ($md5sum{$3},$size{$3},$file) = ($1,$2,$3);
  440. &setfile(\$tarfile) if $file =~ m/\.tar\.gz$/;
  441. &setfile(\$difffile) if $file =~ m/\.diff\.gz$/;
  442. }
  443. $newdirectory= $sourcepackage.'-'.$baseversion;
  444. $expectprefix= $newdirectory; $expectprefix.= '.orig' if length($difffile);
  445. length($tarfile) || &error("no tarfile in Files field");
  446. checkstats($tarfile);
  447. checkstats($difffile) if length($difffile);
  448. checktarsane("$dscdir/$tarfile",$expectprefix);
  449. if (length($difffile)) {
  450. &forkgzipread("$dscdir/$difffile");
  451. $/="\n";
  452. $_ = <GZIP>;
  453. do {
  454. # read file header (---/+++ pair)
  455. s/\n$// or &error("diff is missing trailing newline");
  456. /^--- / or &error("expected ^--- in line $. of diff");
  457. $fn= $';
  458. substr($fn,0,length($expectprefix)+1) eq "$expectprefix/" ||
  459. &error("diff patches file ($fn) not in expected subdirectory");
  460. $fn =~ m/\.dpkg-orig$/ &&
  461. &error("diff patches file with name ending .dpkg-orig");
  462. $dirname= $fn;
  463. if ($dirname =~ s,/[^/]+$,, && !defined($dirincluded{$dirname})) {
  464. $dirtocreate{$dirname} = 1;
  465. }
  466. defined($notfileobject{$fn}) &&
  467. &error("diff patches something which is not a plain file");
  468. $_= <GZIP>; s/\n$// ||
  469. &error("diff finishes in middle of ---/+++ (line $.)");
  470. $_ eq '+++ '.$newdirectory.substr($fn,length($expectprefix)) ||
  471. &error("line after --- for file $fn isn't as expected");
  472. $filepatched{$fn}++ && &error("diff patches file $fn twice");
  473. # read hunks
  474. my $hunk = 0;
  475. while (($_ = <GZIP>) && !/^--- /) {
  476. # read hunk header (@@)
  477. s/\n$// or &error("diff is missing trailing newline");
  478. next if /^\\ No newline/;
  479. /^@@ -\d+(,(\d+))? \+\d+(,(\d+))? @\@$/ or
  480. &error("Expected ^@@ in line $. of diff");
  481. my ($olines, $nlines) = ($1 ? $2 : 1, $3 ? $4 : 1);
  482. ++$hunk;
  483. # read hunk
  484. while ($olines || $nlines) {
  485. $_ = <GZIP> or &error("unexpected end of diff");
  486. s/\n$// or &error("diff is missing trailing newline");
  487. next if /^\\ No newline/;
  488. if (/^ /) { --$olines; --$nlines; }
  489. elsif (/^-/) { --$olines; }
  490. elsif (/^\+/) { --$nlines; }
  491. else { &error("expected [ +-] at start of line $. of diff"); }
  492. }
  493. }
  494. $hunk or &error("expected ^\@\@ at line $. of diff");
  495. } while ($_ || !eof(GZIP));
  496. close(GZIP);
  497. &reapgzip;
  498. }
  499. print("$progname: extracting $sourcepackage in $newdirectory\n")
  500. || &syserr("write extracting message");
  501. &erasedir($newdirectory);
  502. &erasedir("$newdirectory.orig");
  503. mkdir("$expectprefix.tmp-nest",0755)
  504. || &syserr("unable to create \`$expectprefix.tmp-nest'");
  505. extracttar("$dscdir/$tarfile","$expectprefix.tmp-nest","$expectprefix");
  506. rename("$expectprefix.tmp-nest/$expectprefix","$expectprefix")
  507. || &syserr("unable to rename \`$expectprefix.tmp-nest/$expectprefix' "
  508. ."to \`$expectprefix'");
  509. rmdir("$expectprefix.tmp-nest")
  510. || &syserr("unable to remove \`$expectprefix.tmp-nest'");
  511. for $dircreate (keys %dirtocreate) {
  512. $dircreatem= "";
  513. for $dircreatep (split("/",$dirc)) {
  514. $dircreatem.= $dircreatep;
  515. if (!lstat($dircreatem)) {
  516. $! == ENOENT || &syserr("cannot stat $dircreatem");
  517. mkdir($dircreatem,0777)
  518. || &syserr("failed to create $dircreatem subdirectory");
  519. }
  520. else {
  521. -d _ || &error("diff patches file in directory \`$dircreate',"
  522. ." but $dircreatem isn't a directory !");
  523. }
  524. }
  525. }
  526. if (length($difffile)) {
  527. rename($expectprefix,$newdirectory) ||
  528. &syserr("failed to rename newly-extracted $expectprefix to $newdirectory");
  529. if ($sourcestyle =~ m/u/) {
  530. mkdir("$expectprefix.tmp-nest",0755)
  531. || &syserr("unable to create \`$expectprefix.tmp-nest'");
  532. extracttar("$dscdir/$tarfile","$expectprefix.tmp-nest",
  533. "$expectprefix");
  534. rename("$expectprefix.tmp-nest/$expectprefix","$expectprefix")
  535. || &syserr("unable to rename \`$expectprefix.tmp-nest/"
  536. ."$expectprefix' to \`$expectprefix'");
  537. rmdir("$expectprefix.tmp-nest")
  538. || &syserr("unable to remove \`$expectprefix.tmp-nest'");
  539. } elsif ($sourcestyle =~ m/p/) {
  540. stat("$dscdir/$tarfile") ||
  541. &syserr("failed to stat \`$dscdir/$tarfile' to see if need to copy");
  542. ($dsctardev,$dsctarino) = stat _;
  543. $dumptar= $sourcepackage.'_'.$baseversion.'.orig.tar.gz';
  544. if (!stat($dumptar)) {
  545. $! == ENOENT || &syserr("failed to check destination \`$dumptar'".
  546. " to see if need to copy");
  547. } else {
  548. ($dumptardev,$dumptarino) = stat _;
  549. if ($dumptardev == $dsctardev && $dumptarino == $dsctarino) {
  550. $dumptar= '';
  551. }
  552. }
  553. if (length($dumptar)) {
  554. system('cp','--',"$dscdir/$tarfile","$dumptar");
  555. $? && subprocerr("cp $dscdir/$tarfile to $dumptar");
  556. }
  557. }
  558. &forkgzipread("$dscdir/$difffile");
  559. defined($c2= fork) || &syserr("fork for patch");
  560. if (!$c2) {
  561. open(STDIN,"<&GZIP") || &syserr("reopen gzip for patch");
  562. chdir($newdirectory) || &syserr("chdir to $newdirectory for patch");
  563. $ENV{'LANG'}= 'C';
  564. exec('patch','-s','-t','-F','0','-N','-p1','-u',
  565. '-V','never','-g0','-b','-z','.dpkg-orig');
  566. &syserr("exec patch");
  567. }
  568. close(GZIP);
  569. $c2 == waitpid($c2,0) || &syserr("wait for patch");
  570. $? && subprocerr("patch");
  571. &reapgzip;
  572. for $fn (keys %filepatched) {
  573. $ftr= "$newdirectory/".substr($fn,length($expectprefix)+1).".dpkg-orig";
  574. unlink($ftr) || &syserr("remove patch backup file $ftr");
  575. }
  576. if (!(@s= lstat("$newdirectory/debian/rules"))) {
  577. $! == ENOENT || &syserr("cannot stat $newdirectory/debian/rules");
  578. &warn("$newdirectory/debian/rules does not exist");
  579. } elsif (-f _) {
  580. chmod($s[2] | 0111, "$newdirectory/debian/rules") ||
  581. &syserr("cannot make $newdirectory/debian/rules executable");
  582. } else {
  583. &warn("$newdirectory/debian/rules is not a plain file");
  584. }
  585. }
  586. $execmode= 0777 & ~umask;
  587. (@s= stat('.')) || &syserr("cannot stat \`.'");
  588. $dirmode= $execmode | ($s[2] & 02000);
  589. $plainmode= $execmode & ~0111;
  590. $fifomode= ($plainmode & 0222) | (($plainmode & 0222) << 1);
  591. for $fn (@filesinarchive) {
  592. $fn= substr($fn,length($expectprefix)+1);
  593. $fn= "$newdirectory/$fn";
  594. (@s= lstat($fn)) || &syserr("cannot stat extracted object \`$fn'");
  595. $mode= $s[2];
  596. if (-d _) {
  597. $newmode= $dirmode;
  598. } elsif (-f _) {
  599. $newmode= ($mode & 0111) ? $execmode : $plainmode;
  600. } elsif (-p _) {
  601. $newmode= $fifomode;
  602. } elsif (!-l _) {
  603. &internerr("unknown object \`$fn' after extract (mode ".
  604. sprintf("0%o",$mode).")");
  605. } else { next; }
  606. next if ($mode & 07777) == $newmode;
  607. chmod($newmode,$fn) ||
  608. &syserr(sprintf("cannot change mode of \`%s' to 0%o from 0%o",
  609. $fn,$newmode,$mode));
  610. }
  611. exit(0);
  612. }
  613. sub checkstats {
  614. my ($f) = @_;
  615. my @s;
  616. my $m;
  617. open(STDIN,"< $dscdir/$f") || &syserr("cannot read $dscdir/$f");
  618. (@s= stat(STDIN)) || &syserr("cannot fstat $dscdir/$f");
  619. $s[7] == $size{$f} || &error("file $f has size $s[7] instead of expected $size{$f}");
  620. $m= `md5sum`; $? && subprocerr("md5sum $f"); $m =~ s/\n$//;
  621. $m =~ m/^[0-9a-f]{32}$/ || &failure("md5sum of $f gave bad output \`$m'");
  622. $m eq $md5sum{$f} || &error("file $f has md5sum $m instead of expected $md5sum{$f}");
  623. open(STDIN,"</dev/null") || &syserr("reopen stdin from /dev/null");
  624. }
  625. sub erasedir {
  626. my ($dir) = @_;
  627. if (!lstat($dir)) {
  628. $! == ENOENT && return;
  629. &syserr("cannot stat directory $dir (before removal)");
  630. }
  631. system 'rm','-rf','--',$dir;
  632. $? && subprocerr("rm -rf $dir");
  633. if (!stat($dir)) {
  634. $! == ENOENT && return;
  635. &syserr("unable to check for removal of dir \`$dir'");
  636. }
  637. &failure("rm -rf failed to remove \`$dir'");
  638. }
  639. use strict 'vars';
  640. sub checktarcpio {
  641. my ($tarfileread, $wpfx) = @_;
  642. my ($tarprefix, $c2);
  643. @filesinarchive = ();
  644. # make <CPIO> read from the uncompressed archive file
  645. &forkgzipread ("$tarfileread");
  646. if (! defined ($c2 = open (CPIO,"-|"))) { &syserr ("fork for cpio"); }
  647. if (!$c2) {
  648. open (STDIN,"<&GZIP") || &syserr ("reopen gzip for cpio");
  649. &cpiostderr;
  650. exec ('cpio','-0t');
  651. &syserr ("exec cpio");
  652. }
  653. close (GZIP);
  654. $/ = "\0";
  655. open (CPIO, "<cpiolog");
  656. while (defined ($fn = <CPIO>)) {
  657. $fn =~ s/\0$//;
  658. # store printable name of file for error messages
  659. my $pname = $fn;
  660. $pname =~ y/ -~/?/c;
  661. if (! $tarprefix) {
  662. if ($fn =~ m/\n/) {
  663. &error("first output from cpio -0t (from \`$tarfileread') ".
  664. "contains newline - you probably have an out of ".
  665. "date version of cpio. GNU cpio 2.4.2-2 is known to work");
  666. }
  667. $tarprefix = ($fn =~ m,([^/]*)[/],)[0];
  668. # need to check for multiple dots on some operating systems
  669. # empty tarprefix (due to regex failer) will match emptry string
  670. if ($tarprefix =~ /^[.]*$/) {
  671. &error("tarfile \`$tarfileread' does not extract into a ".
  672. "directory off the current directory ($tarprefix from $pname)");
  673. }
  674. }
  675. if ($fn =~ m/\n/) {
  676. &error ("tarfile \`$tarfileread' contains object with".
  677. " newline in its name ($pname)");
  678. }
  679. next if ($fn eq '././@LongLink');
  680. my $fprefix = substr ($fn, 0, length ($tarprefix));
  681. my $slash = substr ($fn, length ($tarprefix), 1);
  682. if ((($slash ne '/') && ($slash ne '')) || ($fprefix ne $tarprefix)) {
  683. &error ("tarfile \`$tarfileread' contains object ($pname) ".
  684. "not in expected directory ($tarprefix)");
  685. }
  686. # need to check for multiple dots on some operating systems
  687. if ($fn =~ m/[.]{2,}/) {
  688. &error ("tarfile \`$tarfileread' contains object with".
  689. " /../ in its name ($pname)");
  690. }
  691. push (@filesinarchive, $fn);
  692. }
  693. close (CPIO);
  694. $? && subprocerr ("cpio");
  695. &reapgzip;
  696. $/= "\n";
  697. my $tarsubst = quotemeta ($tarprefix);
  698. @filesinarchive = map { s/^$tarsubst/$wpfx/; $_ } @filesinarchive;
  699. return $tarprefix;
  700. }
  701. sub checktarsane {
  702. my ($tarfileread, $wpfx) = @_;
  703. my ($c2);
  704. %dirincluded = ();
  705. %notfileobject = ();
  706. my $tarprefix = &checktarcpio ($tarfileread, $wpfx);
  707. # make <TAR> read from the uncompressed archive file
  708. &forkgzipread ("$tarfileread");
  709. if (! defined ($c2 = open (TAR,"-|"))) { &syserr ("fork for tar -t"); }
  710. if (! $c2) {
  711. $ENV{'LANG'}= 'C';
  712. open (STDIN, "<&GZIP") || &syserr ("reopen gzip for tar -t");
  713. exec ('tar', '-vvtf', '-'); &syserr ("exec tar -vvtf -");
  714. }
  715. close (GZIP);
  716. my $efix= 0;
  717. open (TAR, "<tarlog");
  718. while (<TAR>) {
  719. chomp;
  720. if (! m,^(\S{10})\s,) {
  721. &error("tarfile \`$tarfileread' contains unknown object ".
  722. "listed by tar as \`$_'");
  723. }
  724. my $mode = $1;
  725. $mode =~ s/^([-dpsl])// ||
  726. &error("tarfile \`$tarfileread' contains object \`$fn' with ".
  727. "unknown or forbidden type \`".substr($_,0,1)."'");
  728. my $type = $&;
  729. if ($mode =~ /^l/) { $_ =~ s/ -\> .*//; }
  730. s/ link to .+//;
  731. if (length ($_) <= 48) {
  732. &error ("tarfile \`$tarfileread' contains incomplete entry \`$_'\n");
  733. }
  734. my $tarfn = substr ($_, 48, length ($_) - 48);
  735. $tarfn = deoctify ($tarfn);
  736. # store printable name of file for error messages
  737. my $pname = $tarfn;
  738. $pname =~ y/ -~/?/c;
  739. # fetch name of file as given by cpio
  740. $fn = $filesinarchive[$efix++];
  741. if ($tarfn ne $fn) {
  742. if ((length ($fn) == 99) && (length ($tarfn) >= 99)
  743. && (substr ($fn, 0, 99) eq substr ($tarfn, 0, 99))) {
  744. # this file doesn't match because cpio truncated the name
  745. # to the first 100 characters. let it slide for now.
  746. &warn ("filename \`$pname' was truncated by cpio;" .
  747. " unable to check full pathname");
  748. } else {
  749. &error ("tarfile \`$tarfileread' contains unexpected object".
  750. " listed by tar as \`$_'; expected \`$pname'");
  751. }
  752. }
  753. # if cpio truncated the name above,
  754. # we still can't allow files to expand into /../
  755. # need to check for multiple dots on some operating systems
  756. if ($tarfn =~ m/[.]{2,}/) {
  757. &error ("tarfile \`$tarfileread' contains object with".
  758. "/../ in its name ($pname)");
  759. }
  760. if ($tarfn =~ /\.dpkg-orig$/) {
  761. &error ("tarfile \`$tarfileread' contains file with name ending in .dpkg-orig");
  762. }
  763. if ($mode =~ /[sStT]/ && $type ne 'd') {
  764. &error ("tarfile \`$tarfileread' contains setuid, setgid".
  765. " or sticky object \`$pname'");
  766. }
  767. if ($tarfn eq "$tarprefix/debian" && $type ne 'd') {
  768. &error ("tarfile \`$tarfileread' contains object \`debian'".
  769. " that isn't a directory");
  770. }
  771. if ($type eq 'd') { $tarfn =~ s,/$,,; }
  772. my $dirname = $tarfn;
  773. if (($dirname =~ s,/[^/]+$,,) && (! defined ($dirincluded{$dirname}))) {
  774. &error ("tarfile \`$tarfileread' contains object \`$pname' but its containing ".
  775. "directory \`$dirname' does not precede it");
  776. }
  777. if ($type eq 'd') { $dirincluded{$tarfn} = 1; }
  778. if ($type ne '-') { $notfileobject{$tarfn} = 1; }
  779. }
  780. close (TAR);
  781. #$? && subprocerr ("tar -vvtf");
  782. &reapgzip;
  783. my $tarsubst = quotemeta ($tarprefix);
  784. %dirincluded = map { s/^$tarsubst/$wpfx/; $_=>1 } (keys %dirincluded);
  785. %notfileobject = map { s/^$tarsubst/$wpfx/; $_=>1 } (keys %notfileobject);
  786. }
  787. no strict 'vars';
  788. sub extracttar {
  789. my ($tarfileread,$dirchdir,$newtopdir) = @_;
  790. &forkgzipread("$tarfileread");
  791. defined($c2= fork) || &syserr("fork for tar -xkf -");
  792. if (!$c2) {
  793. chdir("$dirchdir") || &syserr("cannot chdir to \`$dirchdir' for tar extract");
  794. open(STDIN,"<&GZIP") || &syserr("reopen gzip for cpio -i");
  795. &cpiostderr;
  796. exec('tar','-xkf','-'); &syserr("exec tar -xkf -");
  797. }
  798. close(GZIP);
  799. $c2 == waitpid($c2,0) || &syserr("wait for tar -xkf -");
  800. $? && subprocerr("tar -xkf -");
  801. &reapgzip;
  802. opendir(D,"$dirchdir") || &syserr("Unable to open dir $dirchdir");
  803. @dirchdirfiles = grep($_ ne "." && $_ ne "..",readdir(D));
  804. closedir(D) || &syserr("Unable to close dir $dirchdir");
  805. (@dirchdirfiles==1 && -d "$dirchdir/$dirchdirfiles[0]") ||
  806. &error("$tarfileread extracted into >1 directory");
  807. rename("$dirchdir/$dirchdirfiles[0]", "$dirchdir/$newtopdir") ||
  808. &syserr("Unable to rename $dirchdir/$dirchdirfiles[0] to ".
  809. "$dirchdir/$newtopdir");
  810. }
  811. sub cpiostderr {
  812. open(STDERR,"| egrep -v '^[0-9]+ blocks\$' >&2") ||
  813. &syserr("reopen stderr for cpio to grep out blocks message");
  814. }
  815. sub setfile {
  816. my ($varref) = @_;
  817. if (defined ($$varref)) {
  818. &error ("repeated file type - files " . $$varref . " and $file");
  819. }
  820. $$varref = $file;
  821. }
  822. sub checktype {
  823. if (!lstat("$origdir/$fn")) {
  824. &unrepdiff2("nonexistent",$type{$fn});
  825. } else {
  826. $v= eval("$_[0] _ ? 2 : 1"); $v || &internerr("checktype $@ ($_[0])");
  827. return 1 if $v == 2;
  828. &unrepdiff2("something else",$type{$fn});
  829. }
  830. return 0;
  831. }
  832. sub setopmode {
  833. defined($opmode) && &usageerr("only one of -x or -b allowed, and only once");
  834. $opmode= $_[0];
  835. }
  836. sub unrepdiff {
  837. print(STDERR "$progname: cannot represent change to $fn: $_[0]\n")
  838. || &syserr("write syserr unrep");
  839. $ur++;
  840. }
  841. sub unrepdiff2 {
  842. print(STDERR "$progname: cannot represent change to $fn:\n".
  843. "$progname: new version is $_[1]\n".
  844. "$progname: old version is $_[0]\n")
  845. || &syserr("write syserr unrep");
  846. $ur++;
  847. }
  848. sub forkgzipwrite {
  849. #print STDERR "forkgzipwrite $_[0]\n";
  850. open(GZIPFILE,"> $_[0]") || &syserr("create file $_[0]");
  851. pipe(GZIPREAD,GZIP) || &syserr("pipe for gzip");
  852. defined($cgz= fork) || &syserr("fork for gzip");
  853. if (!$cgz) {
  854. open(STDIN,"<&GZIPREAD") || &syserr("reopen gzip pipe"); close(GZIPREAD);
  855. close(GZIP); open(STDOUT,">&GZIPFILE") || &syserr("reopen tar.gz");
  856. exec('gzip','-9'); &syserr("exec gzip");
  857. }
  858. close(GZIPREAD);
  859. $gzipsigpipeok= 0;
  860. }
  861. sub forkgzipread {
  862. #print STDERR "forkgzipread $_[0]\n";
  863. open(GZIPFILE,"< $_[0]") || &syserr("read file $_[0]");
  864. pipe(GZIP,GZIPWRITE) || &syserr("pipe for gunzip");
  865. defined($cgz= fork) || &syserr("fork for gunzip");
  866. if (!$cgz) {
  867. open(STDOUT,">&GZIPWRITE") || &syserr("reopen gunzip pipe"); close(GZIPWRITE);
  868. close(GZIP); open(STDIN,"<&GZIPFILE") || &syserr("reopen input file");
  869. exec('gunzip'); &syserr("exec gunzip");
  870. }
  871. close(GZIPWRITE);
  872. $gzipsigpipeok= 1;
  873. }
  874. sub reapgzip {
  875. #print STDERR "reapgzip $_[0]\n";
  876. $cgz == waitpid($cgz,0) || &syserr("wait for gzip");
  877. !$? || ($gzipsigpipeok && WIFSIGNALED($?) && WTERMSIG($?)==SIGPIPE) ||
  878. subprocerr("gzip");
  879. close(GZIPFILE);
  880. }
  881. sub addfile {
  882. my ($filename)= @_;
  883. stat($filename) || &syserr("could not stat output file \`$filename'");
  884. $size= (stat _)[7];
  885. my $md5sum= `md5sum <$filename`;
  886. $? && &subprocerr("md5sum $filename");
  887. $md5sum =~ s/^([0-9a-f]{32})\n$/$1/ || &failure("md5sum gave bogus output \`$_'");
  888. $f{'Files'}.= "\n $md5sum $size $filename";
  889. }
  890. # replace \ddd with their corresponding character, refuse \ddd > \377
  891. # modifies $_ (hs)
  892. {
  893. my $backslash;
  894. sub deoctify {
  895. my $fn= $_[0];
  896. $backslash= sprintf("\\%03o", unpack("C", "\\")) if !$backslash;
  897. s/\\{2}/$backslash/g;
  898. @_= split(/\\/, $fn);
  899. foreach (@_) {
  900. /^(\d{3})/ or next;
  901. &failure("bogus character `\\$1' in `$fn'\n") if oct($1) > 255;
  902. $_= pack("c", oct($1)) . $';
  903. }
  904. return join("", @_);
  905. } }