dpkg-source.pl 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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. exec('diff','-u',
  323. '-L',"$basedirname.orig/$fn",
  324. '-L',"$basedirname/$fn",
  325. '--',"$ofnread","$dir/$fn"); &syserr("exec diff");
  326. }
  327. $difflinefound= 0;
  328. $/= "\n";
  329. while (<DIFFGEN>) {
  330. if (m/^binary/i) {
  331. close(DIFFGEN); $/= "\0";
  332. &unrepdiff("binary file contents changed");
  333. next file;
  334. } elsif (m/^[-+\@ ]/) {
  335. $difflinefound=1;
  336. } elsif (m/^\\ No newline at end of file$/) {
  337. &warn("file $fn has no final newline ".
  338. "(either original or modified version)");
  339. } else {
  340. s/\n$//;
  341. &internerr("unknown line from diff -u on $fn: \`$_'");
  342. }
  343. print(GZIP $_) || &syserr("failed to write to gzip");
  344. }
  345. close(DIFFGEN); $/= "\0";
  346. if (WIFEXITED($?) && (($es=WEXITSTATUS($?))==0 || $es==1)) {
  347. if ($es==1 && !$difflinefound) {
  348. &unrepdiff("diff gave 1 but no diff lines found");
  349. }
  350. } else {
  351. subprocerr("diff on $dir/$fn");
  352. }
  353. } elsif (-p _) {
  354. $type{$fn}= 'pipe';
  355. &checktype('-p');
  356. } elsif (-b _ || -c _ || -S _) {
  357. &unrepdiff("device or socket is not allowed");
  358. } elsif (-d _) {
  359. $type{$fn}= 'directory';
  360. } else {
  361. &unrepdiff("unknown file type ($!)");
  362. }
  363. }
  364. close(FIND); $? && subprocerr("find on $dir");
  365. close(GZIP) || &syserr("finish write to gzip pipe");
  366. &reapgzip;
  367. defined($c2= open(FIND,"-|")) || &syserr("fork for 2nd find");
  368. if (!$c2) {
  369. chdir($origdir) || &syserr("chdir to $origdir for 2nd find");
  370. exec('find','.','-print0'); &syserr("exec 2nd find");
  371. }
  372. $/= "\0";
  373. while (defined($fn= <FIND>)) {
  374. $fn =~ s/\0$//; $fn =~ s,^\./,,;
  375. next if defined($type{$fn});
  376. lstat("$origdir/$fn") || &syserr("cannot check orig file $origdir/$fn");
  377. if (-f _) {
  378. &warn("ignoring deletion of file $fn");
  379. } elsif (-d _) {
  380. &warn("ignoring deletion of directory $fn");
  381. } elsif (-l _) {
  382. &warn("ignoring deletion of symlink $fn");
  383. } else {
  384. &unrepdiff2('not a file, directory or link','nonexistent');
  385. }
  386. }
  387. close(FIND); $? && subprocerr("find on $dirname");
  388. &addfile("$basenamerev.diff.gz");
  389. }
  390. if ($sourcestyle =~ m/[prPR]/) {
  391. erasedir($origdir);
  392. }
  393. print("$progname: building $sourcepackage in $basenamerev.dsc\n")
  394. || &syserr("write building message");
  395. open(STDOUT,"> $basenamerev.dsc") || &syserr("create $basenamerev.dsc");
  396. &outputclose(1);
  397. if ($ur) {
  398. print(STDERR "$progname: unrepresentable changes to source\n")
  399. || &syserr("write error msg: $!");
  400. exit(1);
  401. }
  402. exit(0);
  403. } else {
  404. $sourcestyle =~ y/X/p/;
  405. $sourcestyle =~ m/[pun]/ ||
  406. &usageerr("source handling style -s$sourcestyle not allowed with -b");
  407. @ARGV==1 || &usageerr("-x needs exactly one argument, the .dsc");
  408. $dsc= shift(@ARGV);
  409. $dsc= "./$dsc" unless $dsc =~ m:^/:;
  410. $dscdir= $dsc; $dscdir= "./$dscdir" unless $dsc =~ m,^/|^\./,;
  411. $dscdir =~ s,/[^/]+$,,;
  412. open(CDATA,"< $dsc") || &error("cannot open .dsc file $dsc: $!");
  413. &parsecdata('S',-1,"source control file $dsc");
  414. close(CDATA);
  415. for $f (qw(Source Version Files)) {
  416. defined($fi{"S $f"}) ||
  417. &error("missing critical source control field $f");
  418. }
  419. $sourcepackage= $fi{'S Source'};
  420. $sourcepackage =~ m/[^-+.0-9a-z]/ &&
  421. &error("source package name contains illegal character \`$&'");
  422. $sourcepackage =~ m/^[0-9a-z]./ ||
  423. &error("source package name is too short or starts with non-alphanum");
  424. $version= $fi{'S Version'};
  425. $version =~ m/[^-+:.0-9a-zA-Z]/ &&
  426. &error("version number contains illegal character \`$&'");
  427. $version =~ s/^\d+://;
  428. if ($version =~ m/-([^-]+)$/) {
  429. $baseversion= $`; $revision= $1;
  430. } else {
  431. $baseversion= $version; $revision= '';
  432. }
  433. $files= $fi{'S Files'};
  434. for $file (split(/\n /,$files)) {
  435. next if $file eq '';
  436. $file =~ m/^([0-9a-f]{32})[ \t]+(\d+)[ \t]+([0-9a-zA-Z][-+:.,=0-9a-zA-Z_]+)$/
  437. || &error("Files field contains bad line \`$file'");
  438. ($md5sum{$3},$size{$3},$file) = ($1,$2,$3);
  439. &setfile(\$tarfile) if $file =~ m/\.tar\.gz$/;
  440. &setfile(\$difffile) if $file =~ m/\.diff\.gz$/;
  441. }
  442. $newdirectory= $sourcepackage.'-'.$baseversion;
  443. $expectprefix= $newdirectory; $expectprefix.= '.orig' if length($difffile);
  444. length($tarfile) || &error("no tarfile in Files field");
  445. checkstats($tarfile);
  446. checkstats($difffile) if length($difffile);
  447. checktarsane("$dscdir/$tarfile",$expectprefix);
  448. if (length($difffile)) {
  449. &forkgzipread("$dscdir/$difffile");
  450. $/="\n";
  451. while (<GZIP>) {
  452. s/\n$// || &error("diff is missing trailing newline");
  453. if (/^--- /) {
  454. $fn= $';
  455. substr($fn,0,length($expectprefix)+1) eq "$expectprefix/" ||
  456. &error("diff patches file ($fn) not in expected subdirectory");
  457. $fn =~ m/\.dpkg-orig$/ &&
  458. &error("diff patches file with name ending .dpkg-orig");
  459. $dirname= $fn;
  460. if ($dirname =~ s,/[^/]+$,, && !defined($dirincluded{$dirname})) {
  461. $dirtocreate{$dirname} = 1;
  462. }
  463. defined($notfileobject{$fn}) &&
  464. &error("diff patches something which is not a plain file");
  465. $_= <GZIP>; s/\n$// ||
  466. &error("diff finishes in middle of ---/+++ (line $.)");
  467. $_ eq '+++ '.$newdirectory.substr($fn,length($expectprefix)) ||
  468. &error("line after --- for file $fn isn't as expected");
  469. $filepatched{$fn}++ && &error("diff patches file $fn twice");
  470. } elsif (/^\\ No newline at end of file$/) {
  471. } elsif (/^[-+ \@]/) {
  472. } else {
  473. &error ("diff contains unknown line \`$_'");
  474. }
  475. }
  476. close(GZIP);
  477. &reapgzip;
  478. }
  479. print("$progname: extracting $sourcepackage in $newdirectory\n")
  480. || &syserr("write extracting message");
  481. &erasedir($newdirectory);
  482. &erasedir("$newdirectory.orig");
  483. mkdir("$expectprefix.tmp-nest",0755)
  484. || &syserr("unable to create \`$expectprefix.tmp-nest'");
  485. extracttar("$dscdir/$tarfile","$expectprefix.tmp-nest","$expectprefix");
  486. rename("$expectprefix.tmp-nest/$expectprefix","$expectprefix")
  487. || &syserr("unable to rename \`$expectprefix.tmp-nest/$expectprefix' "
  488. ."to \`$expectprefix'");
  489. rmdir("$expectprefix.tmp-nest")
  490. || &syserr("unable to remove \`$expectprefix.tmp-nest'");
  491. for $dircreate (keys %dirtocreate) {
  492. $dircreatem= "";
  493. for $dircreatep (split("/",$dirc)) {
  494. $dircreatem.= $dircreatep;
  495. if (!lstat($dircreatem)) {
  496. $! == ENOENT || &syserr("cannot stat $dircreatem");
  497. mkdir($dircreatem,0777)
  498. || &syserr("failed to create $dircreatem subdirectory");
  499. }
  500. else {
  501. -d _ || &error("diff patches file in directory \`$dircreate',"
  502. ." but $dircreatem isn't a directory !");
  503. }
  504. }
  505. }
  506. if (length($difffile)) {
  507. rename($expectprefix,$newdirectory) ||
  508. &syserr("failed to rename newly-extracted $expectprefix to $newdirectory");
  509. if ($sourcestyle =~ m/u/) {
  510. mkdir("$expectprefix.tmp-nest",0755)
  511. || &syserr("unable to create \`$expectprefix.tmp-nest'");
  512. extracttar("$dscdir/$tarfile","$expectprefix.tmp-nest",
  513. "$expectprefix");
  514. rename("$expectprefix.tmp-nest/$expectprefix","$expectprefix")
  515. || &syserr("unable to rename \`$expectprefix.tmp-nest/"
  516. ."$expectprefix' to \`$expectprefix'");
  517. rmdir("$expectprefix.tmp-nest")
  518. || &syserr("unable to remove \`$expectprefix.tmp-nest'");
  519. } elsif ($sourcestyle =~ m/p/) {
  520. stat("$dscdir/$tarfile") ||
  521. &syserr("failed to stat \`$dscdir/$tarfile' to see if need to copy");
  522. ($dsctardev,$dsctarino) = stat _;
  523. $dumptar= $sourcepackage.'_'.$baseversion.'.orig.tar.gz';
  524. if (!stat($dumptar)) {
  525. $! == ENOENT || &syserr("failed to check destination \`$dumptar'".
  526. " to see if need to copy");
  527. } else {
  528. ($dumptardev,$dumptarino) = stat _;
  529. if ($dumptardev == $dsctardev && $dumptarino == $dsctarino) {
  530. $dumptar= '';
  531. }
  532. }
  533. if (length($dumptar)) {
  534. system('cp','--',"$dscdir/$tarfile","$dumptar");
  535. $? && subprocerr("cp $dscdir/$tarfile to $dumptar");
  536. }
  537. }
  538. &forkgzipread("$dscdir/$difffile");
  539. defined($c2= fork) || &syserr("fork for patch");
  540. if (!$c2) {
  541. open(STDIN,"<&GZIP") || &syserr("reopen gzip for patch");
  542. chdir($newdirectory) || &syserr("chdir to $newdirectory for patch");
  543. exec('patch','-s','-t','-F','0','-N','-p1','-u',
  544. '-V','never','-b','-z','.dpkg-orig');
  545. &syserr("exec patch");
  546. }
  547. close(GZIP);
  548. $c2 == waitpid($c2,0) || &syserr("wait for patch");
  549. $? && subprocerr("patch");
  550. &reapgzip;
  551. for $fn (keys %filepatched) {
  552. $ftr= "$newdirectory/".substr($fn,length($expectprefix)+1).".dpkg-orig";
  553. unlink($ftr) || &syserr("remove patch backup file $ftr");
  554. }
  555. if (!(@s= lstat("$newdirectory/debian/rules"))) {
  556. $! == ENOENT || &syserr("cannot stat $newdirectory/debian/rules");
  557. &warn("$newdirectory/debian/rules does not exist");
  558. } elsif (-f _) {
  559. chmod($s[2] | 0111, "$newdirectory/debian/rules") ||
  560. &syserr("cannot make $newdirectory/debian/rules executable");
  561. } else {
  562. &warn("$newdirectory/debian/rules is not a plain file");
  563. }
  564. }
  565. $execmode= 0777 & ~umask;
  566. (@s= stat('.')) || &syserr("cannot stat \`.'");
  567. $dirmode= $execmode | ($s[2] & 02000);
  568. $plainmode= $execmode & ~0111;
  569. $fifomode= ($plainmode & 0222) | (($plainmode & 0222) << 1);
  570. for $fn (@filesinarchive) {
  571. $fn= substr($fn,length($expectprefix)+1);
  572. $fn= "$newdirectory/$fn";
  573. (@s= lstat($fn)) || &syserr("cannot stat extracted object \`$fn'");
  574. $mode= $s[2];
  575. if (-d _) {
  576. $newmode= $dirmode;
  577. } elsif (-f _) {
  578. $newmode= ($mode & 0111) ? $execmode : $plainmode;
  579. } elsif (-p _) {
  580. $newmode= $fifomode;
  581. } elsif (!-l _) {
  582. &internerr("unknown object \`$fn' after extract (mode ".
  583. sprintf("0%o",$mode).")");
  584. } else { next; }
  585. next if ($mode & 07777) == $newmode;
  586. chmod($newmode,$fn) ||
  587. &syserr(sprintf("cannot change mode of \`%s' to 0%o from 0%o",
  588. $fn,$newmode,$mode));
  589. }
  590. exit(0);
  591. }
  592. sub checkstats {
  593. my ($f) = @_;
  594. my @s;
  595. my $m;
  596. open(STDIN,"< $dscdir/$f") || &syserr("cannot read $dscdir/$f");
  597. (@s= stat(STDIN)) || &syserr("cannot fstat $dscdir/$f");
  598. $s[7] == $size{$f} || &error("file $f has size $s[7] instead of expected $size{$f}");
  599. $m= `md5sum`; $? && subprocerr("md5sum $f"); $m =~ s/\n$//;
  600. $m =~ m/^[0-9a-f]{32}$/ || &failure("md5sum of $f gave bad output \`$m'");
  601. $m eq $md5sum{$f} || &error("file $f has md5sum $m instead of expected $md5sum{$f}");
  602. open(STDIN,"</dev/null") || &syserr("reopen stdin from /dev/null");
  603. }
  604. sub erasedir {
  605. my ($dir) = @_;
  606. if (!lstat($dir)) {
  607. $! == ENOENT && return;
  608. &syserr("cannot stat directory $dir (before removal)");
  609. }
  610. system 'rm','-rf','--',$dir;
  611. $? && subprocerr("rm -rf $dir");
  612. if (!stat($dir)) {
  613. $! == ENOENT && return;
  614. &syserr("unable to check for removal of dir \`$dir'");
  615. }
  616. &failure("rm -rf failed to remove \`$dir'");
  617. }
  618. use strict 'vars';
  619. sub checktarcpio {
  620. my ($tarfileread, $wpfx) = @_;
  621. my ($tarprefix, $c2);
  622. @filesinarchive = ();
  623. # make <CPIO> read from the uncompressed archive file
  624. &forkgzipread ("$tarfileread");
  625. if (! defined ($c2 = open (CPIO,"-|"))) { &syserr ("fork for cpio"); }
  626. if (!$c2) {
  627. open (STDIN,"<&GZIP") || &syserr ("reopen gzip for cpio");
  628. &cpiostderr;
  629. exec ('cpio','-0t');
  630. &syserr ("exec cpio");
  631. }
  632. close (GZIP);
  633. $/ = "\0";
  634. open (CPIO, "<cpiolog");
  635. while (defined ($fn = <CPIO>)) {
  636. $fn =~ s/\0$//;
  637. # store printable name of file for error messages
  638. my $pname = $fn;
  639. $pname =~ y/ -~/?/c;
  640. if (! $tarprefix) {
  641. if ($fn =~ m/\n/) {
  642. &error("first output from cpio -0t (from \`$tarfileread') ".
  643. "contains newline - you probably have an out of ".
  644. "date version of cpio. GNU cpio 2.4.2-2 is known to work");
  645. }
  646. $tarprefix = ($fn =~ m,([^/]*)[/],)[0];
  647. # need to check for multiple dots on some operating systems
  648. # empty tarprefix (due to regex failer) will match emptry string
  649. if ($tarprefix =~ /^[.]*$/) {
  650. &error("tarfile \`$tarfileread' does not extract into a ".
  651. "directory off the current directory ($tarprefix from $pname)");
  652. }
  653. }
  654. if ($fn =~ m/\n/) {
  655. &error ("tarfile \`$tarfileread' contains object with".
  656. " newline in its name ($pname)");
  657. }
  658. next if ($fn eq '././@LongLink');
  659. my $fprefix = substr ($fn, 0, length ($tarprefix));
  660. my $slash = substr ($fn, length ($tarprefix), 1);
  661. if ((($slash ne '/') && ($slash ne '')) || ($fprefix ne $tarprefix)) {
  662. &error ("tarfile \`$tarfileread' contains object ($pname) ".
  663. "not in expected directory ($tarprefix)");
  664. }
  665. # need to check for multiple dots on some operating systems
  666. if ($fn =~ m/[.]{2,}/) {
  667. &error ("tarfile \`$tarfileread' contains object with".
  668. " /../ in its name ($pname)");
  669. }
  670. push (@filesinarchive, $fn);
  671. }
  672. close (CPIO);
  673. $? && subprocerr ("cpio");
  674. &reapgzip;
  675. $/= "\n";
  676. my $tarsubst = quotemeta ($tarprefix);
  677. @filesinarchive = map { s/^$tarsubst/$wpfx/; $_ } @filesinarchive;
  678. return $tarprefix;
  679. }
  680. sub checktarsane {
  681. my ($tarfileread, $wpfx) = @_;
  682. my ($c2);
  683. %dirincluded = ();
  684. %notfileobject = ();
  685. my $tarprefix = &checktarcpio ($tarfileread, $wpfx);
  686. # make <TAR> read from the uncompressed archive file
  687. &forkgzipread ("$tarfileread");
  688. if (! defined ($c2 = open (TAR,"-|"))) { &syserr ("fork for tar -t"); }
  689. if (! $c2) {
  690. $ENV{'LANG'}= 'C';
  691. open (STDIN, "<&GZIP") || &syserr ("reopen gzip for tar -t");
  692. exec ('tar', '-vvtf', '-'); &syserr ("exec tar -vvtf -");
  693. }
  694. close (GZIP);
  695. my $efix= 0;
  696. open (TAR, "<tarlog");
  697. while (<TAR>) {
  698. chomp;
  699. if (! m,^(\S{10})\s,) {
  700. &error("tarfile \`$tarfileread' contains unknown object ".
  701. "listed by tar as \`$_'");
  702. }
  703. my $mode = $1;
  704. $mode =~ s/^([-dpsl])// ||
  705. &error("tarfile \`$tarfileread' contains object \`$fn' with ".
  706. "unknown or forbidden type \`".substr($_,0,1)."'");
  707. my $type = $&;
  708. if ($mode =~ /^l/) { $_ =~ s/ -\> .*//; }
  709. s/ link to .+//;
  710. if (length ($_) <= 48) {
  711. &error ("tarfile \`$tarfileread' contains incomplete entry \`$_'\n");
  712. }
  713. my $tarfn = substr ($_, 48, length ($_) - 48);
  714. $tarfn = deoctify ($tarfn);
  715. # store printable name of file for error messages
  716. my $pname = $tarfn;
  717. $pname =~ y/ -~/?/c;
  718. # fetch name of file as given by cpio
  719. $fn = $filesinarchive[$efix++];
  720. if ($tarfn ne $fn) {
  721. if ((length ($fn) == 99) && (length ($tarfn) >= 99)
  722. && (substr ($fn, 0, 99) eq substr ($tarfn, 0, 99))) {
  723. # this file doesn't match because cpio truncated the name
  724. # to the first 100 characters. let it slide for now.
  725. &warn ("filename \`$pname' was truncated by cpio;" .
  726. " unable to check full pathname");
  727. } else {
  728. &error ("tarfile \`$tarfileread' contains unexpected object".
  729. " listed by tar as \`$_'; expected \`$pname'");
  730. }
  731. }
  732. # if cpio truncated the name above,
  733. # we still can't allow files to expand into /../
  734. # need to check for multiple dots on some operating systems
  735. if ($tarfn =~ m/[.]{2,}/) {
  736. &error ("tarfile \`$tarfileread' contains object with".
  737. "/../ in its name ($pname)");
  738. }
  739. if ($tarfn =~ /\.dpkg-orig$/) {
  740. &error ("tarfile \`$tarfileread' contains file with name ending in .dpkg-orig");
  741. }
  742. if ($mode =~ /[sStT]/ && $type ne 'd') {
  743. &error ("tarfile \`$tarfileread' contains setuid, setgid".
  744. " or sticky object \`$pname'");
  745. }
  746. if ($tarfn eq "$tarprefix/debian" && $type ne 'd') {
  747. &error ("tarfile \`$tarfileread' contains object \`debian'".
  748. " that isn't a directory");
  749. }
  750. if ($type eq 'd') { $tarfn =~ s,/$,,; }
  751. my $dirname = $tarfn;
  752. if (($dirname =~ s,/[^/]+$,,) && (! defined ($dirincluded{$dirname}))) {
  753. &error ("tarfile \`$tarfileread' contains object \`$pname' but its containing ".
  754. "directory \`$dirname' does not precede it");
  755. }
  756. if ($type eq 'd') { $dirincluded{$tarfn} = 1; }
  757. if ($type ne '-') { $notfileobject{$tarfn} = 1; }
  758. }
  759. close (TAR);
  760. #$? && subprocerr ("tar -vvtf");
  761. &reapgzip;
  762. my $tarsubst = quotemeta ($tarprefix);
  763. %dirincluded = map { s/^$tarsubst/$wpfx/; $_=>1 } (keys %dirincluded);
  764. %notfileobject = map { s/^$tarsubst/$wpfx/; $_=>1 } (keys %notfileobject);
  765. }
  766. no strict 'vars';
  767. sub extracttar {
  768. my ($tarfileread,$dirchdir,$newtopdir) = @_;
  769. &forkgzipread("$tarfileread");
  770. defined($c2= fork) || &syserr("fork for tar -xkf -");
  771. if (!$c2) {
  772. chdir("$dirchdir") || &syserr("cannot chdir to \`$dirchdir' for tar extract");
  773. open(STDIN,"<&GZIP") || &syserr("reopen gzip for cpio -i");
  774. &cpiostderr;
  775. exec('tar','-xkf','-'); &syserr("exec tar -xkf -");
  776. }
  777. close(GZIP);
  778. $c2 == waitpid($c2,0) || &syserr("wait for tar -xkf -");
  779. $? && subprocerr("tar -xkf -");
  780. &reapgzip;
  781. opendir(D,"$dirchdir") || &syserr("Unable to open dir $dirchdir");
  782. @dirchdirfiles = grep($_ ne "." && $_ ne "..",readdir(D));
  783. closedir(D) || &syserr("Unable to close dir $dirchdir");
  784. (@dirchdirfiles==1 && -d "$dirchdir/$dirchdirfiles[0]") ||
  785. &error("$tarfileread extracted into >1 directory");
  786. rename("$dirchdir/$dirchdirfiles[0]", "$dirchdir/$newtopdir") ||
  787. &syserr("Unable to rename $dirchdir/$dirchdirfiles[0] to ".
  788. "$dirchdir/$newtopdir");
  789. }
  790. sub cpiostderr {
  791. open(STDERR,"| egrep -v '^[0-9]+ blocks\$' >&2") ||
  792. &syserr("reopen stderr for cpio to grep out blocks message");
  793. }
  794. sub setfile {
  795. my ($varref) = @_;
  796. if (defined ($$varref)) {
  797. &error ("repeated file type - files " . $$varref . " and $file");
  798. }
  799. $$varref = $file;
  800. }
  801. sub checktype {
  802. if (!lstat("$origdir/$fn")) {
  803. &unrepdiff2("nonexistent",$type{$fn});
  804. } else {
  805. $v= eval("$_[0] _ ? 2 : 1"); $v || &internerr("checktype $@ ($_[0])");
  806. return 1 if $v == 2;
  807. &unrepdiff2("something else",$type{$fn});
  808. }
  809. return 0;
  810. }
  811. sub setopmode {
  812. defined($opmode) && &usageerr("only one of -x or -b allowed, and only once");
  813. $opmode= $_[0];
  814. }
  815. sub unrepdiff {
  816. print(STDERR "$progname: cannot represent change to $fn: $_[0]\n")
  817. || &syserr("write syserr unrep");
  818. $ur++;
  819. }
  820. sub unrepdiff2 {
  821. print(STDERR "$progname: cannot represent change to $fn:\n".
  822. "$progname: new version is $_[1]\n".
  823. "$progname: old version is $_[0]\n")
  824. || &syserr("write syserr unrep");
  825. $ur++;
  826. }
  827. sub forkgzipwrite {
  828. #print STDERR "forkgzipwrite $_[0]\n";
  829. open(GZIPFILE,"> $_[0]") || &syserr("create file $_[0]");
  830. pipe(GZIPREAD,GZIP) || &syserr("pipe for gzip");
  831. defined($cgz= fork) || &syserr("fork for gzip");
  832. if (!$cgz) {
  833. open(STDIN,"<&GZIPREAD") || &syserr("reopen gzip pipe"); close(GZIPREAD);
  834. close(GZIP); open(STDOUT,">&GZIPFILE") || &syserr("reopen tar.gz");
  835. exec('gzip','-9'); &syserr("exec gzip");
  836. }
  837. close(GZIPREAD);
  838. $gzipsigpipeok= 0;
  839. }
  840. sub forkgzipread {
  841. #print STDERR "forkgzipread $_[0]\n";
  842. open(GZIPFILE,"< $_[0]") || &syserr("read file $_[0]");
  843. pipe(GZIP,GZIPWRITE) || &syserr("pipe for gunzip");
  844. defined($cgz= fork) || &syserr("fork for gunzip");
  845. if (!$cgz) {
  846. open(STDOUT,">&GZIPWRITE") || &syserr("reopen gunzip pipe"); close(GZIPWRITE);
  847. close(GZIP); open(STDIN,"<&GZIPFILE") || &syserr("reopen input file");
  848. exec('gunzip'); &syserr("exec gunzip");
  849. }
  850. close(GZIPWRITE);
  851. $gzipsigpipeok= 1;
  852. }
  853. sub reapgzip {
  854. #print STDERR "reapgzip $_[0]\n";
  855. $cgz == waitpid($cgz,0) || &syserr("wait for gzip");
  856. !$? || ($gzipsigpipeok && WIFSIGNALED($?) && WTERMSIG($?)==SIGPIPE) ||
  857. subprocerr("gzip");
  858. close(GZIPFILE);
  859. }
  860. sub addfile {
  861. my ($filename)= @_;
  862. stat($filename) || &syserr("could not stat output file \`$filename'");
  863. $size= (stat _)[7];
  864. my $md5sum= `md5sum <$filename`;
  865. $? && &subprocerr("md5sum $filename");
  866. $md5sum =~ s/^([0-9a-f]{32})\n$/$1/ || &failure("md5sum gave bogus output \`$_'");
  867. $f{'Files'}.= "\n $md5sum $size $filename";
  868. }
  869. # replace \ddd with their corresponding character, refuse \ddd > \377
  870. # modifies $_ (hs)
  871. {
  872. my $backslash;
  873. sub deoctify {
  874. my $fn= $_[0];
  875. $backslash= sprintf("\\%03o", unpack("C", "\\")) if !$backslash;
  876. s/\\{2}/$backslash/g;
  877. @_= split(/\\/, $fn);
  878. foreach (@_) {
  879. /^(\d{3})/ or next;
  880. &failure("bogus character `\\$1' in `$fn'\n") if oct($1) > 255;
  881. $_= pack("c", oct($1)) . $';
  882. }
  883. return join("", @_);
  884. } }