dpkg-source.pl 38 KB

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