dpkg-source.pl 38 KB

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